var isClickOnQty = false;
var count=0;
var isReloadMiniCart=null;
var TimerIDArray = new Array();
var TimerIDCount = 0;
var AddToCartArray = new Array();
var AddToCartCount = 0;
var cartItem=0;

function borderOn(val)
{
	var id= "product"+val;
	document.getElementById(id).className='borderOn';
}
	
function borderOff(val)
{
	var id= "product"+val;
	document.getElementById(id).className='borderOff';
}

function proddetail(cat,prod)
{
	location.href="proddetail.php?cat="+cat+"&prod="+prod;
}

function addtocart(form,page)
{
	if(count>0)
		AddToCartArray[AddToCartCount++] = "add('"+form+"','"+page+"',"+AddToCartCount+");";
	else
		add(form,page,0);
}

function add(form,page,cartItemNo)
{
	if(cartItemNo!=0)
		cartItem = cartItemNo;
		
	var formName = form;
	if(page!="cart")
	{
		for (j = 0 ; j < TimerIDCount ; j++)
		{
		    window.clearTimeout(TimerIDArray[j]);
		}
		
		if(form=='mainform0')
		{
			var imagename = document.getElementById("hiddenSmallImage").value;
			document.getElementById("miniCartMsg").innerHTML = '<table border="0" cellspacing="0" cellpadding="0"><tr><td><img src="'+imagename+'" alt="" width="64" height="64"></td><td>Sorry, there is not sufficient stock in this product.</td></tr></table>';
			setOpacity("miniCartMsg",0);
			document.getElementById("miniCartMsg").style.zIndex = "1000";
				
			document.getElementById("miniCartMsg").style.top = (getYOffset()+((document.body.clientHeight-document.getElementById("miniCartMsg").offsetHeight)/2))+"px";
			document.getElementById("miniCartMsg").style.left = (getXOffset()+((document.body.clientWidth-document.getElementById("miniCartMsg").offsetWidth)/2))+"px";
			fadeIn('miniCartMsg',100,300);
			
			TimerIDArray[TimerIDCount++] = window.setTimeout("fadeOut('miniCartMsg',100,1000,'fadoutstart');", 3000);
			TimerIDArray[TimerIDCount++] = window.setTimeout('document.getElementById("miniCartMsg").style.zIndex = "0";', 4000);
		}
		else
		{
			document.getElementById("miniCartMsg").innerHTML="";
			setOpacity("miniCartMsg",0);
			document.getElementById("miniCartMsg").style.display="none";		
			document.getElementById(formName).target = "hiddenFrame";
		}	
	}
	
	if(form!='mainform0')
		document.getElementById(formName).submit();
	else
		return false;	
}

function reloadMiniCart(prodname,prodimage,isAdded)
{
	isReloadMiniCart = true;	
	count +=1;
	if(count==1)
	{
		if(document.getElementById("hiddenIsntCart")!=null)
		{
			if(isAdded!="notadded")
				document.getElementById("miniCartMsg").innerHTML = '<table border="0" cellspacing="0" cellpadding="0"><tr><td><img src="'+prodimage+'" alt="" width="64" height="64"></td><td>'+prodname+' has been added to your order.</td></tr></table>';
			else
			{
				document.getElementById("miniCartMsg").innerHTML = '<table border="0" cellspacing="0" cellpadding="0"><tr><td><img src="'+prodimage+'" alt="" width="64" height="64"></td><td>Sorry, there is not sufficient stock in this product.</td></tr></table>';
				isReloadMiniCart = false;
				count=0;
			}
			if(isAdded!='')
			{
				setOpacity("miniCartMsg",0);
				document.getElementById("miniCartMsg").style.zIndex = "1000";
				
				document.getElementById("miniCartMsg").style.top = (getYOffset()+((document.body.clientHeight-document.getElementById("miniCartMsg").offsetHeight)/2))+"px";
				document.getElementById("miniCartMsg").style.left = (getXOffset()+((document.body.clientWidth-document.getElementById("miniCartMsg").offsetWidth)/2))+"px";
				
				fadeIn('miniCartMsg',100,300);
				TimerIDArray[TimerIDCount++] = window.setTimeout("fadeOut('miniCartMsg',100,1000,'fadoutstart');", 3000);
				TimerIDArray[TimerIDCount++] = window.setTimeout('document.getElementById("miniCartMsg").style.zIndex = "0";', 4000);
			}
		}
	}
	else
	{
		count=0;		
		if(cartItem<AddToCartCount)
			window.setTimeout(""+AddToCartArray[(cartItem)]+"",1);
	}
	
	if(isReloadMiniCart)
	{
		ajx=new AJAXHandler();
		ajx.setTarget('minicartAjx.php');
		ajx.sendRequest("",'POST',1,"cart,cartprodlist","");
	}
}

