var f_frm_obj = _frm();
	
		function _frm() {
			var frm_index = 0;
			for(i=0;i<document.forms.length;i++) {
				frm_name = new String(document.forms[i].name);
				if(frm_name.indexOf("mainForm") != -1) {
					frm_index = i
				}
			}
			return frm_index;
		}

		function _ele(elename) {
			var ele_index = 0;
			for(i=0;i<document.forms[f_frm_obj].elements.length;i++) {
				ele_name = new String(document.forms[f_frm_obj].elements[i].name);
				if(ele_name.indexOf(elename) != -1) {
					ele_index = i
				}
			}
			return ele_index;
		}

function filterNum(str) { 
	//re = /\$|,|>|<|@|#|~|`|&|\%|\*|\^|\&amp;|\(|\)|\+|\=|\[|\-|\]|\[|\}|\{|\;|\:|\'|\"|\&lt;|\&gt;|\?|\|\\|\!|\$|\./g;
	re = /\$|,|>|<|#|~|`|&|\%|\*|\^|\&amp;|\(|\)|\+|\=|\[|\-|\]|\[|\}|\{|\;|\:|\'|\"|\&lt;|\&gt;|\|\\|\!|\$|\./g;
	// remove special characters like "$" and "," etc... 
	return str.replace(re, ""); 
} 

function TrimString(curObj)
		{
			var re;
			re = /^[ \f\n\r\t\v]+/;
			curObj.value = curObj.value.replace(re,"");
			re = /[ \f\n\r\t\v]+$/;
			curObj.value = curObj.value.replace(re,"");
							
		}
function  validateNumber( strValue ) {
				var objRegExp  =  /(^\d\d*$)/;
				//check for numeric characters
				return objRegExp.test(strValue);
}

function Compare_ServiceNo_Fields(Field1, Field2, AltMsg,LengthMsg){

	if (document.getElementById(Field1).value.toString() != document.getElementById(Field2).value.toString()){
			alert (AltMsg);
			return false;
		}else if (document.getElementById(Field1).value.length < 10){
			alert (LengthMsg);
			return false;
		}else if (validateNumber(document.getElementById(Field1).value) == false){
			alert (LengthMsg); 
			return false;		
		}else{
			return true;
		}
}

function Validate_ServiceNo(Field1,LengthMsg){
	if (document.getElementById(Field1).value.length < 10){
			alert (LengthMsg);
			document.forms[f_frm_obj].elements[_ele(Field1)].focus();
			return false;
	}else if (validateNumber(document.getElementById(Field1).value) == false){
			alert (LengthMsg); 
			document.forms[f_frm_obj].elements[_ele(Field1)].focus();
			return false;
	}else{
			return true;
	}
}
function CheckSpecialChars(FldName,ErrFldName){
		
			var FieldValue = document.forms[f_frm_obj].elements[_ele(FldName)].value;
			var regExpression = /^\w+$/;
			
			var matchArray = FieldValue.match(regExpression);
			if (matchArray == null && FieldValue != "") {				
				document.getElementById(ErrFldName).style.display = "inline";
				document.forms[f_frm_obj].elements[_ele(FldName)].focus();
				return false;
			} 
			else
			{
				document.getElementById(ErrFldName).style.display = "none";
				return true;	
			}
		}

	
// Date formating functions Starts
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

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 DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function ValidateDate(FldName){

	var dt=document.forms[f_frm_obj].elements[_ele(FldName)]
	
	if (dt.value == ""){
		return true
	}else{
		if (isDate(dt.value)==false){
			dt.focus()
			return false
		}
	}
    return true
 }
// Date formating functions Ends

// Web Services Functions Starts
/*
			var varRes;
									
			// Call this function onLoad event of the body
			function LoadService()
			{
				// RootPath of the application. set in the include/js/Menu.js file
				service.useService(RootPath + 'max.asmx?WSDL','MySvc');
			}
		
			// Call this function to start validation process
			function Chk_ServiceNo(SvcField)
			{
				var Val = document.getElementById(SvcField).value;
				
				if (Val == "") {
				return false
				}
				
				if (Validate_ServiceNo(SvcField,"Service No must be of 10 digits.") == false)
				return false
				
				varRes = service.MySvc.callService("fn_Is_ServiceNumber_exists",Val);

				return false;
			}
			
			// Call this function onResult event of the body
			function ShowResult()
			{
				if (event.result.id == varRes){
				
					var completeString = event.result.value;
					if (completeString == true)
					alert("Service number already exists in the database.");
					else
					alert("Service number does not exists in the database.");
					
				}
			}
*/
/*
Notes:
--	Example Body tag should be like this
<body id="service" style="BEHAVIOR: url(webservice.htc)" leftMargin="0" topMargin="0"
	onload="LoadService()" onresult="ShowResult()">
	
--  webservice.htc should also be placed on the root of the application.

-- This is the script to Validate the field value 
onClick="return Chk_ServiceNo('txt_Svc_No1')"

*/			
// Web Services Functions Ends

