//Para AJAX
function cerrar(id)
{
				document.getElementById(id).innerHTML =''
}		
function cargaResp(idresp)
{
	xajax_cargaResp(idresp)
}
function olvidoPwd(email)
{
	xajax_olvidoPwd(email)
}
function registrar(em,pawd)
{
	xajax_registrar(em,pawd)
}
function otroPwd(email)
{
	//var myLocalCallback = xajax.callback.create(10000, 20000)
	xajax_otroPwd(email)
}
function deregistrar()
{
	
	xajax_deregistrar()
}

//Funcion que nos dce si una cadena es numérica

function limpia_linea(doc) //Limpia los datos introducidos en la linea actual
{
	doc.form1.iu.selectedIndex=0;
	doc.form1.precio.selectedIndex=0;
	doc.form1.c.value="";
	doc.form1.st.value="";
}

function carrito(idProd,id,tipo,origen) //Para evitar que se le de al boton "Añadir" sin elegir Cantidad y Unidad.
{
		
	if (origen == 0)
	{
		if (tipo=="F")
		{
			document.getElementById("contenedor").innerHTML =xajax_showCarrito(idProd,'4','F',origen)
		}			
		if (tipo=="U")
		{
			document.getElementById("contenedor").innerHTML =xajax_showCarrito(idProd,'4','U',origen)
		}	
	}
	if (origen == 1)
	{
		if (tipo=="U")
		{
			document.getElementById("contenedor").innerHTML =xajax_showCarrito(idProd,'4','X',origen)
		}			
		if (tipo=="F")
		{
			document.getElementById("contenedor").innerHTML =xajax_showCarrito(idProd,'4','O',origen)
		}	
	}	
	if (origen == 2)
	{
			document.getElementById("contenedor").innerHTML =xajax_showCarrito(idProd,'4','R',origen)
	}
}
function somete(val,doc,id) //Para evitar que se le de al boton "Añadir" sin elegir Cantidad y Unidad.
{
	doc.form1.n.value="4";
	if (val == 0)
	{
		if ((doc.form1.st.value!="")&&(doc.form1.st.value!="0"))
		{
			doc.form1.action="http://localhost/e_com/empresa/web/index2.php?id=" + id;
			doc.form1.op.value="ad";
			doc.form1.submit();
			//xajax_addCarrito(doc.form1.i.value,doc.form1.t.value,doc.form1.st2.value,doc.form1.cc.value,doc.form1.of.value,doc.form1.iu2.value,4)
		}	
	}
	if (val == 1)/*Limpiar*/
	{
			doc.form1.action="http://localhost/e_com/empresa/web/index2.php?id=" + id;
			doc.form1.op.value="cl";
			doc.form1.submit();
		  //xajax_clearCarrito(4)
}
	if (val == 21) /*No hay cliente Logueado*/
	{
			doc.form1.action="http://localhost/e_com/empresa/web/index2.php?id=" + id;
			doc.form1.n.value="64";
			doc.form1.op.value="set";
			doc.form1.submit();
		  //xajax_setCarrito(doc.form1.thisTipoPagoFild,64)
			
	}		
	if (val == 211) /*Validación de Cliente*/
	{
			doc.form1.action="http://localhost/e_com/empresa/web/index2.php?id=" + id;
			doc.form1.n.value="641";
			doc.form1.op.value="set";
			doc.clienteEnterForm.submit();
		  //xajax_setCarrito(641)
	}		
	if (val == 22)/* Hay Cliente Logueado*/
	{
			doc.form1.n.value="65";
			doc.form1.action="http://localhost/e_com/empresa/web/index2.php?id=" + id;
			doc.form1.op.value="set";
			doc.form1.submit();
		  //xajax_setCarrito(65)
			
	}		
	if (val == 221)/* Hacer Cliente*/
	{
			doc.form1.n.value="66";
			doc.form1.action="http://localhost/e_com/empresa/web/index2.php?id=" + id;
			doc.form1.op.value="set";
			doc.form1.submit();
		  //xajax_setCarrito(66)
	}		
	if (val == 222)/* Meter Pedido*/
	{
			doc.form1.n.value="66";
			doc.form1.action="http://localhost/e_com/empresa/web/index2.php?id=" + id;
			doc.form1.op.value="set2";
			doc.form1.submit();
		  //xajax_set2Carrito(66)
	}			
	
	if (val == 3)/*Borrar una linea de Carrito*/
	{
			//doc.form1.selProd.value=doc.form2.prodSel.value;
			doc.form1.n.value="5";
			doc.form1.op.value="del";
			doc.form1.submit();
		  //xajax_delCarrito(doc.form1.detSel.value,5)
	}
}
function isNum(vTestValue)
{
	var sField = new String(vTestValue);
	
	if(sField.length==0) { return false; }
	else if(sField.length==1 && (sField.charAt(0) == '.' || sField.charAt(0) == ',' || (sField.charAt(0) == '-'))) { return false; }
	
	for(var x=0; x < sField.length; x++) {
		if((sField.charAt(x) >= '0' && sField.charAt(x) <= '9') || sField.charAt(x) == '.' || sField.charAt(x) == ',' || (sField.charAt(x) == '-' && x==0)) {  }
		else { return false; }
	}
	/**/
	return true;
}

