function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function bookform() {

	var fname = document.getElementById("fname").value; 
	var lname = document.getElementById("lname").value; 
	var contact_no = document.getElementById("contact_no").value; 
	var email = document.getElementById("email").value; 
	var dob_1 =  document.getElementById("dob_1").value;		
	var visit_date = document.getElementById("visit_date").value; 
	var visit_time = document.getElementById("visit_time").value; 		
	var refcode =  document.getElementById("refcode").value;

	
	
    var focus = "";
    var errormsg = "";	
	
	
	if(fname.trim().length == 0 ){
        errormsg+='Please enter your First Name\n';
        if (focus=="") focus = "fname";
        document.getElementById("fname").style.backgroundColor = '#DFBCE7 ';
    }
	if(fname == 'First Name' ){
        errormsg+='Please enter your First Name\n';
        if (focus=="") focus = "fname";
        document.getElementById("fname").style.backgroundColor = '#DFBCE7 ';
    }
	if(lname.trim().length == 0 ){
        errormsg+='Please enter your Last Name\n';
        if (focus=="") focus = "lname";
        document.getElementById("lname").style.backgroundColor = '#DFBCE7 ';
    }
	if(lname == 'Last Name'){
        errormsg+='Please enter your Last Name\n';
        if (focus=="") focus = "lname";
        document.getElementById("lname").style.backgroundColor = '#DFBCE7 ';
    }
	if(contact_no.length==0){
        errormsg+='Please enter your Contact Number\n';
        if (focus=="") focus = "contact_no";
        document.getElementById("contact_no").style.backgroundColor = '#DFBCE7 ';
    }
    else if(contact_no.length > 8){
        errormsg+='Please enter no more than 8 numbers for your Contact Number\n';
        if (focus=="") focus = "contact_no";
        document.getElementById("contact_no").style.backgroundColor = '#DFBCE7 ';
    }
	else if(contact_no.length < 8){
        errormsg+='Please enter at least 8 numbers for your Contact Number\n';
        if (focus=="") focus = "contact_no";
        document.getElementById("contact_no").style.backgroundColor = '#DFBCE7 ';
    }
    else if (!/^-?\d+$/.test(contact_no)){
        errormsg+='Please enter only numbers for your Contact Number\n';
        if (focus=="") focus = "contact_no";
        document.getElementById("contact_no").style.backgroundColor = '#DFBCE7 ';
    }	
	if(email.length == 0){
		errormsg+="Please enter a valid Email Address\n";
            if (focus=="") focus = "email";
            document.getElementById("email").style.backgroundColor = '#DFBCE7 ';
	}
	if(email!= ''){
		if (email.indexOf('@')==-1||email.indexOf('.')==-1||email.indexOf('@.')!=-1){
			errormsg+="Please enter a valid Email Address\n";
			if (focus=="") focus = "email";
			document.getElementById("email").style.backgroundColor = '#DFBCE7 ';
		}    
	}	
	if(dob_1.trim().length == 0){
		errormsg+="Please enter at least 1 Child's D.O.B\n";
            if (focus=="") focus = "dob_1";
            document.getElementById("dob_1").style.backgroundColor = '#DFBCE7 ';
	}
	else if(dob_1 == 'Child D.O.B (1)'){
		errormsg+="Please enter aat least 1 Child's D.O.B\n";
            if (focus=="") focus = "dob_1";
            document.getElementById("dob_1").style.backgroundColor = '#DFBCE7 ';
	}
	
	if(document.getElementById("centre").selectedIndex == 0){
		errormsg+='Please select Preferred Centre\n';
        if (focus=="") focus = "centre";
        document.getElementById("centre").style.backgroundColor = '#DFBCE7 ';
	}
	if(visit_date.trim().length == 0){
		errormsg+="Please enter a Date of Visit\n";
            if (focus=="") focus = "visit_date";
            document.getElementById("visit_date").style.backgroundColor = '#DFBCE7 ';
	}
	if(visit_date == 'Date of Visit'){
		errormsg+="Please enter a Date of Visit\n";
            if (focus=="") focus = "visit_date";
            document.getElementById("visit_date").style.backgroundColor = '#DFBCE7 ';
	}
	if(visit_time.trim().length == 0){
		errormsg+="Please enter a Time of Visit\n";
            if (focus=="") focus = "visit_time";
            document.getElementById("visit_time").style.backgroundColor = '#DFBCE7 ';
	}
	else if(visit_time == 'Preferred Time'){
		errormsg+="Please enter a Time of Visit\n";
            if (focus=="") focus = "visit_time";
            document.getElementById("visit_time").style.backgroundColor = '#DFBCE7 ';
	}
	if (document.getElementById("rdoYes").checked == false &&
		document.getElementById("rdoNo").checked == false ){
		errormsg+='Please select if you are an existing MindChamps parent\n';
        if (focus=="") focus = "rdoYes";
        document.getElementById("rdoYes").style.backgroundColor = '#DFBCE7 ';
	}
	
	if (document.getElementById("chkgoogle").checked == false &&
		document.getElementById("chkyahoo").checked == false &&
		document.getElementById("chkfacebook").checked == false &&
		document.getElementById("chkparents").checked == false &&		
		document.getElementById("chkmumcentre").checked == false &&
		document.getElementById("chkdirectory").checked == false &&		
		document.getElementById("chkstraitstime").checked == false &&
		document.getElementById("chkmypaper").checked == false &&
		document.getElementById("chklian").checked == false &&
		document.getElementById("chktoday").checked == false &&			
		document.getElementById("chkyoungparents").checked == false &&
		document.getElementById("chkkidsguide").checked == false &&
		document.getElementById("chkparentsworld").checked == false &&
		document.getElementById("chkfriends").checked == false &&		
		document.getElementById("chkdirectmailer").checked == false &&
		document.getElementById("chkradio").checked == false &&
		document.getElementById("chkevens").checked == false )
		{
			errormsg+='Please select at least one option for "How did you know about MindChamps?"\n';
			if (focus=="") focus = "chkgoogle";
			document.getElementById("chkgoogle").style.backgroundColor = '#DFBCE7 ';
	}
/*	if(refcode.length != 0){
		if (!/^-?\d+$/.test(document.getElementById("refcode").value)){
        errormsg+='Please enter only numbers for your Ref Code\n';
        if (focus=="") focus = "refcode";
        document.getElementById("refcode").style.backgroundColor = '#DFBCE7 ';
    	}
	}*/
	
    if (errormsg!= ""){
        document.getElementById(focus).focus();
        alert(errormsg);
        return false;

    }else{
        return true;
    }
}
