var errorCount = 0;
var errorMessage = '';
var isErrorFree = true ;


function  ShowErrMsg() {
	//alert('In JS');
	var returnArr = displayError2();			
	var str = returnArr[0];
	var elmsArr = str.split("|");

	var errStr = "";
	for (i=1; i<elmsArr.length; i++) {				
		//errStr = errStr + "&nbsp;&nbsp;" + i + ". " + elmsArr[i] + "<br>";
		errStr = errStr + "*&nbsp;&nbsp;" + elmsArr[i] + "<br>";
	}	
	errStr = errStr + "<hr><div style='padding-top:10px; text-align:center'><input type=button onclick=CloseErrorDiv(); value=Close><div>";
	var divErr = document.getElementById('divErrorMsg');
	divErr.innerHTML = errStr;	
	divErr.style.visibility = "visible";			
	return false;
}

function setListBoxValue(obj, item)
{
	var varData = '';
	for (i=1; i<obj.length; i++){
		var arr = obj.options[i].value.split('|') ;
		if (arr[0] == item){
		obj.options[i].selected =true;
		}
	}
}

function addErrorMessage(errStmt)
{
	errorCount ++ ;
	errorMessage = errorMessage + "|"+ errStmt;
}


function displayError1(text) {

	var errStmt;
	errStmt2 = text;
	addErrorMessage(errStmt2);
	isErrorFree = false;

}


function displayError()
{
	if (errorCount == 0)
		return true;
	else
	{
		var Args = new Array(errorCount,errorMessage) ;
		var url = '../error/error.htm'
		var Features = "dialogHeight:300px;dialogWidth:300px;center:1;status:0;scroll:0;help:0;resizable:0" ;
		window.showModalDialog(url,Args,Features)

		return false;
	}
}

function displayError2()
{
	if (errorCount == 0)
		return true;
	else
	{
		var Args = new Array(errorMessage);
		return Args;
	}
}

function isEmailAddress(objControl, Label)
{

var cell = document.all('td' + objControl.name);



	if (Label == null)
		Label = objControl.Label;
		
			
			var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
				if (filter.test(objControl.value))
				   //	{
						//	if (cell)
						//			cell.style.color = 'Black';
					return true;
					//}*/
	
				else
					{
					 errStmt = Label + ' should be vaild Email Address';
					 addErrorMessage(errStmt);
					 if (cell)
						cell.style.color = 'Red';
					 return false;
					
					}
				
		}

// Removes starting whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function isPhone(objControl,Label) {

	var objVal = trim(objControl.value);
	var Lbl = ' should be in xxx-xxx-xxxx format';
	var sVal = objVal.split("-");
	
	for (var num = 0; num < sVal.length; num++) {
		//alert( trim( sVal[num] ) );
		//alert(sVal[num]);
		//alert( isNaN(sVal[num]) );
		if ( isNaN(sVal[num])  ||  trim(sVal[num]) ==  "" ) {
			errStmt = Label + Lbl;
			addErrorMessage(errStmt);
			return false;
		}
	}
	
	if ( objVal.length < 12 || sVal.length != 3 )
	{
		errStmt = Label + Lbl;
		addErrorMessage(errStmt);
		return false;
	}
	else 
		return true;	
}

function isVaildMonth_Day_ofBirth(Month, Day)
{
	if (( Month == '02' || Month == '04' || Month == '06' || Month == '09' || Month == '11' ) && ( Day > 30) ){
		errStmt = 'Please check Month & Day of birth';
		addErrorMessage(errStmt);
		return false;
	}
	
	if (( Month == '02' ) && ( Day >= 29) ){
		errStmt = 'Please check Month & Day of birth';
		addErrorMessage(errStmt);
		return false;
	}
	
}

function isSame(objControl1, objControl2)
{
	
	var Label1 = objControl1.Label;
	var Label2 = objControl2.Label;
	//alert(Label2);
	if ( trim(objControl1.value) != trim(objControl2.value) )
	{
		errStmt = Label1 + ' and ' + Label2 + ' should be same';
		addErrorMessage(errStmt);
		return false;
	}
	else 
		return true;	
}

function isNumeric(objControl)
{
	
	var Label = objControl.Label;
	
	if (isNaN(objControl.value))
	{
		errStmt = Label1 + ' should be Numeric';
		addErrorMessage(errStmt);
		return false;
	}
	else 
		return true;	
}

function isChecked(objControl, Label)
{
	if(objControl.checked) {
		return true;
	}	
	else {
		errStmt = Label + ' should be Checked';
		addErrorMessage(errStmt);
		return false;
	}
}

function isChecked1(objControl, Label)
{
	if(objControl.checked) {
		return true;
	}	
	else {
		errStmt = Label;
		addErrorMessage(errStmt);
		return false;
	}
}

function isRadioChecked(objControl,objControl1, Label)
{
	if( objControl.checked || objControl1.checked) {
		return true;
	}	
	else {
		errStmt = Label;
		addErrorMessage(errStmt);
		return false;
	}
}

function CheckLength(objControl, iLen, sLabel)
{
	var Label = objControl.Label;

	if ( objControl.value.length < iLen  )
	{
		errStmt = sLabel + ' should be at least ' +  iLen + ' characters';
		addErrorMessage(errStmt);
		return false;
	}
	else 
		return true;	
}

