

if(!qform_validate_highlight_color) var qform_validate_highlight_color  = '#FFE25F';







function validateEmpty(fld,flabel) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = qform_validate_highlight_color; 
        error = flabel + " has not been filled in.\n"
    } else {
        fld.style.background = '';
    }
    return error;  
}


function validate_date(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld)	return "";
	
    var error = "";
	
    if (tfld.value.length == 0) {
        tfld.style.background = qform_validate_highlight_color; 
        error = flabel + " has not been filled in.\n"
    } else {
        tfld.style.background = '';
    }
    return error;  
}


function validate_hidden(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld)	return "";
	
    var error = "";

    return error;  
}



function validate_text(fld,flabel) {

	var tfld = document.getElementById(fld);
	

	if(!tfld)	return "";
	
    var error = "";
	
    if (tfld.value.length == 0) {
        tfld.style.background = qform_validate_highlight_color; 
        error = flabel + " has not been filled in.\n"
    } else {
        tfld.style.background = '';
    }
    return error;  
}



function validate_file(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
    var error = "";
 
    if (tfld.value.length == 0) {
        tfld.style.background = qform_validate_highlight_color; 
        error = flabel + " has not been filled in.\n"
    } else {
        tfld.style.background = '';
    }
    return error;  
}



function validate_textarea(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
    var error = "";
 
    if (tfld.value.length == 0) {
        tfld.style.background = qform_validate_highlight_color; 
        error = flabel + " has not been filled in.\n"
    } else {
        tfld.style.background = '';
    }
    return error;  
}


function validate_checkbox(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	

    var error = "";

	if(!tfld.checked){
		tfld.style.background = qform_validate_highlight_color; 
        error = flabel + " has not been filled in.\n"
    } else {
        tfld.style.background = '';
    }
	
    return error;  
}


function validate_select(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
    var error = "";
	
	if (tfld.value.length == 0) {
		tfld.style.background = qform_validate_highlight_color; 
		error = flabel + " has not been selected.\n"
	} else {
		tfld.style.background = '';
	}
    
    return error;  
}


function validate_province(fld,flabel) {

	return validate_select(fld,flabel);
 
}





function validate_radio(fld,flabel){

	var error = "";
	
	var container = document.getElementById(fld + '_div');

	if(!container) return '';
	
	chosen = ""
	
	var i = 1;
	var checkfield = true;
	
	while(checkfield){
	
		checkfield = document.getElementById(fld + '_' + i);
		
		if(checkfield && checkfield.checked){
		
			chosen = true;
			break;
		
		}
		
		i++;
	}


	if (chosen == "") {

		error = flabel + " has not been selected.\n"
		container.style.background = qform_validate_highlight_color; 

	}else{
		container.style.background = ''; 
	}

	return error;  


}







function validate_multiselect(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	

	var error = "";

	chosen = ""
	len = tfld.length

	for (i = 0; i <len; i++) {
		if (tfld[i].selected) {
			chosen = tfld[i].value
		}
	}

	if (chosen == "") {

		tfld.style.background = qform_validate_highlight_color; 
		error = flabel + " has not been selected.\n"

	}else{

		tfld.style.background = ''; 

	}

	return error;  

}



function validate_multicheckbox(fld,flabel){

	var error = "";
	var container = document.getElementById(fld + '_div');

	chosen = ""
	
	var i = 1;
	var checkfield = true;
	
	while(checkfield){
		
		checkfield = document.getElementById(fld + '_' + i);
		
		if(checkfield && checkfield.checked){
			
			chosen = true;
			break;
		
		}
		
		i++;
	}


	if (chosen == "") {

		error = " ";
		container.style.background = qform_validate_highlight_color; 

	}else{
		container.style.background = ''; 
	}

	return error;  


}



function validateUsername(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (tfld.value == "") {
        tfld.style.background = qform_validate_highlight_color; 
        error = "You didn't enter a username.\n";
    } else if ((tfld.value.length < 5) || (tfld.value.length > 15)) {
        tfld.style.background = qform_validate_highlight_color; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(tfld.value)) {
        tfld.style.background = qform_validate_highlight_color; 
        error = "The username contains illegal characters.\n";
    } else {
        tfld.style.background = '';
    }
    return error;
}



function validate_password(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (tfld.value == "") {
        tfld.style.background = qform_validate_highlight_color;
        error = "You didn't enter a password.\n";
    } else if ((tfld.value.length < 7) || (tfld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        tfld.style.background = qform_validate_highlight_color;
    } else if (illegalChars.test(tfld.value)) {
        error = "The password contains illegal characters.\n";
        tfld.style.background = qform_validate_highlight_color;
    } else if (!((tfld.value.search(/(a-z)+/)) && (tfld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        tfld.style.background = qform_validate_highlight_color;
    } else {
        tfld.style.background = '';
    }
   return error;
} 





function trimstring(s)
{
  return s.replace(/^\s+|\s+$/, '');
}


function validate_email(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
	var error = "";
	
	var trimmedfld = trimstring(tfld.value);     

	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

	if (tfld.value == "") {
		tfld.style.background = qform_validate_highlight_color;
		error = "You didn't enter an email address.\n";
	} else if (!emailFilter.test(trimmedfld)) {
		tfld.style.background = qform_validate_highlight_color;
		error = "Please enter a valid email address.\n";
	} else if (tfld.value.match(illegalChars)) {
		tfld.style.background = qform_validate_highlight_color;
		error = "The email address contains illegal characters.\n";
	} else {
		tfld.style.background = '';
	}

	return error;

}




function validate_phone(fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
    var error = "";
    var stripped = tfld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (tfld.value == "") {
        error = "You didn't enter a phone number.\n";
        tfld.style.background = qform_validate_highlight_color;
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        tfld.style.background = qform_validate_highlight_color;
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        tfld.style.background = qform_validate_highlight_color;
    }
    return error;
}

