var langue ="";

$(document).ready(function(){
	//si 'others' coché, champs preciser visible
	if ( $('#others') != null){
  		var checked = $('#others').attr('checked');
		if(checked){
			$('.preciser').each(function(index) {
    			$(this).css('display','block');
  			});
		} 
		else $('#preciser').each(function(index) {
    		$(this).css('display','none');
  		});
  	}
  	//idem pour strat
  	if ( $('#strat') != null){
  		var checked = $('#strat').attr('checked');
		if(checked){
			$('.preciser_l').each(function(index) {
    			$(this).css('display','block');
  			});
		} 
		else $('#preciser_l').each(function(index) {
    		$(this).css('display','none');
  		});
  	}
  	var value = $('#erreur').val();
  	var todeco = $('#todeco').val();
  	if(value == "yes"){

  	$('#erreur').val("");
		if(todeco =="no")
		show_connexion_error("no");
		else show_connexion_error("yes");
	}

	if (document.forms['formContr'] != null){
		if(isiPhone()){
    		window.scrollTo(0, 1);
		}
		else {
			document.forms['formContr'].elements['UserSai'].focus();
		}
	}
	$('#oilRap').jqm({overlay: 20, modal: true, trigger: false });
	$('#ImpUE').jqm({overlay: 20, modal: true, trigger: false });
	
	$('#oilRap div.close').click(function() { $('#oilRap').jqmHide(); });
	$('#ImpUE div.close').click(function() { 
	$('#ImpUE').jqmHide();
	
	if(detectIE6()==true){
		$("select[name=codeGrPOrig]").css('visibility','visible');
		$("select[name=codep]").css('visibility','visible');
		$("select[name=codeFlux]").css('visibility','visible');
		$("select[name=codeGrPDest]").css('visibility','visible');
		$("select[name=codeTypeData]").css('visibility','visible');
		$("select[name=codePro]").css('visibility','visible');
		$("select[name=campagne]").css('visibility','visible');
	}
	});
	
	$('#oilRap input.yes').click(function() { 
  		if($('#oilRap input.none').val() !=""){
  			callajax('oilRap');
			$('#oilRap').jqmHide();
		}
  	});
  	$('#ImpUE input.yes').click(function() { 
  		if($('#ImpUE input.none').val() !=""){
  			callajax('ImpUE');
			$('#ImpUE').jqmHide();
		}
  	});
	
	if ($('label[for="any"]') != null){
   	 	num1 = random_between(0,5);
   		num2 = random_between(5,10);
   		var texte =num1+" + "+num2;
  		$('label[for="any"]').append(texte+'&nbsp;?&nbsp;<font color="#67C15C">*</font>&nbsp;') ;	
	}

});

//verification formulaire modification mot de passe + traitement + retour user
function checkPassUpdate(){
	
	var oldvalue = $('#oldmdp').val();
	var newvalue1 = $('#newmdp1').val();
	var newvalue2 = $('#newmdp2').val();
	var langage = $('#langage').val();
	
	if(oldvalue == '') {
		show_missing(liboldmdp);
		$('#oldmdp').focus();
		return;
	}
	if(newvalue1 == '') {
		show_missing(libnewmdp);
		$('#newmdp').focus();
		return;
	}
	if(newvalue1 != '') {
		var isok =numerique('mdp',newvalue1);
		if(isok==false){
			show_missing(libspecif_mdp);
			$('#newmdp1').focus();
			return;
		}
		if(newvalue1.length >8){
			show_missing(libspecif_mdp);
			$('#newmdp1').focus();
			return;
		}
	}
	if(newvalue2 == '') {
		show_missing(libconfmdp);
		$('#newmdp2').focus();
		return;
	}
	if(newvalue2 != '') {
		var isok =numerique('mdp',newvalue2);
		if(isok==false){
			show_missing(libconfmdp);
			$('#newmdp2').focus();
			return;
		}
	}
	if( (newvalue2 != '') && (newvalue1 != '') ){ 
		if(newvalue2 != newvalue1 ){ 
			show_missing(liberrmdp);
			return;
		}
		if(newvalue2.length >8){
			show_missing(libspecif_mdp);
			$('#newmdp2').focus();
			return;
		}
	}
	
	$.ajax({
		url: "../phpcommun/upd_pass.php",
		type:'post',
		data:'oldmdp='+oldvalue+'&newmdp1='+newvalue1+'&langue='+langage,
		context: document.body,
		success: function(msg){
 			var cas = msg.substring(0,1);
 			var message = msg.substring(1,msg.length);
 			var color = (cas==0)? 'red':'#67C15C'; 
 			$('#msg_after_update').html(message);
			$('#msg_after_update').css('color',color);
 			$('#msg_after_update').show();
 			if(cas == 0) $('#oldmdp').focus();
		}
	});
}

