/**
* This function evaluates the input string for a correct E-Mail format.
* It calls a function isEmpty to find whether the input string is Empty.
* @param strValue string value.
* @return true, if the Format is right || false otherwise.
*/
function isEmail(objField){
	if(isEmpty(objField.value)){
		alert("Please provide value in Email field.");
		objField.focus();
		return false;
	}
	
	if(objField.value.length > 0){
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(objField.value)){
			return true;
		}
	}
	alert("Invalid E-mail Address! Please re-enter.");
	objField.select();
	return false;
}

function isZero(sSearch){
	var sPattern = /^(0)$/;
	if(sSearch.search(sPattern) != -1){
		return true;
	}
	return false;
}
//<!----------------------------------------------------------------

/**
* Checks the input string parameter for null and empty. 
* This function also validates the input string for whitespace.
* @param strData string input value.
* @return true if the text is null or empty string
* @return false if the text is not null or empty string
*/
function isEmpty(strData){
	if ((strData.length == 0)||(strData == null)){
		return true;
	}

	//Regular expression refers any 0 or more white space with any alphabets.
	//If that expression matches with the input string it returns true, false otherwise.
	if (/^\s*(?=\w)/.test(strData)){
		return false;
	}
    return true;
}

function datecom(objDate1,objDate2){
	var dtStart, dtEnd;
	
	
	var obDate1 = objDate1.split("-");
	var obDate2 = objDate2.split("-");
	dtStart = obDate1[1] +"/"+ obDate1[0] +"/"+ obDate1[2];
	dtEnd = obDate2[1] +"/"+ obDate2[0] +"/"+ obDate2[2];
	
	var dtFirst = new Date(dtStart);
	var dtSec = new Date(dtEnd);
	var intFtime = Math.floor(dtFirst.getTime()/(24*60*60));
	var intStime = Math.floor(dtSec.getTime()/(24*60*60));
	var intDiff = (intStime-intFtime);
	//alert (intDiff);
	if(intDiff < 0){
		return -1;
	}
	else if(intDiff == 0){
		return 0;
	}
	else if(intDiff >= 1){
		return 1;
	}
 }

//<!------------------------------------------------------------------!>

