function strTrim(tmpStr)
{
	tmpStr = tmpStr.replace(/^\s+/,"");//remove leading
	tmpStr = tmpStr.replace(/\s+$/,"");//remove trailing
	return tmpStr;
}
//------------------------------------------------------------------------------------
function trimFields()
{
	for(var i=0; i < obj.elements.length; i++)
	{
		if(obj.elements[i].type == "text" || obj.elements[i].type == "textarea" || obj.elements[i].type == "password")
		{
			obj.elements[i].value = strTrim(obj.elements[i].value);
		}
	}
}
//------------------------------------------------------------------------------------
function chkEmail(tmpStr)
{
	var email_pat = /^[a-z][a-z0-9_\.\-]*[a-z0-9]@[a-z0-9]+[a-z0-9\.\-_]*\.[a-z]+$/i;
	return(email_pat.test(tmpStr));
}

//Checks validity of date fields
function chkDate(tmpStr)
{
	var dt_pat = /^\d{2,2}\/\d{2,2}\/\d{4,4}$/;
	if(!dt_pat.test(tmpStr))
	{
		return false;
	}
	var dtGiven = new Date(tmpStr);
	var arrDt = tmpStr.split("/");
	var dtMon = parseInt(arrDt[0],10);//force decimal or else 08,09 will return 0
	var dtDay = parseInt(arrDt[1],10); //force decimal or else 08,09 will return 0
	var dtYear = parseInt(arrDt[2],10); //force decimal or else 08,09 will return 0
	if((dtGiven.getMonth() != dtMon - 1) || (dtGiven.getDate() != dtDay) || (dtGiven.getFullYear() != dtYear))
	{
		return false;
	}
	return true;
}

//------------------------------------------------------------------------------------
function chkCity(tmpStr)
{
	var city_pat = /^[a-z]*$/i;
	return(city_pat.test(tmpStr));
}

