/**************************************************
The file contains the following funtions

1. validateWholeNumber
2. validateAlphaNumeric
3. CheckDate
4. isValidDateWithoutAlerts
5. TrimString
6. CheckNum
7. isValidDate
8. PrintWindow
9. OpenWindow
***************************************************/
//ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"

var objFirstField
var blnHasError
var strErrorMessage
var strErrorSeparator

blnHasError=false
strErrorSeparator="\n - "
strFirstField=""
strErrorMessage ="The following error(s) were found :";

function ShowErrorAndResetValues()
	{		
		try
		{
			if (objFirstField)
			{
				alert(strErrorMessage );
				objFirstField.focus();
				strErrorMessage ="The following error(s) were found :";
				objFirstField="";
				return false;
			}	
			else
			{
				return true;
			}
		}
		catch(e)
		{
			
		}
	}

function SendMail(objEmail,url)
{
	/*validates and is valid sends mail*/
	
	var objText=document.getElementById(objEmail)
	
	if(validateEmail(objText))
	{
		strNewUrl = url + "?MailTo="+ TrimString(objText.value)
		mailWindow=window.open(strNewUrl,"MailWindow","status=no,resize=no,toolbar=no,scrollbars=no,width=300,height=120,maximize=null")
	}
	
}
function validateEmail(objTextBox)
{
	try
	{
		var str;
		var str = new String(TrimString(objTextBox.value));
		var objRegExp =/^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,3}$/;   // /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
		var flag = str.match(objRegExp);
		
		if (flag == null)
		{
			return false; // invalid string
		}
		else 
		{
			return true; // valid string
		}
	}
	catch (exception){}
}

function validateWholeNumber(source, arguments)
{
	try
	{
		var str;
		var str = new String(arguments.Value);
		
		var objRegExp = /^\d*$/;
		var flag = str.match(objRegExp);
		
		if (flag == null)
		{
			arguments.IsValid = false;
			return ; // invalid string
		}
		else 
		{
			arguments.IsValid = true;
			return ; // valid string
		}
	}
	catch (exception){}
}


function validateAlphaNumeric(source, arguments)
{
	try
	{
		var str;
		var str = new String(arguments.Value);
		
		var objRegExp = /^[a-zA-Z0-9\s]+$/;
		var flag = str.match(objRegExp);
		
		if (flag == null)
		{
			arguments.IsValid = false;
			return ; // invalid string
		}
		else 
		{
			arguments.IsValid = true;
			return ; // valid string
		}
	}
	catch (exception){}
}

function CheckDate(source, arguments)
{
	try
	{
		var dateStr 
		dateStr = new String(arguments.Value);
		//if (isValidDateWithoutAlerts(dateStr)==true) 
		isValidDateWithoutAlerts(dateStr)
		if(strErrMsg=="")
		{
			arguments.IsValid = true;
			return ; // valid string
		}
		else
		{
			source.errormessage= strErrMsg
			arguments.IsValid = false;
			return ; // invalid string
		}		
	}
	catch (exception){}
}


/*
'*****************************************************************
'Function :isValidDateWithoutAlerts
'Purpose  :Checks for valid date but does not alert any message
'Algorithm:
'Input    :Date
'Output   :True/False
'Author   :Dinesh Thakan
'Created On: 14/03/2003
'*****************************************************************
*/
//stores the error message
var strErrMsg
function isValidDateWithoutAlerts(dateStr) 
{
	try
	{
		strErrMsg=""
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
		var matchArray = dateStr.match(datePat); 
		var strMonth;
		if (matchArray == null) 
		{
			strErrMsg="Date is not in a valid format."
			return false;
		}
		month = matchArray[1]; 
		day = matchArray[3];
		year = matchArray[4];
		switch(month)
		{
			
			case "4":
			case "04":
						strMonth="April";
						break;
			case "6":
			case "06":
						strMonth="June"; 
						break;
			case "9":
			case "09":	
						strMonth="September"; 
						break;
			case "11":	
						strMonth="November";
						break;
			
			
		}
		if(year < 1800)
		{
			strErrMsg="Year cannot be less than 1800."
			return false;
		}
		if (year > 2078)
		{
			strErrMsg="Year cannot be greater than 2078";
			return false;
		} 
		if (month < 1 || month > 12) 
		{ 
			strErrMsg="Month must be between 1 and 12."
			return false;
		}
		if (day < 1 || day > 31) 
		{
			strErrMsg="Day must be between 1 and 31."
			return false;
		}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) 
		{
			strErrMsg=strMonth+" "+"does not have 31 days."
			return false;
		}
		if (month == 2) 
		{ 
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day>29 || (day==29 && !isleap)) 
			{
				strErrMsg="February does not have " + day + " days in the year " + year + "."
				return false;
			}
		}
		return true;
	}
	catch (exception){}
}


