//Detects if a value is in the array
Array.prototype.in_array = function(obj) {
	return new RegExp('(^|\,)'+obj+'(\,|$)','gi').test(this);
}
//Toggles the email form on the contact us page open
function openEmailForm() {
	getObj("EmailForm").style.display = "block";
}
//Lets IE users bookmark a page
function bookmark() {
	if (navigator.appName=='Microsoft Internet Explorer') {
		window.external.AddFavorite(window.location,window.document.title);
	} else {
		alert("Sorry, this is an Internet Explorer Only Feature, Hit (Ctrl+D) to bookmark us.");
	}
}
//Shows the price bar and updates
function togglePrice(obj) {
	var row = getObj("price-row");
	if(obj.options[obj.selectedIndex].value == 0) {
		//show the price thing
		row.style.display = "block";
	} else {
		row.style.display = "none";
	}
	if(request)
		getFilters();
}

//holds the ajax query
var request = false;
//creates the ajax query
function createAjaxModule() {
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = false;
			}
		}
	}
}
//shorvut to document.getElement
function getObj(obj) {
	return document.getElementById(obj);
}
//Retreve all the equipment settings
function getFilters() {
	var _manufacturer = getObj("manufacturer");
	var v_manufacturer = _manufacturer.options[_manufacturer.selectedIndex].value;
	var _type = getObj("type");
	var v_type = _type.options[_type.selectedIndex].value;
	var _condition = getObj("condition");
	var v_condition = _condition.options[_condition.selectedIndex].value;
	var _price = getObj("price");
	var v_price = _price.options[_price.selectedIndex].value;
	var _order = getObj("order");
	var v_order = _order.options[_order.selectedIndex].value;
	var _filter = false;
	if(_manufacturer!="All" || _type!="All" || _condition!="All" || _price!="All" || _order!="name") {
		_filter = true;
	}
	listItems(_filter,v_manufacturer,v_type,v_condition,v_price,v_order);
}

//clears a select
function emptySelect(obj) {
	obj.options.length = 0;
}
//update the types (called from manufacturer)
function updateTypes(obj) {
	var val = obj.options[obj.selectedIndex].value;
	var _type = getObj("type");
	var typeval = _type.options[_type.selectedIndex].value;
	emptySelect(_type);
	_type.options[0] = new Option("All","All");
	if(val != "All") {
		for(var i=0;i<manufacturer_array.length;i++) {
			if(manufacturer_array[i][0]==val) {
				//our id matches
				for(var h = 0; h < manufacturer_array[i][2].length;h++) {
					_type.options[h+1] = new Option(manufacturer_array[i][2][h],manufacturer_array[i][2][h]);
				}
			}
		}
	} else {
		var arr_tracker = new Array();
		var count = 1;
		for(var i = 0; i < manufacturer_array.length; i++) {
			for(var h = 0; h < manufacturer_array[i][2].length; h++) {
				if(!arr_tracker.in_array(manufacturer_array[i][2][h]) && manufacturer_array[i][2][h]!="") {
					count++;
					arr_tracker[arr_tracker.length] = manufacturer_array[i][2][h];
				}
			}
		}
		arr_tracker = arr_tracker.sort();
		for(var i = 0;i < arr_tracker.length;i++) {
			_type.options[i+1] = new Option(arr_tracker[i],arr_tracker[i]);
		}
	}
	//correct our entry in the box 
	for(var i = 0;i < _type.options.length;i++) {
		if(_type.options[i].value == typeval) {
			_type.selectedIndex = i;
		}
	}
	if(request)
		getFilters();
}
//update the manufacturers (called from types)
function updateManufacturer(obj) {
	var val = obj.options[obj.selectedIndex].value;
	var _manu = getObj("manufacturer");
	var typeval = _manu.options[_manu.selectedIndex].value;
	emptySelect(_manu);
	_manu.options[0] = new Option("All","All");
	if(val != "All") {
		if(_manu.selectedIndex!=0) {
			return;
		}
		var count = 1;
		for(var i=0;i<manufacturer_array.length;i++) {
			for(var h = 0; h < manufacturer_array[i][2].length;h++) {
				if(manufacturer_array[i][2][h] == val) {
					//this manufacturer sells these items!
					_manu.options[count] = new Option(manufacturer_array[i][1].replace(/&amp;/g,"&"),manufacturer_array[i][0]);
					count++;
				}
			}
		}
	} else {
		for(var i=0;i<manufacturer_array.length;i++) {
			_manu.options[i+1] = new Option(manufacturer_array[i][1].replace(/&amp;/g,"&"),manufacturer_array[i][0]);
		}
	}
	//correct our entry in the box 
	for(var i = 0;i < _manu.options.length;i++) {
		if(_manu.options[i].value == typeval) {
			_manu.selectedIndex = i;
		}
	}
	if(request)
		getFilters();
}
//calls the ajax on the data
function listItems(filter,manufacturer,type,condition,price,order) {
	var url = "equipmentHandler.php?";
	if(filter) {
		url += "filter=true&manufacturer="+manufacturer+"&type="+type+"&condition="+condition+"&price="+price+"&order="+order;
	}
	request.open("GET", url, true);
	request.onreadystatechange = finishUpdatePage;
	request.send(null);
}
//Then fill in the form
function finishUpdatePage() {
	var obj = getObj('equipment-list');
	if(request.readyState == 1) {
		obj.innerHTML='<div style="text-align:center"><img src="images/ajax.gif" alt="Loading..." \/><\/div>';
	}
	if (request.readyState == 4) {
		if (request.status == 200) {
			var response = request.responseText;
			obj.innerHTML= response;
		} else {
			obj.innerHTML = "<div style=''>Error! Status:"+request.status+"<\/div>";
		}
	}
}
function iframeContentAdd(val) {
	var testFrame = getObj("pframe");
	var doc = testFrame.contentDocument;
	if (doc == undefined || doc == null)
		doc = testFrame.contentWindow.document;
	doc.open();
	doc.write("<html><head><title>null<\/title><\/head><body bgcolor='#EEEEEE'>"+val+"<\/body><\/html>");
	doc.close();
	testFrame.style.visibility="visible";
}

