var focusfield="";
var formname="frmenquiry";
var panel_enquiry;
var loadingpanel;
var notifybox;
YAHOO.example.panels = function()
{
	panel_enquiry=new YAHOO.widget.Panel
	(
		"panel_enquiry",
		{ 
			visible:false,
			close:true,
			draggable:true,
			constraintoviewport:true
		}
	); 
	panel_enquiry.render();
	document.getElementById("panel_enquiry").style.display="block";
	panel_enquiry.center();
	
	notifybox=new YAHOO.widget.SimpleDialog
	(
		"notifybox", 
		{
			width : "300px",
			fixedcenter : true,
			visible : false, 
			modal:true,
			constraintoviewport : true,
			buttons : [ { text:"OK", handler:handleSubmit, isDefault:true }]
		}
	);
	notifybox.render();
	notifybox.center();
	document.getElementById("notifybox").style.display="block";
	
	loadingpanel = new YAHOO.widget.Panel
	(
		"loadingpanel",
		{
			width:"240px",
			fixedcenter:true,
			close:false,
			draggable:false,
			zIndex:4,
			modal:true,
			visible:false
		}	
	);
	loadingpanel.render();
	loadingpanel.center();
	document.getElementById("loadingpanel").style.display="block";

	document.getElementById("divmain_content").style.display="block";
}	
YAHOO.util.Event.addListener(window,'load',YAHOO.example.panels);
function addenquiry_onclick(productid)
{
	document.frmenquiry.reset();
	document.frmenquiry.txtproduct_id.value=productid;
	panel_enquiry.center();
	panel_enquiry.show();
	return false;
}
function enquiry_onsubmit()
{
	loadingpanel.show();
	document.getElementById("loadingstatus").innerHTML="Updating, Please wait..";
	var poststr="txtproduct_id="+encodeURIComponent(document.frmenquiry.txtproduct_id.value);
	poststr=poststr+"&txtqty="+encodeURIComponent(document.frmenquiry.txtqty.value);
	mval="0";
	if(document.frmenquiry.chksample_required.checked)
	{
		mval="1";
	}
	poststr=poststr+"&chksample_required="+mval;
	poststr=poststr+"&txtcomments="+encodeURIComponent(document.frmenquiry.txtcomments.value);
	var url="/cart.php?mode=add";
	url=url+"&sid="+Math.random();
	var request2=YAHOO.util.Connect.asyncRequest('POST',url,callback2, poststr);
	return false;
}
var handleSuccess2=function(o)
{
	if(o.responseText !== undefined)
	{
		returnstr=o.responseText;
		
		return_arr=returnstr.split("_~_");
		if(return_arr[0]=="true")
		{
			if((pagename=="productdetails.php")||(pagename=="samplerequest.php"))
			{
				document.getElementById("divbtnadd"+return_arr[1]).innerHTML='<a style="background-image: url(\'/images/incart.gif\');" href="/cart.php">In Enquiry Cart</a>';
			}
			else
			{
				document.getElementById("divbtnadd"+return_arr[1]).innerHTML='<a href="/cart.php"><img src="/images/btnincart.gif" border="0" alt="Already added to enquiry basket" title="Already added to enquiry basket"></a>';
			}
			document.getElementById("divcartsummary").innerHTML=return_arr[2];
			panel_enquiry.hide();
			loadingpanel.hide();
		}
	}
}
var handleFailure2=function(o)
{
	if(o.responseText !== undefined)
	{
		alert("Failure!");
	}
}
var callback2=
{
	success:handleSuccess2,
	failure:handleFailure2,
	argument:['foo','bar']
}
function display_message(msg,mstyle)
{
	if(mstyle=="error")
	{
		notifybox.cfg.setProperty("icon",YAHOO.widget.SimpleDialog.ICON_WARN); 
	}
	notifybox.cfg.setProperty("text",msg); 
	notifybox.show();
}
var handleSubmit=function(o)
{
	
	this.hide();
	if(focusfield!='')
	{
		document.forms[formname][focusfield].focus();
	}
	focusfield="";
}
function validate_search_form()
{
	if(document.frm.txtsearch.value=='')
	{
		alert("Enter keywrods to search!");
		document.frm.txtsearch.focus();
		return false;
	}
	document.frm.action="/search.php";
}
function validate_login_form(dest)
{
	var errstr='';
	var focusfield='';
	if(document.frm.txtuser.value=='')
	{
		errstr=errstr+"Enter email!\n";
		if(focusfield=='')
		{
			focusfield="txtuser";
		}
	}
	if(document.frm.txtpass.value=='')
	{
		errstr=errstr+"Enter password!\n";
		if(focusfield=='')
		{
			focusfield="txtpass";
		}
	}
	if(errstr!='')
	{
		alert(errstr);
		if(focusfield!='')
		{
			document.frm[focusfield].focus();
		}	
		return false;
	}
	document.frm.action="/login.php?dest="+dest;
}
function campaign_onclick(campaignid)
{
	murl="/campaigndetails.php?campaignid="+campaignid;
	ftrs='toolbar=no,menubar=no,location=no,scrollbars=yes,height=550,width=650,left=1,top=1,resizable=yes'
	newWindow = window.open(murl,'campaignWin',ftrs);
	newWindow.focus();
	return false;
}
function help_onclick()
{
	murl="/discounthelp.php";
	ftrs='toolbar=no,menubar=no,location=no,scrollbars=yes,height=550,width=650,left=1,top=1,resizable=yes'
	newWindow = window.open(murl,'discountWin',ftrs);
	newWindow.focus();
	return false;
}
var handleSuccess1 = function(o)
{
	if(o.responseText !== undefined)
	{
		returnstr=o.responseText;
		return_arr=returnstr.split("__");
		if(return_arr[0]=="true")
		{
			document.getElementById("divloading").style.display="none";
			document.getElementById("cart_box").style.display="none";
			if((pagename=="cart.php"))
			{
				window.location.href="/cart.php";
			}
			else
			{
				document.getElementById("cartsummary").innerHTML=return_arr[1];
			}
		}
	}
}

