////////////////////////////////////////
// NAME : Form Validator
// MADE : Raphael Ambrosin
// DATE : 2011-08-23
// CONTACT : raphael.ambrosin@gmail.com
// GNU / GPL 
////////////////////////////////////////

// Fonction Error
function Error(id){
	$("#"+id).css("background-color","#EBCDCD");
	$("#"+id).css("border-color","#cc0000");
}

// Fonction Valid
function Valid(id){
	$("#"+id).css("background-color","#C7D7C6");
	$("#"+id).css("border-color","#539562");
}

// Verification de longueur de chaine 
function VerifFieldLength(id,mini,maxi){
	if($("#"+id).val().length > mini && $("#"+id).val().length < maxi){Valid(id); return 1 ;}
	else{Error(id); return 0 ;}
}

// Verification de correspondance de chaine
function VerifFieldSame(id,type,value){
	if(type == "same"){
		if($("#"+id).val() == value){Valid(id); return 1 ;}
		else{Error(id); return 0 ;}
	}
	else if(type == "!"){
		if($("#"+id).val() != value){Valid(id); return 1 ;}
		else{Error(id); return 0 ;}
	}
	else if(type == "field"){
		if($("#"+id).val() == $("#"+value).val()){Valid(id); return 1 ;}
		else{Error(id); return 0 ;}
	}
}

// Verification de type de chaine
function VerifFieldType(id,type,long){
	if(type == "alpha"){
		if(isNaN($("#"+id).val())){Valid(id);return 1 ;}
		else{Error(id); return 0 ;}
	}
	else if(type == "num"){
		if(isNaN($("#"+id).val()) || $("#"+id).val().length < long){Error(id); return 0 ;}
		else{Valid(id); return 1 ;}
	}
	else if(type == "email"){
		adresse = $("#"+id).val();
		
		verif_1 = adresse.indexOf("@",1);
		verif_2 = adresse.indexOf(".",1);
		if(verif_1 > -1 && verif_2 > -1){
		
		a = adresse.split("@");
		b = a[1].split(".");
		
		var Adresse = a[0].length;
		var Provider = b[0].length;
		var Ext = b[1].length;
		
		if (Adresse >= 2 && Provider >= 2 && Ext >= 2){Valid(id); return 1 ;}
		else{Error(id); return 0 ;}	
		}
		else{
			Error(id); return 0 ;
		}
	}
	else if(type == "date"){
		
		var sdate = $("#"+id).val().split("-");
		
		if(sdate[1] == "01"){sdate[1] = 0;}
		else if(sdate[1] == "02"){sdate[1] = 1;}
		else if(sdate[1] == "03"){sdate[1] = 2;}
		else if(sdate[1] == "04"){sdate[1] = 3;}
		else if(sdate[1] == "05"){sdate[1] = 4;}
		else if(sdate[1] == "06"){sdate[1] = 5;}
		else if(sdate[1] == "07"){sdate[1] = 6;}
		else if(sdate[1] == "08"){sdate[1] = 7;}
		else if(sdate[1] == "09"){sdate[1] = 8;}
		else if(sdate[1] == "10"){sdate[1] = 9;}
		else if(sdate[1] == "11"){sdate[1] = 10;}
		else if(sdate[1] == "12"){sdate[1] = 11;}
		
		var date = new Date();
		date.setFullYear(sdate[2]);
		date.setMonth(sdate[1]);
		date.setDate(sdate[0]);
		date.setHours(0);
		date.setMinutes(0);
		date.setSeconds(0);
		date.setMilliseconds(0);
		Result = date.getTime();
		
		var date2 = new Date();
		Result2 = date2.getTime();
		
		if(Result2 < Result){
		
  			taste = $("#"+id).val().split("-");
			
				if( (taste[0] > 0 && taste[0] <= 31) && (taste[1] > 0 && taste[1] <= 12) && (taste[2] >= 2011 && taste[2] <= 2100)){
					Valid(id); return 1 ;
				}
				else{
					Error(id); return 0 ;
				}		
		}
		else{
			Error(id); return 0 ;
		}
		
	}
}

// Verification de hauteur de chaine
function VerifFieldValue(id,type,value1,value2){
	if(type == "between"){
		if($("#"+id).val() > value1 && $("#"+id).val() < value2){Valid(id); return 1 ;}
		else{Error(id); return 0 ;}
	}
	else if(type == "<"){
		if($("#"+id).val() < value1){Valid(id); return 1 ;}
		else{Error(id); return 0 ;}
	}
	else if(type == ">"){
		if($("#"+id).val() > value1){Valid(id); return 1 ;}
		else{Error(id); return 0 ;}
	}
	else if(type == ">="){
		if($("#"+id).val() >= value1){Valid(id); return 1 ;}
		else{Error(id); return 0 ;}
	}
	else if(type == "<="){
		if($("#"+id).val() <= value1){Valid(id); return 1 ;}
		else{Error(id); return 0 ;}
	}
}

// Verification checked
function VerifFieldChk(id){
	if($("#"+id).is(':checked')){Valid(id); return 1 ;}
	else{Error(id); return 0 ;}
}

// Fonction Global de Verification de Formulaire
function VerifForm(id,btn){
	i = 0;
	$("#"+id+" :input:not(input[type=button])").each(function(){
        if( $(this).attr("alt") != "" ){
			master = $(this).attr("alt").split('|');
			for(var p= 0; p < master.length; p++){
				result = master[p].split('/');
				action = result[0];
				attrib = result[1].split(',');
				if(action == "length"){
					var res = VerifFieldLength(attrib[0],attrib[1],attrib[2]);
					if(res == 0){i++;}
				}
				else if(action == "same"){
					res = VerifFieldSame(attrib[0],attrib[1],attrib[2]);
					if(res == 0){i++;}
				}
				else if(action == "type"){
					var res = VerifFieldType(attrib[0],attrib[1],attrib[2]);
					if(res == 0){i++;}
				}
				else if(action == "value"){
					var res = VerifFieldValue(attrib[0],attrib[1],attrib[2],attrib[3]);
					if(res == 0){i++;}
				}
				else if(action == "chk"){
					res = VerifFieldChk(attrib[0]);
					if(res == 0){i++;}
				}
			}
		}	
    });	
	if(i == 0){	$("#"+btn).show("fast"); }
	else{ $("#"+btn).hide("fast"); }
}
	
function verifValue(id_object,mess){
	if($("#"+id_object).val() == ""){
		$("#"+id_object).val(mess);
	}
}

function delValue(id_object,mess){
	if($("#"+id_object).val() == mess){
		$("#"+id_object).val("");
	}
}
