/*Check the whole form : used on submit*/
function bCheckTheForm( form )
	{
	 var errormessage = new String();
	 var formOk = false;
	 calcTotal();
	with ( form )
		{
		if(bEmpty(Name.value))
		  {errormessage += "\n\nPlease enter your name.";}
		if(bEmpty(Address.value))
		  {errormessage += "\n\nPlease enter your address.";}
		
		if(bEmpty(Email.value))
			 {errormessage += "\n\nPlease enter your email.";}
		else
			{
			if(!bOkEmail(Email.value))
			  {errormessage += "\n\nThe email does not appear to be a valid email address";}
			}
	
		if(bEmpty(Phone.value))
		  {errormessage += "\n\nPlease enter your Phone Number.";}
	
		/*if( ! Student.checked && ! EarlyRegistration.checked && !FullFee.checked )
			{errormessage += "\n\nPlease indicate the type of registration : Student, Early or Full";} */
		}
	formOk = ! (errormessage.length > 2);			
	if (! formOk)
		{alert('The Form was not completed :' + errormessage + '\n'); }

	return formOk ;
	} // end of CheckTheForm()
	
/* ** Functions to check individual fields are valid ** */
function NotEmpty( field )
	{
	var oTarget;
	oTarget = document.getElementById( field.name + '-err' );
	if ( bEmpty(field.value) )
		oTarget.className = "err-show";
	else
		oTarget.className = "err-hide";
	}

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

/*Make sure a checkbox is ticked*/
function Ticked( field )
{
	var oTarget;
	oTarget = document.getElementById( field.name + '-err' );
	if ( ! field.checked)
		oTarget.className = "err-show";
	else
		oTarget.className = "err-hide";
}

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

/*Utility Functions*/
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 bSelectOk( field )
	{
	return ( field.value.indexOf('please') == -1 );
	}
	
function ClearTextareaMsg( field, defaultMsg )
{
	if ( field.value == defaultMsg )
		field.value="";
}

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

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 calcTotal()
{
var fTotal = 0.00;
var form = document.getElementById("reg");
with ( document.forms[0] ) {
	if ( RegistrationType[0].checked  ) fTotal += 395.00;
	if ( RegistrationType[1].checked   ) fTotal += 445.00;
	if ( RegistrationType[2].checked   ) fTotal += 495.00;
	
	if ( IstanbulTour.checked ) fTotal += 50.0;
	if ( MuseumTour.checked ) fTotal += 45.0;
	
	fTotal += getInt(AccompanyingPersonPackage) *400.0;
	fTotal += getInt(AccompanyingPersonIstanbulTour) *50.0;
	fTotal += getInt(AccompanyingPersonMuseumTour) *45.0;
	TotalAmount.value = fTotal;	
	ChequeAmount.value = fTotal;	
	}
	
}

function getInt( field )
	{
	var val=parseInt( field.value )
	if ( isNaN( val ) )
		val = 0;
	return val;	
	}
	
	