/***********************************************
* Email Validation script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i



function validateMailForm(theForm) {
	var valid;
	valid = true;
	var warningMessage =  "The following are Mandatory fields\n"
	var emailText = ""
	var course, location, schedule;
	
	

	if( theForm.email_contact != undefined ){
		if( theForm.email_contact.value.length < 1){
			warningMessage = warningMessage + " - Name\n";
			valid = false;
		}
	}else{
		warningMessage = warningMessage = warningMessage + " - Name\n";
		valid = false;
	}


	if( theForm.email_sender != undefined ){
		if( theForm.email_sender.value.length < 1){
			warningMessage = warningMessage + " - Email\n";
			valid = false;
		}else{
			if ( !checkmail(theForm.email_sender.value ) ){
				warningMessage = warningMessage + " - Please provide a valid email address\n";
				valid = false;
			}
		}
	}else{
		warningMessage = warningMessage + " - Email\n";
		valid = false;
	}


	if( theForm.selectedCourse != undefined ){
		course = theForm.selectedCourse;
		
		if( course.options[course.selectedIndex].value.length < 1){
			warningMessage = warningMessage + " - Selected course\n";
			valid = false;
		}
	}else{
		warningMessage = warningMessage + " - Selected Course\n";
		valid = false;
	}

	if( theForm.preferedLocation != undefined ){
		for( i=0; i < theForm.preferedLocation.length; i++ ){
			if( theForm.preferedLocation[i].checked){
				location = theForm.preferedLocation[i].value;
				break;
			}
		}
		if( location != undefined ){
			if( location.length < 1 ){
				warningMessage = warningMessage + " - Prefered Location\n";
				valid = false;
			}else{
				if( location == "Other" ){
					if( theForm.preferedLocationOther != undefined ){
						if( theForm.preferedLocationOther.value.length < 1){
							warningMessage = warningMessage + " - Prefered Location Other\n";
							valid = false;
						}else{
							location = 	theForm.preferedLocationOther.value;
						}
					}else{
						warningMessage = warningMessage + " - Prefered Location Other\n";
						valid = false;
					}
				}
			}
		}else{
			warningMessage = warningMessage + " - Prefered Location\n";
			valid = false;
		}			
		
	}else{
		warningMessage = warningMessage + " - Prefered Location\n";
		valid = false;
	}
	
	if( theForm.preferedSchedule != undefined ){
		for( i=0; i < theForm.preferedSchedule.length; i++ ){
			if( theForm.preferedSchedule[i].checked){
				schedule = theForm.preferedSchedule[i].value;
				break;
			}
		}
		if( schedule != undefined ){
			if( schedule.length < 1 ){
				warningMessage = warningMessage + " - Prefered schedule\n";
				valid = false;
			}
		}else{
			warningMessage = warningMessage + " - Prefered schedule\n";
			valid = false;
		}			
		
	}else{
		warningMessage = warningMessage + " - Prefered schedule\n";
		valid = false;
	}

	if ( valid ) {
		
		
		emailText = ""
		
		emailText += "(" + course.options[course.selectedIndex].value.toUpperCase() + ") ";
		emailText += course.options[course.selectedIndex].text + "\n\n";
		
		emailText += "Name: " + theForm.email_contact.value + "\n";
		emailText += "Company: " + theForm.company_name.value + "\n";
		emailText += "Email: " + theForm.email_sender.value + "\n\n";
		
		emailText += "Area for course delivery: " + location + "\n";
		emailText += "Preferred delivery time: " + schedule + "\n";
		
		if( theForm.comments != undefined & theForm.comments.value.length > 1 ){
			emailText += "\nAdditional comments:\n";
			emailText += theForm.comments.value;
		}
		
		
		theForm.email_content.value = emailText;
		theForm.email_subject.value = "Wait List Request: " + course.options[course.selectedIndex].value.toUpperCase();
		
		theForm.submit();
	} else {
		alert(warningMessage);
	}
}



function checkmail(e){
	var returnval=emailfilter.test(e)
	return returnval
}


function selectCourse(){
	var i=0;
	var listValue;
	var inputValue;

	var courseList=document.getElementById("selectedCourse");
	for( i=0;i<courseList.length;i++ ){
		listValue = courseList.options[i].value
		inputValue = queryString("course")
		if( listValue == inputValue ){
			courseList.selectedIndex = i;
		}
	}
}

function queryString(attribute) {
	var i=0;
	var locationString="";
	var

	locationString = window.location.search.substring(1);
	if( locationString.length > 0 ){
		paramList = locationString.split("&")
		for (i=0;i<paramList.length;i++) {
			param = paramList[i].split("=");
			if (param[0] == attribute) {
				return param[1];
			}
		}
	}
	return "";
}

