/*
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   File:	                Validate.js
'
'   Description:            Commonly used utility functions for Input Form Validation
'
'   Written by:             Imran khalid
'
'	Language(s) Used:		JavaScript
'
'   Date Written:           Sept 18, 2001
'
'   Date Modify:            Oct 17 2001
'
'   Platform:               Microsoft Internet Explorer
'
'   Copyright:              © 2003 Imran Khalid
'                           All rights reserved
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
*/
function UpdateQty(item)
			{
				itemId = item.name;
				newQty = item.value[item.selectedIndex].text;
				
				document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty;
			}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
function customerPop(Status)
  {
      winCustomer = window.open(Status,'EmailFriend','height=290,width=230,status=0,toolbar=0,resizable,scrollbars=yes');
}
function postDelItem(delfrom){
pid = eval("frmDel" + delfrom + ".part_num.value");
//submitForm = eval("frmDel" + delfrom);
if (confirm("Do you really want to delete this PID="+ pid +" item?")) {
     eval("document.forms['frmDel"+ delfrom +"'].submit()");
 }
}
function validateEditProduct(){
	if(!isEmpty(document.forms['form1'].txtPart_Num))
	if(!isEmpty(document.forms['form1'].txtManu_Part))
	if(!isNumber(document.forms['form1'].txtCost))
	if(!isNumber(document.forms['form1'].txtQty))
	if(!isEmpty(document.forms['form1'].txtDesc))
	
	document.forms['form1'].submit();
}

function validateCartEmail(){
	if(!isEmpty(document.forms['frmSendProduct'].txtEmailAddressTo))
	if(isEmailValid(document.forms['frmSendProduct'].txtEmailAddressTo))
	if(!isEmpty(document.forms['frmSendProduct'].txtEmailAddressFrom))
	if(isEmailValid(document.forms['frmSendProduct'].txtEmailAddressFrom))
	return true;
	return false;
}
function validateFeedbackEmail(){
	if(!isEmpty(document.forms['frmFeedback'].txtEmailAddressTo))
	if(isEmailValid(document.forms['frmFeedback'].txtEmailAddressTo))
	if(!isEmpty(document.forms['frmFeedback'].txtEmailAddressFrom))
	if(isEmailValid(document.forms['frmFeedback'].txtEmailAddressFrom))
	return true;
	return false;
}
function validateFeedbackEmail2(){
	if(!isEmpty(document.forms['frmFeedback'].txtEmailAddressFrom))
	if(isEmailValid(document.forms['frmFeedback'].txtEmailAddressFrom))
	return true;
	return false;
}
function validateEmail(){
	if(!isEmpty(document.forms['frmSave'].txtEmail))
	if(isEmailValid(document.forms['frmSave'].txtEmail))
	return true;
	return false;
}
function validateEmailCartSave(){
	if(!isEmpty(document.forms['frmSaveCart'].txtEmail))
	if(isEmailValid(document.forms['frmSaveCart'].txtEmail))
	document.forms['frmSaveCart'].submit();
}
function validateEmailCartView(){
	if(!isEmpty(document.forms['frmViewCart'].txtEmail))
	if(isEmailValid(document.forms['frmViewCart'].txtEmail))
	document.forms['frmViewCart'].submit();
}
function validateEmailQuoteView(){
	if(!isEmpty(document.forms['frmViewQuote'].txtEmail))
	if(isEmailValid(document.forms['frmViewQuote'].txtEmail))
	document.forms['frmViewQuote'].submit();
}

