
// opcmf form class validation
// dependancy is jquery

var captcha_incorrect = "CAPTCHA is incorrect.";
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.";
var not_number = "The value is not a number.";



if(opcmf.lang == "fr"){

	captcha_incorrect = "CAPTCHA is incorrect.";
	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.";
	not_number = "The value is not a number.";
	

}

//if(!qform_validate_highlight_color) var qform_validate_highlight_color  = '#FFE25F';


function validateEmpty(fld,flabel) {
    
	var error = "";
	 
    if (fld.value.length == 0) {
        fld.style.backgroundColor = op_fvalid.highlight_color; 
        error = flabel + " " + has_not_been_filled + "\n"
    } else {
        fld.style.backgroundColor = '';
    }
    return error;  
}


function validate_date(fld,flabel) {
	return op_fvalid.validate_text(fld,flabel);
}


function validate_hidden(fld,flabel) {
	return op_fvalid.validate_hidden(fld,flabel);
}



function validate_captcha(fld,flabel) {

	var validerror = op_fvalid.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;
	
		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{
					
						//invalidform_reason += captcha_incorrect;
					
						alert(captcha_incorrect);
											
						var captchaimg = document.getElementById(tfld.id + "_captcha_img");
				
						if(captchaimg){
							eval("captchaimg.src = captchaimg.src + " + "'&newdate=" + (new Date()).getTime() + "'");
							tfld.value = "";

						}

					}
					
			}
		});
		
	}
 
}


function validate_text(fld,flabel) {
	return op_fvalid.validate_text(fld,flabel);
}

function validate_file(fld,flabel) {
	return op_fvalid.validate_text(fld,flabel);
}

function validate_textarea(fld,flabel) {
	return op_fvalid.validate_textarea(fld,flabel);
}

function validate_checkbox(fld,flabel) {
	return op_fvalid.validate_checkbox(fld,flabel);
}

function validate_select(fld,flabel) {
	return op_fvalid.validate_select(fld,flabel);
}

function validate_province(fld,flabel) {
	return validate_select(fld,flabel);
}

function validate_radio(fld,flabel){
	return op_fvalid.validate_radio(fld,flabel);
}

function validate_multiselect(fld,flabel) {
	return op_fvalid.validate_multiselect(fld,flabel);
}


function validate_numeric(fld,flabel) {
	return op_fvalid.validate_numeric(fld,flabel);
}


function validate_multicheckbox(fld,flabel){
	return op_fvalid.validate_multicheckbox(fld,flabel);
}

function validateUsername(fld,flabel) {
	return op_fvalid.validateUsername(fld,flabel);
}

function validate_password(fld,flabel) {
	return op_fvalid.validate_password(fld,flabel);
} 

function validate_email(fld,flabel) {
	return op_fvalid.validate_email(fld,flabel);
}

function validate_phone(fld,flabel) {
	return op_fvalid.validate_phone(fld,flabel);
}





