///////////////////////////////////////////////////////////
//
//  common.js
//  JavaScript Functions
//  Original author: Sridhar Ganji
//  
///////////////////////////////////////////////////////////
//  Modification history:
//  
//
///////////////////////////////////////////////////////////

browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);

if (browserName == "Netscape" && browserVer >= 3 || browserName == "Microsoft Internet Explorer" && browserVer >= 4)  
	version = "n3";
else 
	version = "n2";

if (version == "n3") 
{
	//These are the active images on the side next to the border
	cartOn = new Image(32,  197);
	//cartOn.src = "/images/icon_cart_red.gif";
	cartOn.src = "images/icon_cart3.gif";

	//These are the inactive images on the top menu bar
	cartOff = new Image(42,  197);
	cartOff.src = "images/icon_cart3.gif";
}
	
function img_act(imgName) 
{
	if (version == "n3") 
	{
		imgOn = eval(imgName + "On.src");
		document[imgName].src = imgOn;
	}
}

function img_inact(imgName) 
{
	if (version == "n3") 
	{
		imgOff = eval(imgName + "Off.src");
		document[imgName].src = imgOff;
	}
}

//to disallow submit action upon text box entry
function PreventEnter()
{
	return !(window.event && window.event.keyCode == 13);
}

//set the focus on the form element -- login request
function SetFocus()
{
	if (document.frmLoginRequest.txtEmail)
		document.frmLoginRequest.txtEmail.focus();
	else if (document.frmLoginRequest.txtEmail1)
		document.frmLoginRequest.txtEmail1.focus();
}

//set the login page focus
function SetLoginFocus()	
{
	//alert(document.frmLogin.txtUserName.value);
	if (document.frmLogin.txtUserName.value != "")
		document.frmLogin.txtPassword.focus();
	else
		document.frmLogin.txtUserName.focus();
}

function getWindowSize() 
{
	if (document.getElementById('ProductList') != null)
	{
		//alert('in getWindowSize()');
		if (navigator.appName.indexOf("Netscape") == -1) 
		{
			winWidth = document.body.clientWidth;
			winHeight = document.body.clientHeight;
		}
		else
		{
			winWidth = window.innerWidth;
			winHeight = window.innerHeight;
		}
		
		if (winHeight <= 450)
			document.getElementById('ProductList').style.height = '225px';
		else 
			document.getElementById('ProductList').style.height = '370px';
	}
}

function SetBillTo()
{
	// set default
	document.getElementById("ctlCheckout_ddlBilling_State_Province").selectedIndex = 0;
   	
   				
   	// View source to see auto-generated names used below; Make sure consistent among different version of .Net Framework
   	// Use getElementById because auto-added "name" attribute contains a ":", which freaks out Javascript
   	//if (document.getElementById("setBillTo").getAttribute("checked"))
   	if (document.getElementById("ctlCheckout_chkSame_Shipping_Billing_Address").checked) // (getAttribute doesn't work for checked property in NS)
   	{
   		document.getElementById("ctlCheckout_txtBilling_First_Name").setAttribute("value",document.getElementById("ctlCheckout_txtShipping_First_Name").getAttribute("value"));
   		document.getElementById("ctlCheckout_txtBilling_Last_Name").setAttribute("value",document.getElementById("ctlCheckout_txtShipping_Last_Name").getAttribute("value"));
   		//document.getElementById("billing_companyName").setAttribute("value",document.getElementById("shipping_companyName").getAttribute("value"));
   		document.getElementById("ctlCheckout_txtBilling_Address1").setAttribute("value",document.getElementById("ctlCheckout_txtShipping_Address1").getAttribute("value"));
   		document.getElementById("ctlCheckout_txtBilling_Address2").setAttribute("value",document.getElementById("ctlCheckout_txtShipping_Address2").getAttribute("value"));
   		document.getElementById("ctlCheckout_txtBilling_City").setAttribute("value",document.getElementById("ctlCheckout_txtShipping_City").getAttribute("value"));
   		//document.getElementById("billing_state").setAttribute("value",document.getElementById("shipping_state").getAttribute("value"));
   		//document.getElementById("billing_state").setAttribute("selectedIndex",document.getElementById("shipping_state").getAttribute("selectedIndex"));
   		document.getElementById("ctlCheckout_ddlBilling_State_Province").selectedIndex = document.getElementById("ctlCheckout_ddlShipping_State_Province").selectedIndex;
   		document.getElementById("ctlCheckout_txtBilling_Zip_Postal_Code").setAttribute("value",document.getElementById("ctlCheckout_txtShipping_Zip").getAttribute("value"));
   		document.getElementById("ctlCheckout_txtBilling_Country").setAttribute("value",document.getElementById("ctlCheckout_txtShipping_Country").getAttribute("value"));
   		document.getElementById("ctlCheckout_txtBilling_Email").setAttribute("value",document.getElementById("ctlCheckout_txtShipping_Email").getAttribute("value"));
   		document.getElementById("ctlCheckout_txtBilling_Daytime_Phone").setAttribute("value",document.getElementById("ctlCheckout_txtShipping_Daytime_Phone").getAttribute("value"));
   	}
   	else
   	{
   		document.getElementById("ctlCheckout_txtBilling_First_Name").setAttribute("value","");
   		document.getElementById("ctlCheckout_txtBilling_Last_Name").setAttribute("value","");
   		//document.getElementById("billing_companyName").setAttribute("value","");
   		document.getElementById("ctlCheckout_txtBilling_Address1").setAttribute("value","");
   		document.getElementById("ctlCheckout_txtBilling_Address2").setAttribute("value","");
   		document.getElementById("ctlCheckout_txtBilling_City").setAttribute("value","");
   		//if (navigator.userAgent.indexOf("IE")>-1)
   		document.getElementById("ctlCheckout_ddlBilling_State_Province").selectedIndex = 0;
   		//document.getElementById("billing_state").setAttribute("value","");
   		document.getElementById("ctlCheckout_txtBilling_Zip_Postal_Code").setAttribute("value","");
   		document.getElementById("ctlCheckout_txtBilling_Country").setAttribute("value","");
   		document.getElementById("ctlCheckout_txtBilling_Email").setAttribute("value","");
   		document.getElementById("ctlCheckout_txtBilling_Daytime_Phone").setAttribute("value","");
   	}	
}

