// © ATELIER 33 / ALL RIGHTS RESERVED / 2008 / http://www.atelier33.com / \\
//                                          APP LOCPRO1                                        \\
//=============================================================================================\\
//GLOBAL
var GIF0=new Array(); var GIP0=new Array();

//----------------------------
function Locpro01Init(DaForm, DaPart)
{
	var Dis=eval(DaForm); var i=0; var msg=""; var GIFSel=0; GIPSel=0;
	
	//COMMON
	GetDivTxt((Dis.VNMMP_COEFF.value*100),"OVAN_COEFF");
	GetDivTxt((Dis.PH_COEFF.value*1000),"OPH_COEFF");
	GetDivTxt(Monify(Dis.CPU.value),"CPU");
	if(isNaN(Dis.Options__Pertes_honoraires_montant.value) || Dis.Options__Pertes_honoraires_montant.value==""){Dis.Options__Pertes_honoraires_montant.value=0;}
	if(isNaN(Dis.Options__Bris_de_materiel_montant.value) || Dis.Options__Bris_de_materiel_montant.value==""){Dis.Options__Bris_de_materiel_montant.value=0;}
	
	//GIF	
	GIFSel=Dis.Options__Informatique_fixe_montant.options[Dis.Options__Informatique_fixe_montant.selectedIndex].value;
	for(i=0; i<Dis.Options__Informatique_fixe_montant.options.length; i++){GIF0[i]=Dis.Options__Informatique_fixe_montant.options[i];}
	for(i=0; i<Dis.Options__Informatique_fixe_montant.options.length; i++){Dis.Options__Informatique_fixe_montant.options[i]=null; i--;} 
	var no=new Option(); no.value=0; no.text=""; Dis.Options__Informatique_fixe_montant.options[0]=no;	
	
	//GIP
	GIPSel=Dis.Options__Informatique_portable_montant.options[Dis.Options__Informatique_portable_montant.selectedIndex].value;	
	for(i=0; i<Dis.Options__Informatique_portable_montant.options.length; i++){GIP0[i]=Dis.Options__Informatique_portable_montant.options[i];}
	for(i=0; i<Dis.Options__Informatique_portable_montant.options.length; i++){Dis.Options__Informatique_portable_montant.options[i]=null; i--;}  
	var no=new Option(); no.value=0; no.text=""; Dis.Options__Informatique_portable_montant.options[0]=no;
		
	
	switch (DaPart)
	{
	case "EDIT"	:
		GI_Build(Dis.Options__Informatique_fixe_montant, GIF0, Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.substring(Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.length-3, Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.length), GIFSel);
		GI_Build(Dis.Options__Informatique_portable_montant, GIP0, Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.substring(Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.length-3, Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.length), GIPSel);
		Locpro01Calcul(DaForm);
		break;
		
	default : 
		Locpro01Calcul(DaForm);	
		GetVis("OPT_TAB", "none");
		//GetVis("GAR_TAB", "none");
		break;
	}	
}
//----------------------------
function GI_Build(DaBox, DaArray, IsFran, DaSel)
{
	var i=0; var j=1; var msg=IsFran+"\n";
	
	var DaVal=0;
	if (DaSel=="")	{DaVal=DaBox.options[DaBox.selectedIndex].value;}
	else			{DaVal=DaSel}	
	//DaVal=DaBox.options[DaBox.selectedIndex].value; //alert(DaVal)
	
	//alert(IsFran+" |"+DaSel);
	//for(i=0; i<DaArray.length; i++){msg+=DaArray[i].value+"\n";}
	//alert(msg);	
	
	//ADD	
	for(i=0; i<DaBox.options.length; i++){DaBox.options[i]=null; i--;}
	var no=new Option(); no.value=0; no.text=""; DaBox.options[0]=no;	
	for(i=0; i<DaArray.length; i++)
		{
     	if(DaArray[i].value.substring(DaArray[i].value.length-3, DaArray[i].value.length)==IsFran)  
	 	{var no=new Option(); no.value=DaArray[i].value; no.text=DaArray[i].text; DaBox.options[j]=no; j++;}
		}

		
	//OLD SELECTED VAL
	for(i=0; i<DaBox.length; i++)
		{
     	if(DaBox.options[i].value==DaVal)	{DaBox.options[i].selected=true;}
		else								{DaBox.options[i].selected=false;}
		}

		
}
//----------------------------
function GI_Manage(DaForm)
{
	var Dis=eval(DaForm); var GIFSel=0; var GIPSel=0;
	
	GIFSel=Dis.Options__Informatique_fixe_montant.options[Dis.Options__Informatique_fixe_montant.selectedIndex].value;
	GIPSel=Dis.Options__Informatique_portable_montant.options[Dis.Options__Informatique_portable_montant.selectedIndex].value;
	 
	GI_Build(Dis.Options__Informatique_fixe_montant, GIF0, Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.substring(Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.length-3, Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.length), GIFSel);
	GI_Build(Dis.Options__Informatique_portable_montant, GIP0, Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.substring(Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.length-3, Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value.length), GIPSel);
	Locpro01Calcul(DaForm);	
}
//----------------------------
function Locpro01Calcul(DaForm)
{
	var Dis=eval(DaForm); var DaPrice=""; var bFlag=true; var DMsg=""
	var iTotGF=0; var iTotOVAN=0; var iOPH_Coeff=0; var iTotOPH=0; var iTotOBDM=0; var iTotGIF=0; var iTotGIP=0; 
	var iTotCPU=0; 
	var iDays=0; iDaysCoeff=1;
	var iTotal=0; 
	
	
	
	if(check_empty(Dis.Date_prise_effet.value) && check_date(Dis.Date_prise_effet.value))
		{
		if(format_date1(Dis.Date_prise_effet.value, '/')<format_date1(Dis.Date_current.value, '/'))
			{bFlag=false; alert("La date de prise d'effet doit être supérieure ou égale au "+Dis.Date_current.value); Dis.Date_prise_effet.focus();}
		
		if(format_date1(Dis.Date_prise_effet.value, '/')>=format_date1(Dis.Date_echeance.value, '/'))
			{bFlag=false; alert("La date de prise d'effet doit être strictement inférieure à la date d'échéance ("+Dis.Date_echeance.value+")"); Dis.Date_prise_effet.focus();}

		if(bFlag)
			{
			iDays=DayDiff(Dis.Date_echeance.value, Dis.Date_prise_effet.value, "/");		
			if(iDays<0)	{iDaysCoeff=1}
			else		{iDaysCoeff=iDays/365}	
			}	
		}

	DMsg="DayDiff="+iDays+" | iDaysCoeff="+iDaysCoeff+"<br>";
	DMsg+="Date_effet="+format_date1(Dis.Date_prise_effet.value, '/')+" | Date_echeance="+format_date1(Dis.Date_echeance.value, '/')+" | Date_current="+format_date1(Dis.Date_current.value, '/');
	//GetDivTxt(DMsg,"DATE_COMMENT");	
	
	//GF
	if(bFlag && Dis.Garanties__Formule.selectedIndex!=0)
		{
		GetVis("OPT_TAB", "");
		iTotGF=parseFloat(eval("Dis."+Dis.Garanties__Formule.options[Dis.Garanties__Formule.selectedIndex].value+".value"));
		iTotGF=iTotGF*iDaysCoeff;
		}		
	else
		{iTotGF=0;}
	iTotGF0=Roundit(iTotGF, 2);
	iTotGF1=GeneRound(iTotGF, 1000)
	//GetDivTxt(iTotGF+"<br>"+iTotGF0+"<br>"+iTotGF1,"GF_PRIME");
	GetDivTxt(iTotGF1,"GF_PRIME");
		
	//OVAN
	if(Dis.Options__valeur_a_neuf.selectedIndex==2 && Dis.Garanties__Formule.selectedIndex!=0)
		{
		iTotOVAN=parseFloat(iTotGF*parseFloat(Dis.VNMMP_COEFF.value));
		iTotOVAN=iTotOVAN //*iDaysCoeff
		}
	else
		{iTotOVAN=0;}
	iTotOVAN0=Roundit(iTotOVAN, 2);
	iTotOVAN1=GeneRound(iTotOVAN, 1000);
	//GetDivTxt(iTotOVAN+"<br>"+iTotOVAN0+"<br>"+iTotOVAN1,"OVAN_PRIME");
	GetDivTxt(iTotOVAN1,"OVAN_PRIME");
	
	//OPH
	if(!isNaN(Dis.Options__Pertes_honoraires_montant.value) && parseFloat(Dis.Options__Pertes_honoraires_montant.value)>0 && Dis.Garanties__Formule.selectedIndex!=0)
		{
		iTotOPH=parseFloat(Dis.Options__Pertes_honoraires_montant.value*Dis.PH_COEFF.value);
		iTotOPH=iTotOPH*iDaysCoeff
		}
	else
		{iTotOPH=0;}
	iTotOPH0=Roundit(iTotOPH, 2);
	iTotOPH1=GeneRound(iTotOPH, 1000);
	//GetDivTxt(iTotOPH+"<br>"+iTotOPH0+"<br>"+iTotOPH1,"OPH_PRIME");
	GetDivTxt(iTotOPH1,"OPH_PRIME");
	
	//BDM
	if(!isNaN(Dis.Options__Bris_de_materiel_montant.value) && parseFloat(Dis.Options__Bris_de_materiel_montant.value)>0 && Dis.Garanties__Formule.selectedIndex!=0)
		{
		if(parseFloat(Dis.Options__Bris_de_materiel_montant.value)<15245){iOPH_Coeff=Dis.BDM_COEFF1.value}
		if(parseFloat(Dis.Options__Bris_de_materiel_montant.value)>=15245 && parseFloat(Dis.Options__Bris_de_materiel_montant.value)<=30490){iOPH_Coeff=Dis.BDM_COEFF2.value}
		if(parseFloat(Dis.Options__Bris_de_materiel_montant.value)>30490 && parseFloat(Dis.Options__Bris_de_materiel_montant.value)<=53357){iOPH_Coeff=Dis.BDM_COEFF3.value}
		if(parseFloat(Dis.Options__Bris_de_materiel_montant.value)>53357){iOPH_Coeff=0; alert("Le montant maximum est de 53 357 EUR");}
		iTotOBDM=parseFloat(Dis.Options__Bris_de_materiel_montant.value*iOPH_Coeff);
		iTotOBDM=iTotOBDM*iDaysCoeff
		}
	else
		{iTotOBDM=0;}	
	GetDivTxt(iOPH_Coeff*1000,"OBDM_COEFF");
	iTotOBDM0=Roundit(iTotOBDM, 2);
	iTotOBDM1=GeneRound(iTotOBDM, 1000);
	//GetDivTxt(iTotOBDM+"<br>"+iTotOBDM0+"<br>"+iTotOBDM1,"OBDM_PRIME");
	GetDivTxt(iTotOBDM1,"OBDM_PRIME");
	
	//GIF
	if(Dis.Options__Informatique_fixe_montant.selectedIndex!=0 && Dis.Garanties__Formule.selectedIndex!=0)
		{
		iTotGIF=parseFloat(eval("Dis."+Dis.Options__Informatique_fixe_montant.options[Dis.Options__Informatique_fixe_montant.selectedIndex].value+".value"));
		iTotGIF=iTotGIF*iDaysCoeff
		}
	else
		{iTotGIF=0;}
	iTotGIF0=Roundit(iTotGIF, 2);
	iTotGIF1=GeneRound(iTotGIF, 1000);
	//GetDivTxt(iTotGIF+"<br>"+iTotGIF0+"<br>"+iTotGIF1,"OIF_PRIME");
	GetDivTxt(iTotGIF1,"OIF_PRIME");
	
	//GIP
	if(Dis.Options__Informatique_portable_montant.selectedIndex!=0 && Dis.Garanties__Formule.selectedIndex!=0)
		{
		iTotGIP=parseFloat(eval("Dis."+Dis.Options__Informatique_portable_montant.options[Dis.Options__Informatique_portable_montant.selectedIndex].value+".value"));
		iTotGIP=iTotGIP*iDaysCoeff
		}
	else
		{iTotGIP=0;}
	iTotGIP0=Roundit(iTotGIP, 2);
	iTotGIP1=GeneRound(iTotGIP, 1000);
	//GetDivTxt(iTotGIP+"<br>"+iTotGIP0+"<br>"+iTotGIP1,"OIP_PRIME");
	GetDivTxt(iTotGIP1,"OIP_PRIME");
	
	
	//CPU
	if(Dis.Garanties__Formule.selectedIndex!=0)
		{iTotCPU=parseFloat(Dis.CPU.value);}
	else
		{iTotCPU=0;}
	//iTotCPU=Roundit(iTotCPU, 2);
	iTotCPU=GeneRound(iTotCPU, 1000);
	
	//TOTAL
	//iTotal=((iTotGF*100)+(iTotOVAN*100)+(iTotOPH*100)+(iTotOBDM*100)+(iTotGIF*100)+(iTotGIP*100)+(iTotCPU*100))/100;
	iTotal=((iTotGF1*100)+(iTotOVAN1*100)+(iTotOPH1*100)+(iTotOBDM1*100)+(iTotGIF1*100)+(iTotGIP1*100)+(iTotCPU*100))/100;
	GetDivTxt(iTotal,"TOTAL_PRIME");
}
//----------------------------
function Locpro01Edit(DaForm){var Dis=eval(DaForm); Dis.act.value="EDIT"; Dis.method="post"; Dis.submit();}
//----------------------------
function Locpro01Recap(DaForm){var Dis=eval(DaForm); Dis.act.value="RECAP"; Dis.method="post"; Dis.submit();}
//----------------------------
function Locpro01Record(DaForm){var Dis=eval(DaForm); Dis.act.value="RECORD"; Dis.method="post"; Dis.submit();}
//----------------------------
function Locpro01Reset(DaForm){var Dis=eval(DaForm); self.location.reload(true);}
//----------------------------
function Locpro01Pay(DaForm){var Dis=eval(DaForm); Dis.act.value="PAY"; Dis.method="post"; Dis.submit();}
//----------------------------
function Locpro01Check(DaForm)
{
	var Dis=eval(DaForm); var bFlag=true;

		
	//alert(bFlag);
	if(bFlag){Dis.act.value="CHECK"; Dis.method="post"; Dis.submit();}
}
//----------------------------
function Locpro01CheckAdm(DaForm, DaLoc, DaAct)
{
	var Dis=eval(DaForm); //alert(Dis);
	var bFlag=true;

	if(bFlag){Dis.act.value=DaAct; Dis.action=DaLoc; Dis.method="post"; Dis.submit();}
}
//----------------------------
