function doNavigateRes(pstrWhere, pintTot)
{
  var strTmp;
  var intPg; 
  strTmp = document.getElementById("page").value;
  intPg = parseInt(strTmp);
  if (isNaN(intPg)) intPg = 1; 
  if ((pstrWhere == 'F' || pstrWhere == 'P') && intPg == 1)
  {
    alert("You are already on the first page");
    return;
  }
  else if ((pstrWhere == 'N' || pstrWhere == 'L') && intPg == pintTot)
  {
    alert("You are already on the last page");
    return;
  }
  if (pstrWhere == 'F')
    intPg = 1;
  else if (pstrWhere == 'P')
    intPg = intPg - 1;
  else if (pstrWhere == 'N')
    intPg = intPg + 1;
  else if (pstrWhere == 'L')
    intPg = pintTot; 
  else if (!isNaN(pstrWhere))
	  intPg= pstrWhere ;

  if (intPg < 1) intPg = 1;
  if (intPg > pintTot) intPg = pintTot;
  document.getElementById("page").value = intPg;
  document.form_main.submit();
}

function doNavigateResList(pstrWhere, pintTot)
{
  var strTmp;
  var intPg; 
  strTmp = document.getElementById("page").value;
  intPg = parseInt(strTmp);
  if (isNaN(intPg)) intPg = 1; 
  if ((pstrWhere == 'F' || pstrWhere == 'P') && intPg == 1)
  {
    alert("You are already on the first page");
    return;
  }
  else if ((pstrWhere == 'N' || pstrWhere == 'L') && intPg == pintTot)
  {
    alert("You are already on the last page");
    return;
  }
  if (pstrWhere == 'F')
    intPg = 1;
  else if (pstrWhere == 'P')
    intPg = intPg - 1;
  else if (pstrWhere == 'N')
    intPg = intPg + 1;
  else if (pstrWhere == 'L')
    intPg = pintTot; 
  else if (!isNaN(pstrWhere))
	  intPg= pstrWhere ;

  if (intPg < 1) intPg = 1;
  if (intPg > pintTot) intPg = pintTot;
  document.getElementById("page").value = intPg;
  document.form_main.submit();
}

function supp_panier (_id_vhc)
{

	if (_id_vhc > 0 )
	{
		if ( confirm("Are you sure you want to delete this vehicle from your selection?") )
		{
			document.form_main.vhc_suppr.value = _id_vhc;
			document.form_main.submit();
		}
	}
	else if ( _id_vhc == -1 )
	{
		if ( confirm("Are you sure that you want to delete your entire selection?") )
		{
			document.form_main.vhc_suppr.value = _id_vhc;
			document.form_main.submit();
		}
	}
	else
	{
		document.getElementById("sel_"+_id_vhc).checked = false;
	}
}

function doSupprCamion ( _vhc_id )
{
	if ( confirm("Are you sure that you want to delete this vehicle?") )
	{
		submitTo('frmMain','do_delete_vhe.jsp?vhc=' + _vhc_id );
	}
}

function deleteContact ( id_ctc )
{
	if ( confirm("Are you sure that you want to delete this contact?") )
	{
		document.frmMain.action="do_save_contact.jsp?ctc_id=" + id_ctc + "&delete=1";;
		document.frmMain.submit();
	}
}
function deleteMessage ( id_mes )
{
	if ( confirm("Are you sure that you want to delete this business?") )
	{
		document.frmMain.action="do_save_message.jsp?mes_id=" + id_mes + "&delete=1";;
		document.frmMain.submit();
	}
}

function sendPanierProspect( _ddp_id)
{
	if (confirm ("Do you want to empty your shopping basket before adding the vehicles to your selection?"))
	{
		document.frmMain.action="panier.jsp";
		document.getElementById("flag_vide_panier").value= "1";
		document.getElementById("ddp_id").value= _ddp_id;
		document.frmMain.submit();
	}
	else
	{
		document.frmMain.action="panier.jsp";
		document.getElementById("flag_vide_panier").value= "0";
		document.getElementById("ddp_id").value= _ddp_id;
		document.frmMain.submit();
	}
}
function sendPanierMessageReseau( _mes_id)
{
	if (confirm ("Do you want to empty your shopping basket before adding the vehicles to your selection?"))
	{
		document.frmMain.action="panier.jsp";
		document.getElementById("flag_vide_panier").value= "1";
		document.frmMain.submit();
	}
	else
	{
		document.frmMain.action="panier.jsp";
		document.getElementById("flag_vide_panier").value= "0";
		document.frmMain.submit();
	}
}


function generate_catalogue()
{
	if (document.getElementById().value > 0)
		document.form_cat.submit();
	else
	{
		alert("You must choose a language for the catalog to be printed");
		return;
	}
}
function notAvailable ()
{
	alert( "Not available" );
	//return false;
}

function alertDeny()
{
	alert( "Your current profile does not permit you access to this title. \nPlease contact your Truckplus administrator.");
}