var op_fvalid = {


highlight_color: '#FFE25F',

trimstring: function (s){

  return s.replace(/^\s+|\s+$/, '');
},


setvalidstate: function (tfld_container,flabel,state,message){

	var error = '';
	
	if(!tfld_container) return "";
	if(tfld_container.style.display == 'none') return "";
	
	
    if (state == 0) {
	
		tfld_container.style.backgroundColor = this.highlight_color;
		tfld_container.setAttribute("opfvalid", false);
		
		if(message != ""){
			error = flabel + " " + message + "\n"
		}else{
			error = flabel + " " + has_not_been_filled + "\n"
		}
    
	} else {
	
		tfld_container.style.backgroundColor = '';
		tfld_container.setAttribute("opfvalid", true);		
	}
	
	return error;

},


	setdisabilityonce: function(container_id,state){
	
		if(state == true || state == 1) state = "true";
		if(state == false || state == 0 || state == "") state = "false";
				
		// little fix for sloppy coders ... like me :)
		container_id = container_id.replace("_div", "");
	
		if(state == "true"){
		
			jQuery("#" + container_id + "_div").removeClass('opformelement');
			jQuery("#" + container_id + "_div").addClass('opformelement_disabled');
			
			if( 
				jQuery("#" + container_id + "_div").attr('opftype') == "multicheckbox"
			){
				jQuery("#" + container_id + "_div :input").attr('disabled', true);		
		
			}else{
				jQuery('#' + container_id).attr('disabled', true);
			}
				
		}else{
		
			jQuery("#" + container_id + "_div").removeClass('opformelement_disabled');
			jQuery("#" + container_id + "_div").addClass('opformelement');
			
			if( 
				jQuery("#" + container_id + "_div").attr('opftype') == "multicheckbox"
			){
				jQuery("#" + container_id + "_div :input").attr('disabled', false);		
			}else{
				jQuery('#' + container_id).attr('disabled', false);
			}
				
		}
		
			
	},

	setenabled: function(container_id){
	
		this.setdisabilityonce(container_id,false);
					
	},


	// this is sort of odd but necessary for now 
	setcheckabled: function(check_id,state){
		
		if(state == true || state == 1) state = "true";
		if(state == false || state == 0 || state == "") state = "false";
		
		if(state == "true"){
		
			jQuery('#' + check_id).attr('disabled', false);
			jQuery("#" + check_id + "_label").removeClass('opfieldlabel_disabled');
			jQuery("#" + check_id + "_label").addClass('opfieldlabel');
			
		}else{
			jQuery('#' + check_id).attr('disabled', true);
			jQuery("#" + check_id + "_label").removeClass('opfieldlabel');
			jQuery("#" + check_id + "_label").addClass('opfieldlabel_disabled');			
		}
		
					
	},
	
	
	
	setdisabled: function(container_id){
		
		this.setdisabilityonce(container_id,true);
		this.setrequiredonce(container_id,false);
		
			
	},
	

	setrequiredonce: function(container_id,state){
	
		if(state == true || state == 1) state = "true";
		if(state == false || state == 0 || state == "") state = "false";
		
		
		// little fix for sloppy coders ... like me :)
		container_id = container_id.replace("_div", "");
		
		jQuery("#" + container_id + "_div").attr('opfrequired',state);
		
			
	
			
	},

	
	setrequired: function(container_id,state){
	
		if(state == true || state == 1) state = "true";
		if(state == false || state == 0 || state == "") state = "false";
		
		this.setrequiredonce(container_id,state);
		

		
		if(state == "true"){
			this.setdisabilityonce(container_id,false);
		}		
				
	
			
	},
	

	////////////////////////////////////////////////
	// work in progress
	autovalidate: function(container_id) {

		// here we will loop through all field elements of the form_id and get their field type, label and whether they are tagged as required 
		// we will them run validation tests on each required field that is found
		// it a required field is set to non visible, it is ignored
		
		// get all divs with the attribute opvalid in them ...
		
		var fielddivid = '';
		var fieldid = '';
		var fieldtype = '';
		var fieldrequired = false;
		var fielddisabled = false;
		var fieldlabel = '';
		var opvalidflag = '';
		var opisvisible = false;
		
		
		var error = '';
						
		//jQuery('#' + container_id).find('div').each( function(){
		

		
		jQuery('#' + container_id).find('.opformelement').each( function(){

			//alert(container_id);
		
			fielddivid = jQuery(this).attr("id");
							
			fieldid = fielddivid.replace("_div", "");
						
			fieldtype = jQuery(this).attr("opftype");			
			
			fieldlabel = jQuery(this).attr("opflabel");	
			
			fieldrequired = jQuery(this).attr("opfrequired");	

	
				
										
			opisvisible = jQuery(this).is(':visible');
				
			if(fieldrequired == "false") fieldrequired = false;
												
			var dovalidate = true;
				
			if(!opisvisible) dovalidate = false;
			if(!fieldrequired) dovalidate = false;
			if(fielddisabled) dovalidate = false;				
				
			if(fieldtype == 'undefined') dovalidate = false;
			if(jQuery(this).attr('opfignorvalid') == "true") dovalidate = false;
							
				
			if(dovalidate){
				
			
				switch(fieldtype){
		
					case 'text':
					
						error += op_fvalid.validate_text(fieldid,fieldlabel);
						break;
						
					case 'textarea':
						error += op_fvalid.validate_textarea(fieldid,fieldlabel);
						break;				
				
					case 'captcha':
						error += op_fvalid.validate_captcha(fieldid,fieldlabel);
						break;
				
					case 'checkbox':
						error += op_fvalid.validate_checkbox(fieldid,fieldlabel);
						break;

					case 'date':
						error += op_fvalid.validate_date(fieldid,fieldlabel);
						break;
						
					case 'email':
						error += op_fvalid.validate_email(fieldid,fieldlabel);
						break;

					case 'file':
						error += op_fvalid.validate_file(fieldid,fieldlabel);
						break;

					case 'hidden':
						error += op_fvalid.validate_hidden(fieldid,fieldlabel);
						break;

					case 'multicheckbox':
						error += op_fvalid.validate_multicheckbox(fieldid,fieldlabel);
						break;						
						
					case 'multiselect':
						error += op_fvalid.validate_multiselect(fieldid,fieldlabel);
						break;	
						
					case 'numeric':
						error += op_fvalid.validate_numeric(fieldid,fieldlabel);
						break;	

					case 'password':
						error += op_fvalid.validate_password(fieldid,fieldlabel);
						break;	

					case 'phone':
						error += op_fvalid.validate_phone(fieldid,fieldlabel);
						break;	

					case 'province':
						error += op_fvalid.validate_province(fieldid,fieldlabel);
						break;	

					case 'radio':
						error += op_fvalid.validate_radio(fieldid,fieldlabel);
						break;	

					case 'select':
						error += op_fvalid.validate_select(fieldid,fieldlabel);
						break;	

					case 'text':
						error += op_fvalid.validate_text(fieldid,fieldlabel);
						break;	

					case 'textarea':
						error += op_fvalid.validate_textarea(fieldid,fieldlabel);
						break;							
						
									
					}
				
					//if(error == 'undefined') alert(fieldtype + ', ' + fieldid);
				
				//}
			
			}
		
		});
		
		
		return error;
			
			

			
	},
	
	
	
	
validate_textarea: function (fld,flabel){


	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
	var tfld_container = document.getElementById(fld + '_div');	
		
	if(tfld_container.style.display == 'none') return "";
	
	

	
	tfld.value = this.trimstring(tfld.value);   
	   
	var validstate = 1;
	
    if (tfld.value.length == 0) validstate = 0;
	
	return this.setvalidstate(tfld_container,flabel,validstate,has_not_been_filled);



		
	},
	
validate_multiselect: function (fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
	var tfld_container = document.getElementById(fld + '_div');	
		

	var error = "";

	chosen = ""
	var validstate = 1;
	
	len = tfld.length

	for (i = 0; i <len; i++) {
		if (tfld[i].selected) {
			chosen = tfld[i].value
		}
	}

	if (chosen == "") validstate = 0;

	return this.setvalidstate(tfld_container,flabel,validstate,has_not_been_selected);

},
	
validate_date: function (fld,flabel){

	return this.validate_text(fld,flabel);


},

	
validate_text: function (fld,flabel){


	var tfld = document.getElementById(fld);
	if(!tfld)	return "";
	
	var tfld_container = document.getElementById(fld + '_div');	
	
	if(!tfld_container) return "";
	
	if(tfld_container.style.display == 'none') return "";
	
	
	tfld.value = this.trimstring(tfld.value);   
		
	var validstate = 1;
	
    if (tfld.value.length == 0) validstate = 0;
	
	return this.setvalidstate(tfld_container,flabel,validstate,has_not_been_filled);

		
	},
	
validate_email: function (fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
	var tfld_container = document.getElementById(fld + '_div');	
		

	
	var error = "";
	var validstate = 1;
	
	var trimmedfld = this.trimstring(tfld.value);     

	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;

	if (tfld.value == "") {
		validstate = 0;
		error = didnt_enter_email;
	} else if (!emailFilter.test(trimmedfld)) {
		validstate = 0;
		error = enter_valid_email;
	} else if (tfld.value.match(illegalChars)) {
		validstate = 0;
		error = illegal_email;
	}	
	
    return this.setvalidstate(tfld_container,flabel,validstate,error);

},


validate_numeric: function (fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
	var tfld_container = document.getElementById(fld + '_div');	
	
    var error = "";
	var validstate = 1;
	
	tfld.value = this.trimstring(tfld.value);   
	
    var stripped = tfld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (tfld.value == "") {
        error = has_not_been_filled;
        validstate = 0;
    } else if (isNaN(parseInt(stripped))) {
        error = not_number;
        validstate = 0;
	}
	
    return this.setvalidstate(tfld_container,flabel,validstate,error);
},


validate_phone: function (fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
	var tfld_container = document.getElementById(fld + '_div');	
	
    var error = "";
	var validstate = 1;
	
	tfld.value = this.trimstring(tfld.value);   
	
    var stripped = tfld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (tfld.value == "") {
        error = didnt_enter_phone;
        validstate = 0;
    } else if (isNaN(parseInt(stripped))) {
        error = phone_illegal;
        validstate = 0;
    } else if (!(stripped.length == 10)) {
        error = phone_wrong_length;
        validstate = 0;
    }
	
    return this.setvalidstate(tfld_container,flabel,validstate,error);
},
	
validate_hidden: function (fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld)	return "";
	
	var validstate = 1;
	
    if (tfld.value.length == 0) return flabel + " incomplete.\n";

    return ""; //flabel + " incomplete"; 
	
},

