var isAjaxActive=0;

function PostForm(theForm, value){	
	theForm.item.value = value;
}

function handleDiscount(itemvalue){	
	//theForm.item.value = value;
	document.getElementById("item").value=itemvalue;	
//	openAWindow("discountHandler.php?pid="+itemvalue,"OMAP2EVM",300,230,true);
	openAWindow("discountHandler.php?pid="+itemvalue,'MultimediaDaughterCard',300,230,true);
	//openAWindow("http://google.com","Product on discount",300,200,true);
}

function openAWindow( pageToLoad, winName, width, height, center)
{ 
   xposition=0; yposition=0;
   if ((parseInt(navigator.appVersion) >= 4 ) && (center)){
   	xposition = (screen.width - width) / 2;
    yposition = (screen.height - height) / 2;
   } 
   
	//0 => no
	//1 => yes
	
	var args = "";
		args += "width=" + width + "," + "height=" + height + ","
		+ "location=0,"
		+ "menubar=0,"
		+ "resizable=0,"
		+ "scrollbars=0,"
		+ "statusbar=false,dependent,alwaysraised,"
		+ "status=false,"
		+ "titlebar=no,"
		+ "toolbar=0,"
		+ "hotkeys=0,"
		+ "screenx=" + xposition + ","  //NN Only
		+ "screeny=" + yposition + ","  //NN Only
		+ "left=" + xposition + ","     //IE Only
		+ "top=" + yposition;           //IE Only
		//fullscreen=yes, add for full screen
		
		//var args = 
		var dmcaWin = window.open(pageToLoad,winName,args);
		dmcaWin.focus();
		
	//window.showModalDialog(pageToLoad,"","dialogWidth:650px;dialogHeight:500px");	
	
}

function PostViewCart(theForm) 
{
	document.login.action = "../user/validatelogin.php?from=1";
	document.login.method="post";
	document.login.submit();
}

/*
function SubmitForm(type)
{	
	if(type == 'update')
	{
		document.login.SubmitType.value = "update";
		document.login.action = "viewcart.php";
		document.login.method="post";
		document.login.submit();
	}
	else if(type == 'pro_checkout')
	{
		document.login.SubmitType.value = "pro_checkout";
		document.login.action = "../user/validatelogin.php?from=1";
		document.login.method="post";
		document.login.submit();
	}		
}
*/
/*
function DisplayCart()
{   
   var url="displaycart.php";			
	if(makAx())
	{
		callCart(url);
	}	
}
*/
function ValidateInteger(input)
{
	var str=input;
	//var filter=/[^0-9\s]/;
	var filter=/(^-?\d\d*$)/;

	if (filter.test(str))
	{
		return false;
	}
	else
	{
		return true;
	}
}
function test(product_id,qty)
{	
	if (ValidateInteger(qty))
	{
		//alert('Qty must be an integer value');
		document.getElementById('errdisp').innerHTML = 'Quantity must be an integer value';
		box='txtQty'+product_id;	
		document.getElementById(box).value=1;	
		document.getElementById(box).focus();		
	}
}

function addQty(product_id, frm) {
	qty = parseInt(frm.value) + 1;
	QtyChange(product_id, qty);
}

function subtractQty(product_id, frm) {
	qty = parseInt(frm.value) - 1;
	QtyChange(product_id, qty);
}

//function for quantity change
function QtyChange(product_id, qty){
	if (!ValidateInteger(qty)) {	
		if(qty >= 1) {
			if(qty > 9999) {
				document.getElementById('errdisp').innerHTML = 'Quantity cannot be more than 9999 ';
				box = eval('document.login.txtQty' + product_id);
				box.value=9999;
				qty=9999;
			} else {
				document.getElementById('errdisp').innerHTML = "";
			}
		} else {	
			document.getElementById('errdisp').innerHTML = 'Quantity cannot be less than 1 ';
			box = eval('document.login.txtQty' + product_id);
			box.value=1;
			qty=1;
		}
	} else {
		document.getElementById('errdisp').innerHTML = 'Quantity must be an integer value';
		box = eval('document.login.txtQty' + product_id);
		box.value=1; 
		qty=1;
	}
	var url="displaydiscountcart.php?product_id="+product_id+"&qty="+ qty+"&action=qtychange"+"&"+Math.random();
	isAjaxActive = 1;
	if(makAx()) {
		callCart(url);
	}
	//isAjaxActive = 0;
}

function continue_shopping()
{  
	if(isAjaxActive!=1) {
		location.href='webstore.php?cat_id=1';
	}
}

