// JavaScript Document
//============================================================================================================
//~ Checks to see if a required field is blank.  If so, an error message is displayed.
function ForceDataEntry(objField, strFieldName)
{
	// To find out the Value of the control
	var strFieldValue=objField.value;
	if (IsEmpty(strFieldValue))
	{
		alert("You need to enter information for " + strFieldName);
		objField.focus();
		return false;
	}
	return true;
}
//==============================================================================================================

//~ Checks to see if a DropDownList is empty. If so, an error message is displayed.
function ForceComboItems(ddlName, strDdlName)
{
	// To find out the Value of the control
	var ItemCount=ddlName.value;
	if (IsComboEmpty(ItemCount))
		{
			alert("First Select the " + strDdlName);
			ddlName.focus();
			return false;
		}
	return true;
}
//===============================================================================================================
//================================================================================================================
//~ Check whether a given string is empty or not.
function IsEmpty(strTxtBoxValue)
{   if ((strTxtBoxValue == null) || (strTxtBoxValue.length == 0))
	{
		return true;			//^ Returning True, if Empty Text Box found
	}	
}

//=====================================================================================================
//================================================================================================================
//~ Check whether a given DropDown value is empty or not.

function IsComboEmpty(ddlValue)
{   if  (ddlValue =="--Select--")
	{
		return true;			//^ Returning True, if Empty Text Box found
	}	
}

	
//===============================================================================================================
//================================================================================================================
//~ Check whether a given Email is Valid or not.
function IsEmailValid(objField, strFieldName)
{
	// To find out the Value of the control
	var strFieldValue=objField.value;
	if (IsEmpty(strFieldValue))
	{
		return true;
	}
	else
	{
	    var at="@"
		var dot="."
		var lat=strFieldValue.indexOf(at)
		var lstr=strFieldValue.length
		var ldot=strFieldValue.indexOf(dot)
		
		if (strFieldValue.indexOf(at)==-1)
		{
		    alert("Email should be in abc@xyz.com format");
		   objField.focus();
		   return false;
		}
		if (strFieldValue.indexOf(at) == -1 || strFieldValue.indexOf(at) == 0 || strFieldValue.indexOf(at) == lstr)
        {
		   alert("Email should be in abc@xyz.com format");
		   objField.focus();
		   return false;
		}
		if (strFieldValue.indexOf(dot)== lstr || strFieldValue.indexOf(dot)== -1 || strFieldValue.indexOf(dot)== 0  )
        {
		   alert("Email should be in abc@xyz.com format");
		   objField.focus();
		   return false
		}
		if ((ldot+1)  == lstr   )
        {
        
		    alert("Email should be in abc@xyz.com format");
		    objField.focus();
		    return false
		}
		 if (strFieldValue.indexOf(at,(lat+1))!= -1)
         {
		    alert("Email should be in abc@xyz.com format");
		    objField.focus();
		    return false;
		 }
		 if (strFieldValue.substring(lat-1,lat) == dot || strFieldValue.substring(lat+1,lat+2)== dot)
         {
		   alert("Email should be in abc@xyz.com format");
		   objField.focus();
		   return false;
		 }

		 if (strFieldValue.indexOf(dot,(lat+2))== -1)
         {
		    alert("Email should be in abc@xyz.com format");
		    objField.focus();
		    return false;
		 }
		 
		 if (lstr == strFieldValue.indexOf(dot)+2 || lstr == strFieldValue.indexOf(dot)+5)
          {
		       alert("Email should be in abc@xyz.com format");
		       objField.focus();
		       return false;
		       
		      }
		 if (strFieldValue.indexOf(" ")!= -1)
         {
		     alert("Email should be in abc@xyz.com format");
		     objField.focus();
		     return false;
		 }
		return true;
		}
	
	
}
//--------------------------------------------------------------------------------------------------------------------
 //===============================================================================================================
//~ Returns true if the string passed is a valid number
//~ no characters except a -ve sign at the begining is accepted
//~ otherwise, it displays an error message
function ForceNumericEntry(objField, strFieldName)
{
	var strFieldValue = new String(objField.value);
    var intLoopCounter = 0;

	for (intLoopCounter = 0; intLoopCounter < strFieldValue.length; intLoopCounter++)
		//if ((strFieldValue.charAt(intLoopCounter) < '0' || strFieldValue.charAt(intLoopCounter) > '9') && (strFieldValue.charAt(0) != '-'))
		
		if  ((strFieldValue.charAt(intLoopCounter) > '9') && (strFieldValue.charAt(0) != '-'))
		{
			alert(strFieldName + " must be a valid No ");
			objField.focus();
			return false;
		}
	return true;
}
//================================================================================