function getYOffset() {
    var pageY;
    if(typeof(window.pageYOffset)=='number') {
       pageY=window.pageYOffset;
    }
    else {
		pageY=document.body.scrollTop;
    }
    return pageY;
}

function getXOffset() {
    var pageX;
    if(typeof(window.pageXOffset)=='number') {
       pageX=window.pageXOffset;
    }
    else {
		pageX=document.body.scrollLeft;
    }
    return pageX;
}

function submitinquery()
{
	var name = document.getElementById('realname').value.replace(/^\s+|\s+$/g, '');
	var designation = document.getElementById('designation').value.replace(/^\s+|\s+$/g, '');
	var organisation = document.getElementById('organisation').value.replace(/^\s+|\s+$/g, '');
	var phone = document.getElementById('phone').value.replace(/^\s+|\s+$/g, '');
	var fax = document.getElementById('fax').value.replace(/^\s+|\s+$/g, '');
	var email = document.getElementById('email').value.replace(/^\s+|\s+$/g, '');
	var inquiry = document.getElementById('inquiry').value.replace(/^\s+|\s+$/g, '');
	
	if(name=="" || designation=="" || organisation=="" || phone=="" || fax=="" || email=="" || inquiry=="" || !isValidEmail(email))
	{
		if(name=="")
		{
			alert("Please complete \"Name\".");
			document.getElementById('realname').focus();
		}
		else if(designation=="")
		{
			alert("Please complete \"Designation\".");
			document.getElementById('designation').focus();
		}
		else if(organisation=="")
		{
			alert("Please complete \"Organisation\".");
			document.getElementById('organisation').focus();
		}
		else if(phone=="")
		{
			alert("Please complete \"Phone\".");
			document.getElementById('phone').focus();
		}
		else if(fax=="")
		{
			alert("Please complete \"Fax\".");
			document.getElementById('fax').focus();
		}
		else if(email=="")
		{
			alert("Please complete \"E-mail\".");
			document.getElementById('email').focus();
		}
		else if(!isValidEmail(email))
		{
			alert("Please enter a valid \"E-mail\".");
			document.getElementById('email').focus();
		}
		else if(inquiry=="")
		{
			alert("Please complete \"Inquiry\".");
			document.getElementById('inquiry').focus();
		}
	}
	else
		document.mainform.submit();
}

