//setup form mouseovers etc

function initialize() {

if (document.getElementById('fname')){
  AttachEvent(document.getElementById('fname'),"focus",function(){fmi(1);});
  AttachEvent(document.getElementById('fname'),"blur",function(){fmo(1);check_contact(1);});
  AttachEvent(document.getElementById('fname'),"mouseover",function(){fmmo(1);});
  AttachEvent(document.getElementById('fname'),"mouseout",function(){fmmi(1);});
  AttachEvent(document.getElementById('femail'),"focus",function(){fmi(2);});
  AttachEvent(document.getElementById('femail'),"blur",function(){fmo(2);check_contact(2);});
  AttachEvent(document.getElementById('femail'),"mouseover",function(){fmmo(2);});
  AttachEvent(document.getElementById('femail'),"mouseout",function(){fmmi(2);});
  AttachEvent(document.getElementById('ftelephone'),"focus",function(){fmi(3);});
  AttachEvent(document.getElementById('ftelephone'),"blur",function(){fmo(3);check_contact(3);});
  AttachEvent(document.getElementById('ftelephone'),"mouseover",function(){fmmo(3);});
  AttachEvent(document.getElementById('ftelephone'),"mouseout",function(){fmmi(3);});
  AttachEvent(document.getElementById('fmessage'),"focus",function(){fmi(4);});
  AttachEvent(document.getElementById('fmessage'),"blur",function(){fmo(4);check_contact(4);});
  AttachEvent(document.getElementById('fmessage'),"mouseover",function(){fmmo(4);});
  AttachEvent(document.getElementById('fmessage'),"mouseout",function(){fmmi(4);});
  AttachEvent(document.getElementById('buttoncontact'),"click",function(){check_contact_all(5);});
  }
                 
                  
      if (GBrowserIsCompatible()) {
        var icon = new GIcon();
        icon.image = "i/eword.png";
        icon.iconSize = new GSize(45, 45);
        icon.shadowSize = new GSize(91, 62);
        icon.iconAnchor = new GPoint(37, 59);
        icon.infoWindowAnchor = new GPoint(31, 8);
        var map = new GMap(document.getElementById("map_canvas"));
        map.addControl(new GMapTypeControl());
        map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(53.467102, -2.295198), 15);
        map.addOverlay(new GMarker(map.getCenterLatLng(), icon));
      }	   
      
   
      }



function fmi(id){
if (id==1) {document.getElementById('fname').style.border="1px solid #8CA60F";}
if (id==2) {document.getElementById('femail').style.border="1px solid #8CA60F";}
if (id==3) {document.getElementById('ftelephone').style.border="1px solid #8CA60F";}
if (id==4) {document.getElementById('fmessage').style.border="1px solid #8CA60F";}
}

function fmmo(id){
if (id==1) {document.getElementById('fname').style.backgroundColor="#ffffff";document.getElementById('fname').style.color="#000000";}
if (id==2) {document.getElementById('femail').style.backgroundColor="#ffffff";document.getElementById('femail').style.color="#000000";}
if (id==3) {document.getElementById('ftelephone').style.backgroundColor="#ffffff";document.getElementById('ftelephone').style.color="#FFFFFF";}
if (id==4) {document.getElementById('fmessage').style.backgroundColor="#ffffff";document.getElementById('fmessage').style.color="#000000";}
}
function fmmi(id){
if (id==1) {document.getElementById('fname').style.backgroundColor="#FFFFFF";document.getElementById('fname').style.color="#000000";}
if (id==2) {document.getElementById('femail').style.backgroundColor="#FFFFFF";document.getElementById('femail').style.color="#000000";}
if (id==3) {document.getElementById('ftelephone').style.backgroundColor="#FFFFFF";document.getElementById('ftelephone').style.color="#000000";}
if (id==4) {document.getElementById('fmessage').style.backgroundColor="#FFFFFF";document.getElementById('fmessage').style.color="#000000";}
}

function fmo(id){
if (id==1) {document.getElementById('fname').style.border="2px solid #000000";}
if (id==2) {document.getElementById('femail').style.border="2px solid #000000";}
if (id==3) {document.getElementById('ftelephone').style.border="2px solid #000000";}
if (id==4) {document.getElementById('fmessage').style.border="2px solid #000000";}
}



// Validation JS
function check_contact(id){

ok=0;
errorcount=0;
var error = new Array (5);
var errormsg= new Array (5);

if (id==1) {tf=0; //contact name
    if (document.getElementById("fname").value=='') { error[errorcount]=id;tf=1;errorcount++;}
    if (tf==0) {ok=id;}
}

if (id==2) { //email address
  if (document.getElementById("femail").value!='' && checkMail(document.getElementById("femail").value)) {ok=id;} else {
  error[errorcount]=id;errormsg[errorcount]="Please enter your email address";errorcount++;}
}

if (id==3) {tf=0; //telephone
  
  if (document.getElementById("ftelephone").value=='') {tf=1;error[errorcount]=id;errorcount++;}
  if (document.getElementById("ftelephone").value.substring(0,2)!='07' && document.getElementById("ftelephone").value.substring(0,2)!='01' && document.getElementById("ftelephone").value.substring(0,2)!='02') {tf=1;error[errorcount]=id;errorcount++;}
  if (document.getElementById("ftelephone").value.length<10 && document.getElementById("ftelephone").value.length>14) {tf=1;error[errorcount]=id;errorcount++;}
  if (tf==0) {ok=3;}
}

if (id==4) {tf=0; //contact name

    if (document.getElementById("fmessage").value.length==0) { error[errorcount]=id;tf=1;errorcount++;}
    if (tf==0) {ok=id;}
}

if (ok>0) {document.getElementById("er"+ok).src="/images/tick.png"; 
  if (ok==1) {document.getElementById("fname").style.border="2px solid #8CA60F";}
  if (ok==2) {document.getElementById("femail").style.border="2px solid #8CA60F";}
  if (ok==3) {document.getElementById("ftelephone").style.border="2px solid #8CA60F";}
  if (ok==4) {document.getElementById("fmessage").style.border="2px solid #8CA60F";}

}

doneerror=0;
//now show errors on page
for (i=0;i<errorcount;i++) {
if (error[i]){

if (error[i]>0) {document.getElementById("er"+error[i]).src="/images/cross.png";
  if (error[i]==1) {document.getElementById("fname").style.border="1px solid #FF0000";}
  if (error[i]==2) {document.getElementById("femail").style.border="1px solid #FF0000";}
  if (error[i]==3) {document.getElementById("ftelephone").style.border="1px solid #FF0000";}
  if (error[i]==4) {document.getElementById("fmessage").style.border="1px solid #FF0000";}
doneerror=1;}

}

}

return doneerror;

}

function check_contact_all(limit){
  ida=0;
  fail=0;
  for (ida=0;ida<limit;ida++) {
    j=ida;
    ret=check_contact(j);
    if (ret==1) {fail=1;}
  }
  if (fail==0) { document.contactus.submit();} 
  else {alert("Please check your details");}
}

function checkMail(value)
{	var x = value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return 1;
	else return 0;}