validate_checkbox: function (fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
	var tfld_container = document.getElementById(fld + '_div');	
	
    var error = "";
	var validstate = 1;
	
	if(!tfld.checked) validstate = 0;
	
    return this.setvalidstate(tfld_container,flabel,validstate,has_not_been_selected);
	
},


validate_multicheckbox: function (fld,flabel){

	var error = "";
	var tfld_container = document.getElementById(fld + '_div');	
	if(!tfld_container) return "";

	chosen = ""
	var validstate = 1;
		
	
	var i = 1;
	var checkfield = true;
	
	while(checkfield){
		
		checkfield = document.getElementById(fld + '_' + i);
		
		if(checkfield && checkfield.checked){
			
			chosen = true;
			break;
		
		}
		
		i++;
	}


	if (chosen == "") validstate = 0;


    return this.setvalidstate(tfld_container,flabel,validstate,has_not_been_selected);


},

validate_radio: function (fld,flabel){

	// this function may be obsolete ???
	var error = "";
	
	var tfld = document.getElementById(fld);
	var tfld_container = document.getElementById(fld + '_div');	

	if(!tfld_container) return '';
	
	chosen = ""
	var validstate = 1;
	
	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 == "") validstate = 0;


    return this.setvalidstate(tfld_container,flabel,validstate,has_not_been_selected);



},

