/*
* getSearchForm
* >> permet d'afficher la partie du formulaire dsire selon le type de recherche !
*/
function getSearchForm(formnumber,numberOfSearchType) 
{
	for (i=1;i<=numberOfSearchType;i=i+1)
	{
		if (document.forms['simplyForm']['Search_type'+i])
			dumy = true;
		else
			continue;
		if (i==formnumber)
		{
			document.forms['simplyForm']['Search_type'+i].style.display='block';
			if (document.all) // for IE only
				document.forms['simplyForm']['submit_'+i].disabled=''; 
		}
		else
		{
			document.forms['simplyForm']['Search_type'+i].style.display='none';
			if (document.all) // for IE only
				document.forms['simplyForm']['submit_'+i].disabled='disabled';
			if (i<=2) // pour les input text
			{
				document.getElementById("hiddenErrorForm2_"+i).style.color='#000000';
				document.forms['simplyForm']['search'+i].value='';
			}
			else 
			{
				if (i<=8) // pour les listes droulantes
				{
					document.forms['simplyForm']['search'+i+'_1'].style.color='#000000';
					document.forms['simplyForm']['search'+i+'_1'].selectedIndex=0;
					if (i==3)
						updateproductlines3(0);
					else
					{
						if(i==4)
							updateproductlines4(0);
						else
						{
							if(i==5)
								updateproductlines5(0);	
							else
							{
								if(i==6)
									updateproductlines6(0);
								else
								{
									if(i==7)
										updateproductlines7(0);						
								}											
							}								
						}
					}
				}
			}
		}
	}
	
	if (document.getElementById("divrightimg"))
		document.getElementById("divrightimg").style.display = 'none';			
	
	if (formnumber == EQ_REQNUM)
	{
		if (EQ_REQNUM == 9)
		{
			if (document.getElementById("fileresult"))
				document.getElementById("fileresult").style.display = 'block';							
		}
		else
		{
			if (document.getElementById("tableContainer"))
				document.getElementById("tableContainer").style.display = 'block';
			if (document.getElementById("foundRef"))
				document.getElementById("foundRef").style.display = 'block';
			if (document.getElementById("divrightimg"))
				document.getElementById("divrightimg").style.display = 'block';					
		}
	}
	else
	{
		if (document.getElementById("tableContainer"))
			document.getElementById("tableContainer").style.display = 'none';
		if (document.getElementById("foundRef"))
			document.getElementById("foundRef").style.display = 'none';	
		if (document.getElementById("fileresult"))
			document.getElementById("fileresult").style.display = 'none';				
	}
	
	document.forms['simplyForm'].reqnum.value = formnumber;	
}

/*
* verifyForm
* >> vrifie que les champs demands ont t correctement saisis, si oui envoit le formulaire, si non affiche message erreur !
*/
function verifyForm() 
{
	var numsearch = 0;
	numsearch = document.forms['simplyForm'].reqnum.value;
	
	document.forms['simplyForm'].target='_self';
	
	var toSend = true;

	// on vrifie que les critres de texte ont t saisis
	if (numsearch == 1) // search by reference
	{
		// la rfrence doit faire 5 caractres minimum !
		val = document.forms['simplyForm']['search'+numsearch].value.trim();
		if (val.length<5)	
		{
			col='#A80000';
			toSend = false;
		}else
			col='#000000';
		document.getElementById("hiddenErrorForm2_"+numsearch).style.color=col;
	}
	else if (numsearch == 2) // search by name/designation
	{
		// la dsignation doit faire 5 caractres minimum !
		val = document.forms['simplyForm']['search'+numsearch].value.trim();
		if (val.length<5)	
		{
			col='#A80000';
			toSend = false;
		}else
			col='#000000';
		document.getElementById("hiddenErrorForm2_"+numsearch).style.color=col;
	}
	else if (numsearch == 8) // search by supplier
	{
		mysupplier = document.forms['simplyForm']['search'+numsearch+'_1'].value;
		if (mysupplier=="")	
		{
			col='#A80000';
			toSend = false;
		}else
			col='#000000';
		document.forms['simplyForm']['search'+numsearch+'_1'].style.color=col;
		i=1
		while(document.forms['simplyForm']['search'+numsearch+'_1'].options[i] && col!="#000000")
		{
			document.forms['simplyForm']['search'+numsearch+'_1'].options[i].style.color='#000000';
			++i;
		}
	}	
	else if (numsearch == 9)
	{
		if (document.forms['simplyForm']['search_file_xls'])
		{
			var fileval = document.forms['simplyForm']['search_file_xls'].value;
			if (fileval == '')
				toSend = false;
			if (fileval.substring(fileval.length-4,fileval.length).toLowerCase() != '.csv' && fileval.substring(fileval.length-4,fileval.length).toLowerCase() != '.xls')	
				toSend = false;
		}
	}
	else  // search by new product / change of name / change of packaging / unchanged reference
	{
		// business et product line doivent avoir t soumises
		val1 = document.forms['simplyForm']['search'+numsearch+'_1'].value;
		val2 = document.forms['simplyForm']['search'+numsearch+'_2'].value;
		if (!(val1 > 0 && val2 > 0))	
		{
			col='#A80000';
			toSend = false;
		}else
			col='#000000';
		document.forms['simplyForm']['search'+numsearch+'_1'].style.color=col;
		for (i=1;i<=6;i=i+1)
		{
				document.forms['simplyForm']['search'+numsearch+'_1'].options[i].style.color='#000000';
		}	
	}	
	
	if (toSend && numsearch!=9)
		document.forms['simplyForm'].submit();
	return toSend;
}

