///ON LOAD !

$(document).ready( function(){
	
		
		//alert( $("#JquerydynSubHeight2").height())
		$('li a.Portail:first').css({'background': 'url(/theme/ym/img/bg-first-portail.gif) no-repeat left top'});
		
		//Banner rotation
		//$('#BanniereHeader').innerfade({ speed: 'slow', timeout: 12000, type: 'random_start', containerheight: '86px' });
		//$('#BanniereContent').innerfade({ speed: 'slow', timeout: 7000, type: 'random_start', containerheight: '260px' });
					
		$('.reply').click(function (event) {
			event.preventDefault();
			var parentId = $(this).attr('id').split('-');
			if (parentId[1] > 0) {
				$("#ArticleCommentParentId").attr('value', parentId[1]);
				$('#commentForm').remove().insertAfter('#comment-'+parentId[1]);
			}
		});
		
	
	
		$('.increment').click( function (event) {
			event.preventDefault();	
			send='/Documents/increment/'+$(this).attr('filename')+'/'+$(this).attr('id')+'/'+$(this).attr('count');
			$.ajax({
				type: 'POST',
				url: '/Documents/increment/'+$(this).attr('filename')+'/'+$(this).attr('id')+'/'+$(this).attr('count'),
				beforeSend: function(){
				},
					success: function(data){
							$.each(data, function(key, value) {
							});
				},
			});
		});
			
});




/* FUNCTION replace attribute "rel='external'"  BY "target='_blank'" */
function externalLinks() {
if (!document.getElementsByTagName) return;
var anchors = document.getElementsByTagName("a");
for (var i=0; i<anchors.length; i++) {
var anchor = anchors[i];
if (anchor.getAttribute("href") &&
anchor.getAttribute("rel") == "external")
anchor.target = "_blank";
}
}
window.onload = externalLinks;

function f_open_window_max( aURL, aWinName )
{
   var wOpen;
   var sOptions;

   sOptions = 'status=no,menubar=no,scrollbars=yes,resizable=yes,toolbar=no';
   sOptions = sOptions + ',width=' + (screen.availWidth - 10).toString();
   sOptions = sOptions + ',height=' + (screen.availHeight - 122).toString();
   sOptions = sOptions + ',screenX=0,screenY=0,left=0,top=0';

   wOpen = window.open( '', aWinName, sOptions );
   wOpen.location = aURL;
   wOpen.focus();
   wOpen.resizeTo( screen.availWidth/1.2, screen.availHeight/1.2 );
}

/************************Validation de formulaire**************************/

/*****************Function validation formulaire*********************/


/* Valid les chams des qu'on quitte pour un autre champ */
$('#FormsSaveForm input').live('blur', function(){
		ids = $(this).attr('id');
		validAllJQueryFormat(ids)
});
$('#FormsSaveForm select').live('change', function(){
		ids = $(this).attr('id');
		validAllJQueryFormat(ids)
});



$('#ArticleCommentAddForm input').live('blur', function(){
		ids = $(this).attr('id');
		validAllJQueryFormat(ids)
});
$('#ArticleCommentAddForm textarea').live('change', function(){
		ids = $(this).attr('id');
		validAllJQueryFormat(ids)
});



/* Valid les chams du form Nous Joindre sur submit */
function validForm (a, b, c, d, e, f){
	if(validvide(a) == 1 || validvide(b) == 1 || validCourriel(c) == 1 || validconfirmCourriel(d, c) ==  1 || validatePhone() ==  1 || validSelect(e) == 1 || validother(e, f) == 1 || validateSecurity() == 1){
		
		if(validSelect(f) == 1){
			validSelect(f);
			document.getElementById(f).focus();
		}
		
		if(validother(e, f) == 1){
 			validother(e, f);
 			document.getElementById(e).focus();
 		} 



		if(validatePhone() == 1){
			validatePhone();
			document.getElementById(e).focus();
		}
		if(validconfirmCourriel(d, c) == 1){
			validconfirmCourriel(d, c);
			document.getElementById(d).focus();
		}
		if(validCourriel(c) == 1){
			validCourriel(c);
			document.getElementById(c).focus();
		}
		if(validvide(b) == 1){
			validvide(b);
			document.getElementById(b).focus();
		}
		if(validvide(a) == 1){
			validvide(a);
			document.getElementById(a).focus();
		}
		if(validateSecurity() == 1){
			validateSecurity()
		}
		
		return false;
		
	}else{
		return true;
		
	}
}