function isFilled(objControl, Label)
{
	var cell = document.all('td' + objControl.name);
	
	//alert(Label)
	if (Label == null)
		Label = objControl.Label;
	
	if (objControl[0] && (objControl.type == "checkbox" || objControl.type == "radio"))
	{
		for(i=0; i<objControl.length; i++)
		{
			
			if(objControl[i].checked == true)
				return true;
		}
		
		cell = document.all('td' + objControl[0].name);
		
		errStmt = Label + ' cannot be blank';
		addErrorMessage(errStmt);
		if (cell)
			cell.style.color = 'Red';
		return false;
		
	}
	else if (trim(objControl.value) == '')
	//if (trim(objControl.value) == '')
	{
		//alert(Label);
		errStmt = Label + ' cannot be blank';
		addErrorMessage(errStmt);
		if (cell)
			cell.style.color = 'Red';
		return false;
	}
	else
	{	
		//alert('1');
		if (cell)
			cell.style.color = 'Black';
		return true;
	}
}

function isNumeric(objControl, Label)
{

var cell = document.all('td' + objControl.name);



	if (Label == null)
		Label = objControl.Label;


if (objControl.value != "" && isNaN(objControl.value))
{
	errStmt = Label + ' should be numeric';
		addErrorMessage(errStmt);
		if (cell)
			cell.style.color = 'Red';
		return false;
	}
else
//	{
//	if (cell)
//			cell.style.color = 'Black';
		return true;
	//}*/
}
function isEmailAddress(objControl, Label)
{

var cell = document.all('td' + objControl.name);



	if (Label == null)
		Label = objControl.Label;
		
			
			var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
				if (filter.test(trim(objControl.value)))
				   //	{
						//	if (cell)
						//			cell.style.color = 'Black';
					return true;
					//}*/
	
				else
					{
					 errStmt = Label + ' should be vaild Email Address';
					 addErrorMessage(errStmt);
					 if (cell)
						cell.style.color = 'Red';
					 return false;
					
					}
				
		}

//Pankaj --Preserve Zeros's up to X no of places.
//Ex. 	preserveZeros(100.5,3)-> 100.500
//Ex.	preserveZeros(100.5,2)-> 100.50
//Ex.	preserveZeros(100,3)-> 100.000
		
function preserveZeros(val,noOfPlaces)
{
	var i;
	var decimalPlaces = noOfPlaces;
    val = val + '';
	if (decimalPlaces <= 0) return val; 
	var decimalPos = val.indexOf('.');
	if (decimalPos == -1)
	{
		val += '.';
		for (i=0; i<decimalPlaces; i++)
		{
			val += '0';
		}
	}
	else
	{
		var actualDecimals = (val.length - 1) - decimalPos;
		var difference = decimalPlaces - actualDecimals;
		for (i=0; i<difference; i++)
		{
			val += '0';
		}
	}
	
	return val;
}

function isDate1(Object, Label)
{
	var cell = document.all('td' + Object.name);
	if (Label == null)
			Label = Object.Label;
				
	var errors = false;
	errors1 = false;

	if (Object.value == '')
		{
			
			errors = true;
			if (errors==true)
			{
			errStmt = Label + ' cannot be blank';
			addErrorMessage(errStmt);
			cell.style.color = 'Red';
			return true;
			}
		}
	
	else if (Object.value != "") {
	
		FieldValue = Object.value
		if (parseInt(FieldValue.length) != 10)
			errors = true;
	 
		if (FieldValue.charAt(2)!="/" || FieldValue.charAt(5)!="/")
			errors = true;
		
		
		if (errors==true)
			{
				errStmt = Label + ' not in proper (mm/dd/yyyy) date format.';
				addErrorMessage(errStmt);
				cell.style.color = 'Red';
				return true;
			}
		
		FieldValue = FieldValue.replace(/[/]+/g, '')
		
		mm=FieldValue.substring(0,2);
		dd=FieldValue.substring(2,4);
		yyyy=FieldValue.substring(4,8);
	
	
		if((dd<1||dd>31)||(mm<1||mm>12)||(yyyy<1800||yyyy>3000))
			errors1 = true;
		if(((mm==4||mm==6)||(mm==9||mm==11))&&(dd>30))
			errors1 = true;
		if((mm==2)&&((yyyy%4)==0) &&(dd>29))
			errors1 = true;
		if((mm==2)&&((yyyy%4)!=0)&&(dd>28))
			errors1 = true;
		
		if (errors1==true)
		{
			errStmt = Label + ' entered is not a valid Date.';
			addErrorMessage(errStmt);
			cell.style.color = 'Red';
			return true;
		}
	}
	
	else
	{
	cell.style.color = 'Black';
	return false;
	}
}


function isDate(dateStr,Label) {

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
var matchArray = dateStr.match(datePat); // is the format ok?

if (matchArray == null) {
//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
			errStmt = "Please enter " + Label + " as mm/dd/yyyy.";
			addErrorMessage(errStmt);

return false;
}

month = matchArray[1]; // p@rse date into variables
day = matchArray[3];
year = matchArray[5];

if (month < 1 || month > 12) { // check month range
//alert("Month must be between 1 and 12.");
			errStmt = "Month must be between 1 and 12.";
			addErrorMessage(errStmt);

return false;
}

if (day < 1 || day > 31) {
//alert("Day must be between 1 and 31.");
			errStmt = "Day must be between 1 and 31.";
			addErrorMessage(errStmt);

return false;
}

if ((month==4 || month==6 || month==9 || month==11) && day==31) {
//alert("Month "+month+" doesn`t have 31 days!")
			errStmt = "Month "+month+" doesn`t have 31 days";
			addErrorMessage(errStmt);

return false;
}

if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day > 29 || (day==29 && !isleap)) {
//alert("February " + year + " doesn`t have " + day + " days!");
			errStmt = "February " + year + " doesn`t have " + day + " days";
			addErrorMessage(errStmt);

return false;
}
}
return true; // date is valid
}
