$(document).ready(function(){
	//indicateur positionné en haut à droite de la page HTML
	//lorsqu'une requete ajax est en cours, l'indicateur apparait (signalant un chargement)
	//puis disparait une fois la requete ajax terminée
	$('#indicator').ajaxStart(function(){
		$(this).show();
	});
	$('#indicator').ajaxStop(function(){
		$(this).hide();
	});

	$('.showHistoRelance').click(function(){
		$('.histo_relance').SlideToggleDown(10);
	});
	$('.showHistoResearch').click(function(){
		$('.histo_gene').SlideToggleDown(10);
	});
	$('.showHistoProp').click(function(){
		$('.histo_prop').SlideToggleDown(10);
	});

	$('#check_all_partners').click(function(){
		if (this.checked) {
			$('.partner_list').each(function(){
				this.checked = true;
			});
		} else {
			$('.partner_list').each(function(){
				this.checked = false;
			});
		}
	});
});

/**
 *
 * @access public
 * @return void
 **/
function changeStatus(element){
	var url = "http://intranet.waterproof.fr:10515/~lpottier/symfony_projects/A2F_v1/version-1.0/website/index.php/facture/";
	var name = element.id;
	var id = name.substring(8, name.length);

	var options = {
		url: url+"changeStatus",
		type: "POST",
		data: "id="+element.value+"&facture_id="+id
  	};
  	jQuery.ajax(options);

}

/**
 *
 * @access public
 * @return void
 **/
function changePayMode(facture_id, element_value){
	var url = "http://intranet.waterproof.fr:10515/~lpottier/symfony_projects/A2F_v1/version-1.0/website/index.php/facture/";

	var options = {
		url: url+"changePayMode",
		type: "POST",
		data: "pay_mode_id="+element_value+"&facture_id="+facture_id
  	};
  	jQuery.ajax(options);

}

function CountLeft(srcFieldName, maxCount) {
    if(maxCount > 0){
        var txtWidget = document.getElementById(srcFieldName);
        if(txtWidget.value.length > maxCount){
             txtWidget.value = txtWidget.value.substring(0, maxCount);
             return false;
        }
        return true;
    }
}

/**
 *
 * @access public
 * @return void
 **/
function checkAllDeptPartner(region_id){
	if (region_id == 1) {
		$('.region_1').each(function(){
			if (this.checked) {
				for (var i = 2; i < 120; i++) {
					$('.dept_' + i).each(function(){
						this.checked = true;
					});
					$('.region_' + i).each(function(){
						this.checked = true;
					});
				}
			}else{
				for (var i = 2; i < 120; i++) {
					$('.dept_' + i).each(function(){
						this.checked = false;
					});
					$('.region_' + i).each(function(){
						this.checked = false;
					});
				}
			}
		});
	} else {
		$('.region_' + region_id).each(function(){
			if (this.checked) {
				$('.dept_' + region_id).each(function(){
					this.checked = true;
				});
			} else {
				$('.dept_' + region_id).each(function(){
					this.checked = false;
				});
			}
		});
	}
}

/**
 *
 * @access public
 * @return void
 **/
function changeCommercialTextSame(new_text){
	var element = document.getElementById('commercial_text_same');
	element.value = new_text;
}

/**
 *
 * @access public
 * @return void
 **/
function changeCommercialTextNew(new_text){
	var element = document.getElementById('commercial_text_new');
	element.value = new_text;
}

/**
 *Ouverture du comparateur de tableau de garanties en popup
 *
 *
**/
function ouvreFenetre(userId, rsId){
	var url = "../../index.php/product/compareProducts?user_id=" + userId + "&rs_id=" + rsId;
	var ids = new Array();
	var nb = 0;
	var request = url;

	$('.tableToCompare').each(function(){
		if (this.checked) {
			ids[nb] = this.value;
			nb++;
		}
	});

	if (nb <= 1) {
		alert('Vous devez séléctionner au moins deux produits pour pouvoir en faire une comparaison !!');
	} else {
		for (var i=0; i<nb; i++){
			request += '&tableToCompare['+i+']='+ids[i];
		}
		window.open(request,"Comparatif","toolbar=no,resizable=yes,scrollbars=yes,width=700,height=800,screenX=100,screenY=100,top=200,left=250");
	}
}

/**
 *Ouverture du comparateur de tableau de garanties en popup
 *
 *
**/
function openWindow(dest, param_name, prod_id, title){
	var url = "../../index.php/comparateur/"+dest+"?"+param_name+"="+prod_id;

	if (title == 'devis') {
		window.open(url,"","toolbar=no,resizable=no,scrollbars=no,width=600,height=500,screenX=100,screenY=100,top=10,left=250");
	} else {
		if (title == 'appel') {
			window.open(url,"","toolbar=no,resizable=no,scrollbars=no,width=600,height=500,screenX=100,screenY=100,top=10,left=250");
		} else {
			if (title == 'guaranty') {
				window.open(url,"","toolbar=no,resizable=yes,scrollbars=yes,width=600,height=800,screenX=100,screenY=100,top=200,left=250");
			}
		}
	}
}