/* - - - - - - - - - - - - - - - - - - - - - - -
 JavaScript
 21 March 2005 15:37:56
 - - - - - - - - - - - - - - - - - - - - - - - */


     function IsNumeric(sText)
     {
         var ValidChars = "0123456789";
         var IsNumber=true;
         var Char;

         for (i = 0; i < sText.length && IsNumber == true; i++)
         {
             Char = sText.charAt(i);
             if (ValidChars.indexOf(Char) == -1)
             {
                 IsNumber = false;
             }
         }
         return IsNumber;
     }

     /* --------------------- FUNCTION FOR PERIOD CALCULATION ----------------------------- */
    
    function compute() 
    { 
        var str1 = new String();
        var str2 = new String();  
        var arDate1 = new Array();
        var arDate2 = new Array(); 
        var d = new Date(); 
        var date1 = new Date();
        var date2 = new Date();   
  
        s1 = document.frmSingle.optDay1.options[document.frmSingle.optDay1.selectedIndex].value;            
        s2 = document.frmSingle.optMon1.options[document.frmSingle.optMon1.selectedIndex].value;
        str1 = s1 + "/" + s2 ; 
        
        a1 = document.frmSingle.optDay2.options[document.frmSingle.optDay2.selectedIndex].value;
        a2 = document.frmSingle.optMon2.options[document.frmSingle.optMon2.selectedIndex].value;
        
        str2 = a1 + "/" + a2 ;    
            
        arDate1 = str1.split("/"); 
        arDate2 = str2.split("/"); 
    
        date1.setYear  (arDate1[2]);
        date1.setMonth (arDate1[1]);
        date1.setDate  (arDate1[0]);
    
        date2.setYear  (arDate2[2]);
        date2.setMonth (arDate2[1]);
        date2.setDate  (arDate2[0]);

        var delta = date1.getTime() - date2.getTime();
        
        delta = delta / ( 1000 * 60 * 60 * 24 );  // units are now days.
        if (delta <    0) delta = -delta;
        delta = Math.round(delta);
        
        document.frmSingle.txtPeriod.value = delta + 1
    
    }
    
    /* ---------------------- PERIOD/RETURN DATE FUNCTION --------------------------------- */
    
    function autodate()
    {
        if ( document.frmSingle.chkDate[0].checked )
        {
            document.frmSingle.txtPeriod.disabled = true;
            document.frmSingle.txtPeriod.style.backgroundColor = "#CCCCCC" ;
            document.frmSingle.optDay2.disabled = false;
            document.frmSingle.optMon2.disabled = false;
        }
        if ( document.frmSingle.chkDate[1].checked )
        {
            document.frmSingle.txtPeriod.disabled = false;
            document.frmSingle.txtPeriod.style.backgroundColor = "#FFFFFF" ;
            document.frmSingle.optDay2.disabled = true;
            document.frmSingle.optMon2.disabled = true;
        }
    } 
    
    /* --------------------- FUNCTION FOR AUTO DATESELECTION -----------------------------    */    
    
    function autofillDay()
    {        
        document.frmSingle.optDay2.selectedIndex = document.frmSingle.optDay1.selectedIndex    ;
    }
    
    /* -------------- FUNCTION FOR AUTO MONTH / YEAR SELECTION ----------------------------- */    
    
    function autofillMon()
    {
        document.frmSingle.optMon2.selectedIndex = document.frmSingle.optMon1.selectedIndex;
    }

    /* -------------- FUNCTION FOR FAMILY SELECTION ( on ) ----------------------------- */    
    
    function CoverSelection()
    {
        if ( document.frmSingle.chkCover.options[document.frmSingle.chkCover.selectedIndex].value == "Family2" )
        {
            document.frmSingle.txtRate2.selectedIndex = 5;
            document.frmSingle.txtRate3.selectedIndex = 2;
            document.frmSingle.txtRateALT3.value = "2";
            document.frmSingle.txtRate1.disabled = false;
            document.frmSingle.txtRate2.disabled = false;
            document.frmSingle.txtRate3.disabled = true;
            if ( document.frmSingle.txtRate4 != null )
            {
                 document.frmSingle.txtRate4.disabled = true ;
                 document.frmSingle.txtRateALT4.value = "0";
            }
            if ( document.frmSingle.txtRate5 != null )
            {
                document.frmSingle.txtRate5.disabled = true ;
                document.frmSingle.txtRateALT5.value = "0";
            }
            if ( document.frmSingle.txtRate6 != null )
            {
               document.frmSingle.txtRate6.disabled = true;
               document.frmSingle.txtRateALT6.value = "0";
            }
        }
        else if ( document.frmSingle.chkCover.options[document.frmSingle.chkCover.selectedIndex].value == "Family1" )
        {
            document.frmSingle.txtRate2.selectedIndex = 5;
            document.frmSingle.txtRate3.selectedIndex = 1;
            document.frmSingle.txtRateALT3.value = "1";
            document.frmSingle.txtRate1.disabled = false;
            document.frmSingle.txtRate2.disabled = false;
            document.frmSingle.txtRate3.disabled = true;
            if ( document.frmSingle.txtRate4 != null )
            {
                document.frmSingle.txtRate4.disabled = true ;
                document.frmSingle.txtRateALT4.value = "0";
            }
            if ( document.frmSingle.txtRate5 != null )
            {
                document.frmSingle.txtRate5.disabled = true ;
                document.frmSingle.txtRateALT5.value = "0";
            }
            if ( document.frmSingle.txtRate6 != null )
            {
                 document.frmSingle.txtRate6.disabled = true;
                 document.frmSingle.txtRateALT6.value = "0";
            }
        }
        else if ( document.frmSingle.chkCover.options[document.frmSingle.chkCover.selectedIndex].value == "Couple" )
        {
            document.frmSingle.txtRate2.selectedIndex = 0;
            document.frmSingle.txtRate3.selectedIndex = 2;
            document.frmSingle.txtRateALT3.value = "2";
            if ( document.frmSingle.txtRate1 != null )
            {
                document.frmSingle.txtRate1.disabled = true ;
                document.frmSingle.txtRateALT1.value = "0";
            }
            if ( document.frmSingle.txtRate2 != null )
            {
                document.frmSingle.txtRate2.disabled = true ;
                document.frmSingle.txtRateALT2.value = "0";
            }
            if ( document.frmSingle.txtRate3 != null )
            {
                document.frmSingle.txtRate3.disabled = true ;
                document.frmSingle.txtRateALT3.value = "2";
            }
            
            
            if ( document.frmSingle.txtRate4 != null )
            {
                document.frmSingle.txtRate4.disabled = true ;
                document.frmSingle.txtRateALT4.value = "0";
            }
            if ( document.frmSingle.txtRate5 != null )
            {
                document.frmSingle.txtRate5.disabled = true ;
                document.frmSingle.txtRateALT5.value = "0";
            }
            if ( document.frmSingle.txtRate6 != null )
            {
               document.frmSingle.txtRate6.disabled = true;
               document.frmSingle.txtRateALT6.value = "0";
            }
        }
        else
        {
            document.frmSingle.txtRate1.disabled = false;
            document.frmSingle.txtRate2.disabled = false;
            document.frmSingle.txtRate3.disabled = false;
            document.frmSingle.txtRate2.selectedIndex = 0;
            document.frmSingle.txtRate3.selectedIndex = 0;
            if ( document.frmSingle.txtRate4 != null )
            {
                document.frmSingle.txtRate4.disabled = false ;
            }    
            if ( document.frmSingle.txtRate5 != null )
            {
                document.frmSingle.txtRate5.disabled = false ;
            }
            if ( document.frmSingle.txtRate6 != null )
            {
               document.frmSingle.txtRate6.disabled = false;
            }
        }
    }    
    /* ----------- FUNCTION FOR WINTER SPORTS SELECTION ( on ) ----------------------------- */
    
    function disableSeniors()
    {
        if ( document.frmSingle.txtRate4 != null )
        {
            document.frmSingle.txtRate4.selectedIndex = 0;
            document.frmSingle.txtRate4.disabled = true ;

        }

        if ( document.frmSingle.txtRate5 != null )
        {
            document.frmSingle.txtRate5.selectedIndex = 0;
            document.frmSingle.txtRate5.disabled = true ;
        }

        if ( document.frmSingle.txtRate6 != null )
        {
           document.frmSingle.txtRate6.selectedIndex = 0;
           document.frmSingle.txtRate6.disabled = true;
        }
    }
    
    /* ----------- FUNCTION FOR WINTER SPORTS SELECTION ( off ) ----------------------------- */    
    
    function enableSeniors()
    {
        if ( document.frmSingle.txtRate4 != null )
        {
            document.frmSingle.txtRate4.disabled = false ;
        }

        if ( document.frmSingle.txtRate5 != null )
        {
            document.frmSingle.txtRate5.disabled = false ;
        }

        if ( document.frmSingle.txtRate6 != null )
        {
           document.frmSingle.txtRate6.disabled = false;
        }
    }
    
    /* ---------------------- FORM VALIDATION FUNCTION ------------------------------------    */
    
    function CheckForm()
    {    

        //return compute(); // PERIOD CALCULATION FUNCTION CALLED 

        if ( document.frmSingle.chkDate[1].checked )
        {
            if ( document.frmSingle.txtPeriod.value == "" )
            {
                alert ("Please enter the holiday duration");
                document.frmSingle.txtPeriod.focus ();
                return false;
            }
            //Check that only numbers have been entered
            if (!IsNumeric(document.frmSingle.txtPeriod.value))
            {
                alert("Please only enter numbers in the holiday duration");
                document.frmSingle.txtPeriod.focus();
                return false;
            }
        }
        
        if ( document.frmSingle.optArea.value == "" )
        {			
            alert ("Please select the Area");           
            document.frmSingle.optArea.focus ();
            return false;
        }
        
        if (document.frmSingle.chkCover.selectedIndex == 0 )
        {
            alert ("Please select the Cover");
            document.frmSingle.chkCover.focus ();
            return false;
        }
        
       
        if ( document.frmSingle.txtRate6 != null)
        {	
			
			
			var BandExclusionList = "1292:1293:1294:1271:1272:1273:1298:";
			BandExclusionList = BandExclusionList+"1232:1233:1234:1238:1239:1240:1241:1242:1243:1244:1245:1246:1247:1248:1249:1250:1251:1252:1253:1254:";
			BandExclusionList = BandExclusionList+"1255:1256:1257:1258:1259:1260:1261:1262:1263:1264:1265:1266:1267:1268:1269:1270:1271:1272:1273:1274:";
			BandExclusionList = BandExclusionList+"1275:1276:1277:1278:1279:1280:1281:1282:1283:1284:1285:1286:1287:1288:1289:1290:1291:1292:1293:1294:";
			BandExclusionList = BandExclusionList+"1371:1372:1373:1374:1375:1376:1631:1632:1633";
			
           if (document.frmSingle.txtRate6.selectedIndex > 0 && document.frmSingle.optArea.value != "Area2" && document.frmSingle.optArea.value != "Area1" && (BandExclusionList.indexOf(document.frmSingle.Band_ID.value) < 0))
           	{
				alert ("Seniors can only covered within Europe.  Please reselect your Area.");              
				document.frmSingle.optArea.focus();
				return false;
			}           
        }         

        /* if (document.frmSingle.agency_airline != null)
        {
           if (document.frmSingle.agency_airline.value == "")
           {
              alert("Please enter the agency / airline that you booked your holiday / flight with");
              document.frmSingle.agency_airline.focus();
              return false;
           }
        }  */
       
        
        if ( document.frmSingle.chkCover.options[document.frmSingle.chkCover.selectedIndex].value == "Family2" ) //    family selected    
        {
            if ( document.frmSingle.txtRate4 != null )
            {
                if ( document.frmSingle.txtRate4.value > 0 )
                {
                    alert ( "You can not select Seniors" );
                    document.frmSingle.txtRate4.disabled = true ;
                    return false;
                }
            }
            if (document.frmSingle.txtRate5 != null)
            {
                if ( document.frmSingle.txtRate5.value > 0 )
                {
                    alert ( "You can not select Seniors" );
                    document.frmSingle.txtRate5.disabled = true;
                    return false;
                }
            }

            if ( document.frmSingle.txtRate6 != null )
            {
               if ( document.frmSingle.txtRate6.value > 0 )
               {
                  alert("You can not select Seniors");
                  document.frmSingle.txtRate6.disabled = true;
                  return false;
               }
            }
            
            
            if ( document.frmSingle.txtRate3.value > 2 )
            {
                alert ( "You can not select more than 2 Adults"    );
                document.frmSingle.txtRate2.selectedIndex = 5;
                document.frmSingle.txtRate3.selectedIndex = 2;
                document.frmSingle.txtRate3.focus();
                return false;
            }            
        }
    
        if (  document.frmSingle.chkCover.options[document.frmSingle.chkCover.selectedIndex].value == "Other"  ) //    family not selected
        {
            if (( document.frmSingle.txtRate4 != null) && (document.frmSingle.txtRate5 != null) && (document.frmSingle.txtRate6 != null))
            {
               if (document.frmSingle.txtRate1.value == 0 && document.frmSingle.txtRate2.value == 0 && document.frmSingle.txtRate3.value == 0 && document.frmSingle.txtRate4.value == 0 && document.frmSingle.txtRate5.value == 0 && document.frmSingle.txtRate6.value == 0 )
               {
                  alert ( "Please Enter at least 1 Insured Person");
                  document.frmSingle.txtRate3.focus();
                  return false;
               }
            }
            else
            {
                if (( document.frmSingle.txtRate4 != null )  && (document.frmSingle.txtRate5 != null))
                {
                   if ( document.frmSingle.txtRate1.value == 0 && document.frmSingle.txtRate2.value == 0 && document.frmSingle.txtRate3.value == 0 && document.frmSingle.txtRate4.value == 0 && document.frmSingle.txtRate5.value == 0 )
                   {    
                        alert ( "Please Enter at least 1 Insured Person");
                        document.frmSingle.txtRate3.focus();
                        return false;
                   }
                }
                else
                {
                    if (document.frmSingle.txtRate4 != null)
                    {
                       if ( document.frmSingle.txtRate1.value == 0 && document.frmSingle.txtRate2.value == 0 && document.frmSingle.txtRate3.value == 0 && document.frmSingle.txtRate4.value == 0 )
                       {    
                            alert ( "Please Enter at least 1 Insured Person");
                            document.frmSingle.txtRate3.focus();
                            return false;
                       }
                    }
                    else
                    {
                        if ( document.frmSingle.txtRate1.value == 0 && document.frmSingle.txtRate2.value == 0 && document.frmSingle.txtRate3.value == 0 )
                        {
                           alert ( "Please Enter at least 1 Insured Person");
                           document.frmSingle.txtRate3.focus();
                           return false;
                        }
                    }
                }
            }
        }
        
        if (document.frmSingle.Underwriter.value == "Whitehorse")
        {
            if (( document.frmSingle.chkDisclaim1[0].checked == false) && (document.frmSingle.chkDisclaim1[1].checked == false))
            {
                alert (    "Please read and complete the Health Warranty before continuing");
                return false;
            }
            if (( document.frmSingle.chkDisclaim2[0].checked == false) && (document.frmSingle.chkDisclaim2[1].checked == false))
            {
                alert (    "Please read and complete the Health Warranty before continuing");
                return false;
            }
            if (( document.frmSingle.chkDisclaim3[0].checked == false) && (document.frmSingle.chkDisclaim3[1].checked == false))
            {
                alert (    "Please read and complete the Health Warranty before continuing");
                return false;
            }
            if (( document.frmSingle.chkDisclaim4[0].checked == false) && (document.frmSingle.chkDisclaim4[1].checked == false))
            {
                alert (    "Please read and complete the Health Warranty before continuing");
                return false;
            }
            if (document.frmSingle.chkDisclaim4[0].checked == true)
            {
                alert("You must be resident for at least 6 months to purchase insurance.");
                return false;
            }
        }
        else
        {
             if (( document.frmSingle.chkDisclaim1[0].checked == false) && (document.frmSingle.chkDisclaim1[1].checked == false))
            {
                alert (    "Please read and complete the Health Warranty before continuing");
                return false;
            }
            if (( document.frmSingle.chkDisclaim2[0].checked == false) && (document.frmSingle.chkDisclaim2[1].checked == false))
            {
                alert (    "Please read and complete the Health Warranty before continuing");
                return false;
            }
            if (( document.frmSingle.chkDisclaim3[0].checked == false) && (document.frmSingle.chkDisclaim3[1].checked == false))
            {
                alert (    "Please read and complete the Health Warranty before continuing");
                return false;
            }
            if (document.frmSingle.chkDisclaim3[0].checked == true)
            {
                alert("You must be resident for at least 6 months to purchase insurance.");
                return false;
            }
        }
        
        if (!document.frmSingle.chkCoverAbroad.checked)
        {
			alert("Please tick to confirm that you have read and understand the policy restriction notice.");
			document.frmSingle.chkCoverAbroad.focus();
			return false;
		}

        return true;    
    }