function partslookup() {
	var comp = getObj('company');
	var pframe = getObj('pframe');
	var partnum = escape(getObj('partnum').value);
	if(comp.value=="KB") {
		pframe.src='http://www.kubota.com/f/PartsCat/partscatmain.asp';
	} else if(comp.value=="AR") {
		pframe.src='http://partsradar53.arinet.com/scripts/EmpartISAPI.dll?MF&app=ARNC531&lang=EN&TF=Empartweb&loginID=ariensc&loginpwd=consumer&partner=ARNC';
	} else if(comp.value=="BC") {
		pframe.src='http://cgcequip.arinet.com/scripts/EmpartISAPI.dll?MF&app=CGC54&TF=EmpartWeb&partner=CGCSB&lang=EN&loginid=CG999997&loginpwd=2006CGC1030';
	} else if(comp.value=="ECHO") {
		pframe.src="http://www.echo-usa.com/documentation.asp?Model="+partnum+"&lang=E";
	} else if(comp.value=="CUB") {
		pframe.src="http://cubcadet.arinet.com/scripts/EmpartISAPI.dll?MF&app=cub53&tf=EmpartWeb&loginid=cubcadet&loginpwd=test";
	} else if(comp.value=="BG") {
		pframe.src='http://www.billygoat.com/site/productManuals.aspx';
	} else if(comp.value=="HO") {
		pframe.src="http://www.honda-engines.com/Engines_owners_manuals/ownersmanuals/"+partnum.toLowerCase()+".htm";
	} else if(comp.value=="KP") {
		pframe.src="http://www.buykawpower.com/kmcb2c/Menu?action=VIEWPARTB2B&searchpartnumber="+partnum;
	} else if(comp.value=="KO") {
		pframe.src="http://www.kohlerengines.com/service/manuals/manuals_results.jsp";
	} else if(comp.value=="LW") {
		pframe.src="http://littlewonder.com/si-owners.asp";
	} else if(comp.value=="RM") {
		pframe.src="http://dealer.redmax.com/download/"+partnum.toUpperCase().replace("%20","_")+".pdf";
	} else if(comp.value=="SC") {
		pframe.src="http://www.scag.com/manuals.html";
	} else {
		iframeContentAdd("Invalid Manufacturer");
	}
	return false;
}
function updatePartsForm(val) {
	var partnum = getObj('partnum');
	var pframe = getObj('pframe');
	partnum.value="disabled";
	if(val=="KB") {
		partnum.disabled = true;
		pframe.src='http://www.kubota.com/f/PartsCat/partscatmain.asp';
	} else if(val=="AR") {
		partnum.disabled = true;
		pframe.src='http://partsradar53.arinet.com/scripts/EmpartISAPI.dll?MF&app=ARNC531&lang=EN&TF=Empartweb&loginID=ariensc&loginpwd=consumer&partner=ARNC';
	} else if(val=="BC") {
		partnum.disabled = true;
		pframe.src='http://cgcequip.arinet.com/scripts/EmpartISAPI.dll?MF&app=CGC54&TF=EmpartWeb&partner=CGCSB&lang=EN&loginid=CG999997&loginpwd=2006CGC1030';
	} else if(val=="CC") {
		partnum.disabled = true;
		pframe.src='http://cubcadet.arinet.com/scripts/EmpartISAPI.dll?MF&app=cub53&tf=EmpartWeb&loginid=cubcadet&loginpwd=test';
	} else if(val=="BG") {
		partnum.disabled = true;
		pframe.src='http://www.billygoat.com/site/productManuals.aspx';
	} else if(val=="BS") {
		partnum.disabled = true;
		pframe.src='http://www.briggsandstratton.com/maint_repair/manual_and_more/doclist.aspx?category=Engines&manual_type=Illustrated+Parts+Lists&menu=nav3&subMenu=3';
	} else if(val=="KO") {
		partnum.disabled = true;
		pframe.src="http://www.kohlerengines.com/service/manuals/manuals_results.jsp";
	} else if(val=="LW") {
		partnum.disabled = true;
		pframe.src="http://littlewonder.com/si-owners.asp";
	} else if(val=="SC") {
		partnum.disabled = true;
		pframe.src="http://www.scag.com/manuals.html";
	} else if(val=="MC") {
		partnum.disabled = true;
		pframe.src="http://www.millcreekspreaders.com/productSpec.html";
	} else if(val=="SN") {
		partnum.disabled = true;
		pframe.src="http://www.snapper.com/publications.html";
	} else if(val=="TO") {
		partnum.disabled = true;
		pframe.src="https://homeownersolutions.toro.com/portal/server.pt?open=512&objID=214&PageID=0&cached=true&mode=2&userID=19461";
	} else if(val=="TV") {
		partnum.disabled = true;
		pframe.src="http://www.trac-vac.com/index_files/manuals.htm";
	} else if(val=="WO") {
		partnum.disabled = true;
		pframe.src="http://www.woodsequipment.com/manuals.aspx";
	} else if(val=="HO") {
		partnum.disabled = false;
		pframe.src="http://www.css-club.net/honda/pp-service/index.asp";
	} else if(val=="KP") {
		partnum.disabled = false;
		pframe.src="http://www.buykawpower.com/kmcb2c/Menu?action=CATALOGB2B&applicationChart=NO&reset=1";
	} else if(val=="RM") {
		partnum.disabled = false;
		pframe.src="http://redmax.infoaccessipl.com/";
	} else if(val=="YA") {
		partnum.disabled = true;
		pframe.src="http://www.yamaha-motor.com/outdoor/parts/home.aspx";
	} else {
		partnum.disabled = false;
	}
	if(!partnum.disabled) {
		partnum.value="Enter a part number...";
	}
}
function pFormAddLine() {
	var obj = getObj("part-form").childNodes[0]; //tbody
	
	var row = document.createElement("TR");
	obj.appendChild(row);
	var cell1 = document.createElement("TD");
	row.appendChild(cell1);
	var input1 = document.createElement("INPUT");
	input1.setAttribute("name","p_number[]");
	input1.setAttribute("type","text");
	cell1.appendChild(input1);
	var cell2 = document.createElement("TD");
	row.appendChild(cell2);
	var input2 = document.createElement("INPUT");
	input2.setAttribute("name","p_make[]");
	input2.setAttribute("type","text");
	cell2.appendChild(input2);
	var cell3 = document.createElement("TD");
	row.appendChild(cell3);
	var input3 = document.createElement("INPUT");
	input3.setAttribute("name","p_desc[]");
	input3.setAttribute("type","text");
	cell3.appendChild(input3);
	var cell4 = document.createElement("TD");
	row.appendChild(cell4);
	var input4 = document.createElement("INPUT");
	input4.setAttribute("name","p_quantity[]");
	input4.setAttribute("type","text");
	input4.onkeyup = function() { res(input4,numberchr); };
	cell4.appendChild(input4);
	input4.style.textAlign = 'center';
	return false;
}
function updateBilling(obj,name) {
	var box = getObj('b_saa');
	if(box.checked) {
		var field = getObj('b_'+name);
		field.value = obj.value;
	}
}

