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


/**
* This function is used to force to display numeric value with decimal.
* @return false if non-numeric
*/

function formatDecNumber(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";
    }
}


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

/**
* This function checks the value contains only zero.
*
* @param string search text
*/
function isZero(sSearch){
	var sPattern = /^(0)$/;
	if(sSearch.search(sPattern) != -1){
		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(){
  if(event.keyCode < 48 || event.keyCode > 58) 
	  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 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;
}


/**====================================================================
 * 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+",statusbar=no");
		if(!obWin.opener) obWin.opener = _self;
		obWin.focus();
	}
	
	
	/**
	* Opens a free page in  popup window.
	*/
 	function FreepopUpPage(sPage,iWd,iHg,sTopTitle,sLeftTitle,sImagepath,sBottomTitle,sBottomDescription,sRightTitle,sCategory,iID){
		var obWin;

		obWin = window.open(sPage+"?TopTitle="+sTopTitle+"&LeftTitle="+sLeftTitle+"&BigImagepath="+sImagepath+"&BottomTitle="+sBottomTitle+"&BottomDescription="+sBottomDescription+"&RightTitle="+sRightTitle+"&Category="+sCategory+"&ID="+iID,"popWin","width="+iWd+", height="+iHg+",statusbar=no");
		if(!obWin.opener) obWin.opener = _self;
		obWin.focus();
	}

 	function openpopUpPage(sPage,iWd,iHg,sCategory,sIsVertical,sIsMember,iID){
		var obWin;

		obWin = window.open(sPage+"?Category="+sCategory+"&IsVertical="+sIsVertical+"&IsMember="+sIsMember+"&ID="+iID,"popWin","width="+iWd+", height="+iHg+",toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0");
		if(!obWin.opener) obWin.opener = _self;
		obWin.focus();
	}


	/**
	* Opens a the page in a popup window.
	*/
 	function popUp(sPage){
		var obWin;
		//alert(sPage);
		obWin = window.open(sPage,"popWin","scrollbars=1,resizable=1");
		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(Form, page){
		Form.method = "post";
		Form.action = page;
		Form.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 OpenUploadImageForm(imagefor,objname,oldimagename)
	{
		var sURL="../admin/rte/RTE_popup_file_atch_g.asp";
		var width=775;
		var height=395;

		//var width=400;
		//var height=280;
		//var sURL="../UploadForm/ImgUploadform.asp";		
		
		window.open(sURL+"?ImageFor="+imagefor+"&objname="+objname+"&oldimagename="+oldimagename,"","width="+width+",height="+height);
		//window.open("../UploadForm/ImgUploadform.asp?ImageFor="+imagefor+"&objname="+objname+"&oldimagename="+oldimagename,"","width=400,height=280");
}
	
var c_status;
 function selectAll(formObj, isInverse) {
		if(c_status != 1){
			for (var i=0;i < formObj.length;i++){
		      fldObj = formObj.elements[i];
		      if (fldObj.type == 'checkbox')
		      {
			      fldObj.checked = true;			      
		      }
	      }
	      c_status = 1;
		}
		else {
			for (var i=0;i < formObj.length;i++){
		      fldObj = formObj.elements[i];
		      if (fldObj.type == 'checkbox')
		      {
			      fldObj.checked = false;
			      
			  }
		    }
		    c_status = 0;
		}  
	}
//end of select all function
function delete_data(sPage,formObj) {
var d_status=0;

			for (var i=0;i < formObj.length;i++){
		      fldObj = formObj.elements[i];
		      if (fldObj.type == 'checkbox')
		      {
			      if (fldObj.checked)
					d_status=d_status+1;				  
		      }
	      }

		
		if (d_status>0)
		{
			var agree=confirm("Are you sure you would like to delete the selected items?");
			if (agree) {
				formObj.hidMode.value="delete";
				//document.frmAdmin.hidID.value=iID;
				//alert(iID);
				formObj.action = sPage;
				formObj.submit();			
				}
			else
				false;
		}
		else
			alert("You must select anyone to delete");
	}
	function openWin(iID,sMode,sPage,formObj)
	{
			var obWin=eval(formObj);
			//alert('hi' +obWin.hidMode.value);
			obWin.hidMode.value=sMode;
			obWin.hidID.value=iID;
			obWin.action = sPage;
			obWin.submit();
			//alert(sPage);
			/*obWin = window.open(sPage,"popWin","width=350, height=500,scrollbars=yes");
			if(!obWin.opener) obWin.opener = _self;
			obWin.focus();*/
	}
	function CheckLength(Obj,len,minmax,Errmsg)
	{
	if(minmax=="<")
	{
		if(Obj.value.length<=len)
		{
			if(isblank(Errmsg))
			alert("Please Enter length greater than "+ len + " for " + Obj.name);
			else
			alert(Errmsg);
			
			Obj.focus();
			return false;
		}
	}
	else if(minmax==">")
	{
		if(Obj.value.length>=len)
		{
			if(isblank(Errmsg))
			alert("Please Enter length less than "+ len + " for " + Obj.name);
			else
			alert(Errmsg);
			
			Obj.focus();
			return false;
		}
	}
	return true;
}
