var xmlHttp
var pxmlHttp
var elementID=0;
var add=0;


function load(){         
    var r_cont=document.getElementById("right_cont").offsetHeight;
    var m_right=document.getElementById("menu_right").offsetHeight;  
    var div_scroll=100;
    
    div_scroll=r_cont-m_right;        
    document.getElementById("menu_scroll").style.height = div_scroll+'px';
}
 
function confirmDelleteCart(note){
    var agree=confirm(note);
    if (agree){
        delleteAllCart();
    }
    else{
        return false;
    }
}
 
function delleteAllCart(){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}

	var url="getcart.php"
	url=url+"?dellete=dellete"
	url=url+"&sid="+Math.random()	
	xmlHttp.onreadystatechange=stateCartDellete
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)			
}
function stateCartDellete()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {	 	
        document.getElementById("catrProduct").style.display = "none";
        document.getElementById("catrProductEmpty").style.display = "block";
        document.getElementById("cart").innerHTML="<br/>Your Cart is currently empty.";		
	 }
}

function changeCount(id){

	var countID="count"+id;
	var countField=document.getElementById(countID);
	var count = countField.value; 

    var chII="chII"+id;
    var chOAC="chOAC"+id;
    
    	   
	if(elementID!=id) elementID=id;

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}

	var url="getcart.php"
	url=url+"?id="+id
	url=url+"&count="+count
	url=url+"&change=change"
    if(document.getElementById(chII).checked==true) url=url+"&chII="+document.getElementById('priceII').value
    if(document.getElementById(chOAC).checked==true) url=url+"&chOAC="+document.getElementById('priceOAC').value
	url=url+"&sid="+Math.random()	
	xmlHttp.onreadystatechange=stateToCartChange
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)			

}
function stateToCartChange()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
        try {
            eval(xmlHttp.responseText);
        } catch (e) {
            alert(xmlHttp.responseText);
        }

            document.getElementById("cart").innerHTML=cartText
            document.getElementById("allPrice").innerHTML=prodd

            var countID="count"+elementID;
            var priceID="price"+elementID;
            var totalID="total"+elementID;
            
            var chII="chII"+elementID;
            var chOAC="chOAC"+elementID;            

            var count = document.getElementById(countID).value;
            var price = document.getElementById(priceID).innerHTML;

            var totalPrice=count*price;
            
            if(document.getElementById(chII).checked==true) totalPrice=totalPrice+count*document.getElementById('priceII').value
            if(document.getElementById(chOAC).checked==true) totalPrice=totalPrice+count*document.getElementById('priceOAC').value            

            document.getElementById(totalID).innerHTML=totalPrice.toFixed(2)        
            
     }
}
function get_post_code(){
    
        hideSelectBoxes();
           
        var fondiv=document.getElementById("fonfix");    
        fondiv.style.display = "block";    
        
        var bwidth = document.body.clientWidth;
        var bheight=document.body.scrollHeight;
        var getH=GetHeight();
        
        if(getH>bheight) bheight=getH;
        
        var scroll = getScrollY();    
        var topDiv=200+scroll;    
        
        fondiv.style.height = bheight+"px"
        fondiv.style.width = bwidth+"px";
        document.getElementById("postCodeDivCont").style.display = "block";    
        document.getElementById("postCodeDivCont").style.top = topDiv+"px";
            
        var postCode = document.getElementById('bookZip').value;
        xmlHttp=GetXmlHttpObject()
        if (xmlHttp==null)
        {
            alert ("Browser does not support HTTP Request")
            return
        }

        var url="getpost.php"
        url=url+"?postCode="+postCode
        url=url+"&sid="+Math.random()    
        xmlHttp.onreadystatechange=stateToPostCode
        xmlHttp.open("GET",url,true)
        xmlHttp.send(null)            
}

function stateToPostCode()
{    
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     {                              
           document.getElementById("postCodeDiv").innerHTML=xmlHttp.responseText;
           explorerTreeRefreshAll();           
     }else{
           document.getElementById("postCodeDiv").innerHTML="<div style='width: 350px;text-align: center;'><br /><br /><br /><br /><br /><img src='images/indicator.gif' alt='' /></div>";
     }
     
}

