﻿// JScript File
var whitespace = " \t\n\r";
String.prototype.trim = function() {
  var temp = this.replace( /^(\s)*|(\s)*$/ , ""  ); /* remove leading and trailing spaces. */
      temp = temp.replace( /(\s)+/g        , " " ); /* remove inner duplicate spaces. */
	  temp = temp.replace( /\t|\r|\n/g     , ""  ); /* remove tabs newlines and carriage returns. */
      if( temp == " " ) temp = "";                  /* if what in hand is only " " remove it as well. */
	  return temp;
}

function IsInvalid(strRegExp,strFieldValue)
{
	var RegularExp = new RegExp(strRegExp);
	return (RegularExp.test(strFieldValue));
}

function IsRequired(objField)
{
	if (objField )
	{
		if (objField.value.trim() == "")
		{
			return true;
		}
	}
	return false;
}

function IsMatch(objField1,objField2)
{
	if (objField1 && objField2)
	{
		if (objField1.value == objField2.value )
		{
			return true;
		}
	}
	return false;
}

function DisplayError(strInputName,strBoxClass,strInputClass,strTextClass,strTextEndClass,strErrorMessage,strStyle,strStyleValue)
{
	if ( getID("ErrorElementsDiv") )
	{
		if ( getID(strInputName+"_ErrorElementsDiv") )
		{
			//
		}
		else
		{	
			var objInputDiv = document.createElement("div");
			objInputDiv.setAttribute("id",strInputName+"_ErrorElementsDiv");			
			var objErrorBoxDiv = document.createElement("div");
			objErrorBoxDiv.setAttribute("id",strInputName+"ErrorBoxDiv");
			objErrorBoxDiv.innerHTML =  getID("ErrorBox"+strInputName).className;			
			var objInputItemDiv = document.createElement("div");
			objInputItemDiv.setAttribute("id",strInputName+"InputItemDiv");
			objInputItemDiv.innerHTML =  getID("InputItem"+strInputName).className;
			objInputDiv.appendChild(objErrorBoxDiv);
			objInputDiv.appendChild(objInputItemDiv);			
			getID("ErrorElementsDiv").appendChild(objInputDiv);
		}
	}	
    var objErrorBox =getID("ErrorBox"+strInputName) ;
    var objErrorText = getID("ErrorText"+strInputName);
    var objErrorTextEnd = getID("ErrorTextEnd"+strInputName);
    var objInputItem = getID("InputItem"+strInputName);
    
    if (objErrorBox && objErrorText && objErrorTextEnd && objInputItem)
	{
		if (strBoxClass.length > 0 ) objErrorBox.className += " "+strBoxClass ;
		objErrorText.className = strTextClass;
		objErrorText.innerHTML = strErrorMessage;
		objErrorText.style.display = "block";		
		if (strTextEndClass.length > 0 ) objErrorTextEnd.className = strTextEndClass ;  
		if (strInputClass.length > 0 )objInputItem.className  += " "+ strInputClass;  	
		SetTextObjectStyle(objErrorBox,strStyle,strStyleValue);
	}
}

function SetTextObjectStyle(objRoot,strStyle,strStyleValue)
{
	if (objRoot && objRoot.childNodes)
	{
		for (var i=0;i<objRoot.childNodes.length;i++)
		{
			var objTmp = objRoot.childNodes[i];
			if (objTmp)
			{
					var ValidTag = true;
					if (objTmp.tagName)
					{
						if ((objTmp.tagName.toUpperCase() == "SELECT" || objTmp.tagName.toUpperCase() == "INPUT" || objTmp.tagName.toUpperCase() == "TEXTAREA" ))
						{
							ValidTag = false;	
						}
					}					
					if (ValidTag)
					{
						if (objTmp.childNodes && objTmp.childNodes.length )
						{
							SetTextObjectStyle(objTmp,strStyle,strStyleValue);
						}
						else
						{
							if (objTmp.nodeName == "#text")
							{
								eval("objTmp.parentNode.style."+strStyle+"='"+strStyleValue+"'");
							}
						}
					}
			}
		}
	}
}

function ResetError(strInputName,strStyle,strStyleValue)
{
    if ( getID(strInputName+"_ErrorElementsDiv") && getID(strInputName+"ErrorBoxDiv") && getID(strInputName+"InputItemDiv")  )
    {
		var objErrorBox = getID("ErrorBox"+strInputName) ;
		var objErrorText = getID("ErrorText"+strInputName);
		var objErrorTextEnd = getID("ErrorTextEnd"+strInputName);
		var objInputItem = getID("InputItem"+strInputName);

		if (objErrorBox && objErrorText && objErrorTextEnd && objInputItem)
		{
			SetTextObjectStyle(objErrorBox,strStyle,strStyleValue);
			objErrorBox.className = getID(strInputName+"ErrorBoxDiv").innerHTML;
			objErrorText.className = "";
			objErrorText.innerHTML = "";
			objErrorText.style.display = "none";		
			objErrorTextEnd.className = "" ;  
			objInputItem.className  = getID(strInputName+"InputItemDiv").innerHTML ;  	
		}
	}
}


/*
function isWeblink(strWeblink){
	var weblink=new RegExp("http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?");
	return(weblink.test(strWeblink));
}
*/
function isEmail(strEmail){
	var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/i;
	return(email.test(strEmail));
}

/*1:valid;0:invalid numeric;-1:invalid non numeric*/
function isNumeric(strValue,blnInteger){
	var intNumber=parseInt(strValue);
	var dblNumber=parseFloat(strValue);
	if(blnInteger) {
		if(!isNaN(intNumber)){
			if((""+intNumber)==(""+strValue)){return 1;/*valid*/}
			else{return 0;/*invalid but numeric*/}
		}else{return -1;/*invalid and non numeric*/}
	}else{
		if(!isNaN(dblNumber)){
			if((""+dblNumber)==(""+strValue)){return 1;/*valid*/} 
			else{return 0;/*invalid but numeric*/}
		}else{return -1;/*invalid and non numeric*/}
	}
}

/**@return true if s is null or empty.*/
function isEmpty(s){return((s==null)||(s.length==0));}

/**@return true if s is composed of only whitespace characters.*/
function isWhitespace(s){   
	var i;
    /*Is s empty?*/
    if(isEmpty(s))return true;
	/*Search through string's characters one by one until we find a non-whitespace character. When we do, return false; if we don't, return true.*/
    for(i=0;i<s.length;i++){   
        /*Check that current character isn't whitespace.*/
        var c=s.charAt(i);
        if(whitespace.indexOf(c)==-1)return false;
    }
    /* All characters are whitespace. */
    return true;
}

/**@return true if c is a non-numeric letter.*/
function isLetter(c){return(((c>="a")&&(c<="z"))||((c>="A")&&(c<="Z")));}

/**@return true if c is a digit.*/
function isDigit(c){return((c>="0")&&(c<="9"));}

/**@return true if c is a non-alphanumeric but valid e-mail character.*/
function isEmailChar(c){return((c==".")||(c=="_")||(c=="@")||(c=="-"));}

/**@return true if s is a string composed of alphanumeric or e-mail characters only.*/
function hasValidChars(s){
	//alert("s:"+s);
	var i;
    for (i=0;i<s.length;i++){
        var c = s.charAt(i);
        if (!(isLetter(c)||isDigit(c)||isEmailChar(c)))return false;
    }
	return true;
}