function billingInfo(obj) {
	var bfname = getObj('b_fname');
	var blname = getObj('b_lname');
	var bemail = getObj('b_email');
	var bphone = getObj('b_phone');
	if(obj.checked) {
		bfname.value = getObj('p_fname').value;
		blname.value = getObj('p_lname').value;
		bemail.value = getObj('p_email').value;
		bphone.value = getObj('p_phone').value;
		
		bfname.disabled = true;
		blname.disabled = true;
		bemail.disabled = true;
		bphone.disabled = true;
	} else {
		bfname.value = "";
		blname.value = "";
		bemail.value = "";
		bphone.value = "";
		
		bfname.disabled = false;
		blname.disabled = false;
		bemail.disabled = false;
		bphone.disabled = false;
	}
}
function partFormCheck() {
	if(getObj('p_number').value && getObj('p_make').value && getObj('p_desc').value && getObj('p_quantity').value) {
		if(getObj('p_fname').value && getObj('p_lname').value) {
			if(getObj('p_address').value && getObj('p_city').value && getObj('p_zip').value) {
				if(getObj('p_phone').value) {
					return true;
				} else {
					alert("Please fill in a phone number");
				}
			} else {
				alert("Please fill in a complete address");
			}
		} else {
			alert("Please fill in both your first and last name.");
		}
	} else {
		alert("Please fill in atleast one full item.");
	}
	return false;
}

var emailchr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_@.";
var numberchr = '1234567890';
var phonechr = '1234567890()- ';

function res(t,v){
	var w = "";
	for (i=0; i < t.value.length; i++) {
		x = t.value.charAt(i);
		if (v.indexOf(x,0) != -1)
			w += x;
	}
	t.value = w;
}