 $(document).ready(function(){ 
 
  //fix pngs
	$(document).pngFix(); 
   
    //Adjust height of overlay to fill screen when page loads  
    $("#order_catalogue_form").css("height", $("#fixed").height());  
	$("#overlay").css("height", $("#fixed").height());  
	
   
    //When the link that triggers the message is clicked fade in overlay/msgbox  
    $(".order_catalogue_button").click(function(){ 
		$("#overlay").show();
       $(".catalogue_form").fadeIn();  
       return false;  
    });  
	
    //When the message box is closed, fade out  
    $("#catalogue_close").click(function(){  
       $(".catalogue_form").fadeOut(); 
	   $("#overlay").hide();
       return false;  
    });  
    
    //When the message box is closed, fade out  
    $("#thank_you_close").click(function(){  
       $(".thank_you_form").fadeOut(); 
	   $("#overlay").hide();	   
       return false;  
    });  
	
  });  
   
    
//contact us form
 function validateContactForm(theForm) {
 
var reason = "";
//var name = $("#name")
// company = $("#company"),
// email = $("#email"),
// country = $("#country"),
// contact_other = $("#contact_other_input_order"),
 //phone = $("#phone")

  reason += validateEmpty(theForm.name,'Name', 'name_check');
  reason += validateEmpty(theForm.company,'Company', 'company_check');
  reason += validateEmail(theForm.email,'email_check');
  reason += validatePhone(theForm.phone,'phone_check');
  reason += validateCountry(theForm.country,'country_check');
  reason += validateHeardOrder('hear_check');
  reason += validateService('service_check');
 
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
   requestToSend=$('#contact').serialize(); 
   	makeRequest("/common/script/questions.php", '?'+requestToSend);
   $("#overlay").show();
   $(".thank_you_form").fadeIn();  
   return false;  
  }
  

}

 function validateFormOnSubmit(theForm) {
var reason = "";
			var name = $("#name"),
			question=$("#question"),
			email = $("#email"),
			telephone=$("#telephone")

reason += validateEmpty(theForm.question,'Enquiry', 'question_one_check');
  reason += validateEmpty(theForm.name,'Name', 'name_check');
  reason += validateEmail(theForm.email,'email_check');
  reason += validatePhone(theForm.telephone,'phone_check');
 
 
 if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
   $(".catalogue_form").fadeOut();
requestToSend="?name="+name.val()+"&email="+email.val()+"&question="+question.val()+"&phone="+telephone.val();
	makeRequest("/common/script/contact_us.php", requestToSend);
   
   $("#overlay").show();
   $(".thank_you_form").fadeIn(); 
   return false;  
  }
  

}


 function validateOrderFormOnSubmit(theForm) {
var reason = "";
var name = $("#name_order"),
 company = $("#company_order"),
 email = $("#email_order"),
 address1 = $("#address1_order"),
 address2 = $("#address2_order"),
 city = $("#city_order"),
 country = $("#country_order"),
 postcode = $("#postcode_order"),
 contact_other = $("#contact_other_input_order"),
 phone = $("#phone_order")

  reason += validateEmpty(theForm.name_order,'Name', 'name_check_order');
  reason += validateEmpty(theForm.company_order,'Company', 'company_check_order');
  reason += validateEmail(theForm.email_order,'email_check_order');
  reason += validateEmpty(theForm.address1_order,'Address','address_check_order');
  reason += validateEmpty(theForm.city_order,'City', 'city_check_order');
  reason += validatePhone(theForm.phone_order,'phone_check_order');  
  reason += validateCountryOrder(theForm.country_order,'country_check_order');
 reason += validateEmpty(theForm.postcode_order,'Postcode', 'postcode_check_order');  
 reason += validateHeardOrder('hear_check_order');
 
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
  else
  {
   requestToSend=$('#order').serialize();
   $(".catalogue_form").fadeOut();  
   

	makeRequest("/common/script/order.php", '?'+requestToSend);
   $(".thank_you_form").fadeIn();  
   return false;  
  }
  

}

function validateEmpty(fld,title,ncheck) {
    var error = "";
 		ncheck=document.getElementById(ncheck);

    if (fld.value.length == 0) {

        ncheck.style.color = 'Red'; 
        error = title+" has not been filled in.\n"
    } else {
        ncheck.style.color = 'Black';
    }
    return error;  
}

