// JavaScript Document
onerror=handleErr
var txt=""

function handleErr(msg,url,l)
{
	txt="There was an error on this page.\n\n"
	txt+="Error: " + msg + "\n"
	txt+="URL: " + url + "\n"
	txt+="Line: " + l + "\n\n"
	txt+="Click OK to continue.\n\n"
	//alert(txt)
	return true
}

function validate()
{
   with (document.contact)
   {	
   		// Client data checks
		if (name.value == "")
		{
		   alert('Please enter your name.');
		   return false;
		}		
		if (email.value == "")
		{
		   alert('Please enter your email address.');
		   return false;
		}	
		if (email.value != "")
		{
			apos=email.value.indexOf("@"); 
			dotpos=email.value.lastIndexOf(".");
			lastpos=email.value.length-1;
			if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
				alert("Please enter a valid email.");
				return false;
			}
		}
		if (phone.value == "")
		{
		   alert('Please provide a daytime phone number.');
		   return false;
		}
		if (date.value == "")
		{
		   alert('Please provide a date for the event.');
		   return false;
		}
		if (general_info.value == "")
		{
		   alert('Please provide comments or questions.');
		   return false;
		}
     	else
           return true;		
	}
}

function emailvalidation(entered, alertbox) {
	with (entered) {
		apos = value.indexOf("@"); 
		dotpos = value.lastIndexOf(".");
		lastpos = value.length-1;
		if (apos < 1 || dotpos-apos < 2 || lastpos-dotpos > 3 || lastpos-dotpos < 2) {
			if (alertbox) {
				alert(alertbox);
			} 
			return false;
		}
		else {
			return true;
		}
	}
} 

var theImages = new Array() // do not change this
theImages[0] = '1.jpg'
theImages[1] = '2.jpg'
theImages[2] = '3.jpg'
theImages[3] = '4.jpg'
theImages[4] = '5.jpg'
theImages[5] = '6.jpg'
theImages[6] = '7.jpg'
// do not edit anything below this line
var j = 0
var p = theImages.length;
var preBuffer = new Array()
for (i = 0; i < p; i++){
   preBuffer[i] = new Image()
   preBuffer[i].src = theImages[i]
}
var whichImage = Math.round(Math.random()*(p-1));
function showImage()
	{
		document.write('<img class="random" src="images/'+theImages[whichImage]+'"width="120" height="160" border="12" align="center" valign="middle">');
}

var checked = new Array()	// index => value
var checked_idx = new Array()	// value => index
var anchors
var elements = new Array()
function build_href(id, idx)
{	
	//alert('build_href id:'+id+' idx:'+idx)
	// is the checkbox checked
	if(document.getElementById(id).checked) {
		checked[idx] = id
		checked_idx[id] = idx
		//alert("set id:"+id+"\n"+"idx:"+idx)
	} else {
		idx = checked_idx[id]
		checked[idx] = ""
		checked_idx[id] = ""
	}
	var href_org = document.getElementById("go_hidden").value	// get the original href
	href = href_org	// set a local copy
	var get_vals = ""	// clear the get vals string
	for (x in checked)
	{
		id = checked[x]
		//alert('id: '+id+' x: '+x)
		if(id != "") {
			get_vals = get_vals+id+"="+document.getElementById(id).value+"&"
		}
		//alert(get_vals)
	}			
	href = href_org+get_vals	// cat the href and get values
	//alert('href: '+href)
	// get the explicit number of links from the hidden idx - could also count <a /> tags
	//j = document.getElementById("go_idx_hidden").value
	anchors = document.anchors.length
	for (i=0; i<anchors; i++)
	{
		// set each anchor href
		document.getElementById("go_"+i).href = href
	}
	// display the link string that will be called
	document.getElementById("display_href").value = href
			//alert(id)
	
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	
	//var url = "get_productline.php"	// PHP call
	var url = "set_checkbox.php"	// PHP call
	sid = Math.random()	// random number so caching does not occur
	xmlHttp.onreadystatechange = stateProductlineChanged 
	xmlHttp.open("POST",url,false)
	
	// !!!! need this line or POST does not work
   	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")	// cannot use "multipart/form-data"

	// sent the post pairs
	var post_pairs = 'expand='+id+'&sid='+sid
    xmlHttp.send(post_pairs)
	alert(post_pairs)
    // or xmlHttp.send('q='+str+'&sid='+sid)

}