function closePostCodeDiv(){
    showSelectBoxes();
    document.getElementById("fonfix").style.display = "none";
    document.getElementById("postCodeDivCont").style.display = "none";
    document.getElementById("postCodeDiv").innerHTML="";
}


function showSelectBoxes(){
    var selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
        selects[i].style.visibility = "visible";
    }
}
function hideSelectBoxes(){
    var selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
        selects[i].style.visibility = "hidden";
    }
}
function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY ;
}
function GetHeight()
{
        var y = 0;
        if (self.innerHeight)
        {
                y = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        {
                y = document.documentElement.clientHeight;
        }
        else if (document.body)
        {
                y = document.body.clientHeight;
        }
        return y;
}
function add_to_cart(id,count){

		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}

		var url="getcart.php"
		url=url+"?id="+id
		url=url+"&count="+count
		url=url+"&add='add'"
		url=url+"&sid="+Math.random()	
		xmlHttp.onreadystatechange=stateToCart
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)			
}

function stateToCart()
{	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	 {
		document.location='cart.php?p=1';	
	 }
}
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function numbersonly(myfield, e, dec)
{
    var key;
    var keychar;
    if (window.event)
     key = window.event.keyCode;
    else if (e)
     key = e.which;
    else
     return true;
    keychar = String.fromCharCode(key);
    // control keys
    if ((key==null) || (key==0) || (key==8) || 
     (key==9) || (key==13) || (key==27) )
     return true;
    // numbers
    else if ((("0123456789").indexOf(keychar) > -1))
     return true;
    // decimal point jump
    else if (dec && (keychar == "."))
     {
     myfield.form.elements[dec].focus();
     return false;
     }
    else
     return false;
}

function doubleonly(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) ||
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789.").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

function validateBook(){

    var field=document.getElementById("security_code").value;
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null)
    {
        alert ("Browser does not support HTTP Request")
        return
    }

    var url="checkcap.php"
    url=url+"?field="+field
    url=url+"&sid="+Math.random()
    xmlHttp.onreadystatechange=stateChangedCap
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)    
    
    return false;
}

function stateChangedCap() 
{ 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
    {
        capturecode=xmlHttp.responseText             
        if(capturecode != "0"){
            var err=0;
            if(document.getElementById("bookZip").value.length<=0) err++;
            if(document.getElementById("bookType").value.length<=0) err++;
            
            
            if(document.getElementById("bookPrefix").value.length<=0) err++;
            if(document.getElementById("bookFname").value.length<=0) err++;
            if(document.getElementById("bookLname").value.length<=0) err++;
             
            if(!validateEmail(document.getElementById("bookEmail").value)) { err++; }
            if(document.getElementById("bookPhone").value.length<=0) err++;
            if(document.getElementById("bookAddress").value.length<=0) err++;
            if(document.getElementById("security_code").value.length<=0) err++;            
            
            if(err==0){
                document.book.submit();
            }else{
                alert("Please enter the all the details below noting that fields marked * are compulsory.")
            }            
        }else{
            alert("Please enter the all the details below noting that fields marked * are compulsory.")
        }
    }
}

function validateContactUs(){
    
    var err=0;
    var n=document.getElementById("contactName").value.length;
    var msg=document.getElementById("contactMsg").value.length;
    var msg=document.getElementById("security_code").value.length;
    
    if(n<=0) err++;
    if(msg<=0) err++;
    if(!validateEmail(document.getElementById("contactEmail").value)) { err++; }
      
    if(err==0){
        return true;
    }else{
        alert("Please check the following fields have been entered correctly\n\n- your name\n- phone number\n- email address\n- message\n- enter the code shown\n")
        return false;
    }
}


function trim(str)
{
  return str.replace(/^\s+|\s+$/g, '');
}

function validateEmail  (valfield){
    
    var tfld = trim(valfield);  // value of field with whitespace trimmed off
    var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/  ;
    
    if (!email.test(tfld)) {
        return false;
    }else{
        return true;   
    }  
}

