var xmlHttp = false;
  setHTTP();
  
function setHTTP()
{
    try {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
      try {
        xmlHttp = new ActiveXObject("Msxml2.XMLHTTP26");
      } catch (e2) {
        xmlHttp = false;
      }
    }
}

  function funValidEmail(email)
  {
    var validEmail = true;
    
    if (email.length > 0)
    {
        if (trimString(email) != "")
        {
             var validChars = "abcdefghijklmnopqrstuvwxyz.-_0123456789@ABCDEFGHIJKLMNOPQRSTUVWXYZ"
             var firstchar
             var maillen
             var str
             var mail = trimString(email)
             firstchar = mail.substring(0,1)
             maillen = mail.length;
     
             if (firstchar == "@" || firstchar == "." || firstchar == "-")
             {
                validEmail = false;
             }
     
            for (var i=0; i < maillen; i++)
            {
               str = mail.substring(i, i+1);
               if(validChars.indexOf(str)== -1)
               {
                  validEmail = false;
               }
            }
     
            if (mail.indexOf("@",0) != -1 )
            {
                if (mail.indexOf("@",mail.indexOf("@",0)+1)!= -1 )
                {
                  validEmail = false;
                }
            } 
     
            if( (mail.indexOf("@") == -1) || (mail.indexOf(".") == -1) )
            {
                validEmail = false;
            }
        }
        else
        {
            validEmail = false;
        }
    }
    return validEmail;
  }
  
  function trimString(trimThis)
  {
     var localString = trimThis;
     var flag = true;
     var len = localString.length;
     var cnt = 0;
     while( (localString.charAt(cnt) == ' ') && (cnt++ < len) )
     {
     }
    
     if(cnt >= len)
      localString = "";
     else
     {
      localString = localString.substring(cnt,len);
      len = localString.length;
      cnt=len-1;
      while( (localString.charAt(cnt) == ' ') && (cnt-- < len ) )
      {
      }
      if(cnt < len-1)
       localString = localString.substring(0,cnt+1);
     }
     return(localString);
  }
  
  function PhoneNumberFormat(m)
  {
    p1=document.getElementById(m);
    if(blockNonNumbers(p1,window.event,false,false,false))
    {
        id = m;
        ValidatePhone();
    }
    else
    return false;
  }
  // Phone Number Format
  var n;
  var p;
  var p1;
  var id;
    
  function ValidatePhone()
	{
      p=p1.value
      if(p.length==3)
      {
          pp=p;
          d4=p.indexOf('(')
          d5=p.indexOf(')')
          if(d4==-1)
          {
          pp="("+pp;
          if(d5==-1)
          {
            pp=pp+")";
          }
          }
          document.getElementById(id).value="";
          document.getElementById(id).value=pp;
      }
      
      if(p.length>3)
      {
          d1=p.indexOf('(')
          d2=p.indexOf(')')
          if (d2==-1)
          {
              l30=p.length;
              p30=p.substring(0,4)
              strCloseBrace = p.substring(4,5)
              if(p.substring(4,5) == ')')
              {
                p30=p30
              }
              else
              {
                p30=p30+")"
              }
        
              p31=p.substring(4,l30);
              pp=p30+p31;
              document.getElementById(id).value="";
              document.getElementById(id).value=pp;
          }
      }
  
      if(p.length>5)
      {
          p11=p.substring(d1+1,d2);
          if(p11.length>3)
          {
            p12=p11;
            l12=p12.length;
            l15=p.length
            p13=p11.substring(0,3);
            p14=p11.substring(3,l12);
            p15=p.substring(d2+1,l15);
            document.getElementById(id).value="";
            pp="("+p13+")"+p14+p15;
            document.getElementById(id).value=pp;
          }
      
          l16=p.length;
          p16=p.substring(d2+1,l16)
          l17=p16.length;
          if(l17>3&&p16.indexOf('-')==-1)
          {
              p17=p.substring(d2+1,d2+4);
              p18=p.substring(d2+4,l16);
              p19=p.substring(0,d2+1);
              pp=p19+p17+"-"+p18;
              document.getElementById(id).value="";
              document.getElementById(id).value=pp;
          }
          else
          {
              var temp = p.substring(4,6)
              if(temp == '))')
              {
                pp=p.substring(0,d2+1) + p.substring(d2+2,p.length)
                document.getElementById(id).value="";
                document.getElementById(id).value=pp;
              }
          }
      }
    }
    
    function checkPhoneInNumberFormat(phoneNo)
    {
        var flag = false;
        var sign1 = phoneNo.substring(0,1);
        var phone1 = trimString(phoneNo.substring(1,4));
        var sign2 = trimString(phoneNo.substring(4,5));
        var phone2 = trimString(phoneNo.substring(5,8));
        var sign3 = trimString(phoneNo.substring(8,9));
        var phone3 = trimString(phoneNo.substring(9,13));

        if(phoneNo.length < 13)
            flag = true;
        if(phone1.length == 0 || phone2.length == 0 || phone3.length == 0)
          flag = true;

        if(sign1 != '(')
          flag = true;
          
        if(sign2 != ')')
          flag = true;
        
        if(sign3 != '-')
          flag = true;
        
        if(isNaN(phone1) || phone1.indexOf(".") != -1)
          flag = true;
        if(isNaN(phone2) || phone2.indexOf(".") != -1)
          flag = true;
        if(isNaN(phone3) || phone3.indexOf(".") != -1)
          flag = true;
        return flag;
	}
  
