// JavaScript Document
var Pedido={}; //Orientado a objecto.
var auto=null;
Pedido.ini=function()
{
 $("Search").onclick=BuscarCliente;
 $("autocompletado").onblur=Salir;
 $("autocompletado").onclick=Activar;
 Cargando(false);
 $("wholecontents").style.visibility="visible";
 Pedido.createAuto();
 //LoadCat();
}
///////////////////////////////////////////////////////////////////
function ShowInfoBox(type)
{
	if(type==true)
		$('divMoreInfo').style.display="block";
	else
		$('divMoreInfo').style.display="none";
}
///////////////////////////////////////////////////////////////////
function sendRequestFreeListing ()
{
  var catr=$("catr");
  var cat=[];
  for(var i=0;i<catr.options.length;i++)
  {
	  var id=catr.options[i].value;
	  cat.push(id);
  }
  var Category=cat.join(":");
  var ContactName=$("ContactName").value;
  var CompanyName=$("CompanyName").value;
  var Address=$("Address").value;
  var Address1=$("Address1").value;
  var Address2=$("Address2").value;
  var Address3=$("Address3").value;
  var Country=$("Country").value;
  var PostCode=$("PostCode").value;
  var Email=$("Email").value;
  var Tel=$("Tel").value;
  var Mobile=$("Mobile").value;
  var Web=$("Web").value;  


if (
   (Email.indexOf('@') ==-1
  ||Email.indexOf(',') !=-1 
  ||Email.indexOf('.',(2+Email.indexOf('@'))) ==-1 
  ||Email.indexOf('.',(2+Email.indexOf('@'))) ==(Email.length-1)
   )) {
alert ("Please enter a valid email. Expecting something@something.something")
return false;};

if (ContactName == '') {
alert ("Please enter your name");
return false;};
if (CompanyName == '') {
alert ("Please enter your company name");
return false;};
if (Country == '') {
alert ("Please enter your county");
return false;};
if (PostCode == '') {
alert ("Please enter your postcode");
return false;};
if (Email == '') {
alert ("Please enter your email address");
return false;};
if (Tel == '') {
alert ("Please enter your telephone number");
return false;};

  var townr=$("townr");
  var town=[];
  for(var i=0;i<townr.options.length;i++)
  {
	 var id=townr.options[i].value;
	 town.push(id);
  }
  var Town=town.join(":");
 
  var param="Category="+ Category+"&ContactName=" + ContactName +"&CompanyName="+ CompanyName+"&Address="+Address +"&Address1="+Address1 +"&Address2="+Address2 +"&Address3="+Address3 +"&Country="+Country +"&PostCode="+ PostCode+"&Email="+Email +"&Tel="+Tel +"&Mobile="+Mobile +"&Web="+ Web+"&Town="+ Town;
  
  	var url="Register.php";
	new Ajax.Request(url,					 
					{
					 parameters:param,
					 onCreate:function()
					 {
						Cargando(true);
					 },
					 onSuccess:function(transp)
					 {
						 
						Cargando(false); 
						$("myData").innerHTML=transp.responseText;
					 }
					})
  
	
}
///////////////////////////////////////////////////////////////////
function CheckearExistencia(value,text,oSel)
{
  for(var i=0;i<oSel.options.length;i++)
   {
	 var tem=oSel.options[i];
	 if(value==tem.value  && text==tem.text)
	   return true;
   }
   return false;
}
//==================================================================
function MoverRigth(from,to)//mover las opciones de izq a derecah
{
  //from="towns";
 // to="townr";
  var oSel=$(from);
  var oSelr=$(to);
  var text=[];
  var value=[];
  for(var i=0;i<oSel.options.length;i++)
  {  
	  var op=oSel.options[i];
	  if(op.selected)
	   {    
		if(CheckearExistencia(op.value,op.text,oSelr))
		  continue;		
		 var opr=new Option(op.text,op.value);
	     var cant=oSelr.options.length;
	     oSelr.options[cant]=opr;
		 text.push(op.text);
		 value.push(op.value);
	   }
  }
  i=0;
  for(;i<value.length;i++)
  {
	  for(var j=0;j<oSel.options.length;j++)
	  {  var temp=oSel.options[j];
		  if(temp.value==value[i] && temp.text==text[i])
		   { 
		     oSel.options[j]=null;
		     break;
		   }
	  }
  }
}
//==================================================================
function MoverLeft(from,to)
{
  //from=	"townr";
  //to="towns" ;	
  var oSel=$(to);
  var oSelr=$(from);
  var value=[];
  var text=[];
  for(var i=0;i<oSelr.options.length;i++)
  {  
	  var opr=oSelr.options[i];
	  if(opr.selected)
	   {
		if(CheckearExistencia(opr.value,opr.text,oSel))
		  continue;			   
		 var op=new Option(opr.text,opr.value);
	     var cant=oSel.options.length;
	     oSel.options[cant]=op;
		 value.push(opr.value);
		 text.push(opr.text);
	   }
  }
  i=0;
  for(;i<value.length;i++)
  {
	  for(var j=0;j<oSelr.options.length;j++)
	  {  var temp=oSelr.options[j];
		  if(temp.value==value[i] && temp.text==text[i])
		   { 
		     oSelr.options[j]=null;
		     break;
		   }
	  }
  }
}
///////////////////////////////////////////////////////////////////