/**** Demande essai gratuit ****/

//affichage champs saisie précision lors click 'autres' dans les intérêts de l'utilisateur pour tallage 
function show(cas){
	switch(cas){
		case '1':
			var show = ( $('.preciser').css('display') == 'block' )? 'none' : 'block';
			$('.preciser').each(function(index) {
    			$(this).css('display',show);
  			});
  			break;
  		case '2':
  			var show = ( $('.preciser_l').css('display') == 'block' )? 'none' : 'block';
			$('.preciser_l').each(function(index) {
    			$(this).css('display',show);
  			});
  			break;
  		}
  		
}

//verification formulaire demande essai gratuit
function verifForm(){
	var stratchecked = $('#strat').attr('checked');
	var webchecked = $('#web').attr('checked');
	var otherschecked = $('#others').attr('checked');
	var others_prec = $('#others_prec').val();
	var comment = $('#comment').val();

	if($('#FName').val() == ''){
		show_missing(libfname);
		$('#FName').focus();
		return;
	}
	if( $('#LName').val() == ''){
		show_missing(liblname);
		$('#LName').focus();
		return;
	}
	if($('#Phone').val() == ''){
		show_missing(libphone);
		$('#Phone').focus();
		return;
	}
	if($('#AdrEmail').val() == ''){
		show_missing(libmail);
		$('#AdrEmail').focus();
		return;
	}
	if($('#AdrEmail').val() != '') {
		var isok =numerique('mail',$('#AdrEmail').val());
		if(isok==false){
			show_missing(liberrmail);
			$('#AdrEmail').focus();
			return;
		}
	}

	if(otherschecked){
		if($('#others_prec').val() == ''){
			show_missing(libinterest);
			$('#others_prec').focus();
			return;
		}
	}
	
	if(stratchecked == null)stratchecked = 'false';
	if(webchecked == null)webchecked = 'false';
	if(otherschecked == null)otherschecked = 'false';

	if($('#any').val() == ''){
		show_missing(libcode);
		$('#any').focus();
		return;
	}
	else {
    	if($('#any').val() == (num1+num2)) {
            $('#essaigratuit').submit();
        }
        else {
           	show_missing(liberrcode);
			$('#any').focus();
			return;
     	}
	}


}


function teleSGFree() {
	document.forms["formDownFreeSG"].elements["btnSbm"].disabled="true";
	document.formDownFreeSG.submit();
}

/**** Publication auto twitter ****/

function tweet(){
	var texte = $('#content').val();
	$.ajax({
		url: "../twit/tweet_functions.php",
		type:'post',
		data:'tweet_text='+texte,
		success: function(msg){
 			$('#sent').html(msg);
		}
	});
}

/**** Echanges UE/tiers ****/

function change_label(){
	var flux = $('#codeFlux option:selected').val();
	if(flux =='I')$('#label_flux').html('Origine&nbsp;');
	else $('#label_flux').html('Destination');
}

/**** UTILITAIRES ****/



function include(fileName){
    document.write("<script type='text/javascript' src='"+fileName+"'></script>" );
}


function numerique(cas,chaine) {
	var exp ='';
	switch(cas){
		case 'mdp':
			 exp=new RegExp("^[a-zA-Z0-9_-]{8,8}$","g");
			break;
		case 'mail':
			exp =RegExp("^([a-zA-Z0-9_-])+([.]?[a-zA-Z0-9_-]{1,})*@([a-zA-Z0-9-_]{2,}[.])+[a-zA-Z]{2,3}$");
			break
	}
    return exp.test(chaine);
  }


/**** RETOURS USER ****/

