//Insurance Companies and their discounts
    var compName=new Array("kestrel Moon","BB", "Whatever You Drive", "Discount Drivers", "Indirect Curve", "Swindel", "Captain", "Saintly Drivers", "Ipswich Union", "Young Drivers","Atlee", "Good Deal", "Nice Price", "Insure For Less", "Instant Insure")
    var price=new Array(1165, 1231, 1052, 1667, 804, 1010, 510, 890, 490, 803, 923, 1690, 775, 1435, 1223);
    var ageDiscount=new Array(475, 380, 210, 310, 599, 210, 100, 0, 0, 0, 200, 300, 0, 0, 424);
    var femaleDiscount=new Array(0,89,34,291,0,187,0,0,0,203,107,434,0,998,43);
    var mileDiscount=new Array(97,43,23,302,45,35,19,0,0,78,81,112,487,49,113);
    var extraDiscount=new Array(0,180,450,189,0,32,0,0,0,0,92,493,0,0,68);
    var noClaimsDiscount=new Array(98,85,43,93,0,90,40,155,0,82,70,47,30,59,96);
    
//variables to access DOM
var myForm=document.forms[0];
var toolTip = document.getElementById("tipDiv");

//set char input max length
myForm.userAge.maxLength=3;
myForm.userNoClaims.maxLength=1;


//set up highlight form element function on mouseover
function highlight(event){
    event.target.style.backgroundColor="RGB(164,195, 223)";
};

//set up unhighlight form element function on mouse out
function unHL(event){
    if(event.target){
        event.target.style.backgroundColor="white";
    };
    if(event.srcElement){
        event.srcElement.style.backgroundColor="white";
    };
};

//set up and format tooltip
function setUpTip(){
    
    toolTip.style.position="absolute";
    toolTip.style.backgroundColor = "RGB(88,92,109)";
    toolTip.style.color = "white";
    toolTip.style.fontFamily = "Century Gothic";
    toolTip.style.width ="200px";
    toolTip.style.visibility = "hidden";
    toolTip.style.textAlign = "center"; 
};

//show tip function
function showTip(event){
    
    //tip array set up
    var tipArray = new Array ("This field cannot contain any numbers","Your age must be a numerical value","Please enter a number of 0 or more", "You can enter a maximum of 5 years no claims discounts","Press submit to proceed to the results");

    toolTip.style.visibility = "visible";
    toolTip.style.top = event.clientY + 1 + "px";
    toolTip.style.left = event.clientX + 1 + "px";
    var indexNum;
   
   //change id values  on form elements to numbers as id names cannot be numbers in xhtml strict
    //get id of source rollover for FF
    if(event.target){
	indexNum=event.target.id;
    }else if(event.srcElement){  //get id of source rollover for FF
	indexNum=event.srcElement.id;
    };
    switch(indexNum){
	case"a":
	    indexNum=0;
	    break;
	case"b":
	    indexNum=1;
	    break;
	case "c":
	    indexNum=2
	    break;
	case"d":
	    indexNum=3;
	    break;
	case "e":
	    indexNum=4;
	    break;
    };
  
    //write to page the tip for the element id
    toolTip.innerHTML = tipArray[indexNum];
};

//hide tip function
function hideTip(event){
    toolTip.style.visibility ="hidden";
};

//reload page function
function reload(){
location.reload();
};
    

    
//random variables
var totalNoClaims="";
var cheapQuote=price[6];
var cheapCompany="";

//variables relating to xhtml form
var nameValue;
var ageValue;
var genderValue;
var mileageValue;
var noClaimsValue;


//update variables
function update ()
{
    nameValue=(myForm.userName.value);
    ageValue=(myForm.userAge.value);
    genderValue=(myForm.userGender.value);
    mileageValue=(myForm.userMilage.value);
    noClaimsValue=(myForm.userNoClaims.value);
check();
}