function isValidEmail(val)
{
	var emailVal = val;
	apos=emailVal.indexOf("@");
	dotpos=emailVal.lastIndexOf(".");
	
	if (apos<1||dotpos-apos<2 || !(emailVal.match(/^[\w\d!#$%&'*+-\/=?^`{|}~]+(\.[\w\d!#$%&'*+-\/=?^`{|}~]+)*@([a-z\d][-a-z\d]*[a-z\d]\.)*[a-z][-a-z\d]*[a-z]$/))) 
	{
		return false;
	}
	else
	{
		return true;
	}
}

//http://www.codeproject.com/KB/ajax/Flash_user_confirmation.aspx?display=Print
function setOpacity(id, level) {
    var element = document.getElementById(id); 
    element.style.display = 'block';
    element.style.zoom = 1;
    element.style.opacity = level;
    element.style.MozOpacity = level;
    element.style.KhtmlOpacity = level;
    element.style.filter = "alpha(opacity=" + (level * 100) + ");";
}

//http://www.codeproject.com/KB/ajax/Flash_user_confirmation.aspx?display=Print
function fadeOut(id, steps, duration,val) {
    var fadeOutComplete;       
    for (i = 0; i <= 1; i += (1 / steps)) {
    	TimerIDArray[TimerIDCount++] = setTimeout("setOpacity('" + id + "', "  + 
                (1 - i) + ")", i * duration);
      fadeOutComplete = i * duration;
    }
    TimerIDArray[TimerIDCount++] = setTimeout("hide('" + id + "')", fadeOutComplete);
} 

function fadeIn(id, steps, duration, interval, fadeOutSteps, fadeOutDuration){  
   
	var fadeInComplete;
    for (i = 0; i <= 1; i += (1 / steps)) {
      var istr = i+"";
      istr = istr.substring(0, 4);
      if(istr=="0.99")
    	  i=1;
      TimerIDArray[TimerIDCount++] = setTimeout("setOpacity('" + id + "', " + i + ")", i * duration); 
      fadeInComplete = i * duration;             
    }
    //set the timeout to start after the fade in time and the interval to display the 
    //message on the screen have both completed

    TimerIDArray[TimerIDCount++] = setTimeout("fadeOut('" + id + "', " + fadeOutSteps + 
               ", " + fadeOutDuration + ")", fadeInComplete + interval);
}


//http://www.codeproject.com/KB/ajax/Flash_user_confirmation.aspx?display=Print
function hide(id)
{
    document.getElementById(id).style.display = 'none';
}

function AJAXHandler()
{
	var target=null;
	var respVal=null;
	        	
	this.setTarget=function(target)
	{
		this.target=target;
	}
	 	
	this.createRequestObject=function(){
	 		
		var httpRequest=false;
		if(window.XMLHttpRequest){
			httpRequest=new XMLHttpRequest();
		}
		else if(window.ActiveXObject){
			httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
		}
		else{
			alert("This page will not work as desired, lack of support from browser");
		}
			
		return httpRequest;
	}

	var xmlhttp=this.createRequestObject();
	var flag=null;
	var div=null;
	var divsOfPage=null;
	var divs=null;

	this.sendRequest=function(paramslist,method,flagPrm,divId,divSet)
	{
		params=paramslist; 		
		flag=flagPrm; 		
		div = divId;
		divsOfPage = divSet;
		
		if(method=='POST')
		{
			xmlhttp.open("POST",this.target,true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.setRequestHeader("Content-length", params.length);
			xmlhttp.setRequestHeader("Connection", "close");
			xmlhttp.onreadystatechange=this.handleResponce;
			xmlhttp.send(params);
		}
		else if(method=='GET')
		{
			xmlhttp.open("GET",this.target+params,true);
			xmlhttp.onreadystatechange=this.handleResponce;
			xmlhttp.send(null);
		}
			 		    
	}

	this.handleResponce=function()
	{
		if(xmlhttp.readyState==4)
		{
			responce=xmlhttp.responseText;
		
			if(flag==1)
			{
				var divArr =  div.split(",");
				var responceArr = responce.split("{cartitems}");				
				document.getElementById(divArr[0]).innerHTML = responceArr[0];
				if(document.getElementById(divArr[1])!= null)
					document.getElementById(divArr[1]).innerHTML = responceArr[1];
			}
	   }
	   else
	   {
		   if(flag==2)
		   {
			   var divArr =  div.split(",");
			   if(document.getElementById(divArr[0])!= null)
				   document.getElementById(divArr[0]).innerHTML = '<div id="contact"><a href="contact.php"><img src="images/contact.gif" alt="Contact Us" title="Contact Us" width="20" height="78" border="0"></a></div> <p>Shopping Basket</p><p class="details">Updating...</p><table border="0" cellspacing="0" cellpadding="0"><tr><td><a href="cart.php"><img id="basketHeader" src="images/basket.gif" alt="Basket" title="Basket" width="60" height="20" border="0" onmouseover="buttonOn(\'basketHeader\',\'images/basket.gif\');" onmouseout="buttonOff(\'basketHeader\',\'images/basket.gif\');"></a></td><td><a href="cart.php"><img id="checkoutHeader" src="images/checkout.gif" alt="Checkout" title="Checkout" width="72" height="20" border="0" onmouseover="buttonOn(\'checkoutHeader\',\'images/checkout.gif\');" onmouseout="buttonOff(\'checkoutHeader\',\'images/checkout.gif\');"></a></td></tr></table>';
			   //if(document.getElementById(divArr[1])!= null)
				   //document.getElementById(divArr[1]).innerHTML = '<li>Updating..</li>';
		   }
	   }
 	}
}