//
//	Javascript globals
//
window.onload = wonload;
window.onunload = wunload;

function wonload()
{
	return;
}
function wunload()
{
	return;
}
function forgotpassword(id)
{
	var f = document.getElementById(id);
	if (f.login.value == '')
	{
		alert("You must provide your login to retrieve your password");
	} else {
		var url='cmd.php?rnd='+(Math.random()*100000)+'&do=forgotpassword&forgotpassword='+f.login.value;

		var xmlHttp	=GetXmlHttpObject();
		xmlHttp.open("POST",url,false);
		xmlHttp.send(null);

		alert(xmlHttp.responseText);
	}
	return false;
}

function confirmCaptcha(v)
{
	if(xmlHttpIsSupported)
	{
		var url= "index.php?rnd='+(Math.random()*100000)+'&do=captcha&captcha="+v;
		var xmlHttp	=GetXmlHttpObject();
		xmlHttp.open("POST",url,false);
		xmlHttp.send(null);
		return (xmlHttp.responseText=='ok');
	}
	return false;
}

function showDiv(showlist,hidelist)
{
	if (hidelist != '')
	{
		var hideids = hidelist.split(',');
		for (var i = 0; i < hideids.length; i++)
			document.getElementById(hideids[i]).style.display='none';
	}
	if (showlist != '')
	{
		var showids = showlist.split(',');
		for (var i = 0; i < showids.length; i++)
			document.getElementById(showids[i]).style.display='block';
	}
	return false;
}