function ValidateDiscountCode(input)
{
	var str=input;
	var filter=/[^A-Za-z0-9\s]/;

	if (filter.test(str))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function DiscountCodeVerify(pid,code)
{	
    var product_id='D'+pid;
	if(ValidateDiscountCode(code))
	{
		document.getElementById('errdisp').innerHTML = '';
		var url="displaydiscountcart.php?product_id=D"+pid+"&code="+ code+"&action=codeverify"+"&"+Math.random();				
		if(makAx())
		{
			callCart(url);
		}
	}
	else
	{
		document.getElementById('errdisp').innerHTML = '';
		document.getElementById('errdisp').innerHTML = 'Promotion Code contain special characters';		
		value='D'+product_id;		
		document.login.value.focus();
	}
}

function ShowDiscount()
{
	document.login.action = "../user/validatelogin.php?from=4";
	document.login.method="post";
	document.login.submit();
	/*
	var url="displaydiscountcart.php?action=discount"+"&"+Math.random();						
	if(makAx())
	{
		callCart(url);
	}
	*/
}
function HideDiscount()
{	
	var url="displaydiscountcart.php?action=discounthide"+"&"+Math.random();		
	if(makAx())
	{
		callCart(url);
	}		
}

function RemoveItem(id)
{
	$return=confirm('Do you Want to Remove?');
	if($return == true)
	{		
		var id='RM_'+id;		
		var url="displaydiscountcart.php?product_id="+id+"&action=remove&"+Math.random();						
		if(makAx())
		{
			callCart(url);
		}		
	}	
}

function QtyInc(productid,qty)
{
	qty=parseInt(qty)+1;
	box='txtQty'+productid;	
	document.getElementById(box).value=qty;
}

function QtyDec(productid,qty)
{
	qty=parseInt(qty)-1;
	box='txtQty'+productid;	
	document.getElementById(box).value=qty;
}

function shippingdata(theForm,gTotal,sales_tax)
{
	var value;
	value=document.frmpost.cboShippingType.value;	
		
	var type;
	
	if(value == "Direct Billing to Customer A/C")
	{
		document.getElementById('uniquename').style.display = "block";
		type="CustomerCost";
		via="";
	}	
	else if(value == "USA - Overnight")
	{		
		document.getElementById('uniquename').style.display = "none";
		type="DomesticOvernight";
		via="via Overnight Delivery";
	}
	else if(value == "USA - Standard")
	{		
		document.getElementById('uniquename').style.display = "none";
		type="DomesticStandard";
		via="via Standard Delivery";
	}
	else if(value == "International")
	{		
		document.getElementById('uniquename').style.display = "none";
		type="International";
		via="via International Delivery";
	}		
	else
	{
		document.getElementById('uniquename').style.display = "none";
		type='';
		via="";
	}
	
	var url="calculateshippingcost.php?gType="+gTotal+"&tax="+sales_tax+"&value="+value+"&type="+type+"&via="+via+"&action=shippingtype"+"&"+Math.random();				
	if(makAx())
	{
		callShipping(url);
	}
	
	
}

function hideShippingType()
{
	if(document.getElementById('cboShippingType').value == 'Direct Billing to Customer A/C')
	{
		document.getElementById('uniquename').style.display = "block";
	}
	else
	{
		document.getElementById('uniquename').style.display = "none";
	}
}

function ValidateViewBill(theForm, only_android, only_custom_service)
{
	var focused = false;
	var errortext = '';
	var emptytext = 'Please provide information for ';
	var invalidtext = 'Please provide valid information for ';
	
	document.getElementById('errdisp').innerHTML = '';
	
	/* Comments Control Validation 
	if(theForm.comments.value == '')
	{
		errortext +=  ("Please provide your comments\n");
		emptytext += ("Comments,");
		if (!focused) theForm.comments.focus();
		focused = true;
		document.getElementById('cmnt').style.color='red';
	}
	else
	{
		document.getElementById('cmnt').style.color='black';
	}
	*/
	
	/* License aggrement Validation */
	if(theForm.license.checked == false)
	{
		errortext +=  ("Please check on 'I Accept' after reading the Ordering Terms\n");
		emptytext += ("Licence,");
		if (!focused) theForm.license.focus();
		focused = true;
		document.getElementById('li').style.color='red';
	}
	else
	{
		document.getElementById('li').style.color='black';
	}	
	if (theForm.cboShippingType.value== '' && only_android == '0' && only_custom_service == '0')
	{
		errortext += ("Please specify Shipping Type.\n");
		emptytext += ("Shipping Type.,");
		if (!focused) theForm.cboShippingType.focus();
		focused = true;
		document.getElementById('spt').style.color='red';
	}	
	else if (theForm.cboShippingType.value== 'Direct Billing to Customer A/C')
	{
		document.getElementById('spt').style.color='black';
		document.getElementById('canName1').style.color='black';
		if(theForm.Carrier_Name.value != '')
		{		
			if(theForm.Carrier_Name.value == 'Others' && theForm.Carrier_Others_Name.value == '')
			{
				errortext += ("Please specify the Courier Name.\n");
				emptytext += ("Courier Name,");
				if (!focused) theForm.Carrier_Others_Name.focus();
				focused = true;
				document.getElementById('canName').style.color='red';
			}
			else if( theForm.Carrier_Others_Name.value != '' && (! ValidateCompany(theForm.Carrier_Others_Name.value)) )
			{
				errortext += ("Please specify valid Courier Name.\n");
				emptytext += ("Courier Name,");
				if (!focused) theForm.Carrier_Others_Name.focus();
				focused = true;
				document.getElementById('canName').style.color='red';				
			}
			else
			{
				document.getElementById('canName').style.color='black';
			}
			
			if (theForm.Carrier_Name.value != '' && theForm.Carrier_Acc_No.value == '')
			{
				errortext += ("Please specify Courier Account No.\n");
				emptytext += ("Account No.,");
				if (!focused) theForm.Carrier_Acc_No.focus();
				focused = true;
				document.getElementById('can').style.color='red';
			}	
			else if( theForm.Carrier_Acc_No.value != '' && (! ValidateCompany(theForm.Carrier_Acc_No.value)) )
			{
				errortext += ("Please specify valid Courier Account No.\n");
				invalidtext += ("Courier  Account No.,");
				if (!focused) theForm.Carrier_Acc_No.focus();
				focused = true;
				document.getElementById('can').style.color='red';
			}
			else
			{
				document.getElementById('can').style.color='black';				
			}
		}
		else
		{
			errortext += ("Please specify the Courier Name.\n");
			invalidtext += ("Courier Name.,");
			if (!focused) theForm.Carrier_Name.focus();
			focused = true;
			document.getElementById('canName1').style.color='red';
		}
	}	
	
	
	if( errortext == '')
	{	
		/*
		if(document.frmpost.subtype.value == '1')
		{
			document.frmpost.action = "process.php";
		}
		else if(document.frmpost.subtype.value == '2')
		{			
			document.frmpost.action = "viewbill.php";
		}
		else if(document.frmpost.subtype.value == '3')
		{			
			document.frmpost.action = "viewbill.php";
		}
		else
		{
			document.frmpost.action = "viewbill.php";
		}
		document.frmpost.submit();			// Submit the page
	    */	
		return true;
	}
	else
	{
        alert (errortext);
		document.getElementById('errdisp').innerHTML =	'Please provide valid information for all the required fields marked in \'Red\'';
		window.scroll(0, 0);		
		return false;
	}
}

function ValidateViewBillPreview(frmpost)
{
	if(document.frmpost.subtype.value == '1')
	{
		document.frmpost.action = "process.php";
	}
	else if(document.frmpost.subtype.value == '2')
	{			
		document.frmpost.action = "viewbill.php";
	}
	else if(document.frmpost.subtype.value == '3')
	{			
		document.frmpost.action = "viewbill.php";
	}
	else
	{
		document.frmpost.action = "viewbill.php";
	}
	//alert(document.frmpost.subtype.value);	
	//document.frmpost.submit();			// Submit the page
	return true;
}

function ValidateCompany(name)
{
	var str = name;
	var filter=/[^A-Za-z0-9\-_\s\.\,\#]/;

	if (filter.test(str))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function submittype(value)
{	
	document.frmpost.subtype.value=value;
}

function backButtonOverride()
{
  // Work around a Safari bug
  // that sometimes produces a blank page
  setTimeout("backButtonOverrideBody()", 1);  

}

function backButtonOverrideBody()
{
  // Works if we backed up to get here
  try {
    history.forward();
  } catch (e) {
    // OK to ignore
  }
  // Every quarter-second, try again. The only
  // guaranteed method for Opera, Firefox,
  // and Safari, which don't always call
  // onLoad but *do* resume any timers when
  // returning to a page
  setTimeout("backButtonOverrideBody()", 500);
}