/**
* Validate the input for a proper file name.
* @param strFile string File name.
* @return true || false
*/
function isFile(strFile){
	if(isEmpty(strFile)){
		return false;
	}

	var strFind = new String(strFile);
	var intPos = strFind.lastIndexOf("\\");

	if (/^[a-zA-Z]*[^\/:*?"<;>;|]+(?=\.\w{3,4})/.test(strFind.substring(intPos+1,strFile.length))){
		return true;	
	}
	return false;
}
//<!------------------------------------------------------------------!>

/**
* This function evaluates the input string for Whitespace.
* @param strValue string value.
* @return true, if it finds whitespaces || false otherwise.
*/
function isImage(strFile){
	if (!isFile(strFile)){
		return false;
	}

	var strFName = new String(strFile);
	var intIndx = strFName.lastIndexOf('.');
	var strExn = strFName.substring(intIndx+1,strFName.length).toLowerCase();

	if(/^\S*(gif|jpg|bmp)/.test(strExn)){
		return true;
	}
	return false;
}
//<!----------------------------------------------------------------

/**
* This function evaluates the input image file is in jpg format.
* @param strValue string value.
* @return true, if it finds whitespaces || false otherwise.
*/
function isJPG(strFile){
	if (!isFile(strFile)){
		return false;
	}

	var strFName = new String(strFile);
	var intIndx = strFName.lastIndexOf('.');
	var strExn = strFName.substring(intIndx+1,strFName.length).toLowerCase();

	if(/^\S*(jpg|jpeg|pjpeg)/.test(strExn)){
		return true;
	}
	return false;
}

/**
* It shows the layer of help desk
*/
function showHelpDesk(obLay){
	if(obLay.style.display == "block"){
		obLay.style.display = "none";
	}
	else{
		obLay.style.display = "block";
	}
}

/**
* It shows the layer based on the display property
*/
function showLayer(sTag){
	var obLayer = eval("document.getElementById('"+sTag+"')");
	
	if(obLayer.style.display == "block"){
		obLayer.style.display = "none";
	}
	else{
		obLayer.style.display = "block";
	}
}



/**
* This function is used to validate the Phone number field.
* @param obField Form field object.
* @param strField Form field name.
* @return true, if it is in number || false otherwise.
*/
function isPhone(obField, strField){
	var strData = new String(obField.value);
	
	for (var i=0; i < strData.length; i++){
		if ((strData.charAt(i) != '-')&&(strData.charAt(i) < '0') || (strData.charAt(i) > '9')){
			alert("Numeric value is required for the field "+ strField);
			obField.select();
			return false;
		}
	}
	return true;
}
//<!----------------------------------------------------------------

/**
 * addState function populates the US states.
 */
function addState(obElem,sSel){
	var US_state = new Array('Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut','Delaware','District of Columbia','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana','Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts','Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada','New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota','Ohio','Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Vermont','Virginia','Washington','West Virginia','Wisconsin','Wyoming');

	for(var i=1;i<US_state.length; ++i){
		if(US_state[i].match(sSel) != null){
			obElem.options[i] = new Option(US_state[i],US_state[i],'',false);
			obElem.options[i].selected = true;
		}
		else
			obElem.options[i] = new Option(US_state[i],US_state[i],'',false);
	}
}


/**
* This function is used to restrict the non-numeric value.
* @return false if non-numeric keycode
*/
function forceNumber(event){
   var keyCode = event.keyCode ? event.keyCode : event.charCode;
  
  if((keyCode < 48 || keyCode > 58) && keyCode != 8 && keyCode != 9 && keyCode != 37 && keyCode != 39 && keyCode != 46 && keyCode != 116) 
	  return false;
}
//<!----------------------------------------------------------------


/**
 * It forces the user to input only characters
 * @return ture or false
 */
function forceChar(){
	if(event.keyCode > 57 || event.keyCode == 32)
		return true;	
	return false;
} 

//<!----------------------------------------------------------------

/**
* This function is used to restrict the non-numeric value.
* @return false if non-numeric keycode
*/
function forceMoney(event,obField){
	 var keyCode = event.keyCode ? event.keyCode : event.charCode;
	if((keyCode<48 || keyCode>58) && keyCode!=46) 
		return false;

	if(keyCode==46){
		for(var intI = 0; intI < obField.length; intI++){
			if(obField.charAt(intI)=="."){
				return false;
			}
		}
	}
}
//<!----------------------------------------------------------------

/**
* This function checks the number of characters of the given text area. 
* If the characters exceeds the limit it prompts the alert.
* 
*/
function isExceeds(obElm, iLimit){
	if(obElm.value.length >= iLimit){
		alert("The characters length exceeds the limit.");
		obElm.focus();
		return false;
	}
	return true;
}




/**
* Creates a window object and loads the Document file.
* @param url refers the file to be loaded
*/
//<!----------------------------------------------------------------
function openDoc(url,wd,hg,resize){
	var strFeature = "left=0,top=0,width="+wd+", height="+hg+", scrollbars=yes,resizable="+resize;
	var obWin = window.open(url,"newWin",strFeature);
	obWin.focus();
	if(!obWin.opener) obWin.opener = self;
}

/**
* Creates a window object and loads the image file.
* @param url refers the file to be loaded
*/
//<!----------------------------------------------------------------
function openImg(url){
	var obWin = window.open(url,"imgWin","left=0,top=0,width=300,height=250");
	obWin.focus();
	if(!obWin.opener) obWin.opener = self;
}


/**====================================================================
 * NAME		:	datecmp(obDate1,obDate2)
 * DESC		:	Compares two date value, returns whether the second date is greater or equal.
 * PARAM	:	obDate1 start date
 *				obDate2 End date
 * RETURN	:	second date property as, 
 *				1 	= greater
 *				0	= Equal
 *				-1	= smaller	
 *====================================================================*/
 function datecmp(objDate1,objDate2){
	 var dtStart, dtEnd;
	
	var obDate1 = objDate1.split("/");
	var obDate2 = objDate2.split("/");
	dtStart = obDate1[1] +"/"+ obDate1[2] +"/"+ obDate1[0];
	dtEnd = obDate2[1] +"/"+ obDate2[2] +"/"+ obDate2[0];	
	
	
	var dtFirst = new Date(dtStart);
	var dtSec = new Date(dtEnd);
	var intFtime = Math.floor(dtFirst.getTime()/(24*60*60));
	var intStime = Math.floor(dtSec.getTime()/(24*60*60));
	var intDiff = (intStime-intFtime);
	
	if(intDiff < 0){
		return -1;
	}
	else if(intDiff == 0){
		return 0;
	}
	else if(intDiff >= 1){
		return 1;
	}
	 }
 
 
 	

	/**
	* Opens a the page in a popup window.
	*/
 	function popUpPage(sPage,iWd,iHg){
		var obWin;
		//alert(sPage);
		obWin = window.open(sPage,"popWin","width="+iWd+", height="+iHg+",statusbar=no,scrollbars=yes");
		if(!obWin.opener) obWin.opener = _self;
		obWin.focus();
	}


	/*
	* This function is used to highlight the selected row
	*/
	function rollOver(obElem){
		//alert(obElem);
		//alert(obElem.style.backgroundColor);
		document.getElementById("svr1").style.backgroundColor = "red";
		//document.getElementById("svr1").style.backgroundColor = "#C3D8FB";
		//}
	}


	/**
	 * This function is used to check the deblicate user name in the database
	 *
  	 */
	function authenticate(sTarget){
		var bFlag = 0;
		var obXttp;
		
		if(window.ActiveXObject){
			obXttp = new ActiveXObject("Microsoft.XMLHTTP")
		}
		else{
			obXttp = new XMLHttpRequest();
		}
		
		obXttp.open("GET",sTarget,false);
		obXttp.send();
		
		if(obXttp.readyState == 4){
			if(obXttp.status == 200){
				bFlag = obXttp.responseText;
			}
		}
		
		return bFlag;
	}

/**
* strComp function compares two input strings and return result in boolean value.
* @param strFirst base text on which the other string is compared
* @param strSec compared string
* @return True || False.
*/
function strComp(strFirst,strSec){
	if (strFirst.length == strSec.length){
		var strResult = new String(strFirst);

		if (strResult.search(strSec) != -1){
			return true;
		}
	}
	return false;
}

//<!----------------------------------------------------------------
/**
 * Function setCaption displays and hides a caption for the text field according to the users onfocus and blur event.
 */
 function setCaption(obElement,blnSts){
	if(obElement.value == obElement.defaultValue){
		if(blnSts == 1)
			obElement.value = "";
		else
			obElement.value = obElement.defaultValue;
	}
 }
 
	/**
	 * getFile identifies the file name from the physical path and returns the separated file.
	 */
	function getFile(fileName){
		var strRaw = new String(fileName);		
		var strFile = "";
		var intLpos = strRaw.lastIndexOf("\\");
	
		strFile = strRaw.substr(intLpos+1,(strRaw.length - intLpos));
	
		return strFile;
	}

	/**
	 * This function is to check the selected file is a document.	
	 */
	function isDocument(obElem){
		var strExt = obElem.value.split("\.").pop();
		
		if(strExt.match("doc") || strExt.match("pdf"))
			return true;
		else{
			alert("Please select only .doc or .pdf files");
			obElem.select();
			return false;
		}
	}


	/**
	* ImageFrame 
	* Resize the window based on the image size
	*/
	function imageFrame(){
		var obImg = document.images[0];
		var intHg = document.body.clientHeight;		
		var intWd = document.body.clientWidth;

		intHg = obImg.height - intHg;
		intWd = obImg.width - intWd;

		window.resizeBy(intWd, intHg);
		self.focus();
	}
 
 /**=============================================================================
 * NAME		:	getDefault()
 * DESC		:	This function clears the default value of the control when the focus is move on it.
 * PARAM	:	obElem - Element object
 * =============================================================================
 */
 
 	function getDefault(obElem){
		if(obElem.defaultValue.match(obElem.value) != null){
			obElem.value = "";
		}
	}

/**=============================================================================
 * NAME		:	setDefault()
 * DESC		:	This function sets the default value of the form controls.
 *				It sets the default focus when the control is empty on last focus event.
 * PARAM	:	Form form name
 * =============================================================================
 */
	function setDefault(Form){
		for(var i = 0; i < Form.elements.length; i++){
			if(isEmpty(Form.elements[i].value)){
				Form.elements[i].value = Form.elements[i].defaultValue;
			}
		}
	}
 
 /**=============================================================================
 * NAME		:	defaultFocus()
 * DESC		:	This function sets the focus to the first field of the form.
 * PARAM	:	Form form name
 * =============================================================================
 */
	function defaultFocus(){
		var F = document.forms[0];
		if(F){
			if(F.elements[0] && !F.elements[0].disabled){
				F.elements[0].focus();
			}
		}
	}
	
	/*==============================================================================
	 * NAME		:	setTarget(page)	
	 * DESC		:	This function sets the target page for the form action property.
	 * PARAM	: 	Page string page name
	 *==============================================================================*/
 	function setTarget(page){
		var F = document.forms[0];
		
		F.method = "post";
		F.action = page;
		F.submit();
	}
 
 	
	/*==============================================================================
	 * NAME		:	printPage()	
	 * DESC		:	This function calls the window.print() function.
	 *==============================================================================*/
	function printPage(){
		if(window.print){
			window.print();	
		}
	}
	
	/**
	 *  Add to Favorite function adds the webpage to the Favorite collections.
	 */
	function addFavorite(){
		window.external.addFavorite(location.href,document.title);
	}

	/**
	 *  setImg function sets the image in the image placeholder.
	 */
	function setImg(path,obImg){
		if(path != ''){
			obImg.setAttribute('src', path);	
			obImg.style.display = "block";
		}
		else{
			obImg.style.display = "none";
		}
		
	}
	
	/**
	 *  setImg function sets the image in the image placeholder.
	 */
	function control(Form,bFlag){
		for(var i=0; i<Form.elements.length; i++){
			if(Form.elements[i].type != "submit" && Form.elements[i].type != "button"){	
				Form.elements[i].disabled = bFlag;
			}
		}
	}
	

	function isLeapYear(year){
		if((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0))
			return true;
		else
			return false;
	}
	
	
	function monthDays(obElement){
		var Mon	= obElement[0].value;
		var Day = obElement[1].value;
		var Year = obElement[2].value;
		
		if(Mon == 4 || Mon == 6 || Mon == 9 || Mon == 11){
			if(Day > 30){
				obElement[1].value = 30;
			}
		}
		else {
			if(Mon == 2){
				if(isLeapYear(Year)){
					if(Day > 29){
						obElement[1].value = 29;
					}
				}
				else{
					if(Day > 28){
						obElement[1].value = 28;
					}
				}
			}
		}
	}
	
	
	
	function listing(obElem){
		obElem.style.backgroundColor="#507688";
		obElem.style.cursor='hand';
	}
	
	
	function listing_hover(obElem){
		obElem.style.backgroundColor="#A5B8C1";
	}
	
	
	function set_target(sDest){
		window.location.href = sDest;
		window.focus();
	}
	
	function formatDec(field, decplaces) {  
				num = parseFloat(field.value);
				if (!isNaN(num)) {
					var str = "" + Math.round (eval(num) * Math.pow(10,decplaces));
					if (str.indexOf("e") != -1) {
						return "Out of Range";
					}
					while (str.length <= decplaces) {
						str = "0" + str;
					}
					var decpoint = str.length - decplaces;
					//alert(str.substring(0,decpoint) + "." + str.substring(decpoint,str.length));
					field.value = str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
				} else {
					return "NaN";
				}
				//alert("adf");
			}
			
			/*

	*purpose   : checking URL validation
	*argument  :  links
	*/
	function checkURL(value) { 
	var tomatch= /[A-Za-z0-9\.-]{3,}\.[A-Za-z]{3}/
	if(tomatch.test(value))
	{
	return true; }
	else  {
		alert("Please enter the valid link");
		return false;
	}
	}
	
	
	function toggleBox(szDivID, iState) //
      {	
	var obj = document.layers ? document.layers[szDivID] :
	document.getElementById ?  document.getElementById(szDivID).style :
	document.all[szDivID].style;
	obj.visibility = document.layers ? (iState ? "show" : "hide") :	(iState ? "visible" : "hidden");

     }

    function layerShow(V_name,H_name) {
	var a_layer = V_name;
	var i_layer = H_name;
	toggleBox(a_layer, 1);
	toggleBox(i_layer, 0);	
	}
	
	
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: David Leppek :: https://www.azcode.com/Mod10

Basically, the alorithum takes each digit, from right to left and muliplies each second
digit by two. If the multiple is two-digits long (i.e.: 6 * 2 = 12) the two digits of
the multiple are then added together for a new number (1 + 2 = 3). You then add up the 
string of numbers, both unaltered and new values and get a total sum. This sum is then
divided by 10 and the remainder should be zero if it is a valid credit card. Hense the
name Mod 10 or Modulus 10. */
function isCreditCard(ccNumb) { 

// v2.0
var valid = "0123456789"  // Valid digits in a credit card number
var len = ccNumb.length;  // The length of the submitted cc number
var iCCN = parseInt(ccNumb);  // integer of ccNumb
var sCCN = ccNumb.toString();  // string of ccNumb
sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
var iTotal = 0;  // integer total set at zero
var bNum = true;  // by default assume it is a number
var bResult = false;  // by default assume it is NOT a valid cc
var temp;  // temp variable for parsing string
var calc;  // used for calculation of each digit

// Determine if the ccNumb is in fact all numbers
for (var j=0; j<len; j++) {
  temp = "" + sCCN.substring(j, j+1);
  if (valid.indexOf(temp) == "-1"){bNum = false;}
}

// if it is NOT a number, you can either alert to the fact, or just pass a failure
if(!bNum){
  /*alert("Not a Number");*/bResult = false;
}

// Determine if it is the proper length 
if((len == 0)&&(bResult)){  // nothing, field is blank AND passed above # check
  bResult = false;
} else{  // ccNumb is a number and the proper length - let's see if it is a valid card number
  if(len >= 15){  // 15 or 16 for Amex or V/MC
    for(var i=len;i>0;i--){  // LOOP throught the digits of the card
      calc = parseInt(iCCN) % 10;  // right most digit
      calc = parseInt(calc);  // assure it is an integer
      iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
      i--;  // decrement the count - move to the next digit in the card
      iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
      calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
      calc = calc *2;                                 // multiply the digit by two
      // Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
      // I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
      switch(calc){
        case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
        case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
        case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
        case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
        case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
        default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
      }                                               
    iCCN = iCCN / 10;  // subtracts right most digit from ccNum
    iTotal += calc;  // running total of the card number as we loop
  }  // END OF LOOP
  if ((iTotal%10)==0){  // check to see if the sum Mod 10 is zero
    bResult = true;  // This IS (or could be) a valid credit card number.
  } else {
    bResult = false;  // This could NOT be a valid credit card number
    }
  }
}
// change alert to on-page display or other indication as needed.
/*if(bResult) {
  alert("This IS a valid Credit Card Number!");
}
if(!bResult){
  alert("This is NOT a valid Credit Card Number!");
}*/
  return bResult; // Return the results
}