function validateCountry(fld,ncheck) {
    var error = "";
	var index=document.getElementById('country').selectedIndex;
				ncheck=document.getElementById(ncheck);

    if (index == ' ') {
        ncheck.style.color = 'Red'; 
        error = "Country has not been selected.\n"
    } else {
        ncheck.style.color = 'Black'; 
    }
    return error;  
}

function validateCountryOrder(fld,ncheck) {
    var error = "";
	var index=document.getElementById('country_order').selectedIndex;
				ncheck=document.getElementById(ncheck);

    if (index == ' ') {
        ncheck.style.color = 'Red'; 
        error = "Country has not been selected.\n"
    } else {
        ncheck.style.color = 'Black'; 
    }
    return error;  
}

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

function validateEmail(fld,ncheck) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   			ncheck=document.getElementById(ncheck);

    if (fld.value == "") {
        ncheck.style.color = 'Red'; 
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        ncheck.style.color = 'Red'; 
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        ncheck.style.color = 'Red'; 
        error = "The email address contains illegal characters.\n";
    } else {
        ncheck.style.color = 'Black'; 
    }
    return error;
}

function validatePhone(fld,ncheck) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    
   			ncheck=document.getElementById(ncheck);

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        ncheck.style.color = 'Red'; 
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        ncheck.style.color = 'Red'; 
    } else if (!(stripped.length > 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        ncheck.style.color = 'Red';  }
		else {
        ncheck.style.color = 'Black'; 
    }
    return error;
}

function validateServices(ncheck)
{
var error="";
var c_value = "";
   			ncheck=document.getElementById(ncheck);

for (var i=0; i < document.contact.products.length; i++)
   {
   if (document.contact.products[i].checked)
      {
      c_value = c_value + document.contact.products[i].value + "\n";
      }
   }
 for (var i=0; i < document.contact.service.length; i++)
   {
   if (document.contact.service[i].checked)
      {
      c_value = c_value + document.contact.products[i].value + "\n";
      }
   }  
   c_value=c_value.trim();

   if (c_value == "") {
           ncheck.style.color = 'Red';
           error = "You didn't select a product or service.\n";
    }
else {
        ncheck.style.color = 'Black'; 
    }	
    return error;
}

function validateHeard(ncheck)
{
var error="";
var c_value = "";
   			ncheck=document.getElementById(ncheck);

for (var i=0; i < document.contact.hear.length; i++)
   {
   if (document.contact.hear[i].checked)
      {
      c_value = c_value + document.contact.hear[i].value + "\n";
      }
   }
 
	c_value = c_value + document.contact.contact_other.value;
   c_value=c_value.trim();

     if (c_value == "") {
           ncheck.style.color = 'Red';
           error = "You didn't tell us how you heard about us.\n";
    }
else {
        ncheck.style.color = 'Black'; 
    }	
    return error;
}

function validateHeardOrder(ncheck)
{
cb=$('input:checkbox[class=contact_check]:checked').serialize();

var error="";
var c_value="";

	if (cb.length==0)
	{ 
	c_value = c_value + document.order.contact_other_input_order.value;
	c_value=c_value.trim();
	}
	else
	{
	c_value="true";
	}
	
    if (c_value == "") {
     //      ncheck.style.color = 'Red';
          error = "You didn't tell us how you heard about us.\n";
    }
else {
     //   ncheck.style.color = 'Black'; 
    }	
    return error;
}
  
     function makeRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
     // http_request.onreadystatechange = alertContents;
      http_request.open('POST', url + parameters, false);
      http_request.send(null);
	  
	  if (http_request.status == 200) {
            //alert(http_request.responseText);
            duplicate_email = http_request.responseText;
         }
   }
   
  function select_product(dropdown)
{
    var myindex  = dropdown.selectedIndex
    var SelValue = dropdown.options[myindex].value
	if (SelValue!="")
	{
    top.location.href = SelValue;
    }
    return true;
}

function validateService(ncheck)
{
cb=$('input:checkbox[class=contact_check]:checked').serialize();

var error="";
var c_value="";

	if (cb.length!=0)
	{ 
	c_value="true";
	}
	
    if (c_value == "") {
     //      ncheck.style.color = 'Red';
          error = "You didn't tell us which product or service you are interested in.\n";
    }
else {
     //   ncheck.style.color = 'Black'; 
    }	
    return error;
}