var handleFailure1 = function(o)
{
	if(o.responseText !== undefined)
	{
		alert("Failure!");
	}
}

var callback1 =
{
  success:handleSuccess1,
  failure:handleFailure1,
  argument:['foo','bar']
}
function validate_addtocart()
{
	if(document.frmaddtocart.txtaddqty.value=='')
	{
		alert("Enter qty to add to cart!");
		return false;
	}
	else
	{
		if(!isInt(document.frmaddtocart.txtaddqty.value))
		{
			alert("Enter only integer value for qty to add to cart!");
			return false;
		}
	}
	document.getElementById("divloading").style.display="block";
	var poststr = "txtselected_product="+document.frm.txtselected_product.value+"&txtselected_qty="+document.frmaddtocart.txtaddqty.value;
	var url="/cart.php?mode=add&callfrom="+caller;
	url=url+"&sid="+Math.random();
	var request1=YAHOO.util.Connect.asyncRequest('POST', url, callback1, poststr);
	return false;
}
var handleSuccess = function(o)
{
	if(o.responseText !== undefined)
	{
		returnstr=o.responseText;
		return_arr=returnstr.split("__");
		if(return_arr[0]=="true")
		{
			popUp = document.getElementById('cart_box');
			popUp.style.display="block";
			popUp.style.top = Math.round((document.body.clientHeight/2)-100+document.body.scrollTop)+'px';
			popUp.innerHTML=return_arr[1];
		}
	}
}

var handleFailure = function(o)
{
	if(o.responseText !== undefined)
	{
		alert("Failure!");
	}
}

var callback =
{
  success:handleSuccess,
  failure:handleFailure,
  argument:['foo','bar']
}
function addtocart_onclick(productid,productoptionid)
{
	document.frm.txtselected_product.value=productid+"_"+productoptionid;
	document.frm.txtselected_qty.value=document.frm['txtqty_'+productid+"_"+productoptionid].value;
	var poststr = "txtselected_product="+document.frm.txtselected_product.value+"&txtselected_qty="+document.frm.txtselected_qty.value;
	var url="/cart.php?mode=displayaddform&callfrom="+caller;
	url=url+"&sid="+Math.random();
	var request=YAHOO.util.Connect.asyncRequest('POST', url, callback, poststr);
	return false;
}
function isInt(arg)
{
	if(parseInt(arg).toString()!=arg)
	{
		return false;
	}
	return true;
}
