// formatString (OBJECTNAME objName,PATTERN strFormatPattern,DATATYPE DataType [, BOOLEAN emptyOK==false])
function formatString(objName, strFormatPattern, DataType, emptyOK) {


   var strText = objName.value;
   var strReturn ='';
   var strComp ='';
   var iCounter=0;
   var acceptableChars = "1234567890";
   var strMsg ='';
   if (strText != '')
   {
    for ( var i=0; i<=strText.length-1; i++ )
    {
        if ( acceptableChars.indexOf(strText.charAt(i)) != -1 )
        {
            strComp= strComp+strText.charAt(i);
        }
        else
        {
        if ((strText.charAt(i)!= "/") &&(strText.charAt(i)!= "-") && (strText.charAt(i)!= "."))
        {
          if (DataType =="Telephone" || DataType =="Pager" || DataType =="Fax")
          {
            strMsg='This Telephone is invalid. The valid Telephone formats are 123/456/7890 (or) 123-456-7890 (or) 1234567890.';
            warnInvalid(objName,strMsg)
            return false;
          }
          if (DataType =="SocialSecurityNumber"){
            strMsg="This Social Security No. is invalid. The valid Social Security No. formats are 123-45-6789 (or) 123456789.";
            warnInvalid(objName,strMsg)
            return false;
          }

        }
        }

    }

        var intLength;
    intLength= strComp.length;

    if ((DataType =="Telephone")|| (DataType =="Pager") || (DataType =="Fax"))
    {
      if (intLength < 10 || intLength > 10)
      {
        strMsg="This Telephone is invalid. The valid Telephone formats are 123-456-7890 (or) 1234567890.";
        warnInvalid(objName,strMsg)
        return false;
      }
    }
    if ((DataType =="SocialSecurityNumber")&& (intLength < 9 || intLength > 9) ){
      strMsg="This Social Security No. is invalid. The valid Social Security No. formats are 123-45-6789 (or) 123456789.";
      warnInvalid(objName,strMsg)
      return false;
    }
    if ((DataType =="SchwabAccountNumber")&& (intLength < 8 || intLength > 8) ){
      strMsg="This Master Account Number is invalid. The valid formats are ####-#### (or) ########.";
      warnInvalid(objName,strMsg)
      return false;
    }
	if ((DataType =="EmployerIdentNo")&& (intLength < 9 || intLength > 9) ){
      strMsg="The Employer Identification Number is invalid. The valid formats are ##-####### (or) #########.";
      warnInvalid(objName,strMsg)
      return false;
    }

    for ( var i=0; i<=strFormatPattern.length-1; i++ )
        {
            if (strFormatPattern.charAt(i)=='#')
            {
                strReturn=strReturn+strComp.charAt(iCounter);
                iCounter=iCounter+1;
            }
            else
                strReturn=strReturn+strFormatPattern.charAt(i);
        }

   }

   objName.value= strReturn;
   return true;
}

function warnInvalid (theField, s)
{
    theField.select()
    alert(s)
    theField.focus()
    return false
}
//DATE VALIDATION - START

function checkdate(objName) {

var datefield = objName;
var strtemp = datefield.value;
var strtemp1 = replace(strtemp,"/","");

//Check for integer
if (strtemp1 != ""){
  if (IsNUMERIC(strtemp1) == false || strtemp1.length != 8){
    datefield.select();
    alert("That date is invalid. The valid date formats are MM/DD/YYYY (or) MMDDYYYY.");
    datefield.focus();
    return false;
  }

  if (datefield.value.length >=5){
    datefield.value=strtemp1.substr(0,2) + '/' + strtemp1.substr(2,2) + '/' + strtemp1.substr(4,4);
  }
}

if (datefield.value.length >= 10){
  if (chkdate(objName) == false) {
    datefield.select();
    alert("That date is invalid. The valid date formats are MM/DD/YYYY (or) MMDDYYYY.");
    datefield.focus();
    return false;
  }
  else {
    if (strtemp1.substr(4,4) <= 1900.){
      datefield.select();
      alert("The year should be greater than 1900");
      datefield.focus();
      return false;
    }
    return true;
  }
 }
}

