/**
 * 
 */
function test(){	

	var hiddenDiv, visibleDiv, e;
	visibleDiv = document.getElementById('pageIsLoading');
	document.getElementById("pageIsLoading").style.display="inline";
	(hiddenDiv= document.createElement('div')).style.display= "none";

	//Move all the body's elements to the hiddenDiv:
	while (e= document.body.firstChild) {
	hiddenDiv.appendChild(e);
	}

	//Now the body is empty.
	//Insert centerPage into the body
	document.body.appendChild(visibleDiv);
	//Insert the hiddenDiv into the body.
	document.body.appendChild(hiddenDiv);
	 // document.getElementById("textbody").style.visibility="hidden";
	//  document.getElementById("textbody").backgroundColor="red";
	//  document.getElementById("textbody").style.zIndex="-3";	  
//	document.getElementById("pageIsLoading").style.display="inline";
	 // document.getElementById("box1").styleClass="divloading";
	//  document.getElementById("box1").style.z-index="100";	
}
function getKey(keyStroke) {
	var evt = keyStroke ? keyStroke:window.event;
	var t = window.event.srcElement.type;
	var keyCode = (document.layers) ? keyStroke.which : event.keyCode;	
	//var keyString = String.fromCharCode(keyCode).toLowerCase();
	//alert(keyCode);
	var leftArrowKey = 37;
	var backSpaceKey = 8;
	var escKey = 27;
	if(t && ((t =='text' && window.event.srcElement.readOnly==false) || t =='textarea' || t =='file')){
	//do not cancel the event
	}else{		
	if((window.event.altKey && window.event.keyCode==leftArrowKey) || (keyCode == escKey) || (keyCode == backSpaceKey))
		//	||(keyCode==13))
	{
		 if (evt.stopPropagation)    evt.stopPropagation();
		 if (evt.cancelBubble!=null) evt.cancelBubble = true;
		// alert(evt.cancelBubble)
		return false;
	}
	}
	}
/*
function cancelBack(event)   
{   alert("fdg"+event.keyCode);
alert(event.srcElement.form +"SSS"+ event.srcElement.isTextEdit);
    if ((event.keyCode == 8 ||    
       (event.keyCode == 37 && event.altKey) ||    
       (event.keyCode == 39 && event.altKey))   
        &&    
       (event.srcElement.form == null || event.srcElement.isTextEdit == false)   
      )   
    {   alert("dff");
        event.cancelBubble = true;   
        event.returnValue = false;
      // return false;
    }   
}   */
function disableSub(){      
	     // if(document.getElementById('load'))
	      //  document.getElementById('load').style.display="inline";

	  document.getElementById("submit").disabled=true;
    /// test();   
     
      //return false;
        }

function GetEndDate(startDate, days)
{
    var endDate = new Date(startDate.getTime());
    endDate.setDate(endDate.getDate() - days);
    return endDate;
}

function isRecentDate(field) {
	
	var allowBlank = true;	
	var date = new Date();
	date.setHours(0, 0, 0, 0);
	var allowedDate=GetEndDate(date,2);
	var maxYear = date.getFullYear();
	var minYear = maxYear;
	var maxMonth = date.getMonth();
	var maxDate = date.getDate();
	var errorMsg = "";
	if (field.value =="" || field.value == "DD/MM/YYYY")
		return false;
	// regular expression to match required date format
	re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
	if (field.value != '') {
		if (regs = field.value.match(re)) {
			var enteredDate = new Date(regs[3],regs[2]-1,regs[1]);		
		//	alert(enteredDate+":::"+allowedDate+"con1::"+(enteredDate<allowedDate)+"con2::"+(enteredDate>date));
			if (regs[1] < 1 || regs[1] > 31)
				errorMsg = "Invalid value for day: " + regs[1];
			else if (regs[2] < 1 || regs[2] > 12)
				errorMsg = "Invalid value for month: " + regs[2];

			if ((regs[2] == 4 || regs[2] == 6 || regs[2] == 9 || regs[2] == 11)
					&& regs[1] == 31) {
				errorMsg = "Invalid value for day: " + regs[1];
			}

			if (regs[2] == 2
					&& (regs[1] > 29 || ((regs[3] % 4 == 0
							&& regs[3] % 100 != 0 || regs[3] % 400 == 0) == false && regs[1] == 29)))
				errorMsg = "Invalid value for day (for Feb month): " + regs[1];

			if(!(enteredDate>=allowedDate && enteredDate<=date))
				errorMsg = " Not a valid DD date";

		} else
			errorMsg = "Invalid date format: " + field.value;
	} else if (!allowBlank)
		errorMsg = "Empty date not allowed!";

	if (errorMsg != "") {
		alert(errorMsg);
		field.value = "";
		field.focus();
		return false;
	}
	return true;
}