/* Valid les chams du form Commentaires sur submit */
function validComment (a, b, c){
	if(validvide(a) == 1 || validCourriel(b) == 1 || validvide(c) == 1 ){
		
		if(validvide(c) == 1){
			validvide(c);
			document.getElementById(c).focus();
		}
		if(validCourriel(b) == 1){
			validCourriel(b);
			document.getElementById(b).focus();
		}
		if(validvide(a) == 1){
			validvide(a);
			document.getElementById(a).focus();
		}
		
		return false;
		
	}else{
		return true;
		
	}
}

function validAllJQueryFormat(ids){
		/*Case pour savoir de quel type de champ il sagit*/
		switch (ids) {
 			case 'courriel':
				validCourriel(ids);
 				break;
			case 'confirm' :
				/*Permet d'extraire le id du champ courriel*/
				email = ids.slice(ids.indexOf("_")+1);
				validconfirmCourriel(ids, email);
 				break;
			case 'phone' :
				validatePhone();
				break;
			case 'fax' :
				break;
			case 'heard_by':
				/*Validation du select*/
				validSelect(ids);
				otherbox('Other', ids);
				break;
			case 'comment' :
				validTextArea(ids);
				break;
			case 'recaptcha_response_field' :
				validateSecurity();
				break;
			case 'Other' :
				validother(ids, 'heard_by');
				break;
			
		case 'FormsContactByCourriel' :
				break;
		case 'FormsContactByTelephone' :
				break;
			
			default:
				validvide(ids)
				break;
 
		}
}

function validvide(objet){
	
	a = document.getElementById(objet).id;
	a = a+"_err";
	if(document.getElementById(objet).value == ""){
			document.getElementById(a).innerHTML = "requis";
			document.getElementById(objet).style.borderColor = "#ff0000";
			ERR = 1;
	}else{
		/*if(!noSpace(objet) || !specialcaracter(objet)){
			document.getElementById(a).innerHTML = "Caract&egrave;res non-autoris&eacute;e (<strong>"+displayForbiddenCaracter(objet)+"</strong>)";
			document.getElementById(objet).style.borderColor = "#ff0000";
			ERR = 1;
		}else{*/
			document.getElementById(a).innerHTML = "";
			document.getElementById(objet).style.borderColor = "";
			ERR = 0;
		//}
	}
	return ERR;
}

function validCourriel(original){
		a = document.getElementById(original).id;
		a = a+"_err";
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.getElementById(original).value)){
			document.getElementById(a).innerHTML = "";
			document.getElementById(original).style.borderColor = "";
			ERR = 0;
			
		}else{
		document.getElementById(a).innerHTML = "Courriel invalide";
		document.getElementById(original).style.borderColor = "#ff0000";
		ERR = 1;
			

		}
		if(document.getElementById(original).value.length == 0){
			document.getElementById(a).innerHTML = "requis";
			document.getElementById(original).style.borderColor = "#ff0000";
			ERR = 1;
		}
	return ERR;
}