//Función que nos comprueba si un mail es correcto

function isEmail (email, bEmptyOK) {
reEmail = /^[A-Z, a-z, 0-9,_]+\.{0,1}[A-Z, a-z, 0-9,_]+\@[A-Z, a-z, 0-9,_]+\.[A-Z, a-z, 0-9,_]+/;

if (trim(email)!="" && !reEmail.test(email)) { if (bEmptyOK)
return true;
else
return false;
}
return true;
}

// Function to confirm form submission
// Author : Nilesh Dosooye
// pass message to popup to function and it will send true or false depending on user choice
// call it with onSubmit on a form

function userConfirmationAlert(message)
{

	 var whatToReturn = true;
     if (!confirm(message)) 
	 {
	   whatToReturn = false;
	 }
	 return (whatToReturn);
}


// Functions to Check and Uncheck all CheckBoxes
// Author : Nilesh Dosooye
// Usage : <input type=button name="CheckAll"   value="Check All" onClick="checkAll(document.myform.list)">
//         <input type=button name="UnCheckAll" value="Uncheck All" onClick="uncheckAll(document.myform.list)">

function checkAll(field)
{
        for (i = 0; i < field.length; i++)
        field[i].checked = true ;
}

function uncheckAll(field)
{
        for (i = 0; i < field.length; i++)
        field[i].checked = false ;
}

// Function to open a Modal Window
// Author : Nilesh Dosooye
// Users can specify all parameters needed as hidden variables
// e.g
//<input type=hidden name="toolbar" value="0">
//<input type=hidden name="location" value="0">
//<input type=hidden name="directories" value="0">
//<input type=hidden name="status" value="0">
//<input type=hidden name="menubar" value="0">
//<input type=hidden name="scrollbars" value="0">
//<input type=hidden name="resizable" value="0">
//<input type=hidden name="width" value="800">
//<input type=hidden name="height" value="800">
// <input type=hidden name="url" value="http://nilesh.dosooye.com/">

function openNewModalWindow(form1) {    

var address = form1.url.value;   
var option = "toolbar=" + form1.toolbar.value +",location=" + form1.location.value +",directories=" + form1.directories.value +",status=" + form1.status.value +",menubar="+ form1.menubar.value +",scrollbars=" + form1.scrollbars.value  +",resizable=" + form1.resizable.value +",width="+ form1.width.value +",height=" + form1.height.value;
var win4 = window.open(address, form1.windowName.value,option);
win4.document.write (form1.inf.value);
}