function funPrint()
{
    window.print();
}  

function getState(formName,objZipCode)
{

    var zip=trimString(objZipCode.value);
    var zipCodeErrText="";

    if(isNaN(zip))
    {
      zipCodeErrText= "Zip Code should be Numeric";
    }    
    else if(parseFloat(zip) == 0)
    {
      zipCodeErrText= "Zip Code should not be zero";
    }    
    else if(zip.length < 5 || zip.indexOf(".") != -1)
    {
      zipCodeErrText= "Zip Code should be Numeric and 5 Digits";
    }
    else
    {
      for(i=0;i<zip.length;i++)
      {
        var chr=zip.substring(i,i+1);
        if ( chr<"0" || chr>"9" )
        {
          zipCodeErrText= "Invalid Zip Code";
        }
      }
    }  
    if(zipCodeErrText.length > 0)
    {
        alert(zipCodeErrText);
        objZipCode.focus();
        return false;
    }
    else
    {
        document.getElementById(formName).submit();    
        return true;
    }
}

function getZipCodeValidation(formName,objZipCode)
{
    var zip=trimString(objZipCode.value);
    var zipCodeErrText="";

    if(isNaN(zip))
    {
      zipCodeErrText= "Zip Code should be Numeric";
    }    
    else if(parseFloat(zip) == 0)
    {
      zipCodeErrText= "Zip Code should not be zero";
    }    
    else if(zip.length < 5 || zip.indexOf(".") != -1)
    {
      zipCodeErrText= "Zip Code should be Numeric and 5 Digits";
    }
    else
    {
      for(i=0;i<zip.length;i++)
      {
        var chr=zip.substring(i,i+1);
        if ( chr<"0" || chr>"9" )
        {
          zipCodeErrText= "Invalid Zip Code";
        }
      }
    }  
    return zipCodeErrText;
}



// This function changes populates the date combo box according to the month selection
function setValues_Date(objMonth, objDay, objYear, objHiddenDay)
{
  
    var isLeapYear = false;
    var mm = objMonth.value; //document.frmQualification.effectiveMonth.value;
    var dd = objDay.value; //document.frmQualification.effectiveDay;
    var yy = parseFloat(objYear.value);//parseFloat(document.frmQualification.effectiveYear.options[document.frmQualification.effectiveYear.selectedIndex].value);

    var cnt = 0;
    var selectedDay = objHiddenDay.value; //document.getElementById('hiddenEffectiveDay').value;
                    
    
    objDay.selectedIndex= -1;
    objDay.options.length= 1;
    
    if (mm==1 || mm==3 || mm==5 || mm==7 || mm==8 || mm==10 || mm==12)
    {
        cnt=31;
    }
    
    if (mm==4 || mm==6 || mm==9 || mm==11)
    {
        cnt=30;
    }
    
    if (mm==2)
    {
      if(yy!=0 && yy % 4==0)
        cnt=29;
      else
        cnt=28;
    }
    
    var currDay = 1;
    for (var i=1; i<=cnt; i++)
    {
        objDay.options.length= objDay.options.length+1;
        objDay.options[i].text=i;
        objDay.options[i].value = i;

        if(selectedDay==0)
        {
          if(currDay==i)
          {
            objDay.options[i].selected=true;
            selectedDay=i;
          }
        }
    }
    if (selectedDay > 0 && selectedDay < objDay.options.length) 
        objDay.options[selectedDay].selected = true;      
    else
        objDay.options[objDay.options.length-1].selected = true;          

}