function radioform(val)
{
	//document.forms['simplyForm']['search_9_9_hidden'].value=val;	
}

/*
* verifyFormIdentification (pour le formulaire d'idenfification vers l'admin !)
* >> vrifie que les champs demands ont t correctement saisis, si oui envoit le formulaire, si non affiche message erreur !
*/
function verifyFormIdentification() 
{
	var toSend = true;

	// login doit tre saisi
	val = document.forms['simplyForm']['User'].value.trim();
	if (val.length<1)	
	{
		disp='inline';
		toSend = false;
	}else
		disp='none';
	document.getElementById("badlogin").style.display=disp;
	
	// password doit tre saisi
	val = document.forms['simplyForm']['Password'].value.trim();
	if (val.length<1)	
	{
		disp='inline';
		toSend = false;
	}else
		disp='none';
	document.getElementById("badpassword").style.display=disp;

	
	return toSend;
}

/*
select_ref : met la valeur de la rfrence slectionner dans le champ cach 'chooseref' puis envoie le formulaire
*/

function select_ref(valeur)
{
	document.forms['simplyForm']['chooseref'].value=valeur;
	document.forms['simplyForm'].submit();
}

function select_ref8(valeur,inref)
{
	document.forms['simplyForm']['chooseref'].value=valeur;
	//document.forms['simplyForm'].reqnum.value='1';
	document.forms['simplyForm']['inreferences'].value = inref;
	document.forms['simplyForm'].submit();
}

/*
	dfinition de la fonction trim(),  appeler comme ca : ma_string.trim()
*/
String.prototype.trim = function(){
	return this.replace(/^\s+/, "").replace(/\s+$/, "");
}




function resize()
{
	if (window.innerHeight) // si Firefox, Mozilla, etc. (<> IE)
	{
		if (document.getElementById("tableContainerMultiRef"))
		{
			document.getElementById("tableContainerMultiRef").style.height = (window.innerHeight - 414) + "px";
		}
		if (document.getElementById("tableContainer"))
		{
			document.getElementById("tableContainer").style.height = (window.innerHeight -355) + "px";
			if (document.getElementById("tbody"))
				document.getElementById("tbody").style.height = (window.innerHeight -394) + "px";
			if (document.getElementById("tbodyBigHeader"))
				document.getElementById("tbodyBigHeader").style.height = (window.innerHeight -464) + "px";				
		}else if (document.getElementById("tableContainerBig"))
		{
			document.getElementById("tableContainerBig").style.height = (window.innerHeight -305) + "px";
			if (document.getElementById("tbodyBig"))
				document.getElementById("tbodyBig").style.height = (window.innerHeight -344) + "px";
		}else if (document.getElementById("statsdiv")) 
		{
			document.getElementById("statsdiv").style.height = (window.innerHeight -260) + "px";
		}
	}
}
window.onload = function()
{
   resize();
}
window.onresize = function()
{
   resize();
}