function openNewModalWindowAndCloseParent(form1,window1) {    

var address = form1.url.value;   
var option = "toolbar=" + form1.toolbar.value +",location=" + form1.location.value +",directories=" + form1.directories.value +",status=" + form1.status.value +",menubar="+ form1.menubar.value +",scrollbars=" + form1.scrollbars.value  +",resizable=" + form1.resizable.value +",width="+ form1.width.value +",height=" + form1.height.value;
var win4 = window.open(address, form1.windowName.value,option);

if (window1.name!="mainWindow")
{
   window1.close();
}
}
function showDiv(nombreCapa)
{
	document.getElementById(nombreCapa).style.visibility="visible";
}
function hideDiv(nombreCapa)
{
	document.getElementById(nombreCapa).style.visibility="hidden";
} 
function capaCheck(objeto,capa)
{
	if (objeto.checked==true)
	{
		showDiv(capa)
	}
	if (objeto.checked==false)
	{
		hideDiv(capa)
	}
}
function capaList(objeto)
{
/*
	Esta función recorrera el objeto listBox y si al elemento seleccionado 
	pondrá la capa del mismo nombre a "visible" y los demás a "hidden".
*/

	var a=objeto.length;
	var i=0;

		while (i<a) // Mientras no llegues al final
		{

			if (objeto.options[i].selected==true)
			{
				showDiv(objeto.options[i].value)
			}
			else
			{
				hideDiv(objeto.options[i].value)
			}
			i++;
		}
}


function añadir_lst(lst,lst1, lst2)
{
	//obtener el seleccionado en  lst1-----> lst1.options[indexSelected-1]
	var v= new Array()
	i=0;
	if (lst1.selectedIndex>-1) // Si hay algún artículo seleccionado
	{
		// comprobar que no esté en lst2
		i=0;
		j=0;//no está
		v=lst.value.split("*")
		while ((v[i]!=null)&&(j==0)&&(v[i]!="")) // Mientras no llegues al final
		{
			if ((v[i]!=lst1.options[lst1.selectedIndex].value)) 
			//Si es distinto y no estás en blanco, se recorre
			{
				i++;
			}
			else
			{
				j=1; //está.
			}
		}
		if (j==0)//Si no está
		{
			lst2[lst2.length]=new Option("", "-1","", false) // Ahora tenemos dos vacios
			lst2[lst2.length-1].text="........................................................................" 
			lst2[lst2.length-2].text=lst1.options[lst1.selectedIndex].text 
			lst2[lst2.length-2].value=lst1.options[lst1.selectedIndex].value
			lst.value=lst.value+lst2[lst2.length-2].value+"*"
		}
	}
}

function añadir_lst2(lst,lst1,lst2,v_lst3,txt,tipo)
{
	//obtener el seleccionado en  lst1-----> lst1.options[indexSelected-1]
	var v= new Array();
	if (isNum(txt.value))
	{
		i=0;
		if (lst1.selectedIndex>-1) // Si hay algún artículo seleccionado
		{
			// comprobar que no esté en lst2
			i=0;
			j=0;//no está
			v=lst.value.split("*")
			while ((v[i]!=null)&&(j==0)&&(v[i]!="")) // Mientras no llegues al final
			{
				
				if (v[i].split("|")[0]!=lst1.options[lst1.selectedIndex].value) 
				//Si es distinto y no estás en blanco, se recorre
				{
					i++;
				}
				else
				{
					j=1; //está.
				}
			}
			if (j==0)//Si no está
			{
				lst2[lst2.length]=new Option("", "-1","", false) // Ahora tenemos dos vacios
				lst2[lst2.length-1].text="........................................................................" 
				lst2[lst2.length-2].text=lst1.options[lst1.selectedIndex].text + " -> " + txt.value
				lst2[lst2.length-2].value=lst1.options[lst1.selectedIndex].value
				lst.value=lst.value+lst2[lst2.length-2].value+"|"+v_lst3+"|"+txt.value+"|"+tipo+"*"
			}
		}
	}
}