function validateCustomer(){

	if(!isEmpty2(document.forms['formupdate'].email))
	if(isEmailValid(document.forms['formupdate'].email))
	if(!isEmpty2(document.forms['formupdate'].passwrd))
	return true;
	return false;
}
function validateLister(){

	if(!isEmpty2(document.forms['lister_login'].email))
	if(isEmailValid(document.forms['lister_login'].email))
	if(!isEmpty2(document.forms['lister_login'].passwrd))
	return true;
	return false;
}
function validateNewCustomer(){
	
	if(!isEmpty2(document.forms['form3'].firstn))
	if(!isEmpty2(document.forms['form3'].LastN))
	if(!isEmpty2(document.forms['form3'].email))
	if(isEmailValid(document.forms['form3'].email))
	if(!isEmpty2(document.forms['form3'].Passwrd))
	return true;
	return false;
}
function validateAddNewProduct(){
	if(!isEmpty(document.forms['form1'].txtPart_Num))
	if(!isEmpty(document.forms['form1'].txtManu_Part))
	if(!isEmpty3(document.forms['form1'].comgrpdesc,document.forms['form1'].txtGrpOther))
	if(!isEmpty3(document.forms['form1'].comcatdesc,document.forms['form1'].txtCatOther))
	if(!isEmpty3(document.forms['form1'].comsubdesc,document.forms['form1'].txtSubOther))
	if(!isNumber(document.forms['form1'].txtCost))
	if(!isNumber(document.forms['form1'].txtQty))
	if(!isNumber(document.forms['form1'].txtWeight))
	if(!isEmpty3(document.forms['form1'].txtMFG,document.forms['form1'].txtMFGOther))
	if(!isEmpty(document.forms['form1'].txtDesc))
	
	document.forms['form1'].submit();
}
function validateAddAdminProduct(){
	if(!isEmpty(document.forms['form1'].txtPart_Num))
	if(!isEmpty(document.forms['form1'].txtManu_Part))
	if(!isEmpty3(document.forms['form1'].comgrpdesc,document.forms['form1'].txtGRPCode))
	if(!isEmpty3(document.forms['form1'].comcatdesc,document.forms['form1'].txtCATCode))
	if(!isEmpty3(document.forms['form1'].comsubdesc,document.forms['form1'].txtSUBCode))
	if(!isNumber(document.forms['form1'].txtCost))
	if(!isNumber(document.forms['form1'].txtQty))
	if(!isNumber(document.forms['form1'].txtWeight))
	if(!isEmpty(document.forms['form1'].txtMFG))
	if(!isEmpty(document.forms['form1'].txtDesc))
	
	document.forms['form1'].submit();
}
function isNumber(vField) { 
str = vField.value;
isPrice = /^\d/;  
status= isPrice.test( str ); 
	if (status == "true" && str >0){
	 return false;
	 }
	else {
		vField.focus();
		alert(vField.name +" is Required. Please Fill numeric value");
		SetHighlight(vField, true);
		return true;
	}
} 
function isEmpty2(vField){

	if(vField.value.length!=0)
	{	
		SetHighlight(vField, false);
		return false;
	}
	else
	{

		vField.focus();
		alert(vField.name +" is Required. Please Fill in");
	
		SetHighlight(vField, true);
		return true;
	}
}

function isEmpty(vField){

	if(vField.value.length!=0)
	{	
		SetHighlight(vField, false);
		return false;
	}
	else
	{

		vField.focus();
		alert(vField.name.substring(3)+" is Required. Please Fill in");
	
		SetHighlight(vField, true);
		return true;
	}
}
//////////////////////////////////////////////////////////////////////////
function isEmpty3(vField, vField2){

	if(vField.value.length!=0 || vField2.value.length!=0)
	{	
		SetHighlight(vField2, false);
		return false;
	}
	else
	{

		vField2.focus();
		alert(vField2.name.substring(3)+" is Required. Please Fill in");
	
		SetHighlight(vField2, true);
		return true;
	}
}
////////////////////////////////////////////////////////////////////////

function SetHighlight(theElement, Highlight)
{
	if(Highlight)
	{
		theElement.focus();
		if(IsExplorer())
		{
			//theElement.style.borderStyle = "double";
			//theElement.style.borderWidth = 4;
		}
	}
	else
	{
		if(IsExplorer())
		{
			//theElement.style.borderStyle = "inset";
			//theElement.style.borderWidth = 2;
		}
	}
}

////////////////////////////////////////////////////////////////////////
function IsExplorer()
{
	var userAgent = navigator.userAgent;
	
	if(userAgent.indexOf("MSIE") >= 0)
		return true;
	else
		return false;
}
////////////////////////////////////////////////////////////////////////

function isEmailValid(varEMail)
{
	
	if(varEMail.value.indexOf("@")!=0)
		if(varEMail.value.indexOf(".")!=0)
			if(varEMail.value.lastIndexOf("@")!=(varEMail.value.length-1))
				if(varEMail.value.lastIndexOf(".")!=(varEMail.value.length-1))
					if(varEMail.value.lastIndexOf(".")!=-1)
						if(varEMail.value.lastIndexOf("@")!=-1)
							if(varEMail.value.indexOf(" ")<0)
								return true;
	alert("Invalid EMail Address");
	return false;
}