function validateForm(f)
{
	// validate each element on the form
	for(var x=0;x<f.elements.length;x++)
	{
		var el = f.elements[x];
		
		// check to see if this element is required
		// and if so, test it against the pattern specified on the form
		if(el.hasAttribute('required'))
		{
			var re = new RegExp(el.getAttribute('required'));
			if (re.exec(el.value)==null) {
				alert(el.getAttribute('errormessage'));
				el.focus();
				return false;
			}
		}
		// if the element is the captcha input
		// validate it against the server
		if(el.name=='captcha')
		{
			if(!confirmCaptcha(el.value))
			{
				alert('{{Invalid_Captcha}}');
				document.getElementById('captchaimage').src='captcha.php?'+(Math.random(0)*1000);
				el.focus();
				return false;
			}
		}
	}
	// All fields validated
	// Check for a specific function for this form
	return form_specific(f);
}
function form_specific(f)
{
	switch(f.name)
	{
		case 'form_calculator':
			f.escrowFee.value = f.transactionAmount.value;
			return false;
		default:
			return true;
	}
}
function accountDispatchEmail(name,email)
{
	var el=document.getElementById('form_message');
	el.toname.value=name;
	el.toemail.value=email;
	showDiv('account_email','accountdiv,account_detail');
	return true;
}
function accountStatusChange(c,accountid)
{
	var f=c.form;
	f.doit.value='changestatus';
	f.changeid.value=accountid;
	f.changestatus.value=c.value;
	f.changetype.value='';
	f.submit();
	return true;
}
function accountTypeChange(c,accountid)
{
	var f=c.form;
	f.doit.value='changetype';
	f.changeid.value=accountid;
	f.changestatus.value='';
	f.changetype.value=c.value;
	f.submit();
	return true;
}
function accountlistrestore()
{
	showDiv('accountdiv','account_email,account_detail');
}
function accountEditDetail(id)
{
	showDiv('accountdiv','account_email,account_detail');
	if(xmlHttpIsSupported)
	{
		var url= 'accounts.php?rnd='+(Math.random()*100000)+'&doit=loadedit&id='+id;
		var xmlHttp	=GetXmlHttpObject();
		xmlHttp.open("POST",url,false);
		xmlHttp.send(null);
		document.getElementById('account_input_table').innerHTML = xmlHttp.responseText;
	}
	showDiv('account_detail','accountdiv,account_email');
	return false;
}
function triplistrestore()
{
	showDiv('tripdiv','trip_detail');
}
function tripEdit(id,source)
{
	showDiv('tripdiv','trip_detail');
	if(xmlHttpIsSupported)
	{
		var url= 'tripticks.php?rnd='+(Math.random()*100000)+'&doit=loadedit&id='+id;
		var xmlHttp	=GetXmlHttpObject();
		xmlHttp.open("POST",url,false);
		xmlHttp.send(null);
		document.getElementById('trip_input_table').innerHTML = xmlHttp.responseText;
	}
	showDiv('trip_detail','tripdiv');
	return false;
}
function newaccountTypeChange(c)
{
	document.getElementById('superuserdiv').style.display = (c.value=='adm')?'block':'none';
	document.getElementById('customerdiv').style.display = (c.value=='cus')?'block':'none';
	document.getElementById('sourcediv').style.display = (c.value=='src')?'block':'none';
}
function sourceChange(c)
{
	switch(c.value)
	{
		case '':
			showDiv('','trip_new_source,trip_inventory');
			return false;
		case 'new':
			var f=c.form;
			f.regular.checked=false;
			f.company.value='';
			f.fname.value='';
			f.mname.value='';
			f.lname.value='';
			f.email.value='';
			f.password.value='';
			f.phone.value='';
			f.fax.value='';
			f.alt_contact_name.value='';
			f.alt_contact_telephone.value='';
			f.geo_country.value='US';
			f.geo_state.value='OH';
			f.addr.value='';
			f.city.value='';
			f.postalcode.value='';
			f.mileage.value=0;
			f.miles_each.value=0;
			f.totalcost.value=0;
			showDiv('trip_new_source','trip_inventory');
			return false;
		default:
			if(xmlHttpIsSupported)
			{
				var url= 'tripticks.php?rnd='+(Math.random()*100000)+'&doit=sourcedata&id='+c.value;
				var xmlHttp	=GetXmlHttpObject();
				xmlHttp.open("POST",url,false);
				xmlHttp.send(null);

				var fields = xmlHttp.responseText.split('&');
				var f=c.form;
				var y=0;
				for(var x=0;x<fields.length;x++)
				{
					keyvalue = fields[x].split('=');
					if(keyvalue[0]=='regular')
						f.regular.checked=(keyvalue[1]!='0');
						
					try {
						eval('f.'+keyvalue[0]+'.value="'+unescape(keyvalue[1])+'";');
					} catch(e) {
						// ignore the data
						y++;
					}
				}
				f.miles_each.value = parseFloat(f.mileage.value)*2;
				computeTripTotalCost(f);
			}
			showDiv('trip_inventory','trip_new_source');
			break;
	}
}

function tripStatusChange(c,id)
{
	if(xmlHttpIsSupported)
	{
		var url= 'tripticks.php?rnd='+(Math.random()*100000)+'&doit=changestatus&status='+c.value+'&id='+id;
		var xmlHttp	=GetXmlHttpObject();
		xmlHttp.open("POST",url,false);
		xmlHttp.send(null);
		alert("Status Changed "+xmlHttp.responseText);
	}
}

function computeTripTotalCost(f)
{
	var t = parseFloat(f.miles_each.value)*parseFloat(f.circuits.value)*parseFloat(f.cost_per_mile.value);
	f.total_cost.value = Math.round(t*100)/100;
	recomputeCosts(f);
}
function recomputeCosts(f)
{
	var tc = parseFloat(f.total_cost.value);
	var purchase_price = f.elements["purchase_price[]"];
	var trip_cost = f.elements['trip_cost[]'];
	var cost = f.elements['cost[]'];
	var ptotal = 0;
	for(var x=0;x<cost.length;x++)
	{
		try {
			var ic = parseFloat(purchase_price[x].value);
		} catch(e) {
			var ic = 0;
		}
		ptotal += ic;
	}
	
	for(var x=0;x<cost.length;x++)
	{
		// purchase price is set
		if(ptotal==0)
		{
			trip_cost[x].value='0.00';
			cost[x].value = purchase_price[x].value;
		} else {
			var purchase = parseFloat(purchase_price[x].value);
			var ppratio = purchase/ptotal;
			if(ppratio==0)
				var trip = 0;
			else
				var trip = Math.round(tc*ppratio*100)/100;
			trip_cost[x].value = Math.round(trip*100)/100;
			cost[x].value = Math.round((purchase+trip)*100)/100;
		}
	}
}

