/////validate.js/////
//-------------------------------------------------
//Purpose: for client-side validation of forms
//Inputs : frmObject, field, options
//Outputs: message displayed using alert()
//Modifications:
//-------------------------------------------------

	// functions to handle in-place client-side validation//
	// frmObject is required, 
	// field is required,
	// Options is optional.


// global variables
var strError = "";
var strFieldName = "";
var strFieldVal = "";
var evalstring = "";

//***** function to check blank spaces
function checkBlank(frmName, strArray)
{
	var fldArray = strArray.split(",");
	var strError = "";
	var trimSpace = "";
	for (var i=0; i<fldArray.length; i=i+2)
	{
		if (eval("document." + frmName + "." + fldArray[i] + ".value") == "")
		{
			strError += fldArray[i+1];
			alert(strError + " is Required");	
			eval("document." + frmName + "." +fldArray[i] + ".focus()");
			return true;
			break;
		}
	}
	return false;
}

//***** function to check valid text value
function validDecimal(frmObject, field)
{
	strFieldName = field.name;
	strFieldVal = field.value;
	evalstring="document." + frmObject.name + "." + strFieldName;
	if (strFieldVal == "")
	{
		return "";
	} else if(isNaN(strFieldVal))
	{
		alert("This is not a Valid Money field");
		eval(evalstring + ".focus();")
		eval(evalstring + ".select();")
	}
	return "";		
}


//***** function to check valid text value
function validText(frmObject, field)
{
	strFieldName = field.name;
	strFieldVal = field.value;
	evalstring="document." + frmObject.name + "." + strFieldName;
	if (strFieldVal == "")
	{
		return "";
	} else if(!isNaN(strFieldVal))
	{
		alert("This is not a numeric field...");
		eval(evalstring + ".focus();")
		eval(evalstring + ".select();")
	}
	return "";		
}

//***** function to check valid integer/number
function validInteger(frmObject, field)
{
	strFieldName = field.name;
	strFieldVal = field.value;
	evalstring="document." + frmObject.name + "." + strFieldName;
	if (strFieldVal == "")
	{
		return "";
	} else if(strFieldVal.search(/[^\d]/) != -1 || strFieldVal == "")
	{
		alert("This field accepts Integers only."); 
		eval(evalstring + ".focus();")
		eval(evalstring + ".select();")
		return false;
	}
	return true;
}

//***** Function to check valid real value
function validRealOLD(frmObject, field, options, canHaveZero)
{
	var str = "";
	var strError = "";
	var strOptions = options;
	strFieldName = field.name;
	strFieldVal = field.value;
	evalstring="document." + frmObject.name + "." + strFieldName;
	var twoParts = strOptions.split(".");
	for(var i=1; i<= twoParts[0]; i++) {str += '9';}
	str += '.';
	for(var i=1; i<= twoParts[1]; i++) {str += '9';}
	if (strFieldVal == "")
	{
		return "";
		
	} else if (!isNaN(strFieldVal))
	{
		if ((canHaveZero == true) && (strFieldVal == 0))
		{
			eval(evalstring + ".value = 0");
		} else if ((canHaveZero != true) && (strFieldVal == 0))
		{
			eval(evalstring + ".value = 0");
			strError = "This Field cannot be Zero...";
		} else if ((canHaveZero != true) && (strFieldVal != 0))
		{
			var flag = 0;
			var twoPartsVal = strFieldVal.split(".");
			if (twoPartsVal[0] == "" || twoPartsVal[0] == null) {flag = 1;} 
			else if (twoPartsVal[1] == "" || twoPartsVal[1] == null) 
			{
				twoPartsVal[1] = '0';
			}
			if (!flag) // two values are there
			{
				if ((twoPartsVal[0] == 0) && (twoPartsVal[1] == 0))
				{
					strError = "This Field cannot be Zero...";
				}
				if ((twoPartsVal[0] != 0) && (twoPartsVal[1] == 0))
				{
					twoPartsVal[1] = '00';
					eval(evalstring + ".value = " + twoPartsVal[0]);
				}
				if ((twoPartsVal[0].length > twoParts[0]) || (twoPartsVal[1].length > twoParts[1]))
				{
					strError = "This Field accepts input in this format " + str;
				} else
				{
					eval(evalstring + ".value = '" + twoPartsVal[0] + "." + twoPartsVal[1] + "';");
					strError = "";
				}
			}
		}
	} else // for not a number
	{
		strError = "This Field accepts input in this format " + str;
	}
	if (strError != "")
	{
		alert(strError);
		eval(evalstring + ".focus();")
		eval(evalstring + ".value ='';")
		return false;
	}
	return true;
}