function set_checkbox(id)
{	
	var checked = document.getElementById(id).checked
	//alert('set_checkbox('+id+') '+checked)
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	
	//var url = "get_productline.php"	// PHP call
	var url = "set_checkbox.php"	// PHP call
	sid = Math.random()	// random number so caching does not occur
	xmlHttp.onreadystatechange = statesetcheckboxChanged
	xmlHttp.open("POST",url,false)
	
	// !!!! need this line or POST does not work
   	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")	// cannot use "multipart/form-data"

	// sent the post pairs
	var post_pairs = 'expand='+id+'&checked='+checked+'&sid='+sid
    xmlHttp.send(post_pairs)
	//alert(post_pairs)
    // or xmlHttp.send('q='+str+'&sid='+sid)
}

function statesetcheckboxChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		//alert('statesetcheckboxChanged: '+xmlHttp.responseText)
		//return true
		document.getElementById("set_checkbox").innerHTML = xmlHttp.responseText 
		showCustomerMenu()
	} 
} 

function showCustomerMenu()
{ 
	//alert('showCustomerMenu()')
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	
	//var url = "get_productline.php"	// PHP call
	var url = "show_customer_menu.php"	// PHP call
	sid = Math.random()	// random number so caching does not occur
	xmlHttp.onreadystatechange = stateCustomerMenu 
	xmlHttp.open("POST",url,false)
	
	// !!!! need this line or POST does not work
   	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")	// cannot use "multipart/form-data"

	// sent the post pairs
	var post_pairs = 'sid='+sid
    xmlHttp.send(post_pairs)
	//alert(post_pairs)
    // or xmlHttp.send('q='+str+'&sid='+sid)
}

function SYM()
{	
	//alert('SYM()')
	xmlHttp = GetXmlHttpObject()
	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request")
		return
	} 
	
	//var url = "get_productline.php"	// PHP call
	var url = "SYM.php"	// PHP call
	sid = Math.random()	// random number so caching does not occur
	xmlHttp.onreadystatechange = stateSYM
	xmlHttp.open("POST",url,false)
	
	// !!!! need this line or POST does not work
   	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")	// cannot use "multipart/form-data"

	// sent the post pairs
	var post_pairs = 'sid='+sid
    xmlHttp.send(post_pairs)
	//alert(post_pairs)
    // or xmlHttp.send('q='+str+'&sid='+sid)
}

function stateSYM() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		//alert('stateSYM()')
		document.getElementById("insert_SYM").innerHTML = xmlHttp.responseText 
		//win(xmlHttp.responseText)
		//confirm('stateSYM: '+xmlHttp.responseText)
		//return true
	} 
} 

function stateCustomerMenu() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		document.getElementById("insert_customer_menu").innerHTML = xmlHttp.responseText 
		//win(xmlHttp.responseText)
		//alert('stateCustomerMenu: '+xmlHttp.responseText)
		//return true
	} 
} 



// used to change style color
function onmouseover_button(id) {
	document.getElementById(id).style.color="red"
	//showCustomerMenu()
}
function onmouseout_button(id) {
	document.getElementById(id).style.color="black"
}
function onmouseover_productline_button(id) {
	document.getElementById(id).style.color="red"
	showCustomerMenu()
	showProductlinePOST(id)
}
function onmouseout_productline_button(id) {
	document.getElementById(id).style.color="black"
}

function showProductlinePOST(id)
{ 
	document.getElementById("insert_display").style.visibility="visible";	// make visible
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	} 
	
	var url = "get_productline.php";	// PHP call
	//var url = "products.php"	// PHP call
	sid = Math.random();	// random number so caching does not occur
	xmlHttp.onreadystatechange = stateProductlineChanged; 
	xmlHttp.open("POST",url,false);
	
	// !!!! need this line or POST does not work
   	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");	// cannot use "multipart/form-data"

	// sent the post pairs
	var post_pairs = 'expand='+id+'&sid='+sid;
    xmlHttp.send(post_pairs);
	//alert(post_pairs)
    // or xmlHttp.send('q='+str+'&sid='+sid)
}

var mywindow;

function stateProductlineChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=800");
		if(mywindow) mywindow.document.write(xmlHttp.responseText);
		//alert(xmlHttp.responseText)
		document.getElementById("insert_display").innerHTML = xmlHttp.responseText; 
		//document.getElementById("ProductlineList_display").innerHTML = xmlHttp.responseText 
	} 
} 

