function GetSiteUrl()
{
	if(document.location.host=="www.comfortstyle.com.au")
		return "http://www.comfortstyle.com.au";
	else if(document.location.host=="comfortstyle.com.au")
		return "http://comfortstyle.com.au";
	else if(document.location.host=="www.comfortstylefurniture.com.au")
		return "http://www.comfortstylefurniture.com.au";
	else if(document.location.host=="comfortstylefurniture.com.au")
		return "http://comfortstylefurniture.com.au";
	else if(document.location.host=="staging.comfortstyle.com.au")
		return "http://staging.comfortstyle.com.au";
	else if(document.location.host=="comfortstyle.dhs.local")
		return "http://comfortstyle.dhs.local";
	else // Probably-not-working final fallback.
		return ("http://comfortstyle.local");
}

function GetRandNumber()
{
	var now = new Date();
	var hours = now.getHours();
	var minutes = now.getMinutes();
	var seconds = now.getSeconds()
	var randomnumber=Math.floor(Math.random()*100000);
	
	var RandNum=randomnumber+""+hours+""+minutes+""+seconds;
	
	return RandNum;
	
}

var xmlHttp
function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest) {
		objXMLHttp=new XMLHttpRequest()	}
	else if (window.ActiveXObject)	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")	}
	if(objXMLHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	return objXMLHttp
}

function showMenuDiv(divid) {

	//alert(divid);
	//document.getElementById(divid).style.filter = 'alpha(opacity = 10)';
	//document.getElementById(divid).style.opacity = '0.5';
	//document.getElementById(divid).style.cursor='hand';
	
}

function hideMenuDiv(divid) {

	//document.getElementById(divid).style.filter = 'alpha(opacity = 0)';
	//document.getElementById(divid).style.opacity = '0.1';
	//document.getElementById(divid).style.backgroundColor = ' rgb(0,0,0,0) ';
	
}