function isValidDate(field) {
	var allowBlank = true;
	var minYear = 1943;
	var date = new Date();
	var maxYear = date.getFullYear()+10;
	var maxMonth = date.getMonth();
	var maxDate = date.getDate();
	var errorMsg = "";
	if (field.value == "" || field.value == "DD/MM/YYYY")
		return false;
	// regular expression to match required date format
	re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
	if (field.value != '') {
		if (regs = field.value.match(re)) {
			if (regs[1] < 1 || regs[1] > 31)
				errorMsg = "Invalid value for day: " + regs[1];
			else if (regs[2] < 1 || regs[2] > 12)
				errorMsg = "Invalid value for month: " + regs[2];

			if ((regs[2] == 4 || regs[2] == 6 || regs[2] == 9 || regs[2] == 11)
					&& regs[1] == 31) {
				errorMsg = "Invalid value for day: " + regs[1];
			}

			if (regs[2] == 2
					&& (regs[1] > 29 || ((regs[3] % 4 == 0
							&& regs[3] % 100 != 0 || regs[3] % 400 == 0) == false && regs[1] == 29)))
				errorMsg = "Invalid value for day (for Feb month): " + regs[1];

			if ((regs[3] < minYear || regs[3] > maxYear)
					|| (regs[3] == maxYear && regs[2] > maxMonth + 1)
					|| (regs[3] == maxYear && regs[2] == maxMonth + 1 && regs[1] > maxDate))
				errorMsg = "Not a valid year for agreement.";

		} else
			errorMsg = "Invalid date format: " + field.value;
	} else if (!allowBlank)
		errorMsg = "Empty date not allowed!";

	if (errorMsg != "") {
		alert(errorMsg);
		field.value = "";
		field.focus();
		return false;
	}
	return true;
}


function getDateObject(dateString,dateSeperator)
{
//This function return a date object after accepting 
//a date string ans dateseparator as arguments
var curValue=dateString;
var sepChar=dateSeperator;
var curPos=0;
var cDate,cMonth,cYear;

//extract day portion
curPos=dateString.indexOf(sepChar);
cDate=dateString.substring(0,curPos);

//extract month portion	
endPos=dateString.indexOf(sepChar,curPos+1);
cMonth=dateString.substring(curPos+1,endPos);

//extract year portion	
curPos=endPos;
endPos=curPos+5;	
cYear=curValue.substring(curPos+1,endPos);

//Create Date Object
dtObject=new Date(cYear,cMonth-1,cDate);	
return dtObject;
}

function validEndDate(date1,date2){
	if(isValidDate(date1)==false) return false;
	if(isValidDate(date2)==false) return false;
	var startDate=getDateObject(date1.value,"/");
	var endDate=getDateObject(date2.value,"/");
    var currentDate=new Date();	
	if(startDate>=endDate){
		alert("Valid upto date must be greater than agreement start date.");
		date2.value="";
		date2.focus();
		return false;
		}
	
	if(endDate<=currentDate){	
		alert("Valid upto date must be greater than current date.");
		date2.value="";
		date2.focus();
		return false;
		}	
}

