/*
-------------------------------------------------------------------------------------------------------------	
	Description	:	This file contains general purpose library functions. 
-------------------------------------------------------------------------------------------------------------	
*/	

	// Global constants for the error messages
	var Err_Quantity = "Please enter correct quantity."
	
	// category constants
	var Err_CategoryName = "Please enter correct category name. It cann't cantain more than 30 characters."
	var Err_CategoryImage = "Please enter category image."	
	var Err_CategoryDescription = "Please enter correct category description."
	var Err_DeliveryCharges = "Please enter correct Delivery Charges."

	// Billing Address constants
	var Err_Billing_Email = "Please enter the correct format of the E-mail Address of billing information." 
	var Err_Billing_FirstName = "Please enter the correct format of billing first name. It can contain alphabets only." 
	var Err_Billing_LastName = "Please enter the correct format of last name. It can contain alphabets only." 
	var Err_Billing_Address = "Please enter the correct format of billing address." 
	var Err_Billing_City = "Please enter the correct format of billing city." 
	var Err_Billing_State = "Please enter the correct format of billing county." 
	var Err_Billing_Country = "Please enter the correct format of billing country." 
	var Err_Billing_ZipCode = "Please enter the correct format of billing zip code." 
	var Err_Billing_PhoneNumber = "Please enter the correct format of billing phone number." 
	var Err_Billing_Fax = "Please enter the correct format of billing fax." 

	// Shipping Address constants
	var Err_Shipping_Email = "Please enter the correct format of the E-mail Address of shipping information." 
	var Err_Shipping_FirstName = "Please enter the correct format of shipping first name. It can contain alphabets only." 
	var Err_Shipping_LastName = "Please enter the correct format of last name. It can contain alphabets only." 
	var Err_Shipping_Address = "Please enter the correct format of shipping address." 
	var Err_Shipping_City = "Please enter the correct format of shipping city." 
	var Err_Shipping_State = "Please enter the correct format of shipping county." 
	var Err_Shipping_Country = "Please enter the correct format of shipping country." 
	var Err_Shipping_ZipCode = "Please enter the correct format of shipping zip code." 
	var Err_Shipping_PhoneNumber = "Please enter the correct format of shipping phone number." 
	var Err_Shipping_Fax = "Please enter the correct format of shipping fax." 
	
	// Custom Product constants
	var Err_Title = "Please enter the correct format of title." 
	var Err_Description = "Description cannot be more than 255 characters." 
	var Err_CustomProductImage  = "Please enter the custom product image." 

	var Err_FirstName = "First name can contain alphabets only. It can not be empty." 
	var Err_MiddleName = "Middle name can contain alphabets only." 
	var Err_LastName = "Last name can contain alphabets only. It can not be empty." 
	var Err_PhoneNumber = "Phone number can contain digits, hiphens and spaces only." 
	var Err_MobileNumber = "Mobile number can contain digits, hiphens and spaces only." 
	var Err_Fax = "Fax can contain digits, hiphens and spaces only." 
	var Err_Address = "Invalid address. It can not be empty." 
	var Err_City = "Please enter the correct format of city." 
	var Err_ZipCode = "Zip Code can contain digits, hiphens and spaces only." 
	var Err_Email = "Invalid Email address." 
	var Err_WebSite = "Invalid web site address." 	
	var Err_ShortDescription = "Invalid short description. It can't be empty."
	var Err_LongDescription = "Invalid description"
	var Err_PageHeading = "Invalid page heading."

	var Err_MenuDescription = "Invalid menu description."
	var Err_UserName = "User Name can contain alphabets, digits and underscores only. It can't have more than 50 characters and can't be empty."
	var Err_Password = "Password can contain alphabets, digits and underscores only. It can't be empty."
	var Err_OldPassword = "Invalid Old password."
	var Err_NewPassword = "Invalid New password."
	var Err_ConfirmPassword = "Invalid Confirm password."
	var Err_Name = "Invalid Name."
	var Err_CompanyName = "Invalid Company Name."
	var Err_ImageName = "Invalid Image Name. It can't have more than 255 characters and can't be empty."

	// Contact us form constants
	var Err_YourName = "Name can contain alphabets and spaces only. It can't be empty."
	var Err_Comments = "Invalid Comments."
		
	// Customer profile form constants
	var Err_CompanyAddress = "Invalid Company Address."
	var Err_Country = "Please enter the correct format of country."

	// product form constants
	var Err_SerialNumber = "Please enter correct serial number."
	var Err_SerialName = "Please enter a valid Name."
	var Err_UnitPrice = "Please enter a valid Unit price"
	var Err_ProductThumbImage = "Please enter thumb image"
	var Err_ProductFullImage = "Please enter full image"
    var Err_Short_Desc = "Please enter valid short description. It cann't have more than 30 characters."
    var Err_Long_Desc = "Please enter valid long description"
	var Err_Content = "Please enter the content."
	var Err_FoundryItemNo = "Please enter the FoundryItemNo."
	var Err_CategoryItemNo = "Please enter the CategoryItemNo."
	var Err_CategoryName = "Invalid Category name."
	var Err_ProductName = "Invalid Product name."
	var Err_CategoryDescription = "Invalid Category description"
	var Err_NewsTitle = "News Title cannot be blank."
	var Err_SectorTitle = "Sector Title cannot be blank."
	var Err_LinkName = "Invalid Link Name."
	var Err_LinkAddress = "Invalid Link Address."
	var Err_ResourceName = "Resource name can contain alphabets only. It can not be empty." 
	var Err_ContactName = "Please enter the Contact name."
	var Err_Subject = "Please enter valid Subject."
	var Err_Body = "Please enter valid Body text."
	
	// Global constants for the validation patterns
	var vld_Name = /^[a-zA-Z&\*:;\-\.\'\"\\ ]*$/
	var vld_Content = /^[a-zA-Z\s\S]*$/
	var vld_CompanyName = /^[a-zA-Z0-9\.]*$/
	var vld_OtherName = /^[a-zA-Z0-9\.]*$/
	var vld_FirstName = /^[a-zA-Z0-9\.\'\-\"\\*: ]*$/
	var vld_MiddleName = /^[a-zA-Z0-9\.\'\-\"\\*: ]*$/
	var vld_LastName = /^[a-zA-Z0-9\.\'\-\"\\*: ]*$/
	var vld_SectorTitle = /^[a-zA-Z0-9\.\\\' ]*$/
	var vld_Description  = /^[a-zA-Z0-9&\'\"\\.:;\-\s\S ]*$/
	var vld_City =  /^[\s\S]*$/
	var vld_State =  /^[\s\S]*$/
	var vld_Country =  /^[\s\S]*$/
	var vld_Address =  /^[\s\S]*$/
	var vld_Title =  /^[\s\S]*$/
	var vld_RealNumber =/^([0-9]*(\.[0-9]*)?)?$/
	var vld_Integer =/^[0-9]*$/
	var vld_AlphaNumeric = /^[a-zA-Z0-9]*$/
	var vld_URL =  /^[\d\D]*$/
	var vld_FilePath =  /^[a-zA-Z]:(\\[a-zA-Z0-9_]+)+$/
	var vld_UserName =  /^[a-zA-Z0-9_]*$/
	var vld_Password =  /^[a-zA-Z0-9_]*$/
	var vld_ZipCode =  /^[\s\S]*$/
	var vld_Phone =  /^[\s\S]*$/
	var vld_Fax =  /^[\s\S]*$/
	var vld_MobileNumber = 	/^[0-9\(\)\- ]*$/
	var vld_Email =  /^(([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?))?$/
	var vld_WebSite =  /^[\s\S]*$/
	var vld_ShortDescription = /^[a-zA-Z0-9&\'\"\s\S ]*$/
	var vld_LongDescription = /^[a-zA-Z0-9&\'\"\s\S ]*$/
	var vld_SerialNumber =  /^[\s\S]*$/
	var vld_FoundryItemNo =  /^[\s\S]*$/
	var vld_CategoryItemNo =  /^[\s\S]*$/
	var vld_CategoryName = /^[\s\S]*$/
	var vld_ProductName = /^[\s\S]*$/
	var vld_ContactName = /^[a-zA-Z0-9&\'\" ]*$/
	var vld_NewsTitle = /^[a-zA-Z0-9&\'\"\- ]*$/
	var vld_LinkName = /^[a-zA-Z&0-9\'\" ]*$/
	var vld_LinkAddress = /^[a-zA-Z&0-9@.: ]*$/
	var vld_ResourceName = /^[a-zA-Z0-9&\'\" ]*$/
	var vld_Subject = /^[\s\S]*$/
	var vld_Body = /^[\s\S]*$/
	
	var gStErrMsg = ""
/*
-------------------------------------------------------------------------------------------------------------	
	Description	:	This functions validates a value against a validation pattern. If the pattern does not 
					matches the value it appends an error message to the error message string. This function 
					is to be used for form validation.
	Parameters 	:	
					pstrPtn -- validation pattern
					pstrVal -- value to be matched against the pattern 
					pstrMsg -- Constant for the error message 
					pstrErrMsg -- Error message string 
					pblnRqd	-- Required or not 						  
					pintMaxLen -- Maximum Length
-------------------------------------------------------------------------------------------------------------	
*/	
	function ValidateField(pstrPtn, pstrVal, pstrMsg, pstrErrMsg, pblnRqd, pintMaxLen)
	{
		pstrVal = trim(pstrVal);
		var lblnVldFld = true 
		// check whether the field is mandatory
		if ( pblnRqd == true && pstrVal == "" )
		{
			lblnVldFld = false
		}
		// maxlength check
		if( pintMaxLen > -1 && pstrVal.length > pintMaxLen)
		{
			lblnVldFld = false	 		
		}
		// validate value against pattern
		if( eval(pstrPtn).test(pstrVal) == false )
		{
			lblnVldFld = false
		}				
		// generate error message if invalid
		if ( lblnVldFld == false )
		{
			//pstrErrMsg = pstrErrMsg + pstrMsg + "\r"
			pstrErrMsg = pstrMsg
		}
		return pstrErrMsg;
	}
	function isDigit (c)
	{
		return ((c >= "0") && (c <= "9"))
	}
	function trim(s) {
		while (s.substring(0,1) == ' ') {
			s = s.substring(1,s.length);
		}
		while (s.substring(s.length-1,s.length) == ' ') {
			s = s.substring(0,s.length-1);
		}
		return s;
	}