function enlarge(i)
{
	document.getElementById('itemlarge').src = document.getElementById('itemsmall'+i).src;
}

function showDescriptionHelp(i)
{
	if(xmlHttpIsSupported)
	{
		var url= 'inventory.php?rnd='+(Math.random()*100000)+'&doit=descriptionhelp&item_number='+i;
		var xmlHttp	=GetXmlHttpObject();
		xmlHttp.open("POST",url,false);
		xmlHttp.send(null);
		eval("div=document.getElementById('descriptive_phrases_"+i+"');");
		div.innerHTML = xmlHttp.responseText;
		helpid = 'descriptive_phrases_'+i;
		picsid = 'inventory_pics_'+i;
		picinputsid = 'inventory_pics_input_'+i;
		htmlid = 'inventory_link_html_'+i;
		hideids = picsid+','+picinputsid+','+htmlid;
		showDiv(helpid,hideids);
	}
}
function hideDescriptionHelp(i)
{
	eval("document.getElementById('descriptive_phrases_"+i+"').innerHTML='';");
	helpid = 'descriptive_phrases_'+i;
	picsid = 'inventory_pics_'+i;
	showDiv(picsid,helpid);
}

function addText(div,id)
{
	if(xmlHttpIsSupported)
	{
		var url= 'inventory.php?rnd='+(Math.random()*100000)+'&doit=descriptionhelptext&id='+id;
		var xmlHttp	=GetXmlHttpObject();
		xmlHttp.open("POST",url,false);
		xmlHttp.send(null);
		var c=document.getElementById(div);
		c.value += xmlHttp.responseText;
		c.focus();
	}
}

function linktest(c)
{
	var f=c.form;
	var inventory_link = f.elements["inventory_link[]"];
	var testlink = f.elements['testlink[]'];
	for(var x=0;x<testlink.length;x++)
	{
		if(c==testlink[x])
		{
			var w=window.open("","testlink");
			w.document.writeln('<HTML><BODY>');
			w.document.write(inventory_link[x].value);
			w.document.writeln('</BODY></HTML>');
			w.document.close();
			break;
		}
	}
	return false;
}

function printMapRoute(c,div)
{
	var f=c.form;
	var w=window.open("","mapprint");
	w.document.write('<HTML><HEAD><script language="javascript">window.onload=wonload;function wonload() {window.print();window.close();}</script></HEAD><BODY>');
	w.document.write(f.Tname.value+'<br>');
	w.document.write(f.Tstreet.value+'<br>');
	w.document.write(f.Tcity.value+', '+f.Tstate.value+' '+f.Tpostalcode.value+'<br>');
	w.document.write(f.Tphone.value+'<br><br>');
	w.document.write(document.getElementById(div).innerHTML);
	w.document.writeln('</BODY></HTML>');
	w.document.close();
}

function dropPicture(s)
{
	if(xmlHttpIsSupported)
	{
		var itemels = s.split('.');
		eval("var div=document.getElementById('inventory_pics_"+itemels[0]+"');");
		var url= 'inventory.php?rnd='+(Math.random()*100000)+'&doit=droppic&item='+itemels[0]+'&pic='+itemels[1]+'&ext='+itemels[2];
		var xmlAction=GetXmlHttpObject();
		xmlAction.open("POST",url,false);
		xmlAction.send(null);
		div.innerHTML = xmlAction.responseText;
	}
	return false;
}