function Topsearchsubmit()
{
	var tmpsiteurl=GetSiteUrl();
	
	//document.location.href=tmpsiteurl+'/products/productsearch/'+document.getElementById("q").value+'/1';
	//document.location.href=tmpsiteurl+'/productlist.php?q='+document.getElementById("q").value;
	
	
	/*if(document.location.host=="www.comfortstyle.com.au" || document.location.host=="www.comfortstylefurniture.com.au")
		document.frmTopSearch.action=tmpsiteurl+'/product/productsearch/'+document.getElementById("q").value+'/1';
	else
		document.frmTopSearch.action=tmpsiteurl+'/productlist.php?q='+document.getElementById("q").value;
	
	document.frmTopSearch.submit();
	*/
	
	var q=document.getElementById("q").value;
	
	if(q.split(" ").join("")=="")
	{
		document.getElementById("q").value="";
		document.getElementById("q").focus();
	}
	else
	{
		q=trimAll(q);
		var tmpserstring=q.replace(" ","_");
		
		tmpserstring=tmpserstring.replace(" ","_");
		tmpserstring=tmpserstring.replace(" ","_");
		tmpserstring=tmpserstring.replace(" ","_");
		
		tmpserstring = tmpserstring.replace (/[\/\.\,\?!@#$%^&*()<>\s]/g, "");
	
		if(document.location.host=="www.comfortstyle.com.au" || document.location.host=="comfortstyle.com.au" || document.location.host=="www.comfortstylefurniture.com.au" || document.location.host=="staging.comfortstyle.com.au")
			document.location.href=tmpsiteurl+'/furniture/search/'+tmpserstring;
		else
			document.location.href=tmpsiteurl+'/productlist.php?q='+tmpserstring;
	}
	
}

function trimAll(sString) 
{ 
	while (sString.substring(0,1) == ' ') 
	{ 
		sString = sString.substring(1, sString.length); 
	} 
	
	while (sString.substring(sString.length-1, sString.length) == ' ') 
	{ 
		sString = sString.substring(0,sString.length-1); 
	} 
	
	return sString; 
} 

function HeaderAjaxGetStore()
{
	var tmpsiteurl=GetSiteUrl();

	var randomnumbaer=GetRandNumber();
	var url;
	
	url=tmpsiteurl+'/ajax_storesessionid.php?mode=getStoreDetails';		
	url=url+'&randomnum='+randomnumbaer;
	
	xmlHttp_headerstore=GetXmlHttpObject()
	xmlHttp_headerstore.onreadystatechange=function stateChanged() { 
		if (xmlHttp_headerstore.readyState==4 || xmlHttp_headerstore.readyState=="complete")
		{ 
			
			if(xmlHttp_headerstore.responseText!="")
			{
				var tmpstoreadd=xmlHttp_headerstore.responseText
				tmpstoreadd_arr=tmpstoreadd.split("#####");
				
				document.getElementById("headertopstorenamediv").innerHTML=tmpstoreadd_arr[0];
				document.getElementById("headertopstoreaddressdiv").innerHTML=tmpstoreadd_arr[1];
				document.getElementById("headertopstorephonediv").innerHTML=tmpstoreadd_arr[2];
				
				window.location.reload();
			}
		}
		
	 }  
	xmlHttp_headerstore.open("GET",url,true)
	xmlHttp_headerstore.send(null)
}

function HeaderAjaxGetCartDetail()
{
	var tmpsiteurl=GetSiteUrl();
	
	var randomnumbaer=GetRandNumber();
	var url;
	
	url=tmpsiteurl+'/ajax_cartupdate.php?mode=getHeaderCartDetails';		
	url=url+'&randomnum='+randomnumbaer;

	xmlHttp_headercartd=GetXmlHttpObject()
	xmlHttp_headercartd.onreadystatechange=function stateChanged() { 
		if (xmlHttp_headercartd.readyState==4 || xmlHttp_headercartd.readyState=="complete")
		{ 
			if(xmlHttp_headercartd.responseText!="")
			{
				document.getElementById("topcheckoutimglink").style.display="inline";
				document.getElementById("topcheckoutimgnotlink").style.display="none";
				
				if(xmlHttp_headercartd.responseText==1)
					document.getElementById("headertopcartdetaildiv").innerHTML="<a href='"+tmpsiteurl+"/cart.php'  style='color:#FFFFFF;text-decoration: none;'>"+xmlHttp_headercartd.responseText+" Item in your cart</a>";
				else if(xmlHttp_headercartd.responseText>1)
					document.getElementById("headertopcartdetaildiv").innerHTML="<a href='"+tmpsiteurl+"/cart.php'  style='color:#FFFFFF;text-decoration: none;'>"+xmlHttp_headercartd.responseText+" Items in your cart</a>";
				else
				{
					document.getElementById("headertopcartdetaildiv").innerHTML="Your cart is empty";
					
					document.getElementById("topcheckoutimglink").style.display="none";
					document.getElementById("topcheckoutimgnotlink").style.display="inline";
				}
			}
		}
	 }  
	xmlHttp_headercartd.open("GET",url,true)
	xmlHttp_headercartd.send(null)
}

function Ajax_addtocartpopupSwatch(tmpswatchpath,tmpitemno)
{
	tmpsiteurl=GetSiteUrl();
	
	var tmpcartswatch=eval('document.getElementById("cartswatch'+tmpitemno+'")');
	var tmpaddtocartdivpopup=eval('document.getElementById("addtocartdivpopup'+tmpitemno+'")');
	
	
	var tmpswtval=tmpcartswatch.value
	if(tmpswtval=="")
	{
		tmpaddtocartdivpopup.style.display="none";
	}
	else
	{
		var tmpswtval_arr=tmpswtval.split("-");
		tmpaddtocartdivpopup.style.display="inline";
		
		var tmpaddtocartswatchimage=eval('document.getElementById("addtocartswatchimage'+tmpitemno+'")');
		tmpaddtocartswatchimage.src=tmpsiteurl+'/resizedimage.php?image_file='+tmpswatchpath+'/'+tmpswtval_arr[1]+'&mwidth=46&mheight=40';
	}
}

function addtocartpopupsubmit(tmpitemno,tmpHideDivName)
{
	if(check_numericcharacters(tmpitemno)) 
	{
		var tmpsiteurl=GetSiteUrl();
		var tmpProductSwatchTotal=eval('document.getElementById("ProductSwatchTotal'+tmpitemno+'")');
		var tmpcartswatch=eval('document.getElementById("cartswatch'+tmpitemno+'")');
		
		var tmpProductSizeTotal=eval('document.getElementById("ProductSizeTotal'+tmpitemno+'")');
		var tmpcartsize=eval('document.getElementById("cartsize'+tmpitemno+'")');
		
		if(tmpProductSwatchTotal.value>0 && tmpcartswatch.value=="")
		{
			alert("Please select colour");
			tmpcartswatch.focus();
			return false;
		}
		else if(tmpProductSizeTotal.value>0 && tmpcartsize.value=="")
		{
			alert("Please select size");
			tmpcartsize.focus();
			return false;
		}
		else
		{
			var randomnumbaer=GetRandNumber();
			var url;
			
			
			var tmpshoppingCartProductConfigID=eval('document.getElementById("shoppingCartProductConfigID'+tmpitemno+'")');
			var tmpshoppingCartProductQty=eval('document.getElementById("shoppingCartProductQty'+tmpitemno+'")');
			var tmpshoppingCartProductPrice=eval('document.getElementById("shoppingCartProductPrice'+tmpitemno+'")');
			
			if(tmpProductSizeTotal.value>0)
				url=tmpsiteurl+'/ajax_Addtocart.php?shoppingCartProductConfigID=' + tmpcartsize.value;		
			else
				url=tmpsiteurl+'/ajax_Addtocart.php?shoppingCartProductConfigID=' + tmpshoppingCartProductConfigID.value;		
			
			if(tmpProductSwatchTotal.value>0)
			{
				var tmpswtval=tmpcartswatch.value;
				var tmpswtval_arr=tmpswtval.split("-");
				url=url+'&shoppingCartProductSwatchID=' + tmpswtval_arr[0];
			}
			else
				url=url+'&shoppingCartProductSwatchID=0';
			
			url=url+'&shoppingCartProductQty=' + tmpshoppingCartProductQty.value;
			url=url+'&shoppingCartProductPrice=' + tmpshoppingCartProductPrice.value;
			url=url+'&randomnum='+randomnumbaer;
			
			xmlHttp2=GetXmlHttpObject()
			xmlHttp2.onreadystatechange=function stateChanged() { 
				if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
				{ 
					
					var tmp_addtocart_confirm_button=eval('document.getElementById("addtocart_confirm_button'+tmpitemno+'")');
					var tmp_addtocart_confirm_text=eval('document.getElementById("addtocart_confirm_text'+tmpitemno+'")');
					var tmp_swatchnsizechooser=eval('document.getElementById("swatchnsizechooser'+tmpitemno+'")');
					var tmp_addtocart_confirm_button_close=eval('document.getElementById("addtocart_confirm_button_close'+tmpitemno+'")');
					var tmp_addtocart_confirm_complete_text=eval('document.getElementById("addtocart_confirm_complete_text'+tmpitemno+'")');
					
					tmp_addtocart_confirm_button.style.display="none";
					tmp_addtocart_confirm_text.style.display="none";
					tmp_swatchnsizechooser.style.display="none";
					tmp_addtocart_confirm_button_close.style.display="inline";
					tmp_addtocart_confirm_complete_text.style.display="inline";
					HeaderAjaxGetCartDetail();
					
				}
				
			 }
			xmlHttp2.open("GET",url,true)
			xmlHttp2.send(null)
		}
	}
}

function checkemail(email)
{
	 var str=email;
	 var filter=/^.+@.+\..{2,3}$/
	
	 if (filter.test(str))
		testresults=true
	 else 
	 {
		alert("Please enter a valid email address")
		testresults=false
	 }
	 
	 return (testresults)
}

function check_alphacharacters(value)
{ 
	var re = /^[A-Za-z]+$/;
	//alert(re.test(value));
	if (!re.test(value))
		return false;
	else
		return true;
}

function check_numericcharacters(value)
{ 
	var re = /^[0-9]+$/;
	//alert(re.test(value));
	if (!re.test(value))
		return false;
	else
		return true;
}

function check_alphanumericcharacters(value)
{ 
	var re = /^[0-9A-Za-z\n]+$/;
	//alert(re.test(value));
	if (!re.test(value))
		return false;
	else
		return true;
}