function perform_test()
{
	var URL = 'http://www.dearborn.com/Jonesnew/browsertest.asp';
    
	newWindow = window.open(URL, 'new', 'feature=alwaysRaised,height=440,width=700,left=100,top=50,screenX=100,screenY=50,scrollbars,resizable,status');
	newWindow.focus();
}

//validate required info on the form
function validate_form(thisform)
{
	with (thisform)
	{
		//check string first and last name
		var check1= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-' ";
		//check string address
		var check2= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,# ";
		//check string phone and fax numbers
		var check3= "0123456789-() ";
		//check string zip/postal code
		var check4= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789- ";
		//check string city, state, country and company
		var check5= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789. ";
		
		//check if first name is filled out
		if (validate_required(first_name,"First Name must be filled out!")==false)
			{first_name.focus();return false;}

		//validate first name format
		if (validate_field(first_name,check1,"Please enter only alphanumeric and '- space characters for first name!")==false)
			{first_name.focus();return false;}

		//check if last name is filled out
		if (validate_required(last_name,"Last Name must be filled out!")==false)
			{last_name.focus();return false;}

		//validate last name format
		if (validate_field(last_name,check1,"Please enter only alphanumeric and '- space characters for last name!")==false)
			{last_name.focus();return false;}
		
		//validate following fields only if they are entered.
		
		//Check for address field
		if (street.value.length > 0)
		{
			//validate address format
			if (validate_field(street,check2,"Please enter only alphanumeric and .,# space characters for address!")==false)
				{street.focus();return false;}
		}
		
		//Check for city field
		if (city.value.length > 0)
		{
			//validate city format
			if (validate_field(city,check5,"Please enter only alphanumeric and . space characters for city!")==false)
				{city.focus();return false;}
		}	
		//Check for state field
		//if (state.value.length > 0 )
		//{
			//validate state format
		//	if (validate_field(state,check5,"Please enter only alphanumeric and . space characters for state!")==false)
		//		{state.focus();return false;}
		//}
		
		//check to see if state is selected from a drop-down list
		var dropdownIndex = document.getElementById('state1').selectedIndex;
		var dropdownValue = document.getElementById('state1')[dropdownIndex].value;
		
		//set value of hidden state field
		document.getElementById("state").setAttribute("value",dropdownValue);
		
		if (state.value.length > 2)
		{
			alert("Please select state!");
			state1.focus();
			return false;
		}

		//Check for zip field
		if (zip.value.length > 0)
		{
			//validate zip format
			if (validate_field(zip,check4,"Please enter only alphanumeric and - space characters for zip!")==false)
				{zip.focus();return false;}
		}
		//Check for country field
		if (country.value.length > 0)
		{
			//validate country format
			if (validate_field(country,check5,"Please enter only alphanumeric and . space characters for country!")==false)
				{country.focus();return false;}
		}
		//Check for phone field
		if (phone.value.length > 0)
		{
			//validate phone format
			if (validate_field(phone,check3,"Please enter only numeric and -() space characters for phone!")==false)
				{phone.focus();return false;}
		}
		//Check for company field
		if (company.value.length > 0)
		{
			//validate company format
			if (validate_field(company,check5,"Please enter only alphanumeric and . space characters for company!")==false)
				{company.focus();return false;}
		}
		//Check for fax field
		if (fax.value.length> 0)
		{
			//validate fax format
			if (validate_field(fax,check3,"Please enter only numeric and -() space characters for fax!")==false)
				{fax.focus();return false;}
		}
		//Check business phone field
		//if (00N50000001xtwV.value.length > 0)
		//{
			//validate 00N50000001xtwV format (Business phone)
		//	if (validate_field(00N50000001xtwV,check3,"Please enter only numeric and -() space characters for business phone!")==false)
		//		{00N50000001xtwV.focus();return false;}
		//}
		
		//check to required email is filled out
		if (validate_required(email,"Email must be filled out!")==false)
		{email.focus();return false;}

		//validate email format
		if (validate_email(email,"Not a valid e-mail address!")==false)
		{email.focus();return false;}

	}
}

//validate required field
function validate_required(field,alerttxt)
{
	with (field)
	{
		if (value==null||value=="")
		{alert(alerttxt);return false;}
		else {return true}
	}
}

//validate email format
function validate_email(field,alerttxt)
{
	with (field)
	{
		apos=value.indexOf("@");
		dotpos=value.lastIndexOf(".");
		if (apos<1||dotpos-apos<2) 
		{alert(alerttxt);return false;}
		else {return true;}
	}
}

//validate email format
function validate_field(field,checkOK,alerttxt)
{
	with (field)
	{
		// allow ONLY alphanumeric keys hyphen space and apostrophe, no sother ymbols or punctuation
		// this can be altered for any "checkOK" string you desire
		var checkStr = field.value;
		var allValid = true;
		for (i = 0;  i < checkStr.length;  i++)
		{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
		}
		
		if (!allValid)
		{alert(alerttxt);return false;}
		else {return true;}
	}
}