function dateValidationForGTCurrDate(objMonth, objDay, objYear)
{
   
    
    
    var flag = false;
    var errorText = "";
    
    // Getting all the current date values
    var curDay = document.getElementById('currDay').value;
    var curMonth = document.getElementById('currMonth').value;
    var curYear = document.getElementById('currYear').value;
    
    // Getting the selected values
    var selMon = objMonth //document.frmQualification.effectiveMonth.options[document.frmQualification.effectiveMonth.selectedIndex].value;
    var selDay = objDay //document.frmQualification.effectiveDay.options[document.frmQualification.effectiveDay.selectedIndex].value;
    var selYear = objYear //document.frmQualification.effectiveYear.options[document.frmQualification.effectiveYear.selectedIndex].value;

    if(selMon*selDay*selYear !=0)
    {
        // if year are not same then no need to check
        if (parseFloat(selYear) > parseFloat(curYear))
        {
            flag = false;
        }
        else if (parseFloat(curYear) == parseFloat(selYear))
        {
            if ((parseFloat(curMonth) == parseFloat(selMon)) && (parseFloat(curDay) > parseFloat(selDay)))
                flag = true;
    
            if (parseFloat(curMonth) > parseFloat(selMon))
                flag = true;
        }
    
        if (flag == true)
        {
            errorText = " Date cannot be less than Current Date.\n";
        }
    }
    else
    {
        errorText = "";//"Proposed Effective "; 
        flag  = true;                    
        var error = "";
        
        if (parseFloat(selMon) == 0)
        {
            error = error + " Month";
        }
    
        if (parseFloat(selDay) == 0)
        {
            if(error.length == 0)
            {
                error = error + " Day";
            }
            else
            {
                error = error + ", Day";    
            }
        }
    
        if (selYear == 0)
        {
            if(error.length == 0)
            {
                error = error + " Year";
            }
            else
            {
                error =error +  ", Year";    
            }
        }
        errorText = errorText +  error + " value should be selected.\n";
    }
    return errorText;//return flag;
}


function dateValidationForLTCurrDate(objMonth, objDay, objYear)
{
    // Date is manadatory and equal or less than current date
    
    var flag = false;
    var errorText = "";
    
    // Getting all the current date values
    var curDay = document.getElementById('currDay').value;
    var curMonth = document.getElementById('currMonth').value;
    var curYear = document.getElementById('currYear').value;
    
    // Getting the selected values
    var selMon = objMonth.value;
    var selDay = objDay.value;
    var selYear = objYear.value;

    if(selMon*selDay*selYear !=0)
    {
        // if year are not same then no need to check
        if (parseFloat(curYear) > parseFloat(selYear))
        {
            flag = false;
        }
        else if (parseFloat(selYear) == parseFloat(curYear))
        {
            if ((parseFloat(selMon) == parseFloat(curMonth)) && (parseFloat(selDay) > parseFloat(curDay)))
                flag = true;
    
            if (parseFloat(selMon) > parseFloat(curMonth))
                flag = true;
        }
    
        if (flag == true)
        {
            errorText = " Date cannot be greater than Current Date.\n";
        }
    }
    else
    {
        errorText = "";
        flag  = true;                    
        var error = "";
        
        if (parseFloat(selMon) == 0)
        {
            error = error + " Month";
        }
    
        if (parseFloat(selDay) == 0)
        {
            if(error.length == 0)
            {
                error = error + " Day";
            }
            else
            {
                error = error + ", Day";    
            }
        }
    
        if (selYear == 0)
        {
            if(error.length == 0)
            {
                error = error + " Year";
            }
            else
            {
                error =error +  ", Year";    
            }
        }
        errorText = errorText +  error + " value should be selected.\n";
    }
    return errorText;//return flag;
}