//------------------------------------------------------------------------------------
function chkURL(tmpStr)
{
	var url_pat = /^(http|https|ftp):\/\/([\w-]+\.)+[\w-]+(\/[\w-\.\/?%&amp;,=#@\/:]*)?/;
	return(url_pat.test(tmpStr));
}

//------------------------------------------------------------------------------------
function chkImage(tmpStr)
{
	var img_pat = /(.jpg)/;
	return(img_pat.test(tmpStr))
}

function refreshCaptcha(imgid)
{
	var img = new Image();
	img.src = 'captcha/show_captcha.php?hash='+parseInt(Math.random() * 10000000000);
	document.getElementById(imgid).src = img.src;
}

//Checks CC Number by pattern
function chkCCNum(tmpStr)
{
	var cc_pat = /^(\d{14,16})$/
	return(cc_pat.test(tmpStr));
}

//------------------------------------------------------------------------------------
function NewWindow(pageName)
{
	window.open(pageName, '', 'width=520,height=600,toolbar=0,menubar=0,location=0,left=0,top=0,scrollbars=1');
}

function printOrderDetails(orderID, isAdmin)
{
	var url = (isAdmin == 'N')?'admin/':'';
	window.open(url+'print_order_details.html?id='+orderID, '', 'width=600,height=650,left=200,scrollbars=1,top=30,toolbar=0,menubar=1,location=0');
}


function addtoCart(productID)
{
	self.location = webServer+'/cart_process.php?opt=add&id='+productID;
}

// Checks coupon code
function chkCoupon(tmpStr)
{
	var coupon_pat = /^[A-Z0-9]+$/;
	return(coupon_pat.test(tmpStr));
}

// Validate search form
function validateSearch(types)
{
	zipcode = objSearch.zipcode.value;
	zipcode = strTrim(zipcode);
	if(zipcode == 'Enter Zip Code...' || zipcode == '')
	{
		alert("Please enter Zip Code for Searching Dealer.");
		objSearch.zipcode.value = zipcode;
		objSearch.zipcode.focus();
		return;
	}
	if(types == 'International')
	{
		// Everything fine
		self.location = "dealers.php?type=International&zipcode="+encodeURIComponent(zipcode)+"&distance="+encodeURIComponent(objSearch.distance.value);
		return true;
	}
	else
	{
		// Everything fine
		self.location = "dealers.php?zipcode="+encodeURIComponent(zipcode)+"&distance="+encodeURIComponent(objSearch.distance.value);
		return true;
	}
}

//Used to call click event of button by pressing enter key
function clickButton(e)
{
	var evt = e ? e : window.event;
	if(evt.keyCode == 13)
	{
		validateSearch();
	}
}

function validateLogin()
{
	if(objLogin.login_email.value == '' || objLogin.login_email.value == 'Login Email')
	{
		alert("Please enter Login Email Address");
		objLogin.login_email.focus();
		return false;
	}
	if(objLogin.login_email.value != '' || objLogin.login_email.value == 'Login Email')
	{
		if(!chkEmail(objLogin.login_email.value))
		{
			alert("Please enter Valid Email Address.");
			objLogin.login_email.focus();
			objLogin.login_email.select();
			return false;
		}
	}
	if(objLogin.login_password.value == '' || objLogin.login_password.value == 'Login Email')
	{
		alert("Please enter Password");
		objLogin.login_password.focus();
		return false;
	}
	return true;
}


//------------------------------------------------------------------//
//Generic AJAX object for all types of HTTP get/post work			//
//Author: Debabrata Kar (dk.webtenet@gmail.com)						//
//Usage:															//
//	var ajax = new AJAX();											//
//	var arrParam = new Array();										//
//	arrParam['name1'] = 'value1';									//
//	arrParam['name2'] = 'value2';									//
//	arrParam['name3'] = 'value3';									//
//	ajax.getRequest(url, arrParam, responseHandler);				//
//	OR																//
//	ajax.postRequest(url, arrParam, responseHandler);				//
//																	//
//	NOTE: You do not need to escape() or encodeURIComponent() the	//
//	parameter names or values. AJAX will do it on its own.			//
//	You need to define responseHandler() function that will handle	//
//	response back from the server, be it XML or anything else		//
//------------------------------------------------------------------//
//The AJAX object
function AJAX()
{
	//Private variables (properties)
	var __httpRequest = null;
	var __callbackFunc = null;

	//Private method: __createHttpRequest()
	var __createHttpRequest = function()
	{
		if(window.XMLHttpRequest) //Mozilla, Safari etc
		{
			__httpRequest = new XMLHttpRequest();
		}
		else if(window.ActiveXObject) //IE
		{
			try
			{
				__httpRequest = new ActiveXObject("MSXML2.XMLHTTP");
			}
			catch (e)
			{
				try
				{
					__httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e)
				{
					//Do whatever you need to do here
					alert("AJAX cannot be used with your browser!");
				}
			}
		}
	}

	//Private method: __createParameters(arr)
	var __createParameters = function(arr)
	{
		var parameters = ""; //Initialize
		for(x in arr)
		{
			var pName = encodeURIComponent(x);
			var pVal = encodeURIComponent(arr[x]);
			parameters = (parameters == "")?pName+'='+pVal:parameters+'&'+pName+'='+pVal;
		}
		return parameters;
	}

	//Private method: __handleResponse()
	var __handleResponse = function()
	{
		if(__httpRequest.readyState == 4)
		{
			__callbackFunc(__httpRequest.responseText);
		}
	}

	//Public method: getRequest(url, arrParam, callbackFunc)
	this.getRequest = function(url, arrParam, callbackFunc)
	{
		__createHttpRequest() //recreate ajax object to defeat cache problem in IE
		__callbackFunc = callbackFunc;
		if(__httpRequest)
		{
			var param = __createParameters(arrParam);
			__httpRequest.onreadystatechange = __handleResponse;
			//Include a random number to defeat IE cache problem
			__httpRequest.open('GET', url+"?ajaxhash="+Math.random()+'&'+param, true);
			__httpRequest.send(null)
		}
	}

	//Public method: postRequest()
	this.postRequest = function(url, arrParam, callbackFunc)
	{
		__createHttpRequest() //recreate ajax object to defeat cache problem in IE
		__callbackFunc = callbackFunc;
		if (__httpRequest)
		{
			var param = __createParameters(arrParam);
			__httpRequest.onreadystatechange = __handleResponse;
			__httpRequest.open('POST', url, true);
			__httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
			__httpRequest.setRequestHeader("Content-length", param.length);
			__httpRequest.setRequestHeader("Connection", "close");
			__httpRequest.send(param);
		}
	}
}

function checkState()
{
	if(obj.b_state.selectedIndex > 0 && obj.b_state.selectedIndex < 66)
		obj.b_country.selectedIndex = 230;
	if(obj.b_state.selectedIndex > 66)
		obj.b_country.selectedIndex = 37;
	if(obj.s_state.selectedIndex > 0 && obj.s_state.selectedIndex < 66)
		obj.s_country.selectedIndex = 230;
	if(obj.s_state.selectedIndex > 66)
		obj.s_country.selectedIndex = 37;
	if(obj.b_state.selectedIndex != 0 && obj.b_state.selectedIndex != 66)
		obj.b_other_state.value = '';
	if(obj.s_state.selectedIndex != 0 && obj.s_state.selectedIndex != 66)
		obj.s_other_state.value = '';
}

function checkOtherState()
{
	if(obj.b_state.selectedIndex != 0 && obj.b_state.selectedIndex != 66 && obj.b_other_state.value != '')
	{
		obj.b_state.selectedIndex = 0;
		obj.b_country.selectedIndex = 0;
	}
	if((obj.b_country.selectedIndex == 37 || obj.b_country.selectedIndex == 230) && obj.b_other_state.value != '')
	{
		obj.b_state.selectedIndex = 0;
		obj.b_country.selectedIndex = 0;
	}
	if(obj.s_state.selectedIndex != 0 && obj.s_state.selectedIndex != 66 && obj.s_other_state.value != '')
	{
		obj.s_state.selectedIndex = 0;
		obj.s_country.selectedIndex = 0;
	}
	if((obj.s_country.selectedIndex == 37 || obj.s_country.selectedIndex == 230) && obj.s_other_state.value != '')
	{
		obj.s_state.selectedIndex = 0;
		obj.s_country.selectedIndex = 0;
	}
}

function checkCountry()
{
	if(obj.b_country.selectedIndex != 37 && obj.b_country.selectedIndex != 230)
	{
		obj.b_state.selectedIndex = 0;
	}
	else
	{
		obj.b_other_state.value = '';
	}
	if(obj.s_country.selectedIndex != 37 && obj.s_country.selectedIndex != 230)
	{
		obj.s_state.selectedIndex = 0;
	}
	else
	{
		obj.s_other_state.value = '';
	}

}

/*function checkState()
{
	if(obj.b_state.selectedIndex > 0 && obj.b_state.selectedIndex < 52)
		obj.b_country.selectedIndex = 230;
	if(obj.b_state.selectedIndex > 52)
		obj.b_country.selectedIndex = 37;
	if(obj.s_state.selectedIndex > 0 && obj.s_state.selectedIndex < 52)
		obj.s_country.selectedIndex = 230;
	if(obj.s_state.selectedIndex > 52)
		obj.s_country.selectedIndex = 37;
	if(obj.b_state.selectedIndex != 0 && obj.b_state.selectedIndex != 52)
		obj.b_other_state.value = '';
	if(obj.s_state.selectedIndex != 0 && obj.s_state.selectedIndex != 52)
		obj.s_other_state.value = '';
}
function checkOtherState()
{
	if(obj.b_state.selectedIndex != 0 && obj.b_state.selectedIndex != 52 && obj.b_other_state.value != '')
	{
		obj.b_state.selectedIndex = 0;
		obj.b_country.selectedIndex = 0;
	}
	if(obj.s_state.selectedIndex != 0 && obj.s_state.selectedIndex != 52 && obj.s_other_state.value != '')
	{
		obj.s_state.selectedIndex = 0;
		obj.s_country.selectedIndex = 0;
	}
}
function checkCountry()
{
	if(obj.b_country.selectedIndex != 37 && obj.b_country.selectedIndex != 230)
	{
		obj.b_state.selectedIndex = 0;
	}
	if(obj.s_country.selectedIndex != 37 && obj.s_country.selectedIndex != 230)
	{
		obj.s_state.selectedIndex = 0;
	}
}*/

function fillShipTo(chk)
{
	if(chk.checked)
	{
		//============copy Billing Address into Shipping Address fields============
		obj.s_first_name.value = obj.b_first_name.value;
		obj.s_last_name.value = obj.b_last_name.value;
		obj.s_company_name.value = obj.b_company_name.value;
		obj.s_address.value = obj.b_address.value;
		obj.s_city.value = obj.b_city.value;
		obj.s_state.selectedIndex = obj.b_state.selectedIndex;
		obj.s_other_state.value = obj.b_other_state.value;
		obj.s_country.selectedIndex = obj.b_country.selectedIndex;
		obj.s_zip.value = obj.b_zip.value;
		obj.s_phone.value = obj.b_phone.value;
		obj.s_fax.value = obj.b_fax.value;
		obj.s_email.value = obj.b_email.value;
	}
	else
	{
		//============erase the fields============
		obj.s_first_name.value = '';
		obj.s_last_name.value = '';
		obj.s_company_name.value = '';
		obj.s_address.value = '';
		obj.s_city.value = '';
		obj.s_state.selectedIndex = 0;
		obj.s_other_state.value = '';
		obj.s_country.selectedIndex = 0;
		obj.s_zip.value = '';
		obj.s_phone.value = '';
		obj.s_fax.value = '';
		obj.s_email.value = '';
	}
}