//***** Function to check valid real value
function validReal(frmObject, field, options, canHaveZero)
{
	var str = "";
	var strError = "";
	var strOptions = options;
	strFieldName = field.name;
	strFieldVal = field.value;
	evalstring="document." + frmObject.name + "." + strFieldName;
	var twoParts = strOptions.split(".");
	for(var i=1; i<= twoParts[0]; i++) {str += '9';}
	str += '.';
	for(var i=1; i<= twoParts[1]; i++) {str += '9';}
	if (strFieldVal == "")
	{
		return "";
		
	} else if (!isNaN(strFieldVal))
	{
		if ((canHaveZero == true) && (strFieldVal == 0))
		{
			eval(evalstring + ".value = 0");
		} else if ((canHaveZero != true) && (strFieldVal == 0))
		{
			eval(evalstring + ".value = 0");
			strError = "This Field cannot be Zero...";
		} else if (((canHaveZero != true) && (strFieldVal != 0)) || ((canHaveZero == true)&&(strFieldVal != 0)))
		{
			var flag = 0;
			if (strFieldVal.indexOf('.') == -1) strFieldVal += '.0';
			var twoPartsVal = strFieldVal.split(".");
			if ((twoPartsVal[0] == 0) && (twoPartsVal[1] == 0)) strError = "This Field cannot be Zero...";
			if ((twoPartsVal[0] != 0) && (twoPartsVal[1] == 0))
			{
				twoPartsVal[1] = '00';
				eval(evalstring + ".value = " + twoPartsVal[0]);
			}
			if ((twoPartsVal[0].length > twoParts[0]) || (twoPartsVal[1].length > twoParts[1]))
				strError = "This Field accepts input in this format " + str;
			else
			{
				eval(evalstring + ".value = '" + twoPartsVal[0] + "." + twoPartsVal[1] + "';");
				strError = "";
			}				
			if (twoPartsVal[1] == "" || twoPartsVal[1] == null) twoPartsVal[1] = '0';
		}
	} else // for not a number
	{
		strError = "This Field accepts input in this format " + str;
	}
	if (strError != "")
	{
		alert(strError);
		eval(evalstring + ".focus();")
		eval(evalstring + ".value ='';")
		return false;
	}
	return true;
}
//***** Function to check for valid date
function validDATE(frmObject, field)
{
	strFieldName = field.name;
	strFieldVal = field.value;
	if (strFieldVal == "")
	{
		return true;
	} else
	{
	
		evalstring="document." + frmObject.name + "." + strFieldName;
	
		var reDatePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
		var matchArray = strFieldVal.match(reDatePat);
		if (matchArray == null) 
		{
			strError = "This date must be in m/d/yyyy or m-d-yyyy format.";
		}
		else
		{
			var strError=""
			var strMonth = matchArray[1]; // parse date into variables
			var strDay = matchArray[3];
			var strYear = matchArray[5];
			if (strMonth < 1 || strMonth > 12) 
			{ // check month range
				strError += "Month must be between 1 and 12.";
			}
			if (strDay < 1 || strDay > 31) 
			{
				strError +="Day must be between 1 and 31.";
			}
			if ((strMonth==4 || strMonth==6 || strMonth==9 || strMonth==11) && strDay==31)
			{
				strError +="Month "+strMonth+" doesn't have 31 days!";
			}
			if (strMonth == 2) 
			{ // check for february 29th
				var blnIsleap = (strYear % 4 == 0 && (strYear % 100 != 0 || strYear % 400 == 0));
				if (strDay>29 || (strDay==29 && !blnIsleap)) 
				{
					strError += "February " + strYear + " doesn't have " + strDay + " days!";
				}
			}
			if (strYear == "0000") 
			{
				strError += "Year cannot be 0000 ";
			}
			
			if (strYear <= 1752) 
			{
				strError += " Year cannot be less than 1753";
			}
			 
		}
		if(strError.length>=1)
		{
		    alert(strError);
			eval(evalstring + ".focus();")
			eval(evalstring + ".select();")
			return false;
		}
	}
	return true;
}