function ReferToUnderwriter(formName)
{
    document.getElementById(formName).action = "../common/ReferUnderwriter.jsp";
    document.getElementById(formName).submit();
}
function checkIntegerValue(value)
{
    var errorText = "";
    if (isNaN(value))
    {
        errorText = " should be numeric";
    }  
    else if (parseFloat(value) == 0)
    {
        errorText = " cannot be equal to 0";
    }
    else if (parseFloat(value) < 0)
    {
        errorText = " cannot be less than 0";
    }
    else if (value.indexOf(".") != -1)
    {
        errorText = " cannot be in decimal format";
    }
    return errorText;
}
  
function checkNumericValue(value)
{
    var errorText = "";
    if (isNaN(value))
    {
        errorText = " should be numeric";
    }  
    else if ((parseFloat(value) == 0))
    {
        errorText = " cannot be equal to 0";
    }
    else if (parseFloat(value) < 0)
    {
        errorText = " cannot be less than 0";
    }
    return errorText;
}



function validateInput(field)
{
    with (field)
    {
        if (value==null||trimString(value)=="")
            return false;
        else
            return true;
    }
}

function validateSelectionBox(field)
{
    with (field)
    {
        if (value==null||trimString(value)==0)
            return false;
        else
            return true;
    }
}

//for blokno other then "numbers"
function blockNonNumbers(obj, e, allowDecimal, allowNegative, allowDollar)
{
    var key;
    var isCtrl = false;
    var keychar;
    var reg;
    var allowCommas = false;
    
    if(allowDollar)
        allowCommas = true;

    if(window.event) 
    {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey
    }
    else if(e.which) 
    {
        key = e.which;
        isCtrl = e.ctrlKey;
    }
	
    if (key != '$' && key != ',' && isNaN(key))
        return true;
    
    keychar = String.fromCharCode(key);
    
    // check for backspace or delete, or if Ctrl was pressed
    //alert(key + ', isCtrl = ' + isCtrl + ',key = ' + key)
    if (key == 8 || isCtrl)
    {
        return true;
    }
    else if(key == 13)
    {
        return true;
    }
    
    reg = /\d/;
    
    var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
    var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
    var isFirst$ = allowDollar ? keychar == '$' && obj.value.indexOf('$') == -1 : false;
    var isCommas = allowCommas ? keychar == ',' : false;
    
    //var isFirst$ = allowDollar ? keychar == ',' : false;
    
    return isFirstN || isFirstD || isFirst$ || isCommas || reg.test(keychar);
}

function validateEmail(objEmail)
{
    var emailVal = trimString(objEmail.value);
    if(emailVal.length != 0)
    {
        var filter=/^[a-zA-Z]+[0-9]*[_\.]*[a-zA-Z0-9]+@[a-zA-Z]+([\-][a-zA-Z]+)*(\.[a-zA-Z][a-zA-Z]+)+$/i;
    
        if (filter.test(emailVal))
            return true;
        else
        {
            alert("Please Provide Valid Email Address.")
            objEmail.focus();
            return false;
        }
    }
}

function validatePhoneNo(objPhoneNo)
{
    var phoneVal = trimString(objPhoneNo.value);
    if(phoneVal.length != 0)
    {
        //alert(phoneVal.match('('));
    }
}

function AllowNumberOnly(EleId,Format)

{
    EleValue=document.getElementById(EleId).value
    formatInteger(EleValue, Format,EleId,"");
}
function formatInteger( integer, pattern,formField, doller ) 
{ 
	
        var key = ( window.event.keyCode || window.event.charCode || window.event.which || 0 );
        
        if(key!=9)
        {
            var result = ''; 
            integerIndex = integer.length - 1; 
            patternIndex = pattern.length - 1; 
                   
            while ( (integerIndex >= 0) && (patternIndex >= 0) ) 
            {
                    var digit = integer.charAt( integerIndex );
                    integerIndex--; 
                    // Skip non-digits from the source integer (eradicate current formatting). 
                     if ( (digit < '0') || (digit > '9') )      continue; 
                    
       
       // Got a digit from the integer, now plug it into the pattern. 
                    while ( patternIndex >= 0 ) 
                    { 
                            var patternChar = pattern.charAt( patternIndex ); 
                            patternIndex--; 
                            // Substitute digits for '#' chars, treat other chars literally. 
                            if ( patternChar == '#' ) 
                            { 
                                    result = digit + result;
                                    break; 
                            } 
                            else 
                            { 
                                    result = patternChar + result; 
                            } 
                    } 
            } 
            
            if(doller.length>0 && result.length==0)
            {
              if(formField!='')
                  document.getElementById(formField).value="";
            }
            else
            {
                 var resutnwithdoller=result; 
                 if(resutnwithdoller.length>11)
                 {
                   resutnwithdoller = resutnwithdoller.substring(0,10);
                 }
                  if(formField!='')
                        document.getElementById(formField).value = doller+resutnwithdoller;
            }
            return result; 
     }       
}