function chkdate(objName) {
var strDatestyle = "US"; //United States date style
var strDate;
var strDateArray;
var strDay;
var strMonth;
var strYear;
var intday;
var intMonth;
var intYear;
var booFound = false;
var datefield = objName;
var strSeparatorArray = new Array("-"," ","/",".");
var intElementNr;
var err = 0;
var strMonthArray = new Array(12);
strMonthArray[0] = "Jan";
strMonthArray[1] = "Feb";
strMonthArray[2] = "Mar";
strMonthArray[3] = "Apr";
strMonthArray[4] = "May";
strMonthArray[5] = "Jun";
strMonthArray[6] = "Jul";
strMonthArray[7] = "Aug";
strMonthArray[8] = "Sep";
strMonthArray[9] = "Oct";
strMonthArray[10] = "Nov";
strMonthArray[11] = "Dec";
strDate = datefield.value;
if (strDate.length < 1) {
return true;
}
for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
strDateArray = strDate.split(strSeparatorArray[intElementNr]);
if (strDateArray.length != 3) {
err = 1;
return false;
}
else {
strDay = strDateArray[0];
strMonth = strDateArray[1];
strYear = strDateArray[2];
}
booFound = true;
   }
}
if (booFound == false) {
if (strDate.length>5) {
strDay = strDate.substr(0, 2);
strMonth = strDate.substr(2, 2);
strYear = strDate.substr(4);
   }
}
if (strYear.length == 2) {
strYear = '20' + strYear;
}
// US style
if (strDatestyle == "US") {
strTemp = strDay;
strDay = strMonth;
strMonth = strTemp;
}
intday = parseInt(strDay, 10);
if (isNaN(intday)) {
err = 2;
return false;
}
intMonth = parseInt(strMonth, 10);
if (isNaN(intMonth)) {
for (i = 0;i<12;i++) {
if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
intMonth = i+1;
strMonth = strMonthArray[i];
i = 12;
   }
}
if (isNaN(intMonth)) {
err = 3;
return false;
   }
}
intYear = parseInt(strYear, 10);
if (isNaN(intYear)) {
err = 4;
return false;
}
if (intMonth>12 || intMonth<1) {
err = 5;
return false;
}
if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
err = 6;
return false;
}
if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
err = 7;
return false;
}
if (intMonth == 2) {
if (intday < 1) {
err = 8;
return false;
}
if (LeapYear(intYear) == true) {
if (intday > 29) {
err = 9;
return false;
}
}
else {
if (intday > 28) {
err = 10;
return false;
}
}
}
if (strDatestyle == "US") {
//datefield.value = strMonthArray[intMonth-1] + " " + intday+" " + strYear;
}
else {
//datefield.value = intday + " " + strMonthArray[intMonth-1] + " " + strYear;
}
return true;
}
function LeapYear(intYear) {
if (intYear % 100 == 0) {
if (intYear % 400 == 0) { return true; }
}
else {
if ((intYear % 4) == 0) { return true; }
}
return false;
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
//DATE VALIDATION - END
//GENERAL FUNCTIONS - START

function replace(string,text,by) {
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function trim(string)
{
  var strtemp = string.replace( /^\s*/, "" )    //Ltrim
  strtemp =strtemp.replace( /\s*$/, "" ); //Rtrim
  return strtemp
}


function IsNUMERIC(check_tobe)
{
  var acceptableChars = "1234567890";
  for ( var i=check_tobe.length-1; i>=0; i-- )
  {
    if ( acceptableChars.indexOf(check_tobe.charAt(i))==-1 )
    return false;
  }
   return true;
}

function reformat (s)

{   var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < reformat.arguments.length; i++) {
       arg = reformat.arguments[i];
       if (i % 2 == 1) resultString += arg;
       else {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;

}

function checkEmail(objName) {
if (objName.value != "")
{
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(objName.value)){
    return (true)
  }
  objName.select();
  alert("Invalid E-mail Address. The valid E-mail address formats are abc@theonline401k.com (or) abc@co.in.");
  objName.focus();
  return (false)
}
}

function checkZIPCode (theField, emptyOK)
{   if (checkZIPCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    { var normalizedZIP = stripCharsInBag(theField.value, ZIPCodeDelimiters)    
      if (!isZIPCode(normalizedZIP, false))
         return warnInvalid (theField, "Please Enter a Valid Zipcode. Valid zipcode formats are 99999 or 999999999 or 99999-9999.");
      else
      {  // if you don't want to insert a hyphen, comment next line out
         theField.value = reformatZIPCode(normalizedZIP)
         return true;
      }
    }
}

function toggleElement(name, display) {
		var element = document.getElementById(name);
		element.style.display = display;
}

function toggleElement(elementId) {
	 if(document.getElementById(elementId).style.display == 'none') {
		document.getElementById(elementId).style.display = '';
	 } else {
		 document.getElementById(elementId).style.display = 'none';
	 }
	 
	 return false;
}

function getParameter ( queryString, parameterName ) {
   // Add "=" to the parameter name (i.e. parameterName=value)
   var parameterName = parameterName + "=";
   if ( queryString.length > 0 ) {
      // Find the beginning of the string
      begin = queryString.indexOf ( parameterName );
      // If the parameter name is not found, skip it, otherwise return the value
      if ( begin != -1 ) {
         // Add the length (integer) to the beginning
         begin += parameterName.length;
         // Multiple parameters are separated by the "&" sign
         end = queryString.indexOf ( "&" , begin );
      if ( end == -1 ) {
         end = queryString.length
      }
      // Return the string
      return unescape ( queryString.substring ( begin, end ) );
   }
   // Return "null" if no parameter has been found
   return "null";
   }
}

function validForChars(str, acceptableChars) {
  for (var i = str.length - 1; i >= 0; i--) {
    if (acceptableChars.indexOf(str.charAt(i)) == -1)
    	return false;
  }
  return true;
}

function changecss(theClass,element,value) {
	//documentation for this script at http://www.shawnolson.net/a/503/
	 var cssRules;
	 if (document.all) {
	  cssRules = 'rules';
	 }
	 else if (document.getElementById) {
	  cssRules = 'cssRules';
	 }
	
	 for (var S = 0; S < document.styleSheets.length; S++){
	  for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
	   if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
	    document.styleSheets[S][cssRules][R].style[element] = value;
	   }
	  }
	 }	
	}
	
	function isEmpty(val) { 
			if (val == null || val == "") {
				return true;
			}
			return false;
		}
		
function enableElement(id, val) {
	var element = document.getElementById(id);
	element.disabled = !val;
}

function setElementValue(elId, val) {
	document.getElementById(elId).value = val;
}

function existsInArray(array, value) {
	if (typeof(array) == "undefined" || array == null) {
		return -1;
	}
	
	var i;
	for (i = 0; i < array.length; i++) {
		if (array[i] == value)
			return i;
	}
	
	return -1;
}

function isIeBrowser() {
	var userAgent = window.navigator.userAgent;
	var msie = userAgent.indexOf("MSIE");
	
	if (msie != -1) {
		return true;
	} else {
		return false;
	}
}

function keepMeAlive(imgName) {
	myImg = document.getElementById(imgName);
	if (myImg) {
		myImg.src = myImg.src.replace(/\?.*$/, '?' + Math.random());
	}
}

function isUsCurrency (input) {
	return RegExp(/^\$?\d+(\.\d{2})?$/).test(String(input).replace(/^\s+|\s+$/g, ""));
}
