/**
 *   This file contains the validations 
 *
 *  @author:	Dilip Nair, Tata Infotech Ltd.
 *
 *  @Creation Date: 06/04/2002 
 *  
 *  @Last Update Date: 
 *
 *  @Source Id: SPZ/S/1132 1020 100
 *
 *  @Change History: 
 */

//Deepak.
//These functions are for LookUp.jsp

function fnSubmit()
{	 
	if (fnValidateNull(document.ccLookUp.newItemCode.value, document.ccLookUp.oldItemCode.value, document.ccLookUp.upcc.value))
	{
		if( !validateAnySpaces(document.ccLookUp.newItemCode.value, 'New Item Code') )
    	{   
    		document.ccLookUp.newItemCode.focus();
    	 	return false;
    	}
		if( !validateAnySpaces(document.ccLookUp.oldItemCode.value, 'Old Item Code') )
    	{   
    		document.ccLookUp.oldItemCode.focus();
    	 	return false;
    	}
		if( !validateAnySpaces(document.ccLookUp.upcc.value, 'UPC') )
    	{   
    		document.ccLookUp.upcc.focus();
    	 	return false;
    	}

		if (!trim(document.ccLookUp.newItemCode.value).length==0)
		{	    		
	    	if( !fnValidateNewItemCd(document.ccLookUp.newItemCode.value, 'New Item Code') )
			{
				return;
			}	
		}	    
		else
		{
			if (!trim(document.ccLookUp.oldItemCode.value).length==0)
			{	    		
				if( !fnValidateOldItemCd(document.ccLookUp.oldItemCode.value, 'Old Item Code') )
				{
					return;
				}	
			}
			else
			{			
				if (!trim(document.ccLookUp.upcc.value).length==0)
				{
					if( !fnValidateUpcc(document.ccLookUp.upcc.value, 'UPC') )
					{
						return;			
					}
				}
			}
		}
						
		document.ccLookUp.newItemCodeHdn.value = document.ccLookUp.newItemCode.value;
		document.ccLookUp.oldItemCodeHdn.value = document.ccLookUp.oldItemCode.value;
		document.ccLookUp.upccHdn.value = document.ccLookUp.upcc.value;
		document.ccLookUp.dcCode.value = document.ccLookUp.distributionCenter.options[document.ccLookUp.distributionCenter.selectedIndex].value;
		document.ccLookUp.submit();
		return true;
    }	    
}


function fnValidateNull(strValue1,strValue2,strValue3)
{
//  Check for NO VALUES entered 
	if(((!strValue1) || (strValue1==""))&&
	   ((!strValue2) || (strValue2==""))&&
	   ((!strValue3) || (strValue3=="")))
	{		
		alert("Please enter New Item Code, Old Item Code or UPC");
		document.ccLookUp.newItemCode.focus();
		return(false);
	}
//  Check for MULTIPLE VALUES entered 
	if(((strValue1) || (!strValue1==""))&& 
	   ((strValue3) || (!strValue3=="")))
	{
		alert("Please enter ONE (New Item Code, Old Item Code or UPC)");
		document.ccLookUp.upcc.focus();
		return false;
	}
	if(((strValue1) || (!strValue1==""))&& 
	   ((strValue2) || (!strValue2=="")))
	{
		alert("Please enter ONE (New Item Code, Old Item Code or UPC)");
		document.ccLookUp.oldItemCode.focus();
		return false;
	}
	if(((strValue2) || (!strValue2==""))&& 
	   ((strValue3) || (!strValue3=="")))
	{
		alert("Please enter ONE (New Item Code, Old Item Code or UPC)");
		document.ccLookUp.upcc.focus();
		return false;
	}
	return true;
}


