//Top Flight Javascript Functions
function Obscure( sStr )
{
iLen = sStr.length;
aRet = new Array();

for ( i=0; i < iLen; i++ )
	aRet[i] = sStr.charAt(i);


for ( i=0; i < ( iLen/ 2 ); i += 2 )
	{
	
	sTemp = aRet[i];
	aRet[i] = aRet[iLen-i -1];
	aRet[iLen-i-1]=sTemp;
		}

return aRet.join('');
}

function Unhide (sAddress)
{
sStr = '<a href="mailto:';
sStr += Obscure(sAddress);
sStr += '">';
sStr += Obscure(sAddress);
sStr += "</a>";
return sStr;
}


function loadMap() 
	 {
      if (GBrowserIsCompatible()) {
      var map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(51.771300, 0.094765 ), 15); //cm20 1pd : Top-Flight
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.addOverlay(new GMarker(map.getCenter()));

      }
    }

function sValidDate( sDate )
	{
	var sRet = "";

	if ( sDate.length != 0 )
		{
		re = /\d\d\/\d\d\/\d\d/
		if ( ! re.test( sDate ) )
			sRet += sDate + ' is an invalid date format ( not dd/mm/yy )';
		else
			{
			var sDay = sDate.substr(0,2);
			var iMonth = parseInt(sDate.substr(3,2), 10) - 1;  //js indexes months from 0
			var sMonth = iMonth.toString(10);
			var sYear = "20" + sDate.substr(6, 2);
			var dDate = new Date();
			
			dDate.setFullYear( sYear, sMonth, sDay );
			var iNewMonth = dDate.getMonth();
			if ( iNewMonth != iMonth ) 
				sRet += sDate + ' is not a valid date.';
			}
		}
	return sRet;
	}

function bValidEmail(sStr)
	{
	var bRetval = true;
	var aSplit;
	
//Check the field is not too small	
// Use > not less-than to avoid a HTML warning from the W3C Validator
	if (5 > sStr.length  )
		bRetval = false;

//Check the @ is present in the middle of some text, and split into name and domain			
	if ( bRetval )
      {
      var aSplit = sStr.match("^(.+)@(.+)$");
      if(aSplit == null || aSplit[1] == null || aSplit[2] == null) 
         bRetval = false;
     	}

//Check the name
	if ( bRetval )
      {
	    var regexp_name=/^\"?[\w-_\.]*\"?$/;
	    if(aSplit[1].match(regexp_name) == null) 
	      bRetval = false;
	  	}

//Check the domain
   if ( bRetval )
      {
	    var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
	    if(aSplit[2].match(regexp_domain) == null)
	      bRetval=false;
	    }
	    
	return bRetval;
	}	


function bCheckAdmin(oForm)
	{
	var sErr = " ";
	var bRet = true;
//	alert('ca called' + oForm.displayfrom.value );

	if (oForm.sector.value == "0")
		sErr += "\n" + 'Please choose a sector';
		
	if (oForm.title.value.length < 1)
		sErr += "\n" + 'Please choose a job title';

	if (oForm.location.value.length < 1)
		sErr += "\n" + 'Please enter the location';

	if (oForm.salary.value.length < 1)
		sErr += "\n" + 'Please enter the benefits';

	if (oForm.company.value.length < 1)
		sErr += "\n" + 'Please enter the company';

	if (oForm.contact.value.length < 1 )
		sErr += "\n" + 'Please enter the internal contact';
	else
		{
		if ( !bValidEmail(oForm.contact.value) )
			sErr += "\n" + "Please check the format of the email address in the contact field";
		}
		
	sTemp = sValidDate(oForm.displayfrom.value);
	if (sTemp.length > 1 ) 
		sErr += "\n" + 'Display from : ' + sTemp; 

	sTemp = sValidDate(oForm.displayto.value);
	if (sTemp.length > 1 ) 
		sErr += "\n" + 'Display to : ' + sTemp; 
		
	if ( sErr.length > 2 )
		{
		alert( "Please check the form and re-submit : " + sErr );
		bRet = false;
		}
									
	return bRet;
	}
	
function bCheckApply(oForm)
	{
	var sErr = new String();
	var bRet = true;
//	alert('ca called' + oForm.name.value );

	if (sTrim(oForm.name.value).length < 2)
		sErr += "\n" + 'Please enter your name';

	if (sTrim(oForm.upload.value).length < 4)
		sErr += "\n" + 'Please upload your CV';
		
	if (sTrim(oForm.email.value).length > 0)
		{
		if ( ! bValidEmail( oForm.email.value ) )
			sErr += "\n" + 'The email address you entered is not in a valid format';
		}

	if ( sErr.length > 2 )
		{
		alert( "Please check the form and re-submit : " + sErr );
		bRet = false;
		}
									
	return bRet;
	}


function sTrim(sString)
{
return sRTrim(sLTrim(sString));
}

function sRTrim(sString)
{
while(sString.charAt((sString.length -1))==" ")
	sString = sString.substring(0,sString.length-1);
return sString;
}


function sLTrim(sString)
{
while(sString.charAt(0)==" ")
	sString = sString.substring(1,sString.length);
return sString;
}

function formSubmit(sFormName)
{
document.getElementById(sFormName).submit();
}