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

function Hidden ()
{
var sStr = '<a href="mailto:';
sStr += Obscure('ko.nrj.hepraknroo@ghuj');
sStr += '">';
sStr += Obscure('ko.nrj.hepraknroo@ghuj');
sStr += "</a>";
return sStr;
}

function Obscure( sStr )
{
var iLen = sStr.length;
var aRet = new Array();
var i;
var sTemp;

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 bCheckTheForm( form )
	{
	 var errormessage = new String();
	 var name = Trim(form.name.value);
	 var email = Trim(form.email.value);
	 var formOk = false;

	if(bEmpty(name))
	  {errormessage += "\n\nPlease fill in the \"Name\" field";}
	if(bEmpty(email))
		 {errormessage += "\n\nPlease fill in the \"email\" field";}
	else
		{
		if(!bOkEmail(email))
		  {errormessage += "\n\nThe entry for \"email\" does not appear to be a valid email address";}
		}

	formOk = ! (errormessage.length > 2);			
	if (! formOk)
		{alert('The Form was not completed :' + errormessage + '\n'); }
	else
		{
		if ( form.id == 'autores-form' )
			{alert('Thanks : the report is being emailed to you.' + '\n');	}

		if ( form.id == 'contact' )
			{alert('Thanks : we will get in touch as soon as possible.' + '\n');	}
		}

	return formOk;
	} 

function bCheckCallForm( form )
	{
	 var errormessage = new String();
	 var email = Trim(form.email.value);
	 var formOk = false;

	if(bEmpty(form.firstname.value))
	  {errormessage += "\n\nPlease fill in the \"first name\" field";}

	if(bEmpty(form.lastname.value))
	  {errormessage += "\n\nPlease fill in the \"last name\" field";}

	if(bEmpty(form.city.value))
	  {errormessage += "\n\nPlease fill in the \"city\" field";}

	if(bEmpty(form.county.value))
	  {errormessage += "\n\nPlease fill in the \"County/State/Province\" field";}

	if(bEmpty(form.postcode.value))
	  {errormessage += "\n\nPlease fill in the \"Post/Zip Code\" field";}

	if(bEmpty(form.country.value))
	  {errormessage += "\n\nPlease fill in the \"country\" field";}

	if(bEmpty(form.phone.value))
	  {errormessage += "\n\nPlease fill in the \"Telephone (Landline)\" field";}

	if(bEmpty(form.email.value))
		 {errormessage += "\n\nPlease fill in the \"email\" field";}
	else
		{
		if(!bOkEmail(form.email.value))
		  {errormessage += "\n\nThe entry for \"email\" does not appear to be a valid email address";}
		}

	if(bEmpty(form.hear.value))
	  {errormessage += "\n\nPlease fill in the \"where did you hear about this website\" field";}


	formOk = ! (errormessage.length > 2);			
	if (! formOk)
		{alert('The Form was not completed :' + errormessage + '\n'); }
	else
		{alert('Thanks for your enqiry.' + '\n\n' +'We will get back to you as soon as possible.' + '\n');}

	return formOk;
	} // end of CheckCallForm()


function bCheckPrecallForm( form )
	{
	 var errormessage = new String();
	 var email = Trim(form.email.value);
	 var formOk = false;

	if(bEmpty(form.name.value))
	  {errormessage += "\n\nPlease fill your name.";}

	if(bEmpty(email))
		 {errormessage += "\n\nPlease fill in your Email.";}
	else
		{
		if(!bOkEmail(email))
		  {errormessage += "\n\nThe entry for \"email\" does not appear to be a valid email address";}
		}

	if(bEmpty(form.done.value))
	  {errormessage += "\n\nPlease enter what it is you have accomplished since our last call.";}

	if(bEmpty(form.notdone.value))
	  {errormessage += "\n\nPlease enter what it is you didn't get done but intended to.";}

	if(bEmpty(form.chal.value))
	  {errormessage += "\n\nPlease enter the challenges & problems you are facing now.";}

	if(bEmpty(form.opp.value))
	  {errormessage += "\n\nPlease enter the opportunities that are available now.";}

	if(bEmpty(form.work.value))
	  {errormessage += "\n\nPlease enter what you want to work at during the call";}

if(bEmpty(form.promise.value))
	  {errormessage += "\n\nPlease enter what you promise to do by the next call.";}


	formOk = ! (errormessage.length > 2);			
	if (! formOk)
		{alert('The Coaching Call Preparation Form was not completed :' + errormessage + '\n'); }
	else
		{alert('Thanks for completing the Coaching Call Preparation Form.' + '\n\n' +'We look forward to the call.' + '\n' );}

	return formOk;
	} // end of CheckPrecallForm()


function bOkEmail(ss)
	{
	var retval = true;
	var split;
	ss = Trim(ss);
//Check the field is not too small	
	if (5 > ss.length  )
		retval = false;

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

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

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

function ClearTextareaMsg( field, defaultMsg )
{
	if ( field.value == defaultMsg )
		field.value="";
}

function bNotEmpty( field )
	{
	var oTarget;
	oTarget = document.getElementById( field.name + '-err' );
	if ( bEmpty(field.value) )
		oTarget.className = "err-show";
	else
		oTarget.className = "err-hide";
	}

function bEmpty(ss)
	{
	return ( Trim(ss).length < 1 )
	}

function bEmailFieldOk( field )
	{
	var oTarget;
	oTarget = document.getElementById( field.name + '-err' );
	if ( ! bOkEmail(Trim(field.value)) )
		oTarget.className = "err-show";
	else
		oTarget.className = "err-hide";
	}