//***** Function to Trim the blank spaces before and after the field value
function jsTrim(frmObject, field)
{
	var tformname;
	var tfieldname;
	var tfieldval;
	
	tformname = frmObject.name; 
	tfieldname = field.name;
	tfieldval = field.value;
	var tnewval = "";
	
	while(''+tfieldval.charAt(0) == ' ')
	{
		tfieldval = tfieldval.substring(1,tfieldval.length);
	}
		
	while(tfieldval.charAt(tfieldval.length-1)+'' == ' ')
	{
		tfieldval = tfieldval.substring(0,(tfieldval.length-1));
	}
	
	var tvalstring="document." + tformname + "." + tfieldname;
	eval(tvalstring + ".value=tfieldval;")
}


//***** Field Change Validation 
function validateChange(frmObject)
{
	//eval("document." + frmObject.name + ".txtValidateChange.value = 1");
}


//***** Exit Change Validation 
function validateChangeExit(frmObject)
{
	if (eval("document." + frmObject.name + ".txtValidateChange.value == 1"))
	{
		if (confirm("All changes you made will be lost.\nDo you want to continue?"))
		{
			return true;
		}
	}
	else
	{
		return true;
	}
}


//***** Converts all single quotes, ampersands and spaces to url encoded value
function convertSpChars(strValue)
{
	strValue = strValue.value;
	strValue = strValue.replace(/\s/g,"%20");
	strValue = strValue.replace(/&/g,"%26");
	strValue = strValue.replace(/'/g,"%27");
	return strValue;
}

function convertSpChars1(frmObject, field)
{

	var tformname;
	var tfieldname;
	var tfieldval;
	
	tformname = frmObject.name; 
	tfieldname = field.name;
	strValue = field.value;
	
	strValue = strValue.replace(".","");	
	strValue = strValue.replace("'","");
	strValue = strValue.replace(",","");
	strValue = strValue.replace("-","");
	strValue = strValue.replace("?","");
	field.value = strValue;
	
}
function convertSpChars2(frmObject, field)
{

	var tformname;
	var tfieldname;
	var tfieldval;
	
	tformname = frmObject.name; 
	tfieldname = field.name;
	strValue = field.value;
	

	strValue = strValue.replace("'","");
	strValue = strValue.replace(",","");
	strValue = strValue.replace("-","");
	strValue = strValue.replace("?","");
	field.value = strValue;
	
}

//***** To check for valid time
function validTime(frmObject, field) 
{
	timeStr = field.value;
	var evalstring="document." + frmObject.name + "." + field.name;
	if (timeStr == "") 
	{
		return true;
	}
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;
	var matchArray = timeStr.match(timePat);
	if (matchArray == null) 
	{
		alert("Time is not in a valid format.");
		eval(evalstring + ".focus();")
		eval(evalstring + ".select();")
		return false;
	}
	hour = matchArray[1];
	minute = matchArray[2];
	second = matchArray[4];
	ampm = matchArray[6];
	if (second=="") { second = null; }
	if (ampm=="") { ampm = null }
	if (hour < 0  || hour > 23) 
	{
		alert("Hour must be between 1 and 12.");
		eval(evalstring + ".focus();")
		eval(evalstring + ".select();")
		return false;
	}
	if (hour <= 12 && ampm == null) 
	{
		alert("You must specify AM or PM.");
		eval(evalstring + ".focus();")
		eval(evalstring + ".select();")
		return false;
	}
	if  (hour > 12 && ampm != null) 
	{
		alert("hours cannot be greater than 12.");
		eval(evalstring + ".focus();")
		eval(evalstring + ".select();")
		return false;
	}
	if (minute<0 || minute > 59) 
	{
		alert ("Minute must be between 0 and 59.");
		eval(evalstring + ".focus();")
		eval(evalstring + ".select();")
		return false;
	}
	if (second != null && (second < 0 || second > 59)) 
	{
		alert ("Second must be between 0 and 59.");
		eval(evalstring + ".focus();")
		eval(evalstring + ".select();")
		return false;
	}
return true;
}


//***** Function to check valid email
function validEmail(frmObject, field) 
{ 
	alert("checking");
	var sEmail = field.value;
	if (sEmail == "")
	{
		return true;
	} else if (sEmail.length > 5) 
		{
			alert(sEmail.indexOf('@'));
			alert(sEmail.indexOf('.'));
			if ((sEmail.indexOf('@') == -1)||(sEmail.indexOf('.') == -1))
			{	
				alert(sEmail.indexOf('@'));
				alert(sEmail.indexOf('.'));
				alert("Not a valid Email ID 1");
				field.value="";
				field.focus();
				return false;
			} else
			{
			 return true;
			}	
		} 
return false;
} 
// function to validate dates from two fields

function validateFromToDates(fromObject,toObject)
{ 
   fromObject=fromObject.value;
   toObject=toObject.value;
	if ((fromObject == "") || (toObject == ""))
	{
		return false
	} else
	{
		var strFromdate=new Date(fromObject.replace(/-/g,"/"));
		var strTodate=new Date(toObject.replace(/-/g,"/"));
	
		strFromdate = strFromdate.valueOf();
		strTodate = strTodate.valueOf()
		if(strTodate<strFromdate)
		{
			alert ("The To date must be greater than From date...");
			return false;
		}
	}
	return true;
		
}

// function to validate dates from two fields with third paramater

function validateDatesWithError(fromObject,toObject, errString)
{ 
   fromObject=fromObject.value;
   toObject=toObject.value;
	if ((fromObject == "") || (toObject == ""))
	{
		return false
	} else
	{
		var strFromdate=new Date(fromObject.replace(/-/g,"/"));
		var strTodate=new Date(toObject.replace(/-/g,"/"));
	
		strFromdate = strFromdate.valueOf();
		strTodate = strTodate.valueOf()
		if(strTodate<strFromdate)
		{
			alert (errString);
			return false;
		}
	}
	return true;
		
}

function GetDateTime(strDate)
{
	var strDate = new Date(strDate)
	var dd = strDate.getDate(), mm = strDate.getMonth()+1, yyyy = strDate.getYear();
	return mm + "/" + dd + "/" + yyyy 
	//+ " " + ampm(new Date())
}
function padout(number) 
{ 
	return (number < 10) ? '0' + number : number; 
}
function ampm(mydate)
{
    var hours = mydate.getHours();
    var minutes = padout(mydate.getMinutes());
    var seconds = padout(mydate.getSeconds());
    var adjhours = (hours == 0) ? 12 : ((hours < 13) ? hours : hours-12);
    return ((adjhours < 10) ? ' ' : '') + adjhours + ':' + minutes + ':' + seconds + ((hours < 12) ? ' AM' : ' PM');
}