function isDate(field) {
	var allowBlank = true;
        var cutOffDate = document.forms[0].overageCutOffDate.value;
	var minYear = cutOffDate.substr(6,4);
	var date = new Date();
	var maxYear = date.getFullYear();
	var maxMonth = date.getMonth();
	var maxDate = date.getDate();
	var errorMsg = "";
	if (field.value == "" || field.value == "DD/MM/YYYY")
		return false;
	// regular expression to match required date format
	re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
	if (field.value != '') {
		if (regs = field.value.match(re)) {
			if (regs[1] < 1 || regs[1] > 31)
				errorMsg = "Invalid value for day: " + regs[1];
			else if (regs[2] < 1 || regs[2] > 12)
				errorMsg = "Invalid value for month: " + regs[2];

			if ((regs[2] == 4 || regs[2] == 6 || regs[2] == 9 || regs[2] == 11)
					&& regs[1] == 31) {
				errorMsg = "Invalid value for day: " + regs[1];
			}

			if (regs[2] == 2
					&& (regs[1] > 29 || ((regs[3] % 4 == 0
							&& regs[3] % 100 != 0 || regs[3] % 400 == 0) == false && regs[1] == 29)))
				errorMsg = "Invalid value for day (for Feb month): " + regs[1];

			if ((regs[3] < minYear || regs[3] > maxYear)
					|| (regs[3] == maxYear && regs[2] > maxMonth + 1)
					|| (regs[3] == maxYear && regs[2] == maxMonth + 1 && regs[1] > maxDate))
				errorMsg = "Not a valid Date of Birth";

		} else
			errorMsg = "Invalid date format: " + field.value;
	} else if (!allowBlank)
		errorMsg = "Empty date not allowed!";
	if (errorMsg != "") {
		alert(errorMsg);
		field.value = "";
		field.focus();
		return false;
	}
	return true;
}

