function validateOrder(theForm) {
var reason = "";
  reason += validateFill(theForm.surname,'- Please enter your First Name\n');
  reason += validateFill(theForm.familyname,'- Please enter your Last Name\n');
  reason += validateFill(theForm.street1,'- Please enter your Street Address\n');
  reason += validateFill(theForm.street2,'- Please enter your Phone Number\n');
  reason += validateFill(theForm.city,'- Please enter a City\n');
  reason += validateSelect(theForm.state,'- Please choose a Province\n');
  reason += validateFill(theForm.zip,'- Please enter a ZIP code (write "NA" for none)\n');
  reason += validateSelect(theForm.findus,'- Please let us know how you found us\n');
  reason += validateEmail(theForm.email,1);
  if (reason != "") {
    alert("Some information is missing or incorrect:\n\n" + reason);
    return false;
  }
  return true;
}

function validateTryout(theForm) {
var reason = "";
  reason += validateFill(theForm.trial_name,'- Please enter your Name\n');
  reason += validateEmail(theForm.trial_email,1);
  reason += validateSelect(theForm.trial_findus,'- Please let us know how you found us\n');
  if (reason != "") {
    alert("Some information is missing or incorrect:\n\n" + reason);
    return false;
  }
  return true;
}

function validateReferals(theForm) {
var reason = "";
  reason += validateFill(theForm.friend1_name,'- Please enter the Name of Friend 1\n');
  reason += validateFill(theForm.friend2_name,'- Please enter the Name of Friend 2\n');
  reason += validateFill(theForm.friend3_name,'- Please enter the Name of Friend 3\n');
  reason += validateReferEmail(theForm.friend1_email,1);
  reason += validateReferEmail(theForm.friend2_email,2);
  reason += validateReferEmail(theForm.friend3_email,3);
  if (reason != "") {
    alert("Some information is missing or incorrect:\n\n" + reason);
    return false;
  }
  return true;
}

function validateFill(fld1,fld2) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers

    if (fld1.value == "") {
        fld1.style.background = '#ea8147';
        fld1.value = '';
        error = fld2;
    } else {
        fld1.style.background = '#BEE092';
    }
   return error;
}

function validateSelect(fld1,fld2) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers

    if (fld1.value == 0) {
        fld1.style.background = '#ea8147';
        error = fld2;
    } else {
        fld1.style.background = '#BEE092';
    }
   return error;
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateReferEmail(fld,fld2) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = '#ea8147';
        error = "- You did not enter an email address for Friend " + fld2 + ".\n";
     } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#ea8147';
        error = "- Please enter a valid email address for Friend " + fld2 + ".\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#ea8147';
        error = "- The email address of " + fld2 + "contains illegal characters.\n";
    } else {
        fld.style.background = '#BEE092';
    }
    return error;
}


function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

    if (fld.value == "") {
        fld.style.background = '#ea8147';
        error = "- You did not enter an email address.\n";
     } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#ea8147';
        error = "- Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#ea8147';
        error = "- The email address contains illegal characters.\n";
    } else {
        fld.style.background = '#BEE092';
    }
    return error;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function update_moduleprice(new_price)
{
  document.getElementById('moduleprice').value=new_price;
  document.getElementById('totalprice').innerHTML=addCommas((new_price*1)+(document.getElementById('movieprice').value*1));
  document.getElementById('paynow').innerHTML=addCommas(((new_price*1)+(document.getElementById('movieprice').value*1))/2)
}
function update_movieprice(new_price)
{
  document.getElementById('movieprice').value=new_price;
  document.getElementById('totalprice').innerHTML=addCommas((new_price*1)+(document.getElementById('moduleprice').value*1));
  document.getElementById('paynow').innerHTML=addCommas(((new_price*1)+(document.getElementById('moduleprice').value*1))/2)
}

function update_price_to_private(module)
{
  document.getElementById('module_select').value='private training course';
  document.getElementById('module').value=module;
  document.getElementById('totalprice').innerHTML=addCommas((module*1)+(document.getElementById('movie').value*1));
  document.getElementById('paynow').innerHTML=addCommas(((module*1)+(document.getElementById('movie').value*1))/2)
}

function update_price_to_group(module)
{
  document.getElementById('module_select').value='group training course';
  document.getElementById('module').value=module;
  document.getElementById('totalprice').innerHTML=addCommas((module*1)+(document.getElementById('movie').value*1));
  document.getElementById('paynow').innerHTML=addCommas(((module*1)+(document.getElementById('movie').value*1))/2)
}

function update_price_to_pattaya(module)
{
  document.getElementById('module_select').value='Pattaya';
  document.getElementById('module').value=module;
  document.getElementById('totalprice').innerHTML=addCommas((module*1)+(document.getElementById('movie').value*1));
  document.getElementById('paynow').innerHTML=addCommas(((module*1)+(document.getElementById('movie').value*1))/2)
}

function update_price_to_bangkok(module)
{
  document.getElementById('module_select').value='Bangkok';
  document.getElementById('module').value=module;
  document.getElementById('totalprice').innerHTML=addCommas((module*1)+(document.getElementById('movie').value*1));
  document.getElementById('paynow').innerHTML=addCommas(((module*1)+(document.getElementById('movie').value*1))/2)
}