//==================================================================
function EliminarElementoFrom(id)
{
  var oSel=$(id);
  var value=[];
  var text=[];
  for(var i=0;i<oSel.length;i++)
  {
	if(oSel.options[i].selected)
	{
		text.push(oSel.options[i].text);
		value.push(oSel.options[i].value);
	}
  }
  if(value.length>0)
  {
	if(!confirm("Are you sure to remove from the list")) 
	  return;
	for(var i=0;i<text.length;i++)
	{
	  for(j=0;j<oSel.options.length;j++)
	  {
		 var temp=oSel.options[j];
	     if(temp.text==text[i] && temp.value==value[i])
		 {
			oSel.options[j]=null;
			break;
		 }
	  }
	}
  }
}
///////////////////////////////////////////////////////////////////
function ActualizarList(q)
{
	var id=q.value;
	var param='sId='+id;
	var url="UpdateCatList.php";
	new Ajax.Request(url,					 
					{
					 parameters:param,
					 onCreate:function()
					 {
						Cargando(true);
					 },
					 onSuccess:function(transp)
					 {
						 
						Cargando(false); 
						$("divCat").innerHTML=transp.responseText;
					 }
					})
}

//////////////////////////////////////////////////////////////////
function AnadirListing()
{
	var url="RequestListing.php";
	new Ajax.Request(url,					 
					{
					 onCreate:function()
					 {
						Cargando(true);
					 },
					 onSuccess:function(transp)
					 {
						 
						Cargando(false); 
						$("myData").innerHTML=transp.responseText;
					 }
					});
}
//////////////////////////////////////////////////////////////////
function SendMsg()
{
	var subject=$("subject").value;
	var to=$("email").value;
	var msg=$("msg").value;
	var param="subject="+subject+"&to="+to+"&msg="+msg+"&name="+name;
	var url="MailSend.php";
	new Ajax.Request(url,					 
					{parameters:param,
					 onCreate:function()
					 {
						Cargando(true);
					 },
					 onSuccess:function(transp)
					 {
						 
						Cargando(false); 
						$("msgState").innerHTML=transp.responseText;
						$("fMail").reset();
					 }
					});
}
//===============================================================
function LoadPage(url)
{
	new Ajax.Request(url,					 
					{
					 onCreate:function()
					 {
						Cargando(true);
					 },
					 onSuccess:function(transp)
					 {
						 
						Cargando(false); 
						$("myData").innerHTML=transp.responseText;
					 }
					});
}
//===============================================================
function Salir()
{
 if($("autocompletado").value=="")
 {
   $("autocompletado").value="(e.g. Trade/Profession)";
   $("autocompletado").style.color="gray";
 }
}
/////////////////////////////////////////////////////////////////
function Activar()
{
	if($("autocompletado").value=="(e.g. Trade/Profession)")
	{ 
		$("autocompletado").value="";
	 	$("autocompletado").style.color="black";
	}
}

