function checkPostCode (toCheck) 
{
  // This array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (/^([a-z]{1,2}[0-9]{1,2})(\s*)([0-9]{1}[abdefghijlnpqrstuwxyx][a-z])$/i);
  // Expression for postcodes: ANA NAA, and AANA  NAA
  pcexp.push (/^([a-z]{1,2}[0-9]{1}[a-z]{1})(\s*)([0-9]{1}[abdefghijlnpqrstuwxyx][a-z])$/i);
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against both post codes
  for (i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop

      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}

function test_code () 
{
	var frm = document.frm_pc;

	if (checkPostCode (frm.postcode.value)) 
	{
		frm.postcode.value=checkPostCode (frm.postcode.value)
		frm.action = 'search_technician.php';
		frm.submit();
	} 
	else 
	{
		document.getElementById('wrongcode').style.visibility='visible';
	}
}

function test_callback()
{
	var frm = document.frm_pc;
	if(frm.client_pc.value == "" || frm.repair_needed.options[frm.repair_needed.selectedIndex].value == "null" || frm.client_name.value == "" || frm.client_num.value == "")
	{
		//all fields not filled in so display warning div
		document.getElementById('wrongdetails').style.visibility='visible';
	}
	else
	{
		//set the action of the form and submit
		frm.action = "../scripts/contact.hub.php?type=1";
		frm.submit();
	}
}

function check_contact(sectionid)
{
	var frm = document.frm_pc;
	if(frm.forename.value == "" || frm.surname.value == "" || frm.email.value == "" || frm.enquiry.value == "")
	{
		//all fields not filled in so display warning div
		document.getElementById('contactdetails').style.visibility='visible';
	}
	else
	{
		//set the action of the form and submit
		frm.action = "../scripts/contact.hub.php?type=2&sectionid="+sectionid;
		frm.submit();
	}
}

function check_recruit_contact()
{
	var frm = document.frm_contact;
	if(frm.name.value == "" || frm.tel.value == "" || frm.preftime.value == "" || frm.enquiry.value == "")
	{
		//all fields not filled in so display warning div
		document.getElementById('recruitcontactdetails').style.visibility='visible';
	}
	else
	{
		//set the action of the form and submit
		frm.action = "../scripts/contact.hub.php?type=3";
		frm.submit();
	}

}

function check_email()
{
	var frm = document.frm_pc;
	if(frm.email.value == "")
	{
		//all fields not filled in so display warning div
		document.getElementById('email').style.visibility='visible';
	}
	else
	{
		//set the action of the form and submit
		frm.action = "home.php";
		frm.submit();
	}
}

function check_callback()
{
	var frm = document.frm_pc;
	if(frm.forename.value == "" || frm.surname.value == "" || frm.email.value == "" || frm.postcode.value == "")
	{
		//all fields not filled in so display warning div
		document.getElementById('callback').style.visibility='visible';
	}
	else
	{
		//set the action of the form and submit
		frm.action = "scripts/contact.hub.php?type=3";
		frm.submit();
	}
}

function hide_warning(whichone)
{
	switch(whichone)
	{
		case 1: //postcode validation error
			document.getElementById('wrongcode').style.visibility='hidden';
			document.frm_pc.postcode.focus();
			break;
		case 2: //callback form error
			document.getElementById('wrongdetails').style.visibility='hidden';
			break;
		case 3: //no username and password entered into login box
			document.getElementById('wronguserdetails').style.visibility='hidden';
			break;
		case 4: //no database match for username and password
			document.getElementById('nouser').style.visibility='hidden';
			break;
		case 5: //contact form not correctly filled in
			document.getElementById('contactdetails').style.visibility='hidden';
			break;
		case 6: //callback request form not correctly filled in
			document.getElementById('callback').style.visibility='hidden';
			break;
		case 7: //email field not correctly filled in
			document.getElementById('email').style.visibility='hidden';
			break;
		case 8: //RECRUITMENT email field not correctly filled in
			document.getElementById('recruitcontactdetails').style.visibility='hidden';
			break;
	}
}

function change_service(varid)
{
	document.getElementById("servicestxt").src = "../includes/services.inc.php?sid="+varid;
}

function change_casestudy(varid)
{
	document.getElementById("studytxt").src = "../includes/casestudies.inc.php?sid="+varid;
}

function change_franchise(varid)
{
	if(varid == 0)
	{
		strurl = "../includes/franchise.inc.php?sid=";
	}
	else
	{
		strurl = "../includes/franchise.inc.php?sid="+varid;
	}
		document.getElementById("franchisetxt").src = strurl;
}

function clear_field(fieldid, clearon)
{
	var field = document.getElementById(fieldid);
	
	if(field.value == clearon)
	{
		field.value = '';
	}
}

function check_field(fieldid,origval)
{
	var field = document.getElementById(fieldid);
	
	if(field.value == '')
	{
		field.value = origval;
	}
}

function check_login()
{
	var frm = document.frm_login;
	if(frm.username.value == "" || frm.password.value == "" || frm.username.value == 'Username' || frm.password.value == 'Password')
	{
		//all fields not completed - notify user
		parent.frames['content'].document.getElementById('wronguserdetails').style.visibility='visible';
	}
	else
	{
		frm.action = './useraccess.hub.php?type=1';
		frm.submit();
	}
}

function logout_user()
{
	parent.frames['content'].location='../useraccess.hub.php?type=2';
}

function pop_window(int_width,int_height,str_url,str_scrollbars)
{
	window.open(str_url,'popup','width='+int_width+',height='+int_height+',scrollbars='+str_scrollbars);
}

function center_window(int_width,int_height,int_focus,int_resize)
{
	xPos = (screen.availWidth - int_width) / 2;
	yPos = (screen.availHeight - int_height) / 2;
	window.moveTo(xPos,yPos);
	if(int_resize == 1)
	{
		window.resizeTo(int_width,int_height);
	}
	if(int_focus == 1)
	{
		window.focus();
	}
}

var nav = window.Event ? true : false;
if (nav) {
   window.captureEvents(Event.KEYDOWN);
   window.onkeydown = NetscapeEventHandler_KeyDown;
} else {
   document.onkeydown = MicrosoftEventHandler_KeyDown;
}

function NetscapeEventHandler_KeyDown(e) 
{
	if (e.which == 13 && e.target.type == 'text' && e.target.name != 'username' && e.target.name != 'password')
	{
		test_code();
		return false;
	}
	return true;
}

function MicrosoftEventHandler_KeyDown() 
{
	if (event.keyCode == 13 && event.srcElement.type == 'text' && event.srcElement.name != 'username' && event.srcElement.name != 'password')
	{
		test_code();
		return false;
	}
	return true;
}

function pop_tandc()
{
	window.open('../tandc.htm','revive','width=606,height=293,scrollbars=no');
}
function pop_privacy()
{
	window.open('../privacy.htm','revive','width=606,height=293,scrollbars=no');
}