function copyReferences()
{	
	j=0;
	if(document.forms['simplyForm']['search_9_10'].value == insert_references)
	{
		document.forms['simplyForm']['search_9_10'].value = '';
	}else{
		tab = document.forms['simplyForm']['search_9_10'].value.split("\n");
		if (tab.length>1)
		{
			document.forms['simplyForm']['search_9_10'].value = '';
			document.forms['simplyForm']['search_9_10'].style.visibility = 'hidden';			
			for (i=0;i<tab.length;++i)
			{
				if (tab[i].trim() != "")
				{
					document.forms['simplyForm']['search_9_8_'+j].value = tab[i].trim();
					document.forms['simplyForm']['search_9_8_'+j].style.display = "block";
					++j;
				}
			}
			if (j>0)
			{
				document.forms['simplyForm']['refresh_img'].style.visibility = "visible";
			}		
		}
		else if(tab.length==1 && tab[0].trim()!='')
		{
			document.forms['simplyForm']['search_9_10'].value = '';
			document.forms['simplyForm']['search_9_10'].style.visibility = 'hidden';			
			if (document.forms['simplyForm']['search_9_8_0'])
			{
				document.forms['simplyForm']['search_9_8_0'].style.display = "block";	
				document.forms['simplyForm']['search_9_8_0'].focus();	
				document.forms['simplyForm']['search_9_8_0'].value=tab[0];
			}
			
		}
	}	
}



function refuserToucheEntree(event,i)
{
    eventIsEnter = false;
	
	// Compatibilit IE / Firefox
    if(!event && window.event) {
        event = window.event;
    }
    // IE
    if(event.keyCode == 13) {
        event.returnValue = false;
        event.cancelBubble = true;
		eventIsEnter = true;
    }
    // DOM
    if(event.which == 13) {
        event.preventDefault();
        event.stopPropagation();
		eventIsEnter = true;
    }
	
	if (eventIsEnter)
	{
		j=i+1;
		if (document.forms['simplyForm']['search_9_8_'+j])
			document.forms['simplyForm']['search_9_8_'+j].focus();		
	}
}

function addnextinput(i)
{
	if (i<499)
	{
		if (document.forms['simplyForm']['search_9_8_'+i].value.trim() != '')
		{
			j=i+1;
			if (document.forms['simplyForm']['search_9_8_'+j].style.display == "none")
			{
				document.forms['simplyForm']['search_9_8_'+j].value = "";
				document.forms['simplyForm']['search_9_8_'+j].style.display = "block";	
				document.forms['simplyForm']['refresh_img'].style.visibility = "visible";
				document.forms['simplyForm']['search_9_10'].style.visibility = 'hidden';
			}
		}
	}
}

function resetForm(numbcase)
{
	if (numbcase == 9)
	{
		//history.back(-1);
		//refreshReferences();	
	}
}

function refreshReferences(mynumberofformref)
{
	document.forms['simplyForm']['search_9_10'].value = insert_references;
	document.forms['simplyForm']['search_9_10'].style.visibility = 'visible';
	for (i=0;i<mynumberofformref;++i)
	{
		document.forms['simplyForm']['search_9_8_'+i].value = '';
		document.forms['simplyForm']['search_9_8_'+i].style.display = "none";
	}	
	document.forms['simplyForm']['refresh_img'].style.visibility = "hidden";
}

function getLoaderImage(idimage,nomimage)
{
	if (document.getElementById(idimage))
	{
		if (nomimage == "")
			nomimage = "loader.gif";
		document.getElementById(idimage).src = commondir+"images/"+nomimage;
	}
}

function declarationProductEquivalence(keyprod,idprod,name,mybrand,oldname,newname,access)
{
	// access = true  (=> utilisateur interne) => ouverture, enregistrement, impression
	// access = false (=> utilisateur externe) => impression uniquement
	
	var imgsrc = document.getElementById("declarEqui_"+idprod).src;
	if (imgsrc == imgsrc.replace("tocreate.png",""))
		var pdfiscreated = true;
	else
		var pdfiscreated = false;
		
	var url = commondir + "attestationConformite/index.php";
	
	return myDeclarationProductEquivalence(pdfiscreated,url,keyprod,idprod,name,mybrand,oldname,newname,access);
}