function TrimString(str)
{
	try
	{
		var st=new String(str)
		stpos=0;
		strln=st.length;
		while(stpos<strln)
		{
			if(st.substr(stpos,1)!=' ')
			{
				break;
			}
			stpos=stpos+1;
		}
		
		edpos=strln-1;
		while(edpos>=stpos)
		{
			if(st.substr(edpos,1)!=' ')
			{
				break;
			}
			edpos=edpos-1;
		}
		var retst=st.substring(stpos,edpos+1);
		return retst;
	}
	catch (exception){}
}

/*
Function to allow numeric entry 
Input:	(1)obj - Object for Textbox
		(2)SubmitOnEnter - Set the Enter Key active for submitting form. Set 0 for making Inactive and Set 1 to make active
	
*/
function CheckNum(eventObject)
{
	try
	{
		if(navigator.family=="IE")
		{	
			if( ((eventObject.keyCode >= 96) && (eventObject.keyCode <= 105)) ||
				((eventObject.keyCode >= 48) && (eventObject.keyCode <= 57)) ||
				(eventObject.keyCode == 8)|| 
				(eventObject.keyCode == 9) ||
				(eventObject.keyCode == 46) || 
				(eventObject.keyCode == 35) || 
				(eventObject.keyCode == 36))
				return true;
			else
				return false;
		}
		else
		{
			alert(window.captureEvents(Event.KEYPRESS));
			if(eventObject.which!=13 && !(eventObject.which>47&&eventObject.which<58||eventObject.which==8||eventObject.which==0)) 
				return false;
		}
	}
	catch (exception)
	{
		alert(exception);
	}
}

function CheckNum(obj,evt)
{
	evt = (evt) ? evt : ((event) ? event : null);
						
	var charCode = (evt.charCode) ? evt.charCode : evt.keyCode
	
	if( ((charCode >= 48) && (charCode <= 57)) ||
		(charCode == 8)|| 
		(charCode == 9))
		return true;
	else
		return false;
	
}

//Function checks for valid date and returns true/false accordingly by Dinesh Thakan Used in Messaging Search Page.
/*
'*****************************************************************
'Function :isValidDate
'Purpose  :Checks for valid date
'Algorithm:
'Input    :Date
'Output   :True/False
'Author   :Dinesh Thakan
'Created On: 16/11/2002
'*****************************************************************
*/

function isValidDate(dateStr) 
{
	try
	{
		var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;
		var matchArray = dateStr.match(datePat);
		var strMonth; 
		if (matchArray == null) 
		{
			alert("Date is not in a valid format.")
			return false;
		}
		month = matchArray[1]; 
		day = matchArray[3];
		year = matchArray[4];
		switch(month)
		{
			
			case "4":
			case "04":
						strMonth=strApril;
						break;
			case "6":
			case "06":
						strMonth=strJune; 
						break;
			case "9":
			case "09":	
						strMonth=strSeptember; 
						break;
			case "11":	
						strMonth=strNovember;
						break;
			
			
		}
		if(year < 1800)
		{
			alert("Year cannot be less than 1800.");
			return false;
		}
		if (year > 2078)
		{
			alert("Year cannot be greater than 2078");
			return false;
		} 
		if (month < 1 || month > 12) 
		{ 
			alert("Month must be between 1 and 12");
			return false;
		}
		if (day < 1 || day > 31) 
		{
			alert("Day must be between 1 and 31.");
			return false;
		}
		if ((month==4 || month==6 || month==9 || month==11) && day==31) 
		{
			alert(strMonth +" "+" does not have 31 days!")
			return false;
		}
		if (month == 2) 
		{ 
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day>29 || (day==29 && !isleap)) 
			{
				alert("February does not have " + day + " days!");
				return false;
			}
		}
		return true;
	}
	catch (exception){}
}

