// JavaScript Document
function popUpWindow(URLStr, left, top, width, height)
{
	if(popUpWin)
	{
		if(!popUpWin.closed) popUpWin.close();
	}
	popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

// Validate a new basic candidate registration
function validate_enquiry(frm)
{
	var oform = frm
	if (trimAll(oform.Name.value) == "")
	{
		alert("Please enter your Name")
		oform.Name.focus()
		return false
	}
	if (trimAll(oform.Email.value) == "")
	{
		alert("Please enter your Email Address")
		oform.Email.focus()
		return false
	}
	// check for a valid email address entered
	if (!emailCheck(trimAll(oform.Email.value))) 
	{
		oform.Email.focus()
		return false
	} 
	if (trimAll(oform.Company.value) == "")
	{
		alert("Please enter your Company name")
		oform.Company.focus()
		return false
	}
	if (trimAll(oform.Address1.value) == "")
	{
		alert("Please enter your first address line")
		oform.Address1.focus()
		return false
	}
	if (trimAll(oform.Town.value) == "")
	{
		alert("Please enter your Town/City Name")
		oform.Town.focus()
		return false
	}
	if (trimAll(oform.PostCode.value) == "")
	{
		alert("Please enter your Post Code")
		oform.PostCode.focus()
		return false
	}
	if (trimAll(oform.Country.value) == "")
	{
		alert("Please enter your Country")
		oform.Country.focus()
		return false
	}
	// convert the field contents
	oform.Name.value = trimAll(oform.Name.value)
	oform.Email.value = MakeLowerCase(oform.Email.value)
	oform.Company.value = trimAll(oform.Company.value)
	oform.Address1.value = trimAll(oform.Address1.value)
	oform.Town.value = trimAll(oform.Town.value)
	oform.PostCode.value = trimAll(oform.PostCode.value)
	oform.PostCode.value = MakeUpperCase(oform.PostCode.value)
	oform.Country.value = trimAll(oform.Country.value)
	oform.submit()
}

// trims off leading and trailing spaces
function trimAll(strValue) 
{
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

// convert to field to uppercase 
function MakeUpperCase(strValue)
{
	var re = / /g;
	strValue = strValue.replace(re, "");
	var re = /'/g;
	strValue = strValue.replace(re, "`");
	strValue = strValue.toUpperCase();
  return strValue;
}

function MakeLowerCase(strValue)
{
	var re = / /g;
	strValue = strValue.replace(re, "");
	var re = /'/g;
	strValue = strValue.replace(re, "`");
	strValue = strValue.toLowerCase();
  return strValue;
}

// check and validate an email address
function emailCheck (emailStr) 
{
	//var emailStr=ofield.value
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	// Begin with the coarse pattern to simply break up user@domain into different pieces that are easy to analyze.
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	  // Too many/few @'s or something; basically, this address doesn't even fit the general mould of a valid e-mail address.
		alert("The Email address seems incorrect (check for @ and .'s problems)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		alert("The Email username doesn't seem to be valid.")
		return false
	}
	// if the e-mail address is at an IP address (as opposed to a symbolic host name) make sure the IP address is valid. 
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("The Email Destination IP address is invalid!")
			return false
			}
		}
		return true
	}
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("The Email domain name doesn't seem to be valid.")
		return false
	}
	/* Now we need to break up the domain to get a count of how many atoms it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>4) {
	   // the address must end in a two letter or three letter word.
	   alert("The email address must end in a three-letter domain, or two letter country.")
	   return false
	}
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="The Email address is missing a hostname!"
	   alert(errStr)
	   return false
	}
	return true;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// Menu functions
function menuover(oitem)
{
	oitem.style.bgcolor="#0000FF";
}
function menuout(oitem)
{
	oitem.style.backcolor="#CCCCCC";
}

//resizing iframe
function iFrameHeight(name) 
{
	if(document.getElementById && !(document.all)) 
	{
		h = document.getElementById(name).contentDocument.body.scrollHeight;
		document.getElementById(name).style.height = h;
	}
	else if(document.all) 
	{
		h = document.frames(name).document.body.scrollHeight;
		document.all.name.style.height = h;
	}
}

// the following two functions work together and support IE and Firefox
function getDocHeight(doc) 
{ 
	var docHt = 0, sh, oh; 
	if (doc.height) 
	{ 
		docHt = doc.height; 
	} 
	else if (doc.body) 
	{ 
		if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight; 
		if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight; 
		if (sh && oh) docHt = Math.max(sh, oh); 
	} 
	return docHt; 
} 

function fnResizeIframe(sIFrameName) 
{ 
	var iframeWin = window.frames[sIFrameName]; 
	var iframeEl = window.document.getElementById? window.document.getElementById(sIFrameName): document.all? document.all[sIFrameName]: null; 
	if ( iframeEl && iframeWin ) 
	{ 
		iframeEl.style.height = "auto"; 
		var docHt = getDocHeight(iframeWin.document); 
		if (docHt) iframeEl.style.height = docHt + "px"; 
	} 
	else 
	{ //firefox 
		var docHt = window.document.getElementById(iframeName).contentDocument.height; 
		window.document.getElementById(iframeName).style.height = docHt + "px"; 
	} 
} 

// my iFrame source loader routine
function iframe_loader(sIFrameName,page_name)
{
	window.document.getElementById(sIFrameName).src=page_name;
}

/***********************************************
* IFrame SSI script II- © Dynamic Drive DHTML code library (http://www.dynamicdrive.com)
* Visit DynamicDrive.com for hundreds of original DHTML scripts
* This notice must stay intact for legal use
***********************************************/

function resizeCaller() 
{
	var dyniframe=new Array()
	for (i=0; i<iframeids.length; i++)
	{
		if (document.getElementById)
		resizeIframe(iframeids[i])
		//reveal iframe for lower end browsers? (see var above):
		if ((document.all || document.getElementById) && iframehide=="no")
		{
			var tempobj=document.all? document.all[iframeids[i]] : document.getElementById(iframeids[i])
			tempobj.style.display="block"
		}
	}
}

function resizeIframe(frameid)
{
	var currentfr=document.getElementById(frameid)
	if (currentfr && !window.opera)
	{
		currentfr.style.display="block"
		if (currentfr.contentDocument && currentfr.contentDocument.body.offsetHeight) //ns6 syntax
			currentfr.height = currentfr.contentDocument.body.offsetHeight+FFextraHeight; 
		else if (currentfr.Document && currentfr.Document.body.scrollHeight) //ie5+ syntax
			currentfr.height = currentfr.Document.body.scrollHeight;
		if (currentfr.addEventListener)
			currentfr.addEventListener("load", readjustIframe, false)
		else if (currentfr.attachEvent)
		{
			currentfr.detachEvent("onload", readjustIframe) // Bug fix line
			currentfr.attachEvent("onload", readjustIframe)
		}
	}
}

function readjustIframe(loadevt) 
{
	var crossevt=(window.event)? event : loadevt
	var iframeroot=(crossevt.currentTarget)? crossevt.currentTarget : crossevt.srcElement
	if (iframeroot)
		resizeIframe(iframeroot.id);
}

function loadintoIframe(iframeid, url)
{
	if (document.getElementById)
		document.getElementById(iframeid).src=url
}

