
// opcmf form class validation
// dependancy is jquery

var has_not_been_filled = "has not been filled in.";
var has_not_been_selected = "has not been selected.";
var didnt_enter_username = "You didn't enter a username.";
var username_wrong_length = "The username is the wrong length.";
var username_illegal = "The username contains illegal characters.";
var wrong_length = "is the wrong length.";
var contains_illegal = "contains illegal characters.";
var didnt_enter_email = "You didn't enter an e-mail address.";
var enter_valid_email = "Please enter a valid e-mail address.";
var illegal_email = "The e-mail address contains illegal characters.";
var didnt_enter_phone = "You didn't enter a phone number.";
var phone_illegal = "The phone number contains illegal characters.";
var phone_wrong_length = "The phone number is the wrong length. Make sure you included an area code.";



if(opcmf_lang == "fr"){

	has_not_been_filled = "n'a pas été rempli.";
	has_not_been_selected = "n'a pas été sélectionné.";
	didnt_enter_username = "Vous n'avez pas entrer un nom d'utilisateur.";
	username_wrong_length = "Le nom d'utilisateur n'est pas la bonne longueur.";
	username_illegal = "Le nom d'utilisateur contient des caractères.";
	wrong_length = "n'est pas la bonne longueur.";
	contains_illegal = "contient des caractères non autorisés.";
	didnt_enter_email = "Vous n'avez pas entré une adresse e-mail.";
	enter_valid_email = "S’il vous plaît entrer une adresse de courriel valide.";
	illegal_email = "L'adresse e-mail contient des caractères.";
	didnt_enter_phone = "Vous n'avez pas entrer un numéro de téléphone.";
	phone_illegal = "Le numéro de téléphone contient des caractères.";
	phone_wrong_length = "Le numéro de téléphone n'est pas la bonne longueur. Assurez-vous que vous avez inclus un code régional.";
	

}




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 + "\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 + "\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_captcha(fld,flabel) {


	// defug by returning null
	//return '';
	
	var validerror = validate_text(fld,flabel);
	
	if(validerror == ""){
	
		var tfld = document.getElementById(fld);
		
		var ajaxparams= "cma=Pages:Form:Remote&func=jsValidateCaptcha&args[keystring]=" + tfld.value + "&args[valholder]=validerror&args[fieldname]=" + tfld.id;
		
	
		///async
	
		jQuery.ajax({
	 
				async: true,
				type: "POST",
				url: "index.php",
				data: ajaxparams,
				success: function(msg){
				
							
					if(msg == ''){
					
						precaptchavalid = 'fullyprocessed';
										
						if(invalidform_reason == ''){
							document.getElementById('opform').submit();
						}
						
					
					}else{
					
						alert(msg);
								
					
						var captchaimg = document.getElementById(tfld.id + "_captcha_img");
				
						if(captchaimg){
							eval("captchaimg.src = captchaimg.src + " + "'&newdate=" + (new Date()).getTime() + "'");
							tfld.value = "";
							//return '';
						}
						
						//return '';								
										
					}
					
			}
		});

		
	}else{
	
		invalidform_reason += validerror;
	
		//return validerror;
		
	}
	
 
}



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 + "\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 + "\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 + "\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 + "\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(i > 20) break; // prevent endless loop
	}


	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 = didnt_enter_username + "\n";
    } else if ((tfld.value.length < 5) || (tfld.value.length > 15)) {
        tfld.style.background = qform_validate_highlight_color; 
        error = username_wrong_length + "\n";
    } else if (illegalChars.test(tfld.value)) {
        tfld.style.background = qform_validate_highlight_color; 
        error = username_illegal + "\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";
		error = flabel + " " + has_not_been_filled + "\n"
	} else if ((tfld.value.length < 3) || (tfld.value.length > 15)) {
        error = flabel + " " + wrong_length + "\n";
        tfld.style.background = qform_validate_highlight_color;
    } else if (illegalChars.test(tfld.value)) {
        error = flabel + " " + contains_illegal + "\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 = didnt_enter_email + "\n";
	} else if (!emailFilter.test(trimmedfld)) {
		tfld.style.background = qform_validate_highlight_color;
		error = enter_valid_email + "\n";
	} else if (tfld.value.match(illegalChars)) {
		tfld.style.background = qform_validate_highlight_color;
		error = illegal_email + "\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 = didnt_enter_phone + "\n";
        tfld.style.background = qform_validate_highlight_color;
    } else if (isNaN(parseInt(stripped))) {
        error = phone_illegal + "\n";
        tfld.style.background = qform_validate_highlight_color;
    } else if (!(stripped.length == 10)) {
        error = phone_wrong_length + "\n";
        tfld.style.background = qform_validate_highlight_color;
    }
    return error;
}