function quitar_lst(lst,ind,lst2)
{
	var v= new Array()
	v=lst.value.split("*")
	cad="";
	if (lst2.options[lst2.selectedIndex].value!="-1")
	{
		lst.value="";
		i=0;
		while(v[i]!="")
		{
				//cad =cad + "v[" + i + "]="  + v[i] + " comparado con [" + lst2.selectedIndex +  "]= " + lst2.options[lst2.selectedIndex].value
				//alert(cad)
				if (v[i]!=lst2.options[lst2.selectedIndex].value)
				{
					lst.value=lst.value + v[i] + "*"
				}
				i++;
		}
		lst2.options[lst2.selectedIndex]=null
	}
}
function quitar_lst2(lst,ind,lst2)
{
	var v= new Array()
	v=lst.value.split("*")
	var cad="";
	if (lst2.options[lst2.selectedIndex].value!="-1")
	{
		lst.value="";
		i=0;
		while(v[i]!="")
		{
				if ((v[i].split("|")[0])!=(lst2.options[lst2.selectedIndex].value))
				{
					lst.value=lst.value + v[i] + "*";
				}
				i++;
		}
		lst2.options[lst2.selectedIndex]=null
	}
}

// Calcula el TOTAL de un producto
function calculaO(precio,cantidad,im,of,obj,obj2)
{
	if (cantidad.value!="0")
	{
		//alert(precio.options[precio.selectedIndex].text);
		obj.value=precio.value*cantidad.value*(1-(of.value/100))*(1+(im.value/100))
		//alert(precio.options[precio.selectedIndex].text);
		//alert(cantidad.value);
		obj2.value=obj.value
	}
}
function calculaF(precio,cantidad,im,obj,obj2)
{
	if (cantidad.value!="0")
	{
		//alert(precio.options[precio.selectedIndex].text);
		obj.value=precio.value*cantidad.value*(1+(im.value/100));
		//alert(precio.options[precio.selectedIndex].text);
		//alert(cantidad.value);
		obj2.value=obj.value
	}
}
function calculaProm(precio,cantidad,obj,obj2)
{
	if (cantidad.value!="0")
	{
		//alert(precio.options[precio.selectedIndex].text);
		obj.value=precio.value*cantidad.value;
		//alert(precio.options[precio.selectedIndex].text);
		//alert(cantidad.value);
		obj2.value=obj.value
	}
}

function calculaX(precio,cantidad,of,im,obj,obj2)
{
	if (precio.selectedIndex!="0")
	{
		//alert(precio.options[precio.selectedIndex].text);
		obj.value=precio.options[precio.selectedIndex].text*cantidad.value*(1-(of.value/100))*(1+(im.value/100))
		//alert(precio.options[precio.selectedIndex].text);
		//alert(cantidad.value);
		obj2.value=obj.value
	}
	else
	{
	}
}
function calculaU(precio,cantidad,im,obj,obj2)
{
	if (precio.selectedIndex!="0")
	{
		//alert(precio.options[precio.selectedIndex].text);
		obj.value=precio.options[precio.selectedIndex].text*cantidad.value*(1+(im.value/100))
		//alert(precio.options[precio.selectedIndex].text);
		//alert(cantidad.value);
		obj2.value=obj.value
	}
	else
	{
	}
}

// Calcula el Subtotal de un producto

function calculaSubO(precio,cantidad,im,of,obj,obj2)
{
	if (cantidad.value!="0")
	{
		//alert(precio.options[precio.selectedIndex].text);
		obj.value=precio.value*cantidad.value*(1-(of.value/100));
		//alert(precio.options[precio.selectedIndex].text);
		//alert(cantidad.value);
		obj2.value=obj.value
	}
}
function calculaSubF(precio,cantidad,im,obj,obj2)
{
	if (cantidad.value!="0")
	{
		//alert(precio.options[precio.selectedIndex].text);
		obj.value=precio.value*cantidad.value;
		//alert(precio.options[precio.selectedIndex].text);
		//alert(cantidad.value);
		obj2.value=obj.value
	}
}
function calculaSubProm(precio,cantidad,obj,obj2)
{
	if (cantidad.value!="0")
	{
		//alert(precio.options[precio.selectedIndex].text);
		obj.value=precio.value*cantidad.value;
		//alert(precio.options[precio.selectedIndex].text);
		//alert(cantidad.value);
		obj2.value=obj.value
	}
}