//Function checks for valid date and returns true/false accordingly by Dinesh Thakan Used in Messaging Search Page.
/*
'*****************************************************************
'Function :PrintWindow
'Purpose  :Prints the window
'Algorithm:
'Input    :
'Output   :
'Author   :Rajat Rajvanshi
'Created On: 28-Jul-2003
'*****************************************************************
*/
function PrintWindow()
{
	try
	{
		if (window.print) 
			self.print();
		setTimeout('self.close()',2000);
	}
	catch (exception){}
}

//Function checks for valid date and returns true/false accordingly by Dinesh Thakan Used in Messaging Search Page.
/*
'*****************************************************************
'Function :OpenWindow
'Purpose  :Opens the window for print/word/excel
'Algorithm:
'Input    : Filename, WindowName, ShowMenuBar (0 to hide, 1 to show. this is for menu for Word/Excel output)
'Output   :
'Author   :Rajat Rajvanshi
'Created On: 28-Jul-2003
'*****************************************************************
*/
function OpenWindow(FileName, WindowName, ScrollBar, MenuBar, Resizable,width,height)
{
	try
	{	
		
		var intTop;
		var intLeft;
		var strWindowStatus;
		
		if (window.screen.availWidth > 750)
		{
		//800 x 600 or better
		if (window.screen.availHeight>710)
			{
				intTop = (window.screen.availHeight-500)/2;
				intLeft = (window.screen.availWidth-750)/2
			}
		else
			{
				intTop = (window.screen.availHeight-450)/2;
				intLeft = (window.screen.availWidth-720)/2
			}
		}
		else
		{
			//small screen
			intTop = 5;
			intLeft = 5;
			
		}
		
		if (navigator.family=="gecko")
			height = height+200;
		
		strWindowStatus = "width=" + width + ",height=" + height +",left=" + intLeft + ",top=" + intTop + ",resizable=" + Resizable + ",scrollbars=" + ScrollBar + ",menubar=" + MenuBar 
		
		
		return window.open(FileName,WindowName,strWindowStatus);
	}
	catch(exception){}
}


function HideLayer(pLayer, bCascade) {
	for (var i=0; i<pLayer.length; i++) {
		pLayer[i].visibility = "hide";
		if (bCascade)
			HideLayer(pLayer[i].document.layers, true);
	}
}
		
function HideDivTags() {
	var pDivs  = "";
	var sDivNm = ""		;
	if (navigator.family == "IE") 
		pDivs  = document.all.tags("div");	
	else
		pDivs  = document.getElementsByTagName("div");	

	for (var i=0; i<pDivs.length; i++) {
		sDivNm = pDivs[i].id;
		if (sDivNm.substr(0,3) == "mnu")
			pDivs[i].className = "hide";
	}
}
		
function Toggle(pAnkr, pDivLyr) {
							
	var nArgs = arguments.length;
	var pLayer = document.layers;
	switch (navigator.family) {
		case "IE":
			HideDivTags();
			//alert(document.all["mnuDropA"].id);
		break;
		case "nn":
			HideLayer(pLayer, true);
			//alert(document.layers["mnuDropA"]);
		break;
		case "gecko":
			HideDivTags();
			pDivLyr = document.getElementById(pDivLyr);
		break;
		default:
		break;
	}
	if (nArgs != 0) {				
		// check which browser we are
		var pDynElem = (pLayer ? pDivLyr.visibility : pDivLyr.className);
		
		// be smart about namimg display classes
		pDynElem = (pDynElem = "hide" ? "show" : "hide");
		
		// make the changes to the style or class
		switch(navigator.family) {
			case "nn":
				pDivLyr.visibility = pDynElem;
				if (pDynElem == "show") {
					pDivLyr.left = pAnkr.x;
					pDivLyr.top  = pAnkr.y + 15;
				}
			break;
			default:
				pDivLyr.className = pDynElem;
			break;
		}
	}
	return(false);
}

