var newwindow;
function openwindow(url)
{
newwindow=window.open(url,'name','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=650,height=450');
	if (window.focus) {newwindow.focus()}

}

function openwindow2(url)
{
newwindow=window.open(url,'name','toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=600,height=600');
	if (window.focus) {newwindow.focus()}

}

function setField(mychoice) {
document.registration_form.choice.value = mychoice;
document.registration_form.submit()
}

function setField_hiphop(mychoice) {
document.hiphop_registration_form.choice.value = mychoice;
document.hiphop_registration_form.submit()
}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

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

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 = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validateGender(fld)
{
	var error = "";
	myOption = -1;
	for (i=fld.length-1; i > -1; i--) 
	{
		if (fld[i].checked) 
		{
			myOption = i; 
			i = -1;
		}
	}
	if (myOption == -1) 
	{
		error = "Please choose either female or male.\n";
	}
	return error;
}

function validate_Policy(fld)
{
	var error = "";
	if ( fld.checked == false )
    {
        error = "In order to register online you must accept the conditions on the Payment and Refund Policy\n" ;
    }	
	return error;
}


function is_at_least_one_check( form_object,checkbox_name ) 
{ 

    var total=0;
    for(var i=0; i < form_object[checkbox_name].length; i++)
    {
        if(form_object[checkbox_name][i].checked)
        {
            return true;
        }
        
    }
    
    return false; 
}


function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateEmail(theForm.Email);
  reason += validateEmpty(theForm.Name);
  reason += validateEmpty(theForm.Last_Name);
  reason += validateGender(theForm.Gender);
  reason += validate_Policy(theForm.Agree_Policy);
  if  ( is_at_least_one_check(theForm,"Class_Details[]") ==  false)
  {
	reason += "Please select a course or courses you would like to attend.\n";  
  }
  if (reason != "" ) {
    alert("Some fields need correction:\n" + reason);
    return false;
  }
 

  return true;
}



function ActionDeterminator()
{
if(document.payment_form.payment[0].checked == true) {
   document.payment_form.action = 'http://www.dancevancouver.ca/salsa-class-registration.php';
   }
if(document.payment_form.payment[1].checked == true) {
   document.payment_form.action = 'http://www.dancevancouver.ca/agree_policy.php';
   document.payment_form.method = 'post';

   }
return true;
}

function ActionDeterminatorTest()
{
if(document.payment_form.payment[0].checked == true) {
   document.payment_form.action = 'http://www.dancevancouver.ca/test/salsa-class-registration.php';
   //document.payment_form.method = 'post';
   //document.payment_form.submit();
   }
if(document.payment_form.payment[1].checked == true) {
   document.payment_form.action = 'http://www.dancevancouver.ca/test/secured-online-payment.php';
   document.payment_form.method = 'post';

   }
return true;
}

/* 	
'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=420,height=400,left=430,top=23'

newwindow=window.open(url,'name','height=400,width=600,scrollbars=yes'); 


*/