function checkNumDossier ( _formname, _fieldname, _disId, _disNum, _vhcId )
{
	// R?cup?ration du html field
	var val = eval ( "document." + _formname + "." + _fieldname + ".value" );
	
	if ( val == null )
		return false;

	// G?n?ration du numero de dossier
	val = _disNum + "-" + val

	// Generation du filtre SQL
	sqlfilter = "WHERE dis_id = " + _disId;
	
	if ( _vhcId != "" )
		sqlfilter += " AND vhc_id != " + _vhcId;

        sqlfilter += " AND ven_id IS NULL";
                // PATCH PORTEUR REMORQUE
		// Dans la recherche de doublons, ne pas s?lectionner la remorque associ?e ? un porteur
		// car ils auront le m?me Num?ro de Dossier
		sqlfilter 	+= " AND (vehicule.vhc_id NOT IN (SELECT vhc_rem_id FROM vehicule ";
		sqlfilter	+= " left join type on type.typ_id = vehicule.typ_id ";
		sqlfilter	+= " left join modele on modele.mod_id = type.mod_id ";
		sqlfilter	+= " left join marque on marque.mrq_id = modele.mrq_id WHERE modele.gen_id =6 AND (vhc_rem_id IS NOT NULL AND vhc_rem_id != 0) AND vehicule.dis_id = " + _disId + "))";
	

	// Appelle du check BDD
	retour = AJAX_checkField (  'vehicule', 'vhc_num_dossier',  val, sqlfilter )

	// Gestion du retour
	if ( retour == true )
	{
		alert ( "File number already used" );
		return false;
	}
	
	if(eval ( "document." + _formname + ".ima" )!=undefined && checkImmatriculation(_formname, 'ima', _disId, _disNum, _vhcId) == false)
		return false;
	
	return true;
}

function checkImmatriculation ( _formname, _fieldname, _disId, _disNum, _vhcId )
{
	// R?cup?ration du html field
	var val = eval ( "document." + _formname + "." + _fieldname + ".value" );
	
	val = val.replace(' ', '');

	if ( val == null )
		return false;

	// Generation du filtre SQL
	//sqlfilter = "WHERE dis_id = " + _disId;
	sqlfilter = "WHERE true";
	
	if ( _vhcId != "" )
		sqlfilter 	+= " AND vhc_id != " + _vhcId;

        sqlfilter += " AND ven_id IS NULL";
		// PATCH PORTEUR REMORQUE
		// Dans la recherche de doublons, ne pas s?lectionner la remorque associ?e ? un porteur
		// car ils auront la m?me immatriculation
		sqlfilter 	+= " AND (vehicule.vhc_id NOT IN (SELECT vhc_rem_id FROM vehicule ";
		sqlfilter	+= " left join type on type.typ_id = vehicule.typ_id ";
		sqlfilter	+= " left join modele on modele.mod_id = type.mod_id ";
		sqlfilter	+= " left join marque on marque.mrq_id = modele.mrq_id WHERE modele.gen_id =6 AND (vhc_rem_id IS NOT NULL AND vhc_rem_id != 0) AND vehicule.dis_id = " + _disId + "))";
	
	// Appelle du check BDD
	retourUpper = AJAX_checkField (  'vehicule', 'vhc_immatriculation',  val.toUpperCase(), sqlfilter );
	retourLower = AJAX_checkField (  'vehicule', 'vhc_immatriculation',  val.toLowerCase(), sqlfilter );
	// Gestion du retour
	if ( retourUpper == true || retourLower == true)
	{
		alert ( "Registration already used" );
		return false;
	}
	
	return true;
}

function checkDateVente()
{
   var date_ven = document.getElementById("ven_date");
   if (date_ven.value.length == 0) {
   		alert('Incorrect sales date');
   		document.getElementById("ven_date").focus();
   }
   else
   {
		if(!testdate(date_ven)) {			
	   		alert('Incorrect sales date');			
			document.getElementById("ven_date").focus();		
		}
		else
			checkAllField(); // ok format date, on teste les autres champs maintenant
	}	   
}

function alertAntiPopup()
{
	alert ("It appears that your navigator has a popup blocker. \nPlease deactivate the block and reload the page");
}

function trim(string) 
{ 
	return string.replace(/(^\s*)|(\s*$)/g,''); 
} 


function validemail(test)
{	
	 test = trim(test);
	 
	 valeur = test.indexOf("@");
	 valeur2 = test.lastIndexOf(".");
	 valeur3 = valeur2 + 1;
	 longueur = test.length;
	 ext = test.substring(valeur3,longueur);
	 ok=0;
	 temp = ext.length;
	 if ( temp>= 2 && temp<= 6 ) 
	    { ok=1; }
	 else
	    { ok=0; }
	 if ( valeur2 == (valeur + 1) )
	     { ok=0; }
	 if (valeur==0)
	     { ok=0; }
	 if (valeur==-1 || valeur2==-1 || ok==0)
	    { alert("The e-mail address you entered is not valid. \nPlease re-enter it.");
	      return false; }
	 else
	    { return true; }       
}

function envoie_reponse(strNomDemandeur)
{
	if (confirm("Do you confirm that you wish to send this reply to " +strNomDemandeur+ "?"))
	{
		document.form_main.submit();
	}
}

function extractStockCheckSelection()
{
	bChecked = false;
	
	$('[id^=extractVo_]').each(function() {
	    if($(this).attr('checked'))
	    	bChecked = true;
	});
	
	if(!bChecked)
	{
		alert("You must select at least one vehicle.");
		return false;
	}
	
	return true;
}