function validconfirmCourriel(original, first){
		a = document.getElementById(original).id;
		a = a+"_err";
		if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(document.getElementById(original).value)){
			if(document.getElementById(original).value != document.getElementById(first).value && document.getElementById(first).value != ""){
				document.getElementById(a).innerHTML = "Courriel non-identique";
				document.getElementById(original).style.borderColor = "#ff0000";
			ERR = 1
			}else{
				document.getElementById(a).innerHTML = "";
				document.getElementById(original).style.borderColor = "";
				ERR = 0
			}
			
		}else{
		if(document.getElementById(original).value != ""){
			if(document.getElementById(original).value != document.getElementById(first).value){
				document.getElementById(a).innerHTML = "Courriel non-identique";

			}else{
				document.getElementById(a).innerHTML = "Courriel invalide";

			}
		}
		
			document.getElementById(original).style.borderColor = "#ff0000";
			ERR = 1

		}
		if(document.getElementById(original).value.length == 0){
			document.getElementById(a).innerHTML ="requis";
			document.getElementById(original).style.borderColor = "#ff0000";
	
			ERR = 1
		}
	return ERR;	
}

function validNumber(original, conditions){
	a = document.getElementById(original).id;
	a = a+"_err";
	if(document.getElementById(conditions).checked == true){
		if(/^[0-9\s-]*$/.test(document.getElementById(original).value)){
			if(document.getElementById(original).value.length < 12 && document.getElementById(original).value != ""){
				document.getElementById(a).innerHTML = "<br/>num&eacute;ro incomplet";
				document.getElementById(original).style.borderColor = "#ff0000";
		
				ERR = 1;
			}else{
				if(document.getElementById(original).id == "tel" && document.getElementById(original).value == ""){
					document.getElementById(a).innerHTML = "Requis";
					document.getElementById(original).style.borderColor = "#ff0000";
					ERR = 1;
				}else{
					document.getElementById(a).innerHTML = "";
					document.getElementById(original).style.borderColor = "";
					ERR = 0;
				}
			}
			
		}else{
			document.getElementById(a).innerHTML = "<br/>num&eacute;ro incorrecte";
			document.getElementById(original).style.borderColor = "#ff0000";
			
			ERR = 1;
			
		}
	}else{
		ERR = 0;
	}
	return ERR;
}

function validNotRequiredField(objet){
	a = document.getElementById(objet).id;
	a = a+"_err";
	if(!specialcaracter3(objet)){
		document.getElementById(a).innerHTML = "Caract&egrave;res non-autoris&eacute;e (<strong>"+displayForbiddenCaracter(objet)+"</strong>)";
		document.getElementById(objet).style.borderColor = "#ff0000";
		ERR = 1;
	}else{
		document.getElementById(a).innerHTML = "";
		document.getElementById(objet).style.borderColor = "";
		ERR = 0;
	}
	return ERR;
}

function validTextArea(objet){
	a = document.getElementById(objet).id;
	a = a+"_err";
	if(!specialcaracter4(objet)){
		document.getElementById(a).innerHTML = "Caract&egrave;res non-autoris&eacute;e (<strong>"+displayForbiddenCaracter(objet)+"</strong>)";
		document.getElementById(objet).style.borderColor = "#ff0000";
		ERR = 1;
	}else{
		document.getElementById(a).innerHTML = "";
		document.getElementById(objet).style.borderColor = "";
		ERR = 0;
	}
	return ERR;	
}



function noSpace(original){
		if(/^\s*$/.test(document.getElementById(original).value)){
			return false;
		}else{
			return true;
		}
	}
function specialcaracter(original){
	if(/^[a-zA-Z\-]*$/.test(document.getElementById(original).value)){
		return true;
	}else{
		return false;
	}
}

function specialcaracter2(original){
	if(/^[a-zA-Z\s-.]*$/.test(document.getElementById(original).value)){
		return true;
	}else{
		return false;
	}
}

function specialcaracter3(original){
	//Cette fonction valide si le champ contient seulement des accents, des symboles (!"#?$%&*) et des espaces
	//dans les champs non obligatoire
	if(/^[\w\x80-\xFF\s-!?.,]*$/.test(document.getElementById(original).value)){
		return true;
	}else{
		
		//alert(valeura)
		return false;
	}
	
}
function specialcaracter4(original){
	//Cette fonction valide si le champ contient seulement des accents, des symboles (!"#?$%&*) et des espaces
	//dans le textarea
	if(/^[\w\x80-\xFF\s-!?.,]*$/.test($('#'+original).val())){
		return true;
	}else{
		return false;
	}
}