// function to Validate NEW Item Code - Should be numeric , should be <=7 digits 
function fnValidateNewItemCd(strValue,strName)
{	  		   
   if (!trim(strValue).length==0)
   {   	
        // Check if the entered value is numeric   
	    if (!validateNumber(strValue,strName))
		{	
			document.ccLookUp.newItemCode.select();
			document.ccLookUp.newItemCode.focus();
			return false;
		}
	    //Validation if the number is positive.
	    if (!validatePositiveNumber(strValue,strName))
		{	
			document.ccLookUp.newItemCode.select();
			document.ccLookUp.newItemCode.focus();
			return false;
		}	    
		if (!isaPosNum(strValue))
		{
			alert("Please enter a valid Item Code");
			document.ccLookUp.newItemCode.focus();
			return false;
		}

	    strValue = trim(strValue);
	
		var strLength = strValue.length  
		// Truncate leading zeros
		for (i=0;i<(7-strLength);i++ )
		{
			strValue="0" + strValue;
		}
		document.ccLookUp.newItemCode.value = strValue;
		return true;
  }
  return false;
}


// function to Validate OLD Item Code - Should be numeric , should be <=7 digits 
function fnValidateOldItemCd(strValue,strName)
{	  		   
   if (!trim(strValue).length==0)
   {   	
		// Check if the entered value is numeric   
	    if (!validateNumber(strValue,strName))
		{	
			document.ccLookUp.oldItemCode.select();
			document.ccLookUp.oldItemCode.focus();
			return false;
		}
	    //Validation if the number is positive.
	    if (!validatePositiveNumber(strValue,strName))
		{	
			document.ccLookUp.oldItemCode.select();
			document.ccLookUp.oldItemCode.focus();
			return false;
		}	    		
		if (!isaPosNum(strValue))
		{
			alert("Please enter a valid Item Code");
			document.ccLookUp.oldItemCode.focus();
			return false;
		}

	    strValue = trim(strValue);
	
		var strLength = strValue.length  
		// Truncate leading zeros
		for (i=0;i<(7-strLength);i++ )
		{
			strValue="0" + strValue;
		}
		document.ccLookUp.oldItemCode.value = strValue;
		return true;
  }
  return false;

}


// function to Validate UPC - Should be numeric, positive, 12 characters  
function fnValidateUpcc(strValue,strName)
{
	if (!trim(strValue).length==0)
	{
		if (!validateNumber(strValue,strName))
		{	
			document.ccLookUp.upcc.select();
			document.ccLookUp.upcc.focus();
			return false;
		}

		//Validation if the number is positive.
		if (!validatePositiveNumber(strValue,strName))
		{	
			document.ccLookUp.upcc.select();
			document.ccLookUp.upcc.focus();
			return false;
		}
	
		if (strValue.length!= 12)
		{
			//alert("PLEASE ENTER A CUSTOMER UPC AS A 12-DIGIT NUMBER");
			alert("Please enter a Customer UPC as a 12-Digit Number");
			document.ccLookUp.upcc.focus();
			return false;
		}
		return true;
	}
	return false;
}


//Edit for positive non-zero number:
function isaPosNum(s)
{  
	return (parseInt(s) > 0);
}


//These functions are for ContactUs.html
function fnValidateSubject(sValue, sName)
{  
    if( !validateNull(sValue, sName) )
    {   
    	 document.SVCCContact.subject.focus();
    	 return false;
    }
    return true;	
}


function fnValidateComments(sValue, sName)
{  
    if( !validateNull(sValue, sName) )
    {   
    	 document.SVCCContact.comments.focus();
    	 return false;    	
    }
    return true;	
}


function fnValidateEmail(sValue, sName)
{     
    if( !validateNull(sValue, sName) )
    {   
    	 document.SVCCContact.emailAddress.focus();
    	 return false;
    }    
    if( !validateAnySpaces(sValue, sName) )
    {   
    	 document.SVCCContact.emailAddress.focus();
    	 return false;
    }    
    if( !validateEmailAddress(sValue, sName) )
    {   
    	 document.SVCCContact.emailAddress.select(); 
    	 document.SVCCContact.emailAddress.focus();
    	 return false;
    }
    
    return true;	
} 		


// This function is called when the form is submitted.
function fnValidateContact()
{
	if( !fnValidateSubject(document.SVCCContact.subject.value, 'A Subject') )
		return false;
	if( !fnValidateComments(document.SVCCContact.comments.value, 'Comments or Questions') )
		return false;
 	if( !fnValidateEmail(document.SVCCContact.emailAddress.value, 'Your E-Mail Address') )
		return false;		
	return true;	
}

			