function verify_newsletter(){
    
    var err=0;
    var n=document.getElementById("news_name").value.length;
    
    if(n<=0) err++;
    if(!validateEmail(document.getElementById("news_email").value)) { err++; }
        
    if(err==0){
        return true;
    }else{
        alert("The folowing fields are required:\n\n* Name\n* Email")
        return false;
    }                
}

function validate_checkout(){
    
    var err=0;
    if(document.getElementById("userName").value.length<=0) err++;
    if(document.getElementById("userPhone").value.length<=0) err++;
    if(document.getElementById("userZip").value.length<=0) err++;
    
    if(err==0){
        return true;
    }else{
        alert("The folowing fields are required:\n\n* Name\n* Phone\n* Post Code")
        return false;
    }    
}

function check_book(){
    var err=0;
    var bookZip=document.getElementById("bookZip").value;
    var bookType=document.getElementById("bookType").value;
    if(bookZip.length<=0) err++;
    if(bookType<=0) err++;
    
    if(err==0){
        pxmlHttp=GetXmlHttpObject()
        if (pxmlHttp==null)
        {
            alert ("Browser does not support HTTP Request")
            return
        }

        var url="getcharge.php"
        url=url+"?bookType="+bookType
        url=url+"&sid="+Math.random()    
        pxmlHttp.onreadystatechange=stateToCharge
        pxmlHttp.open("GET",url,true)
        pxmlHttp.send(null)        
    }
}
function stateToCharge()
{    
    if (pxmlHttp.readyState==4 || pxmlHttp.readyState=="complete")
     {                 
        try {
            eval(pxmlHttp.responseText);
        } catch (e) {
            alert(pxmlHttp.responseText);
        }
                         
        document.getElementById("charge").innerHTML="Diagnostic charge: &pound;"+diag+" and Labour Charge: &pound;"+lab;

        
        document.getElementById("diagnostic").value = diag;
        document.getElementById("labour").value = lab;
     }
}


function updateddress(num)
{
    /*
     Tidy the address to fit into 4 lines (plus town, county, postcode) & fill the fields from the top.
     Arrange the premise information according to it's content. (e.g. add numbers to the street).
    */

    var count = 0;
    var firstlinecount = 0;
    var address = ['','','','',''];
    var fullAddLines = false;
    var premIsNum = isNum(jsaddressList[num][1]);
    var splitcomplete = false;

    // determine if all the address lines contain data
    if (jsaddressList[num][1] != '' && jsaddressList[num][2] != '' && jsaddressList[num][3] != '' && jsaddressList[num][4] != '' && jsaddressList[num][5] != ''){
        fullAddLines = true;
    }

    // sometimes the premise line will contain flat or unit information seperated from the
    // premise number by a comma ( AB39 2HS ). Or it may contain two address lines
    // ( HD7 5UZ ). The premise number should appear on the next line with the street.
    // Extra address lines should appear on their own line (if one is free).

    var premiseSplit = jsaddressList[num][1].split(",");

    if (premiseSplit.length > 1){
        // more than one address element in the premise field.

        // if the second element is a number, move it to the beginning of the next occupied line.
        if (isNum(premiseSplit[1])){
            jsaddressList[num][1] = premiseSplit[0];
            for (i=2;i<6;i++){
                if (jsaddressList[num][i] != ''){
                    jsaddressList[num][i] = premiseSplit[1]+' '+jsaddressList[num][i];
                    break;
                }
            }
            splitcomplete = true;
        }
        // if all address lines arent full, move the fields down to make room
        // for the extra element found in the premise line.
        if (fullAddLines == false && splitcomplete == false){
            // find the first gap in the top 4 address lines
            for (i=2;i<6;i++){
                if (jsaddressList[num][i] == ''){
                    gap = i;
                    break;
                }
            }
            // all content above the gap must be moved down, starting from 1 above the gap.
            for (i=gap;i>2;i--)    {
                jsaddressList[num][i] = jsaddressList[num][i-1];
            }

            // let the premise line contain the first part of the split
            jsaddressList[num][1] = premiseSplit[0];

            // move the second part of the split into the new blank space.
            jsaddressList[num][2] = premiseSplit[1];
        }
    }



    // loop through the address lines, rearrange the premise and move numbers to the street field
    // fill the array 'address', with the correct data from the top down.
    for (i=1;i<6;i++){
        if (jsaddressList[num][i] != ''){
            if(count == 0){ // if on the first line of the address
                address[0] += jsaddressList[num][i];
                if (fullAddLines == true){
                    address[0] += ', ';
                }
                else{
                    address[0] += ' ';
                }
                firstlinecount++;

                if ((premIsNum == false && fullAddLines == false) || firstlinecount == 2){
                    count++; // proceed to the next address line
                }
            }
            else{
                address[count++] += jsaddressList[num][i];
            }
        }
    }

    // update the main address form with the correctly formatted, selected address.
    parent.document.contactDetails.organisation.value = jsaddressList[num][0];
    parent.document.contactDetails.address1.value = address[0];
    parent.document.contactDetails.address2.value = address[1];
    parent.document.contactDetails.address3.value = address[2];
    parent.document.contactDetails.address4.value = address[3];
    parent.document.contactDetails.town.value = jsaddressList[num][6];
    parent.document.contactDetails.county.value = jsaddressList[num][7];
    parent.document.contactDetails.postcode.value = jsaddressList[num][8];
}