//validate the values entered
function check ()
{
    var isText="";
    var wrongCount = 0;
    var wrongDetails = "";
    if (nameValue=="" ){
        wrongCount++;
        wrongDetails+= wrongCount +".  Please enter your name \n"  
    };
    if(isNaN(nameValue)){
	isText=true;
    }else{
	isText=false;
    };
    if(!isText){
	wrongCount++
	wrongDetails+= wrongCount + ".  You have inputted numbers into the name field \n"
    };
    
    if(ageValue==""){
        wrongCount++;
        wrongDetails+= wrongCount + ".  Please enter your age \n"
    }
    if(isNaN(ageValue)){
        wrongCount++;
        wrongDetails+= wrongCount+ ".  You have entered text instead of a number into the age field \n"
        
    }
    if(ageValue<18 && ageValue !=""){
        wrongCount++;
        wrongDetails+= wrongCount +".  You are not old enough to drive \n"
        
    };
    if(mileageValue ==""){
        wrongCount++;
        wrongDetails+= wrongCount + ".  Please complete your mileage details \n"
    }
    if(isNaN(mileageValue)){
        wrongCount++;
        wrongDetails+=wrongCount + ".  You have entered text instead of a number for your mileage value \n"
    }
    if(mileageValue<0){
        wrongCount++;
        wrongDetails+=wrongCount +".  You cannot have a value less than 0 for your mileage \n"
    }
    if(noClaimsValue==""){
        wrongCount++;
        wrongDetails+= wrongCount+ ".  Please enter an amount for your total no claims discounts \n"
    }
    if(isNaN(noClaimsValue) && noClaimsValue != ""){
        wrongCount++;
        wrongDetails+=wrongCount + ".  You have entered text instead of a number for your total no claims discounts \n"
        
    }
    if(noClaimsValue>5){
        wrongCount++;
        wrongDetails+=wrongCount + ".  You have entered an amount greater than 5 for your no claims discounts, you can have up to a maximum of 5 years \n"
        
    }
    if(ageValue - noClaimsValue<18  && ageValue !=""  && ageValue >=17){
        wrongCount++;
        wrongDetails+= wrongCount + ".  Your data is incorrect, you cannot have " + noClaimsValue + " years no claims discount if you are " + ageValue + " years old.\n"; 
    };

    if (wrongCount==1){
        alert("There is 1 error : \n \n" + wrongDetails);
    }else if(wrongCount>1){
	alert("There are " + wrongCount + " errors: \n \n" + wrongDetails);
        
    }else{
    process()
    };
    
};
//process user data     
function process ()
{
    //set up xhtml table variable
    var table = "<br/><h4>Table of Quotes</h4><table><tr><th>Company Name</th><th>Quote</th></tr>"
    

    //calculate list of eligibility for discounts
    function output()
    {
        var eligibility= "<ul>";
        //add eligiblity statements to variable "eligibility"
        if(mileageValue<10000)
          {
            eligibility+="<li>You <strong>are</strong> entitled to a low mileage discount</li>";
          }
        if (ageValue>25)
            {
            eligibility+="<li>You <strong>are</strong> entitled to an older driver discount</li>";
            };
        if (genderValue=="female")
            {
            eligibility+="<li>You <strong>are</strong> entitled to a female driver discount</li>";
            };
        if (noClaimsValue>0)
            {
            eligibility+="<li>You <strong>are</strong> entitled to " + noClaimsValue + " years no claims discount</li>";
            };
        if (ageValue>25 && genderValue=="female" && mileageValue<10000)
            {
           eligibility+="<li>You <strong>are</strong> entitled to an extra discount</li>";
            };
        eligibility+="</ul>";
            
     //overwrite form with cheapest quote and list of eligibility       
    myForm.innerHTML="<h2>Results</h2><h3>The cheapest quote for you <strong>" + nameValue + "</strong> is \u00A3" + cheapQuote+ " from the company <strong>" + cheapCompany + "</strong></h3>" + eligibility + table + "</table><input type='button' value='Back' onclick= 'reload()'/>";
    //hide tip
    setUpTip();
    };

    //calc cheap quote
    for (var i=0;i<15;i++)
    {
        if (ageValue>25)
        {
            price[i]-=ageDiscount[i];
        };
        if (genderValue=="female")
        {
            price[i]-= femaleDiscount[i];
        };
        if(mileageValue<10000)
        {
            price[i]-=mileDiscount[i];
        };
        if(noClaimsValue>0 || noClaimsValue)
        {
            totalNoClaims=noClaimsValue*noClaimsDiscount[i];
            price[i]-=totalNoClaims;
        };
        if(ageValue>25 && genderValue=="female" && mileageValue<10000)
        {
            price[i]-=extraDiscount[i];
        };
        if(price[i]<cheapQuote)
        {
            cheapQuote=price[i]
            cheapCompany=compName[i];
        };
    
    //adding table tags and data to variable "table"
    table+= "<tr><td>" + compName[i] + "</td><td>\u00A3"+ price[i] + "</td></tr>";
        output();                                                                       
    };

};