function uncheckOther(elem, other) {
	other.checked = false;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function setDefault(elem, def) {
	if (elem != null && trim(elem.value, " ") == "") {
		elem.value = def;
		elem.style.color = "gray";
	}
}
function removeDefault(elem, def) {
	if (elem != null && trim(elem.value, " ") == def) {
		elem.value = "";
		elem.style.color = "black";
	}
}

function alpha(elem, e) {
	var k;
	document.all ? k = e.keyCode : k = e.which;

	if ((k > 64 && k < 91) || (k > 96 && k < 123) || k == 8 || k == 32
			|| (k >= 48 && k <= 57)) {
		// if((k > 96 && k < 123))
		{
			elem.value = elem.value.toUpperCase();
		}

		return true;
	} else
		return false;
}

function onlyNumbers(evt) {
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;

	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
} 

function onlyDate(elem,evt) {
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;
    if(elem.value.length==2||elem.value.length==5){	
    	if (charCode !=47)
    		return false;
    	}
    else{    	
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;
    }
	return true;
}

function onlyAppNo(elem,evt) {
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;	
	
    if(elem.value.length==0){
    	if (charCode !=86 && charCode !=118)
    		return false;
    	}
    else{    	
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;
    }
	return true;
}

function onlyCouponNo(elem,evt) {
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;
	
    if(elem.value.length==0){ 
    	if (charCode !=67 && charCode !=99)
    		return false;
    	}
    else{    	
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;
    }
	return true;
}

function onlyISD(evt) {
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;

	if (charCode > 31 && ((charCode < 48 && charCode != 43) || charCode > 57))
		return false;
	return true;
}

function onlyPhoneNumber(evt) {
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;

	if (charCode != 45 && (charCode > 31 && (charCode < 48 || charCode > 57)))
		return false;

	return true;

}

function onlyEmailId(e) {
	var k;
	document.all ? k = e.keyCode : k = e.which;
//alert(k);
	if ((k >= 64 && k < 91) || (k > 96 && k < 123)||k==95
			|| (k >= 45 && k <= 57 && k!=47)) {
		//	elem.value = elem.value.toUpperCase();
		return true;
	} else
		return false;

}

function onlyAddress(elem,e) {
	var k;
	document.all ? k = e.keyCode : k = e.which;
  // alert(k);
	if ((k > 64 && k < 91) || (k > 96 && k < 123)||k==95||k==92||k==32
			|| (k >= 44 && k <= 57)) {
			elem.value = elem.value.toUpperCase();
		return true;
	} else
		return false;

}

function makeUpper(elem) {
	elem.value = elem.value.toUpperCase();
}

function emailValidator(elem) {
	var emailExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (emailExp.test(elem.value) == true)
		return true;
	else {
		alert("Please Enter valid email Id");
		elem.focus();
		return false;
	}
}

function isAlphabet(elem, helperMsg) {
	var alphaExp = /^[a-zA-Z ]+$/;
	// alert(elem.value.match(alphaExp));
	if (elem.value.match(alphaExp))
		return true;
	else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isAlphaNumeric(elem, helperMsg) {
	var alphaExp = /^[a-zA-Z0-9 ]+$/;
	// alert(elem.value.match(alphaExp));
	if (elem.value.match(alphaExp))
		return true;
	else {
		alert(helperMsg);
		elem.focus();
		return false;
	}

}

function isName(elem, helperMsg) {
	var alphaExp = /^[a-zA-Z0-9. ]+$/;
	// alert(elem.value.match(alphaExp));
	if (elem.value.match(alphaExp))
		return true;
	else {
		alert(helperMsg);
		elem.focus();
		return false;
	}

}

function isMobileNumber(elem, helperMsg) {

	var alphaExp = /^([7-9]{1})([0-9]{9})$/;

	if (elem.value.match(alphaExp))
		return true;

	else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isPhoneNumber(elem, helperMsg) {
	var flag = 0;
	var arr = new Array();
	arr = elem.value.split("-");
	if (arr.length != 2
			|| (arr[0].length == null || arr[0].length < 2 || arr[0].length > 5)
			|| (arr[1].length == null || arr[1].length < 5 || arr[1].length > 8))
		flag = 1;
	else {
		var std = "";
		for ( var i = 0; i < arr[0].length; i++) {
			if (arr[0][i] != 0) {
				std = arr[0].substring(i);
				break;
			}
		}

		switch (std.length) {
		case 2:
			if (arr[1].length != 8)
				flag = 1;
			break;
		case 3:
			if (arr[1].length != 7)
				flag = 1;
			break;
		case 4:
			if (arr[1].length != 6)
				flag = 1;
			break;
		case 5:
			if (arr[1].length != 5)
				flag = 1;
		}
		if (arr[1].charAt(0) == 0 || arr[1].charAt(0) == 1
				|| arr[1].charAt(0) == 7 || arr[1].charAt(0) == 8
				|| arr[1].charAt(0) == 9) {
			helperMsg = " First Digit in phone number will not be 0,1,7,8 or 9.";
			flag = 1;
		}
	}

	if (flag == 1) {
		alert(helperMsg);
		elem.focus();
		return false;
	} else
		return true;
}

function isPIN(elem, helperMsg) {
	var alphaExp = /^([1-8]{1})([0-9]{5})$/;
	// alert(elem.value.match(alphaExp));
	if (elem.value.match(alphaExp))
		return true;
	else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isContactNumber(elem, helperMsg) {
	var alphaExp = /^[/ + 0 - 9 / -/,]+$/;
	if (elem.value.match(alphaExp))
		return true;
	else {
		alert(helperMsg);
		elem.focus();
		return false;
	}
}

function isPAN(elem) {
	var msg = "";
	elem.value = elem.value.toUpperCase();
	var alphaExp = /^([a-zA-Z]{5})([0-9]{4})([A-Za-z]{1})$/;
	// alert(elem.value.length);
	// alert("jjjjj");
	if (elem.value.length < 10)
		msg = "PAN must be of 10 characters";
	else if (elem.value.match(alphaExp))
		return true;
	else
		msg = "Please Enter valid PAN";
	if (msg != "") {
		alert(msg);
		elem.value = "";
		elem.focus();
		return false;
	}
}

function checkNull(elem) {
	if (elem.value == "") {
		alert(msg);
		elem.focus();
		return false;
	} else
		return true;

}

function commonValidations(elem, evt){
    var x=elem.value;
//alert(x.length);
var e = event || evt; // for trans-browser compatibility
var charCode = e.which || e.keyCode;
if(charCode != 39){
    return true;
}
return false;

}

function numbersonly(e, decimal) {
	var key;
	var keychar;

	if (window.event) {
	   key = window.event.keyCode;
	}
	else if (e) {
	   key = e.which;
	}
	else {
	   return true;
	}
	keychar = String.fromCharCode(key);

	if ((key==null) || (key==0) || (key==8) ||  (key==9) || (key==13) || (key==27) ) {
	   return true;
	}
	else if ((("0123456789").indexOf(keychar) > -1)) {
	   return true;
	}
	else if (decimal && (keychar == ".")) {
	  return true;
	}
	else
	   return false;
	}
function onlyPIN (elem, evt)
{
    var x=elem.value;
    //alert(x.length);


	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;
    if(x!=""){
        if ( charCode >= 48 && charCode <= 57 ){
		return true;
        }
    }
	if ( charCode >= 49 && charCode <= 56 ){
		return true;
        }

        alert("Invalid Pincode");
        return false;
}

function onlyAlphabets(evt){
	var e = event || evt; // for trans-browser compatibility
	var charCode = e.which || e.keyCode;
	if ( (charCode >= 65 && charCode <= 90) || (charCode >=97 && charCode<=122)|| charCode == 32){
            return true;
        }else{
            alert("Only Alphabets Allowed");
            e.keyCode=0;
        }
      return false;
}

function checkFormDefaultVal(myForm){
	var elem=myForm.elements;
	var flag=false;
	var defSel=null,currSel=null;
	 for(var i = 0; i < elem.length; i++){            		
		 if(elem[i].type=="select-one") {
			// alert("*"+elem[i].name);
			 flag=true;
			 defSel=null,currSel=null;
			 for(var k=0; k<elem[i].children.length; k++){ 
			  if(elem[i].children[k].selected==true){
				//  alert(k+":shasg "+elem[i].children[k].value);
				  currSel=k;
			  }            			
              if(elem[i].children[k].defaultSelected==true){
            	  defSel=k;                        	 
                // alert(k+":ddddd "+elem[i].children[k].value);                            
               }
			 }
			 if(currSel!=null && defSel!=null && defSel!=currSel){
				 //alert("def:"+defSel+"curr:"+currSel);
				 return true;            			 
			 }
		 }
		 if(elem[i].type=="text" ){            			
			 if(elem[i].defaultValue!=elem[i].value)
				 return true;
		 }		
		 
		 if(elem[i].type=="checkbox"){
			//alert(elem[i].name);
	//		alert(elem[i].defaultChecked != elem[i].checked)
			if(elem[i].defaultChecked != elem[i].checked)
			return true;
			 }
     }
	 return false;
}



function popIdProof(){
	//resetValues("IDENTITYPROOF");
        //alert("shekhar");
        $('#IDENTITYPROOF').val("");       
	   jQuery.ajax({
					  url: "idProofListDropDown?applCategory="+$("#status").val(),
					  type: "POST",
					  data:  {},
					  async: false,
					  beforeSend:function(){
						  //alert(url);
					  },
					  success : function(response, textStatus, jqXHR){
						//  $('#state').removeAttr('disabled');
						  $("#IDENTITYPROOF").html(jqXHR.responseText);
				            return true;
				        }
				});
	}


function popAddrProof(){
	//resetValues("ADDRESSPROOF");
        $('#ADDRESSPROOF').val("");
	   jQuery.ajax({
					  url: "addrProofListDropDown?applCategory="+$("#status").val(),
					  type: "POST",
					  data:  {},
					  async: false,
					  beforeSend:function(){
						  //alert(url);
					  },
					  success : function(response, textStatus, jqXHR){
						//  $('#state').removeAttr('disabled');
						  $("#ADDRESSPROOF").html(jqXHR.responseText);
				            return true;
				        }
				});
	}

   function popDobProof(){
	//resetValues("DOBPROOF");
        $('#DOBPROOF').val("");
	   jQuery.ajax({
					  url: "dobProofListDropDown?applCategory="+$("#status").val(),
					  type: "POST",
					  data:  {},
					  async: false,
					  beforeSend:function(){
						  //alert(url);
					  },
					  success : function(response, textStatus, jqXHR){
						//  $('#state').removeAttr('disabled');
						  $("#DOBPROOF").html(jqXHR.responseText);
				            return true;
				        }
				});
	}
        
        
   function printPDF(){
    var innerText="<html>"+document.getElementById("ackSlip").innerHTML+"</html>";
    var printWindow=window.open('','','left=0,top=0,width=700,height=330,toolbar=0,scrollbars=0,status=0');
    printWindow.document.write(innerText);
    printWindow.document.close();
    printWindow.focus();
    printWindow.print();
    printWindow.close();
    /*document.forms[0].actionType.value="printBatchSlip";
    document.forms[0].submit();*/
}


