function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function clearInputText(id,defaultValue){
	var element = document.getElementById(id);
	if (element.value != defaultValue)
		return;
	element.value = '';
}

function subscribeNewsletter()
{
	var email = document.getElementById('userEmail').value;
	if (!echeck(email))
	{
		alert('Please enter a valid email address');
		return;
	}
	alert('Thank You. Your email address has been added to our mailing list.');
}

function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	    return false
	 }

		 return true					
}

//For selecting products for comparison on the product listing page
var comparableProducts = new Array();
var compareCount = 0;
function addComparableProduct(id,targ){
	if (compareCount>3){
		alert('You cannot compare more than 4 products');
		targ.checked=false;
		return;
	}
	comparableProducts[compareCount++]=id;
}

function removeComparableProduct(id){
	for (i=0;i<compareCount;i++){
		if (comparableProducts[i]==id){
			comparableProducts.splice(i,1);
			compareCount--;
			return;
		}
	}
}

function productChecked(e,id){
	if (e.target) 
		targ = e.target;
	else if (e.srcElement) 
		targ = e.srcElement;
	if(targ.checked)
		addComparableProduct(id,targ);
	else
		removeComparableProduct(id);
}

function goCompare(){
	if (compareCount<2){
		alert('You need to select at least 2 products to compare.');
		return;
	}
	window.location.href="index.php?main_page=product_compare&products="+comparableProducts;
}

function changeSortOrder(cPath,selectId){
	var sortCriteria = document.getElementById(selectId).value;
	var url = "index.php?main_page=index&cPath="+cPath+"&sortby="+sortCriteria;
	createCookie('sortorder',sortCriteria)
	window.location.href=url;
}

function changeResultLotSize(cPath,selectId){
	var lotSize = document.getElementById(selectId).value;
	var url = "index.php?main_page=index&cPath="+cPath+"&count="+lotSize;
	createCookie('lotsize',lotSize)
	window.location.href=url;
}

function getParam(name){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
	  return "";
	else
	  return results[1];
}

function setSelectOption(objCombo,value){
	for(i=0;i<objCombo.options.length;i++)
	{
		if(objCombo.options[i].value==value)
		{
			objCombo.options[i].selected = true;
			break;
		}
	}
}

function getSelectOption(objCombo){
	for(i=0;i<objCombo.options.length;i++)
	{
		if(objCombo.options[i].selected==true)
		{
			return objCombo.options[i].value = true;
		}
	}
	return null;
}

function goSearch(){
	var catid = document.getElementById('searchCategory').value == 0 ? '' : document.getElementById('searchCategory').value;
	var keyword = document.getElementById('searchKeyword').value;
	if (keyword=='')
	{
		alert('Please enter a keyword to search for.');
		return;
	}
	var url = 'index.php?main_page=advanced_search_result&keyword='+keyword+'&search_in_description=1&categories_id='+catid+'&inc_subcat=1&manufacturers_id=&pfrom=&pto=&dfrom=&dto=&x=13&y=5'
	window.location.href = url;
}