function calculaSubX(precio,cantidad,of,im,obj,obj2)
{
	if (precio.selectedIndex!="0")
	{
		//alert(precio.options[precio.selectedIndex].text);
		obj.value=precio.options[precio.selectedIndex].text*cantidad.value*(1-(of.value/100));
		//alert(precio.options[precio.selectedIndex].text);
		//alert(cantidad.value);
		obj2.value=obj.value
	}
	else
	{
	}
}
function calculaSubU(precio,cantidad,im,obj,obj2)
{
	if (precio.selectedIndex!="0")
	{
		//alert(precio.options[precio.selectedIndex].text);
		obj.value=precio.options[precio.selectedIndex].text*cantidad.value;
		//alert(precio.options[precio.selectedIndex].text);
		//alert(cantidad.value);
		obj2.value=obj.value
	}
	else
	{
	}
}

function cargarDivisa(valor,lista)
{
	var i=0;
			while(lista.options[i]!=null)
  		{
				if (lista.options[i].value==valor)
				{
					lista.selectedIndex=i;
					i=1000;
				}
				i++;
  		}
	}
//Coordina dos Listboxes con información asociada directamente campo a campo y calcula el precio de la linea.
function cambia(lst1,lst2,cantidad, obj,of)
{
	lst1.selectedIndex=lst2.selectedIndex;
	cantidad.value="0";
	calcula(lst1,cantidad,obj,of);
}

function cambia2(lst1,lst2,cantidad, obj,of,i)
{
	//alert(lst2.options[lst2.selectedIndex].text);
	lst1.selectedIndex=lst2.selectedIndex;
	i.value=lst2.options[lst2.selectedIndex].text;
	cantidad.value="0";
	calcula(lst1,cantidad,obj,of);
}
	
function getTime() {
var now = new Date()
var hour = now.getHours()
var minute = now.getMinutes()
now = null
var ampm = "" 

if (hour >= 12) {
hour -= 12
ampm = "PM"
} else
ampm = "AM"
hour = (hour == 0) ? 12 : hour

if (minute < 10)
minute = "0" + minute // do not parse this number!

return hour + ":" + minute + " " + ampm
}

function leapYear(year) {
if (year % 4 == 0) // basic rule
return true // is leap year
return false // is not leap year
}

function getDays(month, year) {
var ar = new Array(12)
ar[0] = 31 // January
ar[1] = (leapYear(year)) ? 29 : 28 // February
ar[2] = 31 // March
ar[3] = 30 // April
ar[4] = 31 // May
ar[5] = 30 // June
ar[6] = 31 // July
ar[7] = 31 // August
ar[8] = 30 // September
ar[9] = 31 // October
ar[10] = 30 // November
ar[11] = 31 // December

return ar[month]
}

function getMonthName(month) {
var ar = new Array(12)
ar[0] = "January"
ar[1] = "February"
ar[2] = "March"
ar[3] = "April"
ar[4] = "May"
ar[5] = "June"
ar[6] = "July"
ar[7] = "August"
ar[8] = "September"
ar[9] = "October"
ar[10] = "November"
ar[11] = "December"

return ar[month]
}

function setCal() {
var now = new Date()
var year = now.getYear()
if (year < 1000)
year+=1900
var month = now.getMonth()
var monthName = getMonthName(month)
var date = now.getDate()
now = null

var firstDayInstance = new Date(year, month, 1)
var firstDay = firstDayInstance.getDay()
firstDayInstance = null

var days = getDays(month, year)

drawCal(firstDay + 1, days, date, monthName, year)
}