function isNum(inString) {
    // returns true if inString is in the following form:  23   23A   22-23  23/24. This allows
    // a premise number such as 72A to appear on the same line as the street name.
    if (inString.length == 0)
        return false;
    for (var i=0;i < (inString.length-1);i++){
        var testchar = inString.substring(i,i+1);
        if (((testchar < '0') || (testchar > '9')) && (testchar != '-' ) && (testchar != '/' )){
            return false;
        }
    }
    return true;
}

function get_cuote(){
    
        var postCode = document.getElementById('postCode').value;
        
        var sel = document.getElementById('typeID');
        var opt = sel.options[sel.selectedIndex];
        var typeID = opt.value;
                
        var sel = document.getElementById('brandID');
        var opt = sel.options[sel.selectedIndex];
        var brandID = opt.value;
        
        if(postCode.length<=0 || typeID<=0) {
            document.getElementById("cuote_error").innerHTML="Please enter post code and type of appliance";
        }else{            
            hideSelectBoxes();
               
            var fondiv=document.getElementById("fonfix");    
            fondiv.style.display = "block";    
            
            var bwidth = document.body.clientWidth;
            var bheight=document.body.scrollHeight;
            var getH=GetHeight();
            
            if(getH>bheight) bheight=getH;
            
            var scroll = getScrollY();    
            var topDiv=200+scroll;    
            
            fondiv.style.height = bheight+"px"
            fondiv.style.width = bwidth+"px";
            document.getElementById("quickQuote").style.display = "block";    
            document.getElementById("quickQuote").style.top = topDiv+"px";        
            
        
            xmlHttp=GetXmlHttpObject()
            if (xmlHttp==null)
            {
                alert ("Browser does not support HTTP Request")
                return
            }

            var url="getcuote.php"
            url=url+"?postCode="+postCode
            url=url+"&typeID="+typeID    
            url=url+"&brandID="+brandID    
            url=url+"&sid="+Math.random()    
            xmlHttp.onreadystatechange=stateQuickQuote
            xmlHttp.open("GET",url,true)
            xmlHttp.send(null)            
        }
}

function stateQuickQuote()
{    
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
     {                              
           document.getElementById("quickQuoteDiv").innerHTML=xmlHttp.responseText;
           explorerTreeRefreshAll();           
     }else{
           document.getElementById("quickQuoteDiv").innerHTML="<div style='width: 350px;text-align: center;'><br /><br /><br /><br /><br /><img src='images/indicator.gif' alt='' /></div>";
     }
     
}
function closeQuickQuote(){
    showSelectBoxes();
    document.getElementById("fonfix").style.display = "none";
    document.getElementById("quickQuote").style.display = "none";
    document.getElementById("quickQuoteDiv").innerHTML="";
}
