
/**
* 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(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;
}
//<!----------------------------------------------------------------

/**
* 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;
}
//<!------------------------------------------------------------------!>

/**
* 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 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;
}

//<!----------------------------------------------------------------

//This function parses the phone number into US phone number format.
	function ParseUSNumber(obPhone){
		var FmtStr="";
		var index = 0;
		var LimitCheck;

		LimitCheck = obPhone.value.length;
		while (index != LimitCheck){
			if (isNaN(parseInt(obPhone.value.charAt(index)))){}
			else{ FmtStr = FmtStr + obPhone.value.charAt(index); }
			index = index + 1;
		}
		if(!isEmpty(FmtStr) && FmtStr.length < 10){
			FmtStr=obPhone.value;
			alert("United States phone numbers must have exactly ten digits.");
			obPhone.focus();
			return false;
		}
		else{
			if(FmtStr >= 10)
				FmtStr = "(" + FmtStr.substring(0,3) + ") " + FmtStr.substring(3,6) + "-" + FmtStr.substring(6,10);
		}
		obPhone.value = FmtStr;
	}

/**
 * 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(){
  if(event.keyCode < 48 || event.keyCode > 58) 
	  return false;
}
//<!----------------------------------------------------------------

/**
* This function is used to restrict the non-numeric value except space.
* @return false if non-numeric keycode
*/
function forceNumSpace(){
  if((event.keyCode < 48 || event.keyCode > 58)&&(event.keyCode != 32)) 
	  return false;
}
//<!----------------------------------------------------------------

/**
* This function is used to restrict the non-numeric value.
* @return false if non-numeric keycode
*/
function forceMoney(obField){
	if((event.keyCode<48 || event.keyCode>58) && event.keyCode!=46) 
		return false;

	if(event.keyCode==46){
		for(var intI = 0; intI < obField.length; intI++){
			if(obField.charAt(intI)=="."){
				return false;
			}
		}
	}
}
//<!----------------------------------------------------------------

/**
* 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;
}
	
	/**
	* 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		:	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(obDate1,obDate2){
	var dtStart, dtEnd;
	dtStart = obDate1[0].value +"/"+ obDate1[1].value +"/"+ obDate1[2].value;
	dtEnd = obDate2[0].value +"/"+ obDate2[1].value +"/"+ obDate2[2].value;
	
	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 < 1){
		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+",scrollbars=yes,statusbar=no, resizable=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 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 setCaption displays and hides a caption for the text field according to the users onfocus and blur event.
 */
 function setCaption(obElement,strMsg,blnSts){
	if(blnSts == 1)
		obElement.value = "";
//		obElement.focus();
	else
		obElement.value = strMsg;
 }
 
	/**
	 * 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;
		}
	}

 
 /**=============================================================================
 * 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();
	}
 
 
	/**
	 *  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){
		obImg.src = path;	
		obImg.style.display = "";
	}
	
	/**
	 *  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;
	}
	
	
	//Set row background color
	function listing(obElem){
		obElem.style.backgroundColor="#427E9B";
		obElem.style.cursor='hand';
	}
	
	//Sets row hover background color.
	function listing_hover(obElem){
		obElem.style.backgroundColor= "#5799B9";
	}
	
	//Assigns the window target page.
	function set_target(sDest){
		window.location.href = sDest;
		window.focus();
	}
	