function OnMouseOutCheck(pDiv) {
	
	switch(navigator.family) {
		case "IE":
			if (!pDiv.contains(event.toElement)) 
				Toggle();
			
		break;
		case "nn":
			Toggle();
		break;
		case "gecko":
			
			//var pDivTag = document.getElementById(pDiv);
					
			//alert(pDivTag.innerHTML)
			//if (pDivTag.innerHTML!="") 
			//	Toggle();
			
		break;
		default:
		break;
	}
}


/*
'*****************************************************************
'Function	:CheckGreaterThanZeroNonDecimal
'Purpose	:Check for values greater than 0 and non decimal
'Algorithm	:
'Input		:value
'Output		:True/False
'Author		:Dinesh Thakan
'Created On	: August 1, 2003
'*****************************************************************
*/
function CheckGreaterThanZeroNonDecimal(numValue)
{
	var Interval = numValue;

	if (isNaN(Interval))
	{
		return false;
	}
	if (Interval.indexOf(".") != -1)
	{
		return false;
	}
	if (Interval <= 0 )
	{
		return false;
	}	
	if (isexpo(Interval))
	{
		return false;
	}	
	return true;
}


/*
'*****************************************************************
'Function :isexpo
'Purpose  :Check for exponential values
'Algorithm:
'Input    :Source, Arguments
'Output   :True/False
'Author   :Akash Bhargava
'Created On: 8/10/2002
'*****************************************************************
*/
function isexpo(str)
{
	var strValue = new String(str)
	//Checks for exponensial values
	if (strValue.indexOf("e") >= 0 || strValue.indexOf("E") >= 0)
	{
		return true;
	}	
	else
	{
		return false;
	}
}
/******************************************************************
Function	: CheckAmount
Purpose		: Checks for valid amount text boxes
Input		: amount, allowDecimal, noOfDigitsAfterDecimal
Output		: Integer value  
			  0 : Valid value
			  1 : Empty value
			  2 : Invalid number
			  3 : More than two digits after decimal number
			  4 : Decimal is not allowed
			  5 : Zero is not allowed
			  6 : Negative value is not allowed
Author		: Abhishek Saraswat
Created On	: September 08, 2003
'******************************************************************/
function CheckAmount(amount, allowDecimal,noOfDigitsAfterDecimal)
{
	var intReturnValue;
	var intAfterDecimal;
		
	try
	{
		intReturnValue = -1;
		
		if (TrimString(amount)=='')
		{
			//Empty String
			intReturnValue = 1;
		}
		else if (isNaN(amount))
		{
			//Invalid Number
			intReturnValue = 2;
		}
		else if (isexpo(amount))
		{
			//Invalid Number
			intReturnValue = 2;
		}
		else if (amount.indexOf(".")>-1 && allowDecimal && amount.substr(amount.indexOf(".")+1,amount.length).length >noOfDigitsAfterDecimal)
		{
			//checks for digits after decimal
			intReturnValue =3
		}
		else if(amount.indexOf(".")>-1 && !allowDecimal)
		{
			//Decimal is not allowed
			intReturnValue =4;
		}
		else if(parseFloat(amount) == 0)
		{
			//Zero is not allowed
			intReturnValue =5;
		}
		else if(amount.indexOf("-")>-1)
		{
			intReturnValue = 6;
		}
		else if(parseFloat(amount) > 99999)
		{
			intReturnValue =7;
		}
		else
		{
			//valid number
			intReturnValue =0;
		}
		
		return intReturnValue;
	}
	
	catch(e){//alert(e.description)
			} 	
}
/******************************************************************
Function	: getMessage
Purpose		: returns message according to error condition
Input		: code
Output		: error message
Author		: Abhishek Saraswat
Created On	: September 08, 2003
'******************************************************************/
function getMessage(code)
{
	try
	{
		switch (code)
		{
			case 1:
				return "cannot be left blank.";
				break;
			
			case 2:
				return "is invalid.";
				break;
			
			case 3:
				return "cannot have more than two numbers after decimal.";
			
			case 4:
				return "cannot have decimal value.";
				break;
				
			case 5:
				return "cannot have zero value.";
				break;
				
			case 6:
					return "cannot have negative value.";
					break;
			case 7:
					return " cannot be greater than 99,999.";
					break;		
			
						
		}
	}
catch(e){}
}