function myDeclarationProductEquivalence(imprim,url,keyprod,idprod,name,mybrand,oldname,newname,access)
{
   if (imprim)
   {
	    if (access == "intern")
		{
			openpdf(keyprod,"Declaration Product Equivalence [" + oldname + "]");
		}else
		{
			myprintpdf(keyprod);
		}
		return true;
   }else
   {
	   var xhr_object = null;
	   getLoaderImage("declarEqui_"+idprod,"");
		 
	   if(window.XMLHttpRequest) // Firefox 
		  xhr_object = new XMLHttpRequest(); 
	   else if(window.ActiveXObject) // Internet Explorer 
		  xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	   else {
		  //alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
		  return;
	   } 
	   
	   htmloutput = "non"; // pour tout le monde impression d'un PDF et non d'un HTML
	
	   xhr_object.open("POST", url, true);
	   xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	   xhr_object.send("keyprod="+keyprod+"&brand="+mybrand+"&access="+access+"&oldname="+myaddslashes(oldname)+"&newname="+myaddslashes(newname)+"&htmloutput="+htmloutput); 	
	   
	   xhr_object.onreadystatechange = function() { 
		  if(xhr_object.readyState == 4) {
			 if(xhr_object.responseText != '')
			 {
				   if (access == "intern")
					{
						openpdf(keyprod,"Declaration Product Equivalence [" + oldname + "]");
						getLoaderImage("declarEqui_"+idprod,"pdf.png");
					}else
					{					
						myprintpdf(keyprod);
						getLoaderImage("declarEqui_"+idprod,"print2.png");
					}	
					return true;	
			 }else
				return false;
		  }
	   } 
   }
}

function openpdf(keyprod,mytitle)
{
	myfile = keyprod + ".pdf";
	page = commondir+"attestationConformite/declarationEquivalence/" + myfile;
		
	if (window.innerWidth)
	{
		myleft = ((window.innerWidth - 760) / 2);
		mytop = ((window.innerHeight - 526) / 3);
	}
	else	
	{
		myleft = ((document.body.offsetWidth - 760) / 2);
		mytop = ((document.body.offsetHeight - 526) / 3);		
	}	
	
	ref = new Window(myfile, {className: "alphacube", width:760, height:526, left :myleft,top:mytop,zIndex: 100,  resizable: true, title: mytitle, draggable:true, wiredDrag: false , thefile: myfile, recenterAuto: true, url: page});
	ref.show();
	focus(ref);
	ref.toFront();
}



function myaddslashes(str) {
	str=str.replace("'","@@@");	
	return str;
}

function addslashes(str) {
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');
	return str;
}

function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');
	return str;
}

function printpdfIE(myfile)
{
	if (document.getElementById("pdfframe"))
	{
		var pdfobj = document.getElementById("pdfframe");
		pdfobj.click();
		pdfobj.setActive();
		pdfobj.print();		
		return true;
	}
	return false;
}

function printpdfFF()
{
	var varFrame;
	if (document.all)
		varFrame=parent.pdfframe;
	else
		varFrame=window.frames[0];
	varFrame.focus();
	varFrame.print();
	return true;	
}

function myprintpdf(keyprod)
{
	var filename  = "../Equivalency/common/attestationConformite/declarationEquivalence/" + keyprod + ".pdf";	
	
	if (document.all)
	{
		myprintpdfIE(filename);
	}
	else
	{
		myprintpdfFF(filename);	
	}
}

function myprintpdfIE(myfile)
{
	document.getElementById("divframe").removeChild(document.getElementById('pdfframe'));
	var obj = document.createElement('embed');
	obj.setAttribute("id","pdfframe");
	obj.setAttribute("name","pdfframe");
	obj.setAttribute("width","768px");
	obj.setAttribute("height","1024px");
	obj.setAttribute("src",myfile);
	document.getElementById("divframe").appendChild(obj);
	
	setTimeout(function(){printpdfIE(myfile)},2500);

	return true;		
}


function myprintpdfFF(myfile)
{
	document.getElementById("divframe").removeChild(document.getElementById('pdfframe'));
	var iframe = document.createElement('iframe');
	iframe.src = myfile;
	iframe.id = "pdfframe";
	iframe.name = "pdfframe";
	document.getElementById("divframe").appendChild(iframe);
	if (setTimeout(function(){printpdfFF()},2000))	
		return true;		
}


function startCallback() {
	// make something useful before submit (onStart)
	return true;
}

function completeCallback(response) {
	document.getElementById('fileXLS').innerHTML = response;
}