function displayForbiddenCaracter(objet){
		valeura = "";
		for (i = 0; i < document.getElementById(objet).value.length; ++i) {
			if (valeura.length >= 1){
				valeura += " "+document.getElementById(objet).value.charAt(i).replace(/^[\w\x80-\xFF\s-!?.,]*$/, "");
			}else{
				valeura += document.getElementById(objet).value.charAt(i).replace(/^[\w\x80-\xFF\s-!?.,]*$/, "");
			}
		}
		return valeura;
}


function validSelect(original){
		a = document.getElementById(original).id;
		a = a+"_err";
	if(document.getElementById(original).value == "Choisissez"){
		document.getElementById(a).innerHTML = "Requis";
		document.getElementById(original).style.borderColor = "#ff0000";
		ERR = 1;
	}else{
		document.getElementById(a).innerHTML = "";
		document.getElementById(original).style.borderColor = "";
		ERR = 0;
	}
	return ERR;
}

function validother(objet, autre){

 a = document.getElementById(autre).id;
 a = a+"_err";

 if(document.getElementById(autre).value == "autre"){
 	if(document.getElementById(objet).value == ""){
 		document.getElementById(a).innerHTML = "requis";
 		document.getElementById(objet).style.borderColor = "#ff0000";
 		ERR = 1;
 	}else{
 		/*if(!noSpace(objet) || !specialcaracter2(objet) ){
 		document.getElementById(a).innerHTML = "Caractères non-autoris&eacute;e (<strong>"+displayForbiddenCaracter(objet)+"</strong>)";
 		document.getElementById(objet).style.borderColor = "#ff0000";
 		ERR = 1;
 		}else{*/
 			document.getElementById(a).innerHTML = "";
 			document.getElementById(objet).style.borderColor = "";
 			ERR = 0;
 		//}
 	}
 }else{
 	ERR = 0;
 }
 	return ERR;
} 

function otherbox(original, objet){
	if(document.getElementById(objet).value == "autre"){
		document.getElementById(original).style.visibility ="visible";
		document.getElementById('Other2').style.visibility ="visible";
	}else{
		document.getElementById(original).style.visibility ="hidden";
		document.getElementById('Other2').style.visibility ="hidden";
	}
}



function validateSecurity(){
 Invalide = true;
 strSecurity = document.getElementById("recaptcha_response_field")
 if(strSecurity.value == ''){
 	strSecurity.style.border = '1px solid #dd7627';
 	//document.getElementById('strSecurityError').style.display = 'inline';
 	document.getElementById('strSecurityError').innerHTML = '(requis)';
 	Invalide = false;
 }
 if(Invalide != false){
 	ERR = 0;
 }else{
	 ERR = 1;
 }
 	return ERR;
} 



function validatePhone()
{   		
Invalide = true;
Focus = "";
		strPhone = document.getElementById('phone');
		document.getElementById('phone_err').style.display = 'none';
		strPhone.style.border = '';
   		if(strPhone.value == '') 
   		{ 
   		    strPhone.style.border = '1px solid #dd7627';
   		    document.getElementById('phone_err').style.display = 'inline';
   		    document.getElementById('phone_err').innerHTML = '(requis)'; 
   		    ERR = 1;
   		}
		if(checkInternationalPhone(strPhone.value)==false) 
   		{ 
   		    strPhone.style.border = '1px solid #dd7627';
   		    document.getElementById('phone_err').style.display = 'inline';
   		    document.getElementById('phone_err').innerHTML = '(invalide)'; 
			ERR = 1;   		
		}

	if(ERR != 1)
	{
		ERR=0;
	}
	return ERR;
}
		/*  PHONE VALIDATION*/

var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

/********************************************************************/