/////////////////////////////////////////////////////////////////
function AbriNew(url)
{
	a=null;
	a=window.open(url,"myWindow","width=800,height=800,alwaysRaised=true,menubar=no,toolbar=no");
}
//===============================================================
Pedido.createAuto=function()
{ 
    var secId=$("secId").value;
    var townId=$("town").value;
    //var url="include/obtenerResp.inc.php?secId="+secId;
	var url="include/buscarCus.inc.php?secId="+secId+"&town="+townId;
	
	auto=new Ajax.Autocompleter("autocompletado","opciones",url,
	{afterUpdateElement : getSelectionId,indicator:'indicator1',choices:3,fullSearch:true,ignoreCase:true});
}
function getSelectionId(comp,li)
{
   $("id").value=li.id;				 
}
//===============================================================
function updateAuto(q)
{
	var townId=$("town").value;
	var secId=$("secId").value;
	var url="include/buscarCus.inc.php?secId="+secId+"&town="+townId;
	auto.url=url;
}
//===============================================================
function HandleMenu(q,id)
{
  	$("secId").value=id;	
  	var townId=$("town").value;
	var ulObj=$("ulMenu");
	$("autocompletado").value="(e.g. Trade/Profession)";
    $("autocompletado").style.color="gray";
	for(var i=0;i<ulObj.childNodes.length;i++)
	{
		var oLi=ulObj.childNodes[i];
		if(oLi.tagName && oLi.tagName.toLowerCase()=="li")
		 {		 
		   oLi.className='twolines';
		 }
	}
	q.className='active';
    var secId=$("secId").value;
	var url="include/buscarCus.inc.php?secId="+secId+"&town="+townId;
	auto.url=url;
	url="include/categories.inc.php";
	LoadCat();
}
///////////////////////////////////////////////////////////////////////////
function LoadCat()
{
	var secId=$("secId").value;
	url="include/categories.inc.php";
	var param="secId="+secId;
	new Ajax.Request(url,					 
					{
					 parameters:param,	
					 onCreate:function()
					 {
						Cargando(true);
					 },
					 onSuccess:function(transp)
					 {
						 
						Cargando(false); 
						$("myData").innerHTML=transp.responseText;
					 }
					});
}
//===============================================================
function Cargando(t)
{
  if(t)
    $("cargando").style.display="block";
  else
   $("cargando").style.display="none";
}
//===============================================================
function ShowInfo(sid)
{
	var param="id="+sid;
	var url="include/showInfo.inc.php";
	new Ajax.Request(url,					 
					{
					 parameters:param,	
					 onCreate:function()
					 {
						Cargando(true);
					 },
					 onSuccess:function(transp)
					 {
						 
						Cargando(false); 
						$("myData").innerHTML=transp.responseText;
						//initLightbox();
					 }
					});
}
//////////////////////////////////////////////////////////////////////////
function UpdateLogo(transp)
{
	$('divLogoH').innerHTML=transp.responseText;						
	var aUpdate=["T1","T2","T3","L1","L2","L3","L4"];
					
	for(var i=0;i<aUpdate.length;i++)
	  {
		var returnId="li"+ aUpdate[i];
		var updId="div"+aUpdate[i];
		if($(returnId))
		 {
			$(updId).innerHTML=$(returnId).innerHTML;  
		 }
	  }
				
	$("myData").innerHTML=$("liData").innerHTML;						 
}
//===============================================================
function BuscarClientePrefix(secId,text,town,chr,page)
{
	var param="text="+text+"&town="+town+"&secId="+secId+"&chr="+chr+"&page="+page;	
	var url="include/buscarCustomer.inc.php";
	new Ajax.Request(url,					 
					{
					 parameters:param,	
					 onCreate:function()
					 {
						Cargando(true);
					 },
					 onSuccess:function(transp)
					 {
						 
						Cargando(false); 
						UpdateLogo(transp);
					 }
					});

}
//////////////////////////////////////////////////////////////////////////
function BuscarClienteByCatPrefix(catId,town,chr,page)
{
	var param="catId="+catId+"&town="+town+"&chr="+chr+"&page="+page;	
	var url="include/buscarCustomerByCat.inc.php";
	new Ajax.Request(url,					 
					{
					 parameters:param,	
					 onCreate:function()
					 {
						Cargando(true);
					 },
					 onSuccess:function(transp)
					 {
						Cargando(false); 
						UpdateLogo(transp);
						 
					 }
					});

}

//////////////////////////////////////////////////////////////////////////
function BuscarClienteByCat(catId,page)
{
	var town=$("town").value;
	var param="catId="+catId+"&town="+town+"&page="+page;
	var url="include/buscarCustomerByCat.inc.php";
	new Ajax.Request(url,					 
					{
					 parameters:param,	
					 onCreate:function()
					 {
						Cargando(true);
					 },
					 onSuccess:function(transp)
					 {
						Cargando(false); 
						UpdateLogo(transp);
						 
					 }
					});
}
//===============================================================
window.onload=Pedido.ini;
var myLightbox1=null;
function BuscarCliente(page)
{
	var text=$("autocompletado").value;
	var town=$("town").value;
    /*if(text=="" || text=="(e.g. Trade/Profession)")
	{
		alert("please Enter the keyword to search");
		return;
	}
	*/
	//alert($("secId").value);
	  	var id=$("id").value;
		//if(id)
		//	BuscarClienteByCat(id,1);		
		var secId=$("secId").value;
		//alert(secId);
		var param="text="+text+"&town="+town+"&secId="+secId+"&page="+page;
		var url="include/buscarCustomerText.inc.php";
		new Ajax.Request(url,					 
						{
						 parameters:param,	
						 onCreate:function()
						 {
							Cargando(true);
						 },
						 onSuccess:function(transp)
						 {
							 
							Cargando(false); 
							UpdateLogo(transp);
							 
						 }
						}); 
}
//==============================================================
function CreateViewer()
{
	hs.graphicsDir = 'js/graphics/';
	hs.align = 'center';
	hs.transitions = ['expand', 'crossfade'];
	hs.outlineType = 'glossy-dark';
	hs.wrapperClassName = 'dark';
	hs.fadeInOut = true;
	//hs.dimmingOpacity = 0.75;
	// Add the controlbar
	if (hs.addSlideshow) hs.addSlideshow({
		//slideshowGroup: 'group1',
		interval: 5000,
		repeat: false,
		useControls: true,
		fixedControls: 'fit',
		overlayOptions: {
			opacity: .6,
			position: 'bottom center',
			hideOnMouseOut: true
		}
	});	
}
/////////////////////////////////////////////////////////////////