function drawCal(firstDay, lastDate, date, monthName, year) {
var headerHeight = 50 // height of the table's header cell
var border = 2 // 3D height of table's border
var cellspacing = 4 // width of table's border
var headerColor = "midnightblue" // color of table's header
var headerSize = "+3" // size of tables header font
var colWidth = 30 // width of columns in table
var dayCellHeight = 15 // height of cells containing days of the week
var dayColor = "darkblue" // color of font representing week days
var cellHeight = 20 // height of cells representing dates in the calendar
var todayColor = "red" // color specifying today's date in the calendar
var timeColor = "purple" // color of font representing current time

var text = "" // initialize accumulative variable to empty string
text += '<CENTER>'
text += '<TABLE BORDER=' + border + ' CELLSPACING=' + cellspacing + '>' // table settings
text += '<TH COLSPAN=7 HEIGHT=' + headerHeight + '>' // create table header cell
text += '<FONT COLOR="' + headerColor + '" SIZE=' + headerSize + '>' // set font for table header
text += monthName + ' ' + year 
text += '</FONT>' // close table header's font settings
text += '</TH>' // close header cell

var openCol = '<TD WIDTH=' + colWidth + ' HEIGHT=' + dayCellHeight + '>'
openCol += '<FONT COLOR="' + dayColor + '">'
var closeCol = '</FONT></TD>'

var weekDay = new Array(7)
weekDay[0] = "Sun"
weekDay[1] = "Mon"
weekDay[2] = "Tues"
weekDay[3] = "Wed"
weekDay[4] = "Thu"
weekDay[5] = "Fri"
weekDay[6] = "Sat"

text += '<TR ALIGN="center" VALIGN="center">'
for (var dayNum = 0; dayNum < 7; ++dayNum) {
text += openCol + weekDay[dayNum] + closeCol 
}
text += '</TR>'

var digit = 1
var curCell = 1

for (var row = 1; row <= Math.ceil((lastDate + firstDay - 1) / 7); ++row) {
text += '<TR ALIGN="right" VALIGN="top">'
for (var col = 1; col <= 7; ++col) {
if (digit > lastDate)
break
if (curCell < firstDay) {
text += '<TD></TD>';
curCell++
} else {
if (digit == date) { // current cell represent today's date
text += '<TD HEIGHT=' + cellHeight + '>'
text += '<FONT COLOR="' + todayColor + '">'
text += digit
text += '</FONT><BR>'
text += '<FONT COLOR="' + timeColor + '" SIZE=2>'
text += '<CENTER>' + getTime() + '</CENTER>'
text += '</FONT>'
text += '</TD>'
} else
text += '<TD HEIGHT=' + cellHeight + '>' + digit + '</TD>'
digit++
}
}
text += '</TR>'
}

text += '</TABLE>'
text += '</CENTER>'

document.write(text) 
}

function estadistica()
{

	xajax_estadistica();
	//Arranque2()
}

function regenerarPwd(email,pwd)
{
	var myLocalCallback = xajax.callback.create(10000, 20000);
	xajax_regenerarPwd(email,pwd);
}
function abrir(direccion, pantallacompleta, herramientas, direcciones, estado, barramenu, barrascroll, cambiatamano, ancho, alto, izquierda, arriba, sustituir){
     var opciones = "fullscreen=" + pantallacompleta +
                 ",toolbar=" + herramientas +
                 ",location=" + direcciones +
                 ",status=" + estado +
                 ",menubar=" + barramenu +
                 ",scrollbars=" + barrascroll +
                 ",resizable=" + cambiatamano +
                 ",width=" + ancho +
                 ",height=" + alto +
                 ",left=" + izquierda +
                 ",top=" + arriba;
     var ventana = window.open(direccion,"venta",opciones,sustituir);

}                
function descargar(f)
{
	xajax_descargar(f)
	//document.location.href='http://www.incarsoft.com/web/docs/' + f
}
function descargarAct(f)
{
	xajax_descargarAct(f)
}