/*'*****************************************************************
'Function :CheckImage
'Purpose  :Check for Images
'Algorithm:
'Input    :Source-- > object whose image value has to be checked
'Output   :True/False
'Author   :tripti rara
'Created On: 16/jan/2004
'*****************************************************************
*/


function CheckImage(fileName)
{
		//Retrieves the file extension from the file name
		fileExtension = fileName.substr(fileName.lastIndexOf(".")+1);
		//Convert the file extension to lowercase to compare
		fileExtension = fileExtension.toLowerCase();
		
		if ( !(fileExtension=="jpg"	|| fileExtension=="jpeg" || fileExtension=="gif" || fileExtension=="bmp" || fileExtension=="png" ))
			return false;
		else
			return true;
}		
var objLCPopWindow;			// label company window
var objCusPopWindow;			// customer window
var objSPPopWindow;				// sales person window
var objLTPopWindow;				// label type window
var objPRPopWindow;				// process window
var objCLPopWindow;				// color window
var objMPopWindow;				// material window
var objSamplePopWindow;			// sample window
var objAdrPopWindow;			// address window
var objSPLPopWindow;			// samples added window
function ClosePopUpWindows()
{
	try
	{
		if(objLCPopWindow!=null)
			objLCPopWindow.close();
			
		if(objCusPopWindow!=null)
			objCusPopWindow.close();
			
		if(objSPPopWindow!=null)
			objSPPopWindow.close();
			
		if(objLTPopWindow != null)
			objLTPopWindow.close();
			
		if(objPRPopWindow!=null)
			objPRPopWindow.close();
			
		if(objCLPopWindow!=null)
			objCLPopWindow.close();
			
		if(objMPopWindow!=null)
			objMPopWindow.close();
			
		if(objSamplePopWindow!=null)
			objSamplePopWindow.close();
			
		if(objAdrPopWindow!=null)
			objAdrPopWindow.close();
			
		if(objSPLPopWindow != null)
			objSPLPopWindow.close();
	}
	catch(e){}
}	

/*
'*****************************************************************
'Function   :fResetTextarea 
'Purpose    :Resets the text area
'Algorithm  :
'Input      :Form object
'Output     :
'Author     :
'Created On :
'*****************************************************************
*/

function fResetTextarea (oForm) 
{
	for (var i = 0; i < oForm.length; i++) 
	{
		if (oForm.elements[i].type == "textarea")
		oForm.elements[i].value = oForm.elements[i].defaultValue;
	}
}

/*****************************************************************
    Function : IsValidEmail
    Purpose  : To validate Email address
    Algorithm:
    Input    : string
    Output   :
    Author   : Avinash Panchal
    Created On:09/10/2003
 *****************************************************************/
function IsValidEmail(strEmail)
{

	// following line define a Regular Expression using pattering matching 
	// for validating Email Address.
	var objRegExp =/^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,6}$/;
 
	var flag=strEmail.match(objRegExp);//validate Email Address.
	
	if(flag==null)	//if not valid Email address.
		return false;
	else
		return true;
}

function CheckDecimal(obj,evt)
{
	try
	{
		var intIndex;
		evt = (evt) ? evt : ((event) ? event : null);
							
							
		var charCode = (evt.charCode) ? evt.charCode : evt.keyCode
		
		if( ((charCode >= 48) && (charCode <= 57)) ||(charCode == 8)|| (charCode == 9) || (charCode==46))
		{
			if(charCode == 46)
			{
				
				intIndex = obj.value.indexOf(".");
				
				if(intIndex!=-1)
					return false;
			}
			return true;
		}
		else
			return false;
		

	}
	catch(e){}
}