//Following function will validate all the form field having attribute 'reuired=true' 
function funValidateFormMendFields(thisForm)
{
    var formName = "";
    if(thisForm.id != null || thisForm.id != "")
    {
        formName = thisForm.id;
    }
    else
    {
        formName = thisForm.name;
    }
    
    var elements = $(formName).getElementsBySelector('[required="true"]');
    //var elements = $$('input[required="true"]'); 
    //elements = elements.concat($$('select[required="true"]')); 
    //alert(elements); 
    var errMsg = "Please Check the Following Fields.\n\n"; 
    var ctr =0; 

    //this will chk all the element with required = true value.
    for(var x =0; x < elements.length; x++) 
    { 
        //alert(elements[x].name); 
        if(!validate(elements[x])) 
            errMsg += "["+(++ctr)+"] "+elements[x].readAttribute("label")+"\n"; 
    } 
    
    // this will check the number for the given format 
    elements = $$('input[numberformat]'); 
    for(var x =0; x < elements.length; x++) 
    {
        if(elements[x].value.length != 0)
        {
            if(!chkPhoneNoFormat(elements[x])) 
                errMsg += "["+(++ctr)+"] "+elements[x].readAttribute("label")+" should be in number with format "+elements[x].readAttribute("numberformat")+".\n"; 
        }
    } 

    // this will check the email 
    elements = $$('input[emailvalidate="true"]'); 
    for(var x =0; x < elements.length; x++) 
    { 
        if(elements[x].value.length != 0) 
        { 
            if(!validateEmail(elements[x])) 
                errMsg += "["+(++ctr)+"] "+elements[x].readAttribute("label")+" is not valid.\n"; 
        } 
    } 

    if(ctr != 0) 
    { 
        alert(errMsg); 
        return false; 
    } 
    return true; 
} 

// Used to check eamil validation when page submits.
function validateEmail(objEmail) 
{ 
    if(objEmail.value.strip().length != 0) 
    { 
        var emailRegExp = /^[a-zA-Z]+[0-9]*([-\._]{0,1}[a-zA-Z0-9]+)*@[a-zA-Z0-9]+([-]{0,1}[a-zA-Z0-9]+)*([.][a-zA-Z]{2,4})+$/i; 
        return emailRegExp.test(objEmail.value.strip()); 
    } 
} 


function chkPhoneNoFormat(obj) 
{ 
    var noFormat = obj.readAttribute("numberformat").strip(); 
    var regStr = ""; 
    
    if(obj.value.length != noFormat.length) 
        return false; 
    else 
    { 
        for(var i=0; i < noFormat.length; i++) 
        { 
            if(noFormat.charAt(i) == "#") 
            { 
                regStr += "[0-9]"; 
            } 
            else 
            { 
                regStr += "["+noFormat.charAt(i)+"]"; 
            } 
        } 

        var regExp = new RegExp(regStr); 
        return regExp.test(obj.value); 
    } 
} 

function validate(fieldObj) 
{ 
    
    with(fieldObj) 
    { 
        if(type == "text" || type == "textarea") 
        { 
            if((value.strip() == null || value.strip() == "") && readAttribute("blank") == null) 
                return false; 
            else 
                return true; 
        } 
        else if(type == "radio") 
        { 
            var radioElements = document.getElementsByName(name); 
            var returnValue = false; 
    
            for (var i=0 ; i < radioElements.length; i++) 
            { 
                if(radioElements[i].checked) 
                    returnValue = true; 
            } 
            return returnValue; 
        } 
        else if(type == "checkbox") 
        { 
            return checked; 
        } 
        else 
        { 
            if (value.strip() == null|| value.strip() == "0"|| value.strip() == "") 
                return false; 
            else 
                return true; 
        } 
    } 
}