validate_select: function (fld,flabel){


	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
	var tfld_container = document.getElementById(fld + '_div');	
		

		
	var validstate = 1;
	
	if (tfld.value.length == 0) var validstate = 0;
   
	return this.setvalidstate(tfld_container,flabel,validstate,has_not_been_selected);

		
	},
	
validateUsername: function (fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
	var tfld_container = document.getElementById(fld + '_div');	
		

	
	tfld.value = this.trimstring(tfld.value);   
	
    var error = "";
	var validstate = 1;
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (tfld.value == "") {
        validstate = 0; 
        error = didnt_enter_username;
    } else if ((tfld.value.length < 5) || (tfld.value.length > 15)) {
        validstate = 0; 
        error = username_wrong_length;
    } else if (illegalChars.test(tfld.value)) {
        validstate = 0; 
        error = username_illegal;
    }
	
	return this.setvalidstate(tfld_container,flabel,validstate,error);
},

validate_password: function (fld,flabel) {

	var tfld = document.getElementById(fld);
	if(!tfld) return "";
	
	var tfld_container = document.getElementById(fld + '_div');	
		

	
	tfld.value = this.trimstring(tfld.value);   
		
	
    var error = "";
	var validstate = 1;
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (tfld.value == "") {
        validstate = 0; 
		error = flabel + " " + has_not_been_filled;
	} else if ((tfld.value.length < 3) || (tfld.value.length > 15)) {
        error = flabel + " " + wrong_length + "\n";
        validstate = 0; 
    } else if (illegalChars.test(tfld.value)) {
        error = flabel + " " + contains_illegal;
        validstate = 0; 
    }
	
	return this.setvalidstate(tfld_container,flabel,validstate,error);
} 

	
	

	
}
	
	