function GetXmlHttpObject()
{ 
	var objXMLHttp = null
	if (window.XMLHttpRequest)
	{
		objXMLHttp = new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}

function win(txt) {
	msg=window.open("","msg","height=800,width=800,left=0,top=0");
	msg.document.write("<html><head><title>Debug</title></head>");
	msg.document.write("<body>");
	msg.document.write(txt);
	msg.document.write("</body></html>");
}

function setShipFromBill(){
	if(document.formdata.ship_same_as_bill.checked){ // in this document, from form named formdata, is checkbox checked
		document.formdata.shipTo_firstName.value = document.formdata.billTo_firstName.value;
		document.formdata.shipTo_lastName.value = document.formdata.billTo_lastName.value;
		document.formdata.shipTo_company.value = document.formdata.billTo_company.value;
		document.formdata.shipTo_street1.value = document.formdata.billTo_street1.value;
		document.formdata.shipTo_street2.value = document.formdata.billTo_street2.value;
		document.formdata.shipTo_city.value = document.formdata.billTo_city.value;
		document.formdata.shipTo_state.value = document.formdata.billTo_state.value;
		document.formdata.shipTo_postalCode.value = document.formdata.billTo_postalCode.value;
		document.formdata.shipTo_country.value = document.formdata.billTo_country.value;
	}else{
		document.formdata.shipTo_firstName.value = "";
		document.formdata.shipTo_lastName.value = "";
		document.formdata.shipTo_company.value = "";
		document.formdata.shipTo_street1.value = "";
		document.formdata.shipTo_street2.value = "";
		document.formdata.shipTo_city.value = "";
		document.formdata.shipTo_state.value = "";
		document.formdata.shipTo_postalCode.value = "";
	}
}

function uncheckShipFromBill(){
	document.formdata.ship_same_as_bill.checked = false;
}

// For email
function emailChange(){
	if(document.formdata.billTo_email.value == "") {
		//alert("Enter email.");
	} else {
		//alert("Re-enter email address again to verify.");
       	//document.formdata.billTo_email.value = "";
	}
}


function validate_checkout()
{
   with (document.formdata)
   {
		/*if (USERID.value == "")
		{
		   alert('Account: Please enter a user id.');
		   return false;
		}
		if (PASSWD.value == "")
		{
		   alert('Account: Please enter a password.');
		   return false;
		}*/
		if (billTo_firstName.value == "")
		{
		   alert('Billing Information: Please enter your first name.');
		   return false;
		}
		if (billTo_lastName.value == "")
		{
		   alert('Billing Information: Please enter your last name.');
		   return false;
		}
		if (billTo_street1.value == "")
		{
		   alert('Billing Information: Please enter your street address.');
		   return false;
		}
		if (billTo_city.value == "")
		{
		   alert('Billing Information: Please enter your city.');
		   return false;
		}
		if (billTo_state.value == "")
		{
		   alert('Billing Information: Please enter your state.');
		   return false;
		}
		if (billTo_postalCode.value == "")
		{
		   alert('Billing Information: Please enter your postal code.');
		   return false;
		}
		if (billTo_phoneNumber.value == "")
		{
		   alert('Billing Information: Please enter your daytime phone number.');
		   return false;
		}
		// is email empty?
		if (billTo_email.value == "")
		{
		   alert('Billing Information: Please enter your email address.');
		   return false;
		}
		// validate email form
		if (billTo_email.value != "")
		{
			apos=billTo_email.value.indexOf("@");
			dotpos=billTo_email.value.lastIndexOf(".");
			lastpos=billTo_email.value.length-1;
			if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
				alert("Please enter a valid email.");
				return false;
			}
		}
		if (shipTo_firstName.value == "")
		{
		   alert('Shipping Information: Please enter your first name or check the box to use billing information.');
		   return false;
		}
		if (shipTo_lastName.value == "")
		{
		   alert('Shipping Information: Please enter your last name or check the box to use billing information.');
		   return false;
		}
		if (shipTo_street1.value == "")
		{
		   alert('Shipping Information: Please enter your street address or check the box to use billing information.');
		   return false;
		}
		if (shipTo_city.value == "")
		{
		   alert('Shipping Information: Please enter your city or check the box to use billing information.');
		   return false;
		}
		if (shipTo_state.value == "")
		{
		   alert('Shipping Information: Please enter your state or check the box to use billing information.');
		   return false;
		}
		if (shipTo_postalCode.value == "")
		{
		   alert('Shipping Information: Please enter your postal code or check the box to use billing information.');
		   return false;
		}
     	else
           return true;
	}
}