//==========================================================================================================
// Validates a Date Entered by User 
// Also Forces the User to stick to either "dd/MMM/yyyy" or "dd-MMM-yyyy" Date Formats.

	function isValidDate(objField,strDateName)
	{
     var strDateValue=trimAll(objField.value)
    
    // If Date is Blank, Don't Validate
    if (IsEmpty(strDateValue)) return true;
     
       
     var arr = [];
     arr['JAN'] = 0;
     arr['FEB'] = 1;
     arr['MAR'] = 2;
     arr['APR'] = 3;
     arr['MAY'] = 4;
     arr['JUN'] = 5;
     arr['JUL'] = 6;
     arr['AUG'] = 7;
     arr['SEP'] = 8;
     arr['OCT'] = 9;
     arr['NOV'] = 10;
     arr['DEC'] = 11;
     
     //^ Splitting the date using the divider "/"
     var arrDate = strDateValue.split('/');
     
     //^ If user did not use the divider "/" then,
     if (arrDate.length!=3)
     {
        //^ Splitting the date using the divider "-"
         arrDate = strDateValue.split('-');
     }
     
     // if splitting occured using the correct divider
     if (arrDate.length==3 && strDateValue.length==11)
     {
       //^ d stores date entered by user in Javascript Internal Date Format
       var d = new Date(arrDate[2], arr[arrDate[1].toUpperCase()], arrDate[0]);
     
       //Checking if the date entered is a valid Date or not
       if (!isNaN(d) && d.getFullYear()==arrDate[2] && d.getMonth()==arr[arrDate[1].toUpperCase()] && d.getDate()==arrDate[0])
       {
             return true;
       }
       else
       {
            alert(strDateName + " Valid Date Formats Are : dd/MMM/yyyy or dd-MMM-yyyy");
            objField.focus();
            return false;
       }
     }
     else
     {
        alert("Valid Date Formats Are : dd/MMM/yyyy or dd-MMM-yyyy");
        objField.focus();
        return false;
     }
}

//=============================================================================================================
//=============================================================================================================

// Function for trim any string
function trimAll( strValue )
			 {				
				var objRegExp = /^(\s*)$/;
					      //check for all spaces
					if(objRegExp.test(strValue))
					 {
					strValue = strValue.replace(objRegExp, '');
					if( strValue.length == 0)
						return strValue;
					 }
				        //check for leading & trailing spaces
				    objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
				    if(objRegExp.test(strValue)) 
				     {
					//remove leading and trailing whitespace characters
					strValue = strValue.replace(objRegExp, '$2');
				     }
				return strValue;
			 }


//==================================
function isDecimalValue(fieldName, fieldLabel)
{
    decallowed = 2;  // how many decimals are allowed?
    fieldValue = fieldName.value;
    
    if (isNaN(fieldValue) || fieldValue == "") 
    {
        alert(fieldLabel + " is not a valid number.");
        fieldName.select();
        fieldName.focus();
        return false;
    }
    else 
    {
        if (fieldValue.indexOf('.') == -1) fieldValue += ".";
            dectext = fieldValue.substring(fieldValue.indexOf('.')+1, fieldValue.length);
    
        if (dectext.length > decallowed)
        {
            alert (fieldLabel + " allows up to " + decallowed + " decimal places only.");
            fieldName.select();
            fieldName.focus();
            return false;
        }
        else 
        {
            return true;
        }
    }
}
//================================================================================================================================================================
function ValidateQueryForm()
{
	//# Validating Company Name for blank
	if (!ForceDataEntry(document.QueryForm.txtCompanyName,'Comapany Name')) return false;
	
	//# Validating Company Address for blank
	if (!ForceDataEntry(document.QueryForm.txtCAddress,'Comapany Address')) return false;
		
	//# Validating Contact Person for blank
	if (!ForceDataEntry(document.QueryForm.txtContactPerson,'Contact Person')) return false;
	
	//# Validating Phone No for blank
	if (!ForceDataEntry(document.QueryForm.txtPhoneNo,'Phone No')) return false;
				
	//# Validating Phone No for Numeric
	if (!ForceNumericEntry(document.QueryForm.txtPhoneNo,'Phone No')) return false;
	
	//# Validating Mobile No for blank
	if (!ForceDataEntry(document.QueryForm.txtMobileNo,'Mobile No')) return false;
			
	//# Validating Mobile No for Numeric
	if (!ForceNumericEntry(document.QueryForm.txtMobileNo,'Mobile No')) return false;
					
	//# Validating Email Address for Blank
	if (!ForceDataEntry(document.QueryForm.txtEmailId, 'EmailId')) return false;
	//# Validating Email Address for Valid Format
	if (!IsEmailValid(document.QueryForm.txtEmailId,'Enter proper EmailId')) return false;
	
	//# Drop Down Products should be selected
	if (!ForceComboItems(document.QueryForm.products,'Products')) return false; 
	
	//if (!ForceDataEntry(document.QueryForm.txtQuantity,'Quantity')) return false;
	//if (!ForceNumericEntry(document.QueryForm.txtQuantity,'Quantity')) return false;
	
	//# Drop Down Products should be selected
	if (!ForceComboItems(document.QueryForm.ddlPackingMode,'Packing Mode')) return false; 
	
	//# Drop Down Packing Standard should be selected
	if (!ForceComboItems(document.QueryForm.ddlPackingStd,'Packing Standard')) return false; 
	
	//# Drop Down INCO Terms should be selected
	if (!ForceComboItems(document.QueryForm.ddlIncoTerms,'INCO Terms')) return false; 
	
	//# Drop Down Payment Terms should be selected
	if (!ForceComboItems(document.QueryForm.ddlPayment,'Payment Terms')) return false; 
}
	