function show_missing(text){
	 jError(text,{
		  autoHide : true, 
		  clickOverlay : true, 
		  MinWidth : 150,
		  TimeShown : 3000,
		  ShowTimeEffect : 200,
		  HideTimeEffect : 200,
		  LongTrip :20,
		  HorizontalPosition : 'center',
		  VerticalPosition : 'center',
		  ShowOverlay : true,
   		  ColorOverlay : '#000',
		  OpacityOverlay : 0.2
		});
}

function show_ok(text){
	 jSuccess(text,{
		  autoHide : false, 
		  clickOverlay : false,
		  MinWidth : 150,
		  TimeShown : 3000,
		  ShowTimeEffect : 200,
		  HideTimeEffect : 200,
		  LongTrip :20,
		  HorizontalPosition : 'center',
		  VerticalPosition : 'center',
		  ShowOverlay : true,
   		  ColorOverlay : '#000'
	});
}

function show_more(id){
	$('#'+id+'child').slideDown();
}

function hide_child(id){
	$('#'+id+'child').slideUp();
}

function display(id){
	$("#liste_liens > div").slideUp();
	$('#'+id+'child').slideDown();
}

//specif retours user lors pb multiconnexion
function show_connexion_error(cas){
	switch(cas){
		case 'no':
			jError(libdecoauto,{
				autoHide : false,
		  		clickOverlay : false, 
		  		MinWidth : 150,
		  		TimeShown : 3000,
		  		ShowTimeEffect : 200,
		  		HideTimeEffect : 200,
		  		LongTrip :20,
		  		HorizontalPosition : 'center',
		  		VerticalPosition : 'top',
		  		ShowOverlay : true,
   		  		ColorOverlay : '#000',
		  		OpacityOverlay : 0.2
				});
			break;
		case "yes":
			jError(libdecoauto,{
		  		autoHide : false,
		  		clickOverlay : false, 
		  		MinWidth : 150,
		  		TimeShown : 3000,
		  		ShowTimeEffect : 200,
		  		HideTimeEffect : 200,
		  		LongTrip :20,
		  		HorizontalPosition : 'center',
		  		VerticalPosition : 'top',
		  		ShowOverlay : true,
   		  		ColorOverlay : '#000',
		 		OpacityOverlay : 0.2,
		  		onClosed : function(){ // added in v2.0
					window.location.replace('http://www.strategie-grains.com/Deconx22.php');
		  		}
		  	});
			break;
		
	}
}

function isiPhone(){
    return (  (navigator.platform.indexOf("iPhone") != -1)  );
}

function controleUser() {
	if(document.forms["formContr"].elements["UserSai"] !='')document.forms["formContr"].elements["UserSai"].value = document.forms["formContr"].elements["UserSai"].value.toUpperCase();
}

function validForm() {
	controleUser();
	document.forms["formContr"].submit();
}

function shownext(newfonct){
    $('#'+newfonct+'').jqmShow();
	if(newfonct=="ImpUE" && detectIE6()==true){
		$("select[name=codeGrPOrig]").css('visibility','hidden');
		$("select[name=codep]").css('visibility','hidden');
		$("select[name=codeFlux]").css('visibility','hidden');
		$("select[name=codeGrPDest]").css('visibility','hidden');
		$("select[name=codeTypeData]").css('visibility','hidden');
		$("select[name=codePro]").css('visibility','hidden');
		$("select[name=campagne]").css('visibility','hidden');
	}
}

function callajax(cas){
	var mailto = $('#'+cas+' input.none').val();
	$.ajax({
		url: "../phpcommun/traitements.php",
		type:'post',
		data:'mailtosend='+mailto+'&langue='+$('#langue').val()+'&fonct='+cas,
		context: document.body
	});
}

function detectIE6(){
	var IE6 = false
	var strChUserAgent = navigator.userAgent;
	var intSplitStart = strChUserAgent.indexOf("(",0);
	var intSplitEnd = strChUserAgent.indexOf(")",0);
	var strChMid = strChUserAgent.substring(intSplitStart, intSplitEnd);
	if(strChMid.indexOf("MSIE 6") != -1) IE6 = true;
	return IE6;
}
function random_between(to, from) {
    return Math.floor(Math.random() * (to - from + 1) + from);
}
