//#########################################################################################################  
//	# File Name: common.js
//	# File Version: v 1.0
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2007
//	# Last Modified By:
//	# Last modified On:
//######################################################################################################### 

//==================================================================================================== 
//  Function Name : IsEmpty 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : checks whether a field has value or is blank, it returns false if a field 
//  is empty otherwise true. 
//  Parameters: fld : Field name to be check for blank.
//	string msg : Message if field is blank.	
//---------------------------------------------------------------------------------------------------- 

function IsEmpty(fld,msg) 
{ 
	
	fld.value = Trim(fld.value);
	if((fld.value == "" || fld.value.length == 0) && (msg == '')) 
	{ 
		return false; 
	} 
	if(fld.value == "" || fld.value.length == 0) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
//  Function Name : IsEmptyImage 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : checks whether a image field has value or is blank, it returns false if a field 
//  is empty otherwise true. 
//  Parameters: fld : Field name to be check for blank.
//	string msg : Message if field is blank.	
//---------------------------------------------------------------------------------------------------- 
function IsEmptyImage(fld,msg) 
{ 
	if((fld.value == "" || fld.value.length == 0) && (msg=='')) 
	{ 	
		return false; 
	} 
	if(fld.value == "" || fld.value.length == 0) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
//  Function Name : validateTextArea 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : checks whether a text field has crossed maxlimit of characters specified in maxlimit 
//  parameter
//  Parameters: fld : Field name to be check for validation.
//	maxlimit : specify maxlimit character in field to be allow.
//	string msg : Message if field is crosses define limit.	
//---------------------------------------------------------------------------------------------------- 
function validateTextArea(fld,maxlimit,msg)
{
	if(fld.value.length > maxlimit) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	}
	return true; 	
}
function set_focus(id,e)
{
	if(e && e.keyCode == 13)
	{
		if(document.getElementById(id) && document.getElementById(id).style.display != 'none')
		{
			document.getElementById(id).focus();
		}
  		return false; 
	}
	else
	{
  		return true; 
	}


	/*if(e.keyCode == 13)
	{
		document.getElementById(id).focus();		
		return false;
	}
	else
	{
		return true;
	}*/
	
	
	//
}

//==================================================================================================== 
//  Function Name : Trim 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : Removes leading and trailing spaces from field values. 
//  Parameters: fld : Field name to be Trim.
//---------------------------------------------------------------------------------------------------- 
function Trim(fld)
{
	while(''+fld.charAt(0)==' ')
		fld=fld.substring(1,fld.length);
	while(''+fld.charAt(fld.length-1)==' ')
		fld=fld.substring(0,fld.length-1);
	
	while(''+fld.charCodeAt(0)==13 || ''+fld.charCodeAt(0)==10)
		fld=fld.substring(1,fld.length);
	while(''+fld.charCodeAt(fld.length-1)==13 || ''+fld.charCodeAt(fld.length-1)==10)
		fld=fld.substring(0,fld.length-1);
	return fld;
}

//==================================================================================================== 
//  Function Name : IsSelected 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : checks whether a option is selected, it returns false if a option 
//  is selected otherwise true. 
//  Parameters: fld : Field name to be check for selection.
//	string msg : Message if field is selection.	
//---------------------------------------------------------------------------------------------------- 
function IsSelected(fld,msg) 
{ 
	if(fld.value == "" || fld.value == "0" || fld.value.length == 0) 
	{ 
		alert(msg); 
		fld.focus();
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
//  Function Name : IsEmail 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : checks Email validity. Email must have character @ followed by one or more 
//  dots. It returns flase if Email is invalid otherwise true. 
//  Parameters: fld : Field name to be check for email.
//	string msg : Message if field is not valid email.	
//---------------------------------------------------------------------------------------------------- 
function IsEmail(fld,msg) 
{ 
	fld.value = Trim(fld.value);
	//var regex = /^[A-Za-z0-9!#$%&amp;'*+-\/=?^_`{|}~][A-Za-z0-9!#$%&amp;'*+-\/=?^_`{\|\}~.]{0,63}$/
	//var regex = /^[\w-]+(\.[\w]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/ ; 
    var regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
//  Function Name : IsValidString 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : checks if field value contains only alphanumeric and '_' charactes. Also checks 
//  that alphabetical chars. and '_' must have to be come first and followed by 
//  numbers. It returns false if above conditions will not satisfy otherwise true. 
//  Parameters: fld : Field name to be check for validation.
//	string msg : Message if field is not valid.	

//---------------------------------------------------------------------------------------------------- 
function IsValidString(fld,msg) 
{ 
	var regex = /^[_]*[a-zA-Z_]+[a-zA-Z0-9_]+$/; 
	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
//  Function Name : IsPassword 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : checks if field value contains only alphanumeric and '_' charactes. Also checks 
//  that alphabetical chars. and '_' must have to be come first and followed by 
//  numbers. It returns false if above conditions will not satisfy otherwise true. 
//  Parameters: fld : Field name to be check for password validation.
//	string msg : Message if field is not valid password.	
//---------------------------------------------------------------------------------------------------- 
function IsPassword(fld,msg) 
{ 
    fld.value = Trim(fld.value);
	var regex = /^[_]*[a-zA-Z]+[0-9]+[a-zA-Z0-9]*$/; 
	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
// Function Name : IsLen 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : checks if field value has number of characters between two specified limits. 
// It returns false if no. of chars. is < min. length or > max. length 
// otherwise true. 
// Parameters: fld : Field name to be check for length validation.
// minlen : Minimum required  field length.
// maxlen : Maximum required  field length.
// string msg : Message if field is not valid password.	
//---------------------------------------------------------------------------------------------------- 
function IsLen(fld, minlen, maxlen, msg) 
{ 
	if(fld.value.length < minlen || fld.value.length > maxlen) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
	} 
//==================================================================================================== 
// Function Name : IsCurrency 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : checks if Currency value is in proper format i.e. ',' must be after 1(at first place) 
// or 3 digits also dot '.' must be followed by ',' . '$' is optinal as a first char. 
// It returns false if above condition will not satisfy otherwise true. 
// Parameters: fld : Field name to be check for currency validation.
// string msg : Message if field is not valid currency.	
//---------------------------------------------------------------------------------------------------- 
function IsCurrency(fld,msg) 
{ 
	fld.value = Trim(fld.value);
	val = fld.value.replace(/\s/g, ""); 
	
//	regex = /^\$?\d{1,3}(,?\d{3})*(\.\d{1,2})?$/; 
	regex = /^\d{1,3}(,?\d{3})*(\.\d{1,2})?$/; 
	
	if(!regex.test(val))
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
// Function Name : IsPhone 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : checks if phone field has following characters : 0-9, '-', '+', '(' , ')' . 
// It returns false if there are other than above characters otherwise true . 
// Parameters : fld1 - area code to be checked 
// : fld2 - city code to be checked 
// : fld3 - actual phone no to be checked 
// msg - error message to be displayed 
//---------------------------------------------------------------------------------------------------- 
function IsPhone(fld1,fld2,fld3,msg) 
{ 
	/*var regex = /^[\d-+]+$/; 

	var phone = "(" + fld1.value + ")" + fld2.value + "-" + fld3.value; 
	if(!regex.test(phone)) 
	{ 
		alert(msg); 
		fld1.focus(); 
		return false; 
	} */
return true; 
} 
//    # Created By: Amit Sankhala
//    # Created On: 21 January 2008
//    Last Modified By:
//    Last modified On:
// Purpose : checks if zip field value is of length 5 or 9 . (for U.S. zip code). 
// It returns false if it contains alphabetic chars. or length is not as 
// specified. 
// Parameters: fld : Field name to be check for zipcode validation.
// string msg : Message if field is not valid zipcode.    
//---------------------------------------------------------------------------------------------------- 
function IsZip(fld,msg) 
{ 
    var num = /^[0-9]+$/;     
    if(!num.test(fld.value) || (fld.value.length !=5 && fld.value.length !=9)) 
    { 
        alert(msg); 
        fld.focus(); 
        return false; 
    } 
    return true; 
}  
//==================================================================================================== 
// Function Name : IsDate 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : checks if date is valid according to month selected. 
// i.e. Feb must have 28 or 29 days and also April, June, Sept. and Nov. have 
// 30 days. It returns false if above condition will not satisfy otherwise true. 
// Parameters : m - month field 
// d - day field 
// y - year field 
// msg - error message to be displayed 
//---------------------------------------------------------------------------------------------------- 
function IsDate(m,d,y,msg) 
{ 
	var val1= m.value; 
	var val2= d.value; 
	var val3= y.value; 
	if(val2 > daysInFebruary(val3) && val1 == 02) 
	{ 
		alert(msg); 
		d.focus(); 
		return false; 
	} 
	if((val1 == '04' || val1 == '06' || val1 == '09' || val1 == '11' ) && (val2 > '30')) 
	{ 
		alert(msg); 
		d.focus(); 
		return false; 
	} 

	dt= val1 + '/' + val2 + '/' + val3; 
	return true; 
} 
//==================================================================================================== 
// Function Name : allDigits 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : checks if string contains only digits or not. 
// Parameters: fld : Field name to be check for digit validation.
//---------------------------------------------------------------------------------------------------- 
function allDigits(str) 
{ 
	return inValidCharSet(str,"0123456789"); 
} 
//==================================================================================================== 
// Function Name : inValidCharSet 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : checks Given string contais only character within Specified chartacter set. 
// Parameters: fld : Field name to be check for character set validation.
// charset : specified character set which need to be checked.	
//---------------------------------------------------------------------------------------------------- 
function inValidCharSet(str,charset) 
{ 
	var result = true; 
	for (var i=0;i< str.length;i++) 
	if (charset.indexOf(str.substr(i,1))<0) 
	{ 
		result = false; 
		break; 
	} 
	return result; 
} 
//==================================================================================================== 
// Function Name : daysInFebruary 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : To check days in Feb 
// Parameters: fld : Year in which february day count need to check.
//---------------------------------------------------------------------------------------------------- 
function daysInFebruary (year) 
{ 
	// February has 29 days in any year evenly divisible by four, 
	// EXCEPT for centurial years which are not also divisible by 400. 
	return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 ); 
} 
//==================================================================================================== 
// Function Name : checkExpDate 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : Also it checks whether card is expired or not. It returns false if card is 
// expired otherwise true. 
// Parameters: fldMonth : Month field of credit card which need to check.
// fldyear : Year field of credit card which need to check.	
// string msg : Message if expDate is not valid .	
//---------------------------------------------------------------------------------------------------- 
function checkExpDate(fldmonth,fldyear,msg) 
{ 
	var result = true; 
	var expired = false; 
	if (result) 
	{ 
		var month = fldmonth.value; 
		var year = fldyear.value; 
		
		var now = new Date(); 
		var nowMonth = now.getMonth() + 1; 
		var nowYear = now.getFullYear(); 
		expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month)); 
	} 
	if (expired) 
	{ 
	result = false; 
	fldmonth.focus(); 
	alert(msg); 
	} 

	return result; 
} 
//==================================================================================================== 
// Function Name : checkFileType 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : It checks the file type. It must be either doc or pdf. 
// You can add your customize file extension here for checking 
// Parameters: fld : file extension which need to check.
// string msg : Message if file type is not valid .	
//---------------------------------------------------------------------------------------------------- 
function checkFileType(fld,msg) 
{ 
	var regex = /(.doc|.pdf)$/; 
	if(!regex.test(fld.value)) 
	{ 
	alert(msg); 
	fld.focus(); 
	return false; 
	} 
	return true; 
} 

//==================================================================================================== 
// Function Name : checkImageType 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : It checks the image type. It must be either jpg or gif. 
// Parameters: fld : file which need to check.
// string msg : Message if file type is not valid .	
//---------------------------------------------------------------------------------------------------- 
function checkImageType(fld,msg) 
{ 
	var regex = /(.jpg|.jpeg|.JPG|JPEG|.gif|.GIF)$/; 
	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
// Function Name : checkBannerType 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : It checks the image type. It must be either jpg or gif. 
// Parameters: fld : file which need to check.
// string msg : Message if file type is not valid .	
//---------------------------------------------------------------------------------------------------- 
function checkBannerType(fld,msg) 
{ 
	var regex = /(.jpg|.jpeg|.JPG|JPEG|.gif|.GIF)$/; 
	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
// Function Name : IsFileSize 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : It ckecks the size of the image file. 
// YOU CAN CHECK either in terms of width and height of image or size of image in bytes. 
// Parameters: fld : image which need to check.
// string msg : Message if filesize is not valid .	
//---------------------------------------------------------------------------------------------------- 
function IsFileSize(fld,msg) 
{ 
	var img = new Image(); 
	img.src = fld.value; 
	// var Dimensions = img.width + 'x' + img.height; 
	// var File Size = img.fileSize; 
	if(img.fileSize > 102400) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 

	return true; 
} 
//==================================================================================================== 
// Function Name : IsValidColor
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : checks if field value contains only alphanumeric(a to f & 0 to 9). 
// It returns false if above conditions will not satisfy otherwise true. 
// Parameters: fld : field which need to check for color validation.
// string msg : Message if color is not valid .	
//---------------------------------------------------------------------------------------------------- 
function IsValidColor(fld,msg) 
{ 

	var regex = /[a-fA-F0-9]+[a-fA-F0-9]*$/; 

	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
// Function Name : IsRadioBtnChecked 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : checks atleast one radion button is selected from array of radio button. 
// Parameters: fld : field which need to check for radio button validation.
// string msg : Message if no radio button is checked .	
//---------------------------------------------------------------------------------------------------- 
function IsRadioBtnChecked(fld,msg) 
{ 
	if(fld.length) 
	{ 
		for(var i=0 ; i<fld.length ; i++ ) 
			if(fld[i].checked) 
				return true; 
			alert(msg); 
			return false; 
	} 
	else 
	{ 
		if(fld.checked) 
			return true; 
		alert(msg); 
		return false; 
	} 

} 
//==================================================================================================== 
// Function Name : IsCheckBoxChecked 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : checks atleast one checkbox is checked from array of checkbox.
// Parameters: fld : field which need to check for checkbox validation.
// string msg : Message if no checkbox is checked .	
//---------------------------------------------------------------------------------------------------- 
function IsCheckBoxChecked(fld,msg) 
{ 
	if(fld.length) 
	{ 
		for(var i=0 ; i<fld.length ; i++ ) 
			if(fld[i].checked) 
				return true; 
			alert(msg); 
			return false; 
	} 
	else 
	{ 
		if(fld.checked) 
			return true; 
		alert(msg); 
		return false; 
	} 
} 
//==================================================================================================== 
// Function Name : getcheckFieldLength 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : returns length of check field 
// Parameters: fld : field which need to check for checked length.
//---------------------------------------------------------------------------------------------------- 
function getcheckFieldLength(fld) 
{ 

	var counter = 0; 
		if(fld.length) 
		{ 
			for(i=0 ; i<fld.length ; i++ ) 
				if(fld[i].checked) 
					counter++; 
		} 
		else 
		{ 
			if(fld.checked) 
				counter++; 
		} 
	
	return counter; 
} 
//==================================================================================================== 
// Function Name : unCheckAll 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : uncheck all elements of given field 
// Parameters: fld : field which need to uncheck.
//---------------------------------------------------------------------------------------------------- 
function unCheckAll(fld) 
{ 

	if(fld.length) 
	{ 
		for(i=0 ; i<fld.length ; i++ ) 
			if(fld[i].checked) 
				fld[i].checked = false; 
	} 
	else 
	{ 
		if(fld.checked) 
			fld.checked = false; 
	} 

return true; 
} 
//==================================================================================================== 
// Function Name : Clear_CheckboxSelection() 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : uncheck all checkbox field from given form. 
// Parameters: frm : form name whose all checkbox need to uncheck.
//---------------------------------------------------------------------------------------------------- 
function Clear_CheckboxSelection(frm) 
{ 
	with(frm) 
	{ 
		for(var i=0;i<frm.elements.length;i++) 
		{ 
			if(frm.elements[i].type == 'checkbox') 
				frm.elements[i].checked = false; 
		} 
	} 
} 
//==================================================================================================== 
// Function Name : IsPriorDate 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : check given date against current date if date is prior than current date, it will give error. 
// Parameters: fld : date which need to check for prior date validation.
// string msg : Message if no checkbox is checked .	
//---------------------------------------------------------------------------------------------------- 
function IsPriorDate(fld,msg) 
{ 
	today = new Date(); 
	date = today.getDate(); 
	month = today.getMonth() + 1; 
	year = today.getFullYear(); 
	if(date < 10) 
		date = '0'+date; 
	if(month < 10) 
		month = '0'+month; 
	
	var checkdate = ''; 
	checkdate = year+'-'+month+'-'+date; 
	
	if(fld.value < checkdate) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
// Function Name : IsSubsequentDate 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : check given date against current date if date is successive than current date, it will give error. 
// Parameters: fld : date which need to check for prior date validation.
// string msg : Message if no checkbox is checked .	
//---------------------------------------------------------------------------------------------------- 
function IsSubsequentDate(fld,msg) 
{ 
	today = new Date(); 
	date = today.getDate(); 
	month = today.getMonth() + 1; 
	year = today.getFullYear(); 
	if(date < 10) 
		date = '0'+date; 
	if(month < 10) 
		month = '0'+month; 
	
	var checkdate = ''; 
	checkdate = year+'-'+month+'-'+date; 
	
	if(fld.value > checkdate) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
//==================================================================================================== 
// Function Name : DateAdd 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : This function gives new date by adding supplied days, months and year to supplied date. 
// Parameters: startDate : date in which days,months and year need to add.
// numDays : no of days need to add in given date.
// numMonths : no of months need to add in given date.
// numYears : no of years need to add in given date.
//---------------------------------------------------------------------------------------------------- 
function DateAdd(startDate, numDays, numMonths, numYears) 
{ 
	var returnDate = new Date(startDate.getTime()); 
	var yearsToAdd = numYears; 
	
	var month = returnDate.getMonth() + numMonths; 
	if (month > 11) 
	{ 
	yearsToAdd = Math.floor((month+1)/12); 
	month -= 12*yearsToAdd; 
	yearsToAdd += numYears; 
	} 
	returnDate.setMonth(month); 
	returnDate.setFullYear(returnDate.getFullYear() + yearsToAdd); 
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays); 
	
	return returnDate; 

} 
//==================================================================================================== 
// Function Name : roundAccuracy 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : round no to given accuracy 
// Parameters: num : num which need to round off.
// accuracy : decimal point to which given no need to be round off.
//---------------------------------------------------------------------------------------------------- 
function roundAccuracy(num, accuracy) 
{ 
	var factor=Math.pow(10,accuracy); 
	
	return Math.round(num*factor)/factor; 
} 
//==================================================================================================== 
// Function Name : popupWindowURL 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : To open pop window
// Parameters : 
// url = url to be open in the new window 
// winname = winname is the window name for the reference of that window 
// w is the width 
// h is the height 
// menu is the parameter, if you want menubar to be enabled on the window 
// resize if you wanna resize the window 
// scroll if you needed 
// x and y are the co-ordinates where you wld like to show the window on the screen 
// Return : true or false 
//---------------------------------------------------------------------------------------------------- 

function popupWindowURL(url, winname, w, h, menu, resize, scroll, x, y) { 
if (winname == null) winname = "newWindow"; 
if (w == null) w = 600; 
if (h == null) h = 600; 
if (resize == null) resize = 1; 

menutype = "nomenubar"; 
resizetype = "noresizable"; 
scrolltype = "noscrollbars"; 
if (menu) menutype = "menubar"; 
if (resize) resizetype = "resizable"; 
if (scroll) scrolltype = "scrollbars"; 

if (x == null || y == null) { 
cwin=window.open(url,winname,"status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h); 
} 
else { 
cwin=window.open(url,winname,"top=" + y + ",left=" + x + ",screenX=" + x + ",screenY=" + y + "," + "status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h); 
} 
if (!cwin.opener) cwin.opener=self; 
cwin.focus(); 

return true; 
}

function fnCompare(fld1,fld2,msg)
{
	if(fld1.value != fld2.value)
	{
		alert(msg);
		return false;
	}
	else
		return true;
}
//==================================================================================================== 
// Function Name : autoTab 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
// Purpose : It ckecks the size of the image file. 
// YOU CAN CHECK either in terms of width and height of image or size of image in bytes. 
// Parameters: fld : image which need to check.
// string msg : Message if filesize is not valid .	
//---------------------------------------------------------------------------------------------------- 
function autoTab(input, e) 
{
	var isNN = (navigator.appName.indexOf("Netscape")!=-1);

	var keyCode = (isNN) ? e.which : e.keyCode; 
	
	if(keyCode=='9' || keyCode=='0') 
	{
		return true;
	}
	

	return false;
}
function fnGetConfirmation(strMsg,strUrl)
{
	if(confirm(strMsg))
	{
		location.href=strUrl;
	}
	
}
function fnSubmission(strFormName,strAction)
{
	strFormName.action=strAction;
	strFormName.submit();
	//strFormName.
	//eval("document."+strFormName+".action="+strAction);
	//eval("document."+strFormName+".submit()");
	
}
//==================================================================================================== 
//  Function Name : IsValidString 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : checks if field value contains only alphanumeric and '_' charactes. Also checks 
//  that alphabetical chars. and '_' must have to be come first and followed by 
//  numbers. It returns false if above conditions will not satisfy otherwise true. 
//  Parameters: fld : Field name to be check for validation.
//	string msg : Message if field is not valid.	

//---------------------------------------------------------------------------------------------------- 
function chkSplChar(fld,msg)
{
		var strParam=fld.value;
		//var strPat =/^[a-zA-Z0-9\s#.,']+$/;
		var strPat =/^[a-zA-Z0-9\s#.,_\-\/\']+$/;
	
		if(fld.value.length>0)
		{
			if(strParam.match(strPat)==null)
			{
				alert(msg+" can't contain Special Characters.");
				fld.focus(); 	
				return false;
			}	
		}
		return true;
}
function checkResumeType(fld,msg) 
{ 
	var regex = /(.doc)$/; 
	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 

function fnChkLength(fld,intLength,msg)
{
		if(fld.value.length < intLength)
		{
			alert(msg);
			fld.focus();
			return false;
		}
		return true;
}
function fnDateCompare(date1,date2,msg1,msg2)
{
	var dt1=new Date;
	var dt2=new Date;
	var temp1=0;
	var Arrdt=date1.split("-");
	
	temp1=Arrdt[1]-1;
	
	dt1.setDate(Arrdt[2]);
	dt1.setMonth(temp1);
	dt1.setFullYear(Arrdt[0]);
	var Arrdt=date2.split("-");
	
	temp1=Arrdt[1]-1;
	
	dt2.setDate(Arrdt[2]);
	dt2.setMonth(temp1);
	dt2.setFullYear(Arrdt[0]);
	
	if(dt2<=dt1)
	{
			alert(msg2+" should be greater than "+msg1);
			return false;
	}
	return true;
}
function IsNumber(fld,msg) 
{ 
    //fld.value = Trim(fld.value);
	fld = Trim(fld);
	var regex = /^[0-9]*$/; 
	if(!regex.test(fld)) 
	{ 
		alert(msg); 
		return false; 
	} 
	return true; 
}
function Isnumber(fld,msg) 
{ 
    fld.value = Trim(fld.value);
	var regex = /^[0-9]*$/; 
	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
function checkZip(fld,msg) 
{ 
	var regex = /(.zip|.ZIP)$/; 
	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 

function chk_date_new(strdate,message)
{
			var temp=strdate;
				
			var dt =new Date;	
			var Arrdt=temp.split("-");			
			var temp=0;
			var temp1=0;		
			var temp2=0;
			temp=Arrdt[1];
			temp1=Arrdt[2];
			temp2=Arrdt[0];
			
			var regex = /^[0-9]*$/; 
			if(!regex.test(temp) || !regex.test(temp1) || !regex.test(temp2)) 
			{ 
				alert("Please Enter Date in Numeric form"); 				
				return false; 
			}			
			if(temp>12)
			{
				alert(message);			
				return false;
			}
			if(temp==4 || temp==6 || temp==9 || temp==11)
			{
				if(temp1>30)
				{
						alert(message);
						return false;
				}
			}
			if(temp==1 || temp==3 || temp==5 || temp==7 || temp==8 || temp==10 || temp==12)
			{
				if(temp1>31)
				{
						alert(message);					
						return false;
				}
			}
			if(temp==2)
			{
				if(temp1>29 ||(temp1==29 && temp2%4!=0 ))
				{
					alert(message);					
					return false;
				}
			}
		return true;
}

function ShowMemberdiv(divname)
{
		
	if(divname=='profile')
	{
	   	document.getElementById('member_profile').className = "selected";
	   	document.getElementById('member_contact').className = "";
		document.getElementById('profile').style.display= "";
		document.getElementById('contact').style.display= "none";	
	}
	else
	{
		document.getElementById('member_contact').className = "selected";
		document.getElementById('member_profile').className = "";
		document.getElementById('contact').style.display= "";
		document.getElementById('profile').style.display= "none";	
	}
}//function 	

function getTodaysDate()
{
	var today = new Date();
	var m_names = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");

	var curr_date = today.getDate();
	if (curr_date < 10)
	{
		curr_date = "0"+curr_date;
	}
	var curr_month = today.getMonth();
	var curr_year = today.getFullYear();
	var CurrentDate = curr_year + "-" + m_names[curr_month] + "-" + curr_date;

	return(CurrentDate);
}

//==================================================================================================== 
//  Function Name : CountWords 
//	# Created By: Amit Sankhala
//	# Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : To count number of words in atext enetered
//  Parameters: this_fld : Field name to count nuber of words.
//---------------------------------------------------------------------------------------------------- 
function CountWords (this_field, show_word_count, show_char_count)
{
	if (show_word_count == null)
	{
		show_word_count = true;
	}
	if (show_char_count == null) 
	{
		show_char_count = false;
	}
    
	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	if (fullStr.length <2) 
	{
		word_count = 0;
	}
	if (word_count == 1) 
	{
		wordOrWords = " word";
	}
	else
	{
		wordOrWords = " words";
	}
	if (char_count == 1) 
	{
		charOrChars = " character";
	}
	else 
	{
		charOrChars = " characters";
	}
	
	return word_count;
}

function getHTTPObject() {
  var xmlhttp = false;
  if (window.XMLHttpRequest)
  {
    xmlhttp = new XMLHttpRequest();
  }
  else if (window.ActiveXObject)// code for IE
  {
    try
    {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try
      {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp=false;
      }
    }
  }
  
  return xmlhttp;
}

function checkSpecialCharacters(fieldName, message)
{
	exp=/^[a-zA-Z ]+$/;
	if(exp.test(fieldName.value)!=true)
	{
		alert(message);
		fieldName.focus();
		return false;;
	}
	else
	{
		return true;
	}
}

function chkSplChars(fieldName, message)
{
	exp=/^[a-zA-Z,\.\"\'\-\!\;\(\)\? ]+$/;
	if(exp.test(fieldName.value)!=true)
	{
		alert(message);
		fieldName.focus();
		return false;;
	}
	else
	{
		return true;
	}
}
function IsUrl(fld,msg) 
{ 
	var regex=/^(http:\/\/|https:\/\/)([a-zA-Z0-9:\/._])+$/;
	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 
function IsWebsite(fld,msg) 
{ 

	var regex = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.|http:\/\/|https:\/\/){1}([\w]+)(.[\w]+){1,2}.*$/;
	if(!regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
} 

function IsDisplayUrl(fld,msg) 
{ 
	var regex=/^(http:\/\/|https:\/\/)([a-zA-Z0-9:\/._])+$/;
	if(regex.test(fld.value)) 
	{ 
		alert(msg); 
		fld.focus(); 
		return false; 
	} 
	return true; 
}
function fnIsAllLower(strURL) 
{ 
	// define regular expression matching pattern 
	strPattern = "([A-Z].*)"; 

	// create object of RegExp Class 
	objRegExp = new RegExp(strPattern); 
	boolResult = objRegExp.test(strURL); // test that given string is according to pattern 
	if (boolResult)
	{
		return false; 
	}
	else
	{
		return true;
	}
} 

function checkSpecialCharacters(fieldName, message)
{
	exp=/^[a-zA-Z ]+$/;
	if(exp.test(fieldName.value)!=true)
	{
		alert(message);
		fieldName.focus();
		return false;;
	}
	else
	{
		return true;
	}
}

function chkSplChars(fieldName, message)
{
	exp=/^[a-zA-Z,\.\"\'\-\!\;\(\)\? ]+$/;
	if(exp.test(fieldName.value)!=true)
	{
		alert(message);
		fieldName.focus();
		return false;;
	}
	else
	{
		return true;
	}
}
//====================================================================================================
//	Function Name	:	fnIsFirstCharUpper()
//	Created By: Amit Sankhala
//	Created On: 12 February 2008
//	Last Modified By:
//	Last modified On:
//  Purpose : To check whether the first character of each word is in Upper case or not
//  Parameters: fld: Field Name
//				msg: Message to display if function returns the false value
//----------------------------------------------------------------------------------------------------
function fnIsFirstCharUpper(fld, msg) 
{ 
	var str = fld.value;
	arrString = str.split(" ");
	// define regular expression matching pattern 
	strPattern = "([A-Z].*)"; 

	// create object of RegExp Class 
	objRegExp = new RegExp(strPattern); 

	for(var i=0;i<arrString.length;i++)
	{	
		charToCheckForUpper = arrString[i].substr(0, 1);
		boolResult = objRegExp.test(charToCheckForUpper); // test that given string is according to pattern 
		
		if (!boolResult)
		{
			alert(msg);
			fld.focus();
			return false;
		}
	}
	return true;
} 
//====================================================================================================
//    Function Name    :    fnCheckForBlankSpace()
//    Created By: Amit Sankhala
//    Created On: 03 July 2007
//    Last Modified By:
//    Last modified On:
//  Purpose : To check whether the user name or password contains any blank space or not
//  Parameters: fld: Field Name
//                msg: Message to display if function returns the false value
//----------------------------------------------------------------------------------------------------
function fnCheckForBlankSpace(fld, msg) 
{ 
    var str = fld.value;

    for(var i=0;i<str.length;i++)
    {    
        charToCheckForBlankSpace = str.substr(i, 1);
        
        if (charToCheckForBlankSpace == " ")
        {
            alert(msg);
            fld.focus();
            return false;
        }
    }
    return true;
}
//==================================================================================================== 
// Function Name : CheckboxSelection() 
//    # Created By: Amit Sankhala
//    # Created On: 20 March 2007
//    Last Modified By:
//    Last modified On:
// Purpose : check all checkbox field from given form. 
// Parameters: frm : form name whose all checkbox need to uncheck.
//---------------------------------------------------------------------------------------------------- 
function CheckboxSelection(frm) 
{ 
   
    with(frm) 
    { 
        for(var i=0;i<frm.elements.length;i++) 
        { 
            if(frm.elements[i].type == 'checkbox') 
                frm.elements[i].checked = true; 
        } 
    } 
}
//==================================================================================================== 
// Function Name : CheckboxSelection1() 
//    # Created By: Amit Sankhala
//    # Created On: 21 February 2007
//    Last Modified By:
//    Last modified On:
// Purpose : check all checkbox field from given form. 
// Parameters: frm : form name whose all checkbox need to uncheck.
//---------------------------------------------------------------------------------------------------- 
function CheckboxSelection1(frm) 
{ 
    with(frm) 
    { 
        for(var i=0;i<frm.elements.length;i++) 
        { 
            if(frm.elements[i].type == 'checkbox' && frm.elements[i].style.display == "") 
                frm.elements[i].checked = true; 
        } 
    } 
}
//==================================================================================================== 
// Function Name : IsModuleNumber() 
//    # Created By: Amit Sankhala
//    # Created On: 09 July 2007
//    Last Modified By:
//    Last modified On:
// Purpose : check all checkbox field from given form. 
// Parameters: fld : field value
//              msg : error message
//---------------------------------------------------------------------------------------------------- 
function IsModuleNumber(fld,msg) 
{ 
    fld.value = Trim(fld.value);
    var regex = /^[1-7]*$/; 
    if(!regex.test(fld.value)) 
    { 
        alert(msg); 
        fld.focus(); 
        return false; 
    } 
    return true; 
}  
//====================================================================================================
//    Function Name    :    Show_Focus()
//    Created By: Amit Sankhala
//    Created On: 11 June 2007
//    Last Modified By:
//    Last modified On:
//  Purpose : Take the focus on Username field when page loads
//  Parameters: frm : Form Object.
//----------------------------------------------------------------------------------------------------
function Show_Focus(frm)
{
    frm.txtUsername.focus();
}

function IsValidZipcode(fld,msg) 
{ 
	var regex = /^([a-zA-Z-]*[0-9]+)+[[a-zA-Z-]*$/;

    if(!regex.test(fld.value)) 
    { 
        alert(msg); 
        fld.focus(); 
        return false; 
    } 
    return true; 
} 
//==================================================================================================== 
//  Function Name : CountCharacters
//	# Created By: Amit Sankhala
//	# Created On: 27 September 2007
//	Last Modified By:
//	Last modified On:
//  Purpose : To count number of characters in a text enetered
//  Parameters: this_fld : Field name to count nuber of characters.
//---------------------------------------------------------------------------------------------------- 
function CountCharacters (this_field, show_char_count, show_word_count)
{
	if (show_word_count == null)
	{
		show_word_count = false;
	}
	if (show_char_count == null) 
	{
		show_char_count = true;
	}
	var char_count = this_field.value.length;
	var fullStr = this_field.value + " ";
	var initial_whitespace_rExp = /^[^A-Za-z0-9]+/gi;
	var left_trimmedStr = fullStr.replace(initial_whitespace_rExp, "");
	var non_alphanumerics_rExp = rExp = /[^A-Za-z0-9]+/gi;
	var cleanedStr = left_trimmedStr.replace(non_alphanumerics_rExp, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length -1;
	if (fullStr.length <2) 
	{
		word_count = 0;
	}
	if (word_count == 1) 
	{
		wordOrWords = " word";
	}
	else
	{
		wordOrWords = " words";
	}
	if (char_count == 1) 
	{
		charOrChars = " character";
	}
	else 
	{
		charOrChars = " characters";
	}
	
	return char_count;
}

/** function to set page view level for that page in sesssion **/
function savePageView(type, viewlevel, pagename)
{
  if(type=='set')
    url = "savepageview.php?type="+type+"&viewlevel="+viewlevel+"&pagename="+pagename;
  else
    url = "savepageview.php?type="+type+"&pagename="+pagename;
  
  method = "GET";
  callbackfunction = setPageView;
  Ajax(url,method,callbackfunction);
}

/** callbackfunction **/
function setPageView(viewlevel)
{
   document.getElementById('pageview').value = viewlevel;    
}

/** get page view level for page **/    
function getPageView()
{
    return document.getElementById('pageview').value;
}

/** this function will change the view of the page based on value in session for that page **/
function changePageView(pagename)
{
    viewlevel = getPageView('get','',pagename);
    if(viewlevel == 'expand')
    {
        expand_all();
    }
    if(viewlevel == 'collapse')
    {
        collpase_all();
    }
}
//this function is for paging.
function paging1(frm,val)
{
	document.getElementById("paging").value=val;
	frm.submit();
}

function blinkIt() {
 if (!document.all) return;
 else 
 {
   for(i=0;i<document.all.tags('blink').length;i++)
   {
      s=document.all.tags('blink')[i];
	  s.style.fontWeight = (s.style.visibility=='visible')?'':'bold' ;
	  s.style.visibility=(s.style.visibility=='visible')?'hidden':'visible';

   }
 }
}



function showHideInternationalPhoneStep2(checkboxid)
{
	/** show international format **/	
	if(document.getElementById(checkboxid).checked)
	{
		document.getElementById('international_phone1').style.display = '';
		document.getElementById('international_phone2').style.display = '';
		document.getElementById('international_phone3').style.display = '';
		
		document.getElementById('us_phone1').style.display = 'none';
		document.getElementById('us_phone2').style.display = 'none';
		document.getElementById('us_phone3').style.display = 'none';
	}
	else
	{
		document.getElementById('international_phone2').style.display = 'none';	
		document.getElementById('international_phone1').style.display = 'none';
		document.getElementById('international_phone3').style.display = 'none';
		
		document.getElementById('us_phone1').style.display = '';
		document.getElementById('us_phone2').style.display = '';
		document.getElementById('us_phone3').style.display = '';
	}
}

function showHideInternationalPhone(checkboxid)
{
	/** show international format **/	
	if(document.getElementById(checkboxid).checked)
	{
		document.getElementById('international_phone1').style.display = '';
		document.getElementById('international_phone2').style.display = '';
		
		document.getElementById('us_phone1').style.display = 'none';
		document.getElementById('us_phone2').style.display = 'none';
	}
	else
	{
		document.getElementById('international_phone2').style.display = 'none';	
		document.getElementById('international_phone1').style.display = 'none';
		
		document.getElementById('us_phone1').style.display = '';
		document.getElementById('us_phone2').style.display = '';
	}
}  

function IsValidInternationalPhoneNumber(phone_ext,phone_withoutext, setfocuson)
{
        var checkOK = "0123456789+";
		var checkStr = phone_ext;
		var allValid = true;
		var allNum = "";
		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
	
			if(ch == '+' && i !=0)
			{
				alert("Please enter valid phone number");
				setfocuson.focus();
				return false;
			}
	
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
					break;
				if (j == checkOK.length)
				{
					allValid = false;
					break;
				}
				if (ch != ",")
					allNum += ch;
		}
		
		if (!allValid)
		{
			alert("Please enter only numbers for phone number");
			setfocuson.focus();
			return false;
		}
        
        if(phone_withoutext.length < 10)
        {
            alert("Please Enter Valid Phone Number");
            setfocuson.focus();
            return false;
        }
    
    return true
}

function showZipCode(intPhoneCheckBoxId, arrZipCode,arrZipText)
{        
	
	 if(document.getElementById(intPhoneCheckBoxId).checked == true) 
    {	
       newCodeTxt  =  "Postal Code";
       max_length  = 10;
       max_size    = 10;
    }  
    else
    {
       newCodeTxt =  "Zip Code";
       max_length  = 5;
       max_size    = 5;
    }
    
    for(k=0; k < arrZipCode[0].length; k++)
    {
        zip_id = arrZipCode[0][k];
        
        zip_text =   arrZipText[0][k];
        
        if(document.getElementById(zip_id))
        {
            document.getElementById(zip_id).innerHTML   = newCodeTxt;
            document.getElementById(zip_text).maxLength = max_length;
            document.getElementById(zip_text).size      = max_size;
        }    
    }
}

function showEmpZipCode(intPhoneCheckBoxId, arrZipCode, arrZipText, rowid)
{  
    if(document.getElementById(intPhoneCheckBoxId).checked == true)
    {
       newCodeTxt =  "Postal Code";
       max_length  = 10;
       max_size    = 10;
    }  
    else
    {
       newCodeTxt =  "Zip Code";
       max_length  = 5;
       max_size    = 5;
    }
    
    for(k=0; k < arrZipCode[0].length; k++)
    {
        zip_id   = arrZipCode[0][k];
        zip_text =   arrZipText[0][k]+rowid; 
       
        if(document.getElementById(zip_id+rowid))
        {
            document.getElementById(zip_id+rowid).innerHTML   = newCodeTxt;
            
            document.getElementById(zip_text).maxLength = max_length;
            document.getElementById(zip_text).size      = max_size;
        }    
    }
}

function showIntState(intPhoneCheckBoxId, arrState, arrIntStates)
{
    
    for(k=0; k < arrState[0].length; k++)
    {
        state_id = arrState[0][k];
        int_state_id = arrIntStates[0][k];
        
        if(document.getElementById(intPhoneCheckBoxId).checked == true)
        {
           document.getElementById(int_state_id).style.display = '';
           document.getElementById(state_id).style.display = 'none'; 
        }  
        else
        {
           document.getElementById(state_id).style.display = '';
           document.getElementById(int_state_id).style.display = 'none';
        } 
    }
}

function showEmpIntState(intPhoneCheckBoxId, arrState, arrIntStates, rowid)
{
    
    for(k=0; k < arrState[0].length; k++)
    {
        state_id = arrState[0][k]+rowid;
        int_state_id = arrIntStates[0][k]+rowid;
       
        
        if(document.getElementById(intPhoneCheckBoxId).checked == true)
        {
         
           document.getElementById(int_state_id).style.display = '';
           document.getElementById(state_id).style.display = 'none'; 
        }  
        else
        {
           document.getElementById(state_id).style.display = '';
           document.getElementById(int_state_id).style.display = 'none';
        } 
    }
}
