// VSA JavaScript Document

var mywindow;
function onmouseover_productline_button(id) {
	document.getElementById(id).style.color="red"
	document.getElementById('content_product').style.visibility = 'hidden';	// clear div content_product
	showCustomerMenu()
	showProductlinePOST(id)
}

function showProductlinePOST(id)
{ 
	//alert(id)
	//mywindow = window.open("","_blank", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=900");
	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)
}

function stateProductlineChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		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 onmouseout_productline_button(id) {
	document.getElementById(id).style.color="black"
}

function onmouseover_button(id) {
	document.getElementById(id).style.color="red"
	//showCustomerMenu()
}

function onmouseout_button(id) {
	document.getElementById(id).style.color="black"
}

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

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 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
	} 
} 

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)
	//alert(url)
	// !!!! 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
	//alert(post_pairs)
    xmlHttp.send(post_pairs)
	//alert(post_pairs)
    // or xmlHttp.send('q='+str+'&sid='+sid)
}

function reset_checkbox(id) 
{
	document.getElementById(id).checked = false;	// set the street value to false
	set_checkbox(id);	// now set the server side value to false by virtue of reading street value
}

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

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 validate()
{
   with (document.contact)
   {		
		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 (company.value == "")
		{
		   alert('Please enter your company name.');
		   return false;
		}
		if (phone.value == "")
		{
		   alert('Please enter your phone number');
		   return false;
		}				
		if (general_info.value == "")
		{
		   alert('Please enter a comment or a question.');
		   return false;
		}				
		if (recipient.selectedIndex == 0)
		{
		   alert('Please select a recipient for your message.');
		   return false;
		}
		if (category.selectedIndex == 0)
		{
		   alert('Please select a category.');
		   return false;
		}
		if (category.value == 'Other')
		{
			if (other_category.value == "")
			{
				alert('Please specify the business category of your company.');
				return false;
			}				
		}
		member = false;
		for (i=0; i<towlvcc.length; i++) {
			if (towlvcc[i].checked) {
				//member = i;
				member = true;
			}
		}
		if (!member) {
			alert('Please indicate if your company is a member of the TOWLVCC');
			return false;
		}
     	else
           return true;		
	}
}

// Called by onblur="return validate_email(this.id)"
function validate_email (id) {
	//alert('id: '+id+'; value: '+$F(id));
	if ($F(id) != "")
	{
		apos=$F(id).indexOf("@"); 
		dotpos=$F(id).lastIndexOf(".");
		lastpos=$F(id).length-1;
		if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) {
			alert("Please enter a valid email.");
			return false;
		}
	}
}

function validate_company (id) {
	//alert('id: '+id+'; value: '+$F(id));
	if ($F(id) == "")
	{
		alert("Please enter a company name.");
		return false;
	}
}

function validate_field (id, error_message) {
	//alert('id: '+id+'; value: '+$F(id));
	if ($F(id) == "")
	{
		alert(error_message);
		return false;
	}
}

// load images
function FP_preloadImgs() {//v1.0
	var d = document, a = arguments; 
	if(!d.FP_imgs) d.FP_imgs = new Array();
	for(var i = 0; i < a.length; i++) { 
		d.FP_imgs[i] = new Image; 
		d.FP_imgs[i].src = a[i]; 
	}
}

function FP_swapImg() {//v1.0
	var doc = document, args = arguments, elm, n; 
	doc.$imgSwaps = new Array(); 
	for(n = 2; n < args.length; n += 2) { 
		elm = FP_getObjectByID(args[n]); 
		if(elm) { 
			doc.$imgSwaps[doc.$imgSwaps.length] = elm;
			elm.$src = elm.src; 
			elm.src = args[n+1]; 
		} 
	}
}

function FP_getObjectByID(id,o) {//v1.0
	var c, el, els, f, m, n; 
	if(!o) o = document; 
	if(o.getElementById) el = o.getElementById(id);
	else if(o.layers) c = o.layers; 
	else if(o.all) el = o.all[id]; 
	if(el) return el;
	if(o.id == id || o.name == id) return o; 
	if(o.childNodes) c = o.childNodes;
	if(c)
		for(n = 0; n < c.length; n++) { 
			el = FP_getObjectByID(id,c[n]); 
			if(el) return el; 
		}
	f = o.forms; 
	if(f) 
		for(n = 0; n < f.length; n++) { 
			els = f[n].elements;
			for(m = 0; m < els.length; m++) { 
				el = FP_getObjectByID(id,els[n]); 
				if(el) return el; 
			} 
		}
	return null;
}

function selMenuItem() {
	// <li id="menu_inactive"><a id="index.php" href="index.php">Home</a></li>
	// get the dir name
	var dir = document.getElementById('dir').name;	// php writes <div id="dir" name="'.$_SESSION['dir'].'"></div>
	var pathname = location.pathname;	// read the current URL pathname such as /Alpha/index.php
	page = pathname.slice(pathname.lastIndexOf("/")+1, pathname.length);	// parse page name such as index.php
	var parent = document.getElementById(page).parentNode;	// use anchor id to get the li id 
	parent.id = "menu_active";	// set the id to menu_active to affect menu.css styles
}
