var RequestObject = false;
// var Archivo = 'http://localhost/ajax.php'; //directorio donde tenemos el archivo ajax.php

if (window.XMLHttpRequest) //
RequestObject = new XMLHttpRequest();

if (window.ActiveXObject)
RequestObject = new ActiveXObject("Microsoft.XMLHTTP");

function ReqChange() {
// Si se ha recibido la información correctamente
	if (RequestObject.readyState==4) {

		// si la información es válida
		if (RequestObject.responseText.indexOf('invalid') == -1)
		{
// obtener la respuesta
// Buscamos la div con id datos
		document.getElementById("mostrar").innerHTML=RequestObject.responseText;
	}else {
// Por si hay algun error
		document.getElementById("mostrar").innerHTML = "Error llamando";
	}
}
}

function ValidarCampo(campo,name){	
	var campo1 = allTrim(campo);
		if(campo1.length==0){
			alert(name);
			return false;
		}else{
			return true;
		}
}

function lTrim(sStr){
while (sStr.charAt(0) == " ")
sStr = sStr.substr(1, sStr.length - 1);
return sStr;
}
function rTrim(sStr){
while (sStr.charAt(sStr.length - 1) == " ")
sStr = sStr.substr(0, sStr.length - 1);
return sStr;
}
function allTrim(sStr){
return rTrim(lTrim(sStr));
}


function agregar_usuario(){
		band="No";
		// alert(retorno);
			if (ValidarCampo(document.frmCustomerDetail.txtCustomerFirstName.value,"Escriba su Nombre !!!"))
			if (ValidarCampo(document.frmCustomerDetail.txtCustomerLastName.value,"Escriba su Apellido !!!"))
			if (ValidarCampo(document.frmCustomerDetail.txtCustomerEmail.value ," Escriba su Email"))
			if (ValidarCampo(document.frmCustomerDetail.txtCI.value ,"Por favor escriba su CEDULA DE IDENTIDAD O RIF !!!"))
			if (ValidarCampo(document.frmCustomerDetail.txtCustomerCountry.value,"Escriba su  PAIS"))		
			if (ValidarCampo(document.frmCustomerDetail.txtCustomerState.value ,"Escriba el  ESTADO de su Pais"))
			if (ValidarCampo(document.frmCustomerDetail.txtCustomerCity.value ,"Escriba La CIUDAD donde reside"))		
			if (ValidarCampo(document.frmCustomerDetail.txtCustomerAddress1.value ,"Dirección"))		
			if (ValidarCampo(document.frmCustomerDetail.txtCustomerAddressCreditCard.value ,"Por favor indique la Dirección de Facturación de su Tarjeta de Credito (donde Ud. recibe su estado de cuenta)!!!"))
			if (ValidarCampo(document.frmCustomerDetail.txtPhoneMobile.value ,"Por favor correctamente su número Celular !!!"))
			if (ValidarCampo(document.frmCustomerDetail.txtAreaMobile.value ,"Por favor correctamente su número Celular !!!"))
			if (ValidarCampo(document.frmCustomerDetail.txtUserName.value ,"Por favor escriba el nombre de  USUARIO !!!"))
			if (ValidarCampo(document.frmCustomerDetail.txtPassword.value ,"Por favor escriba el PASSWORD de su cuenta !!!"))
			if (ValidarCampo(document.frmCustomerDetail.txtRePassword.value ,"Por favor RE-ESCRIBA su PASSWORD de confirmación!!!"))
				retorno = "Si";	
			if ((document.frmCustomerDetail.txtPassword.value >"") && (document.frmCustomerDetail.txtRePassword.value>"")) 
			{
			 if (document.frmCustomerDetail.txtPassword.value != document.frmCustomerDetail.txtRePassword.value) 
			 	{
			 	alert("La información de su Password y confirmación no coinciden, Por favor vuelva a escribir su Passwowrd y su confirmación !!!");
				retorno="no";
				}
			}	
				//alert(retorno);
		if(retorno == "Si"){
				band="Si";
			}
			//alert(band);
			
		if(band=="Si"){
			nombre=document.getElementById("txtCustomerFirstName").value;
			apellido=document.getElementById("txtShippingLastName5").value;
			email=document.getElementById("txtCustomerEmail2").value;
			cedula=document.getElementById("txtCI").value;
			pais=document.getElementById("txtCustomerCountry").value;
			estado=document.getElementById("txtCustomerState").value;
			ciudad=document.getElementById("txtCustomerCity").value;
			hab=document.getElementById("textarea4").value;
			fact=document.getElementById("txtCustomerAddressCreditCard").value;
			area_cel=document.getElementById("txtAreaMobile2").value;
			cel=document.getElementById("txtPhoneMobile").value;
			area_casa=document.getElementById("txtAreaHome2").value;
			casa=document.getElementById("txtPhoneCasa").value;
			area_ofic=document.getElementById("txtAreaOffice2").value;
			ofic=document.getElementById("txtPhoneOffice").value;
			sexo=document.getElementById("select6").value;
			d_nac=document.getElementById("select3").value;
			m_nac=document.getElementById("select4").value;
			a_nac=document.getElementById("select5").value;
			usuario=document.getElementById("txtUserName").value;
			pass=document.getElementById("txtPassword").value;
			pass2=document.getElementById("txtRePassword").value;
			parametros="txtUserName="+usuario+"&txtCustomerEmail="+email+"&txtCustomerFirstName="+nombre+"&txtCustomerLastName="+apellido+"&txtCustomerSex="+sexo+"&year="+a_nac+"&month="+m_nac+"&day="+d_nac+"&txtCI="+cedula+"&txtCustomerAddress1="+hab+"&txtAreaHome="+area_casa+"&txtPhoneCasa="+casa+"&txtAreaOffice="+area_ofic+"&txtPhoneOffice="+ofic+"&txtAreaMobile="+area_cel+"&txtPhoneMobile="+cel+"&txtCustomerCity="+ciudad+"&txtCustomerState="+estado+"&txtCustomerCountry="+pais+"&txtPassword="+pass;
			//alert(parametros);
			RequestObject.open("GET","processregister.php?action=new&"+parametros,true);
			RequestObject.send(null);
			RequestObject.onreadystatechange= cargar_resultado;
		}
}
function cargar_resultado(){
	if(RequestObject.readyState==4){
	  
	  if(RequestObject.responseText=="Ok"){
		window.location="cart.php?action=view&register=ok";  
	  }else{
	  	alert(RequestObject.responseText);
	  }
	}else{
		
	}
}
function modificar_banco_de_imagenes(ruta,codigo){
	RequestObject.open("GET","funciones/banco_imagenes.php?ruta="+ruta+"&codigo="+codigo,true);
	RequestObject.send(null);
	RequestObject.onreadystatechange= mostrar_banco_img;
}

function mostrar_banco_img(){
	if(RequestObject.readyState==4){
		//  alert(RequestObject.responseText);
		 document.getElementById("relacion_imagenes").innerHTML=RequestObject.responseText;
	}
}

function CambiarPrecio(precio,codigo){
	// alert(codigo);
	document.getElementById("precio").value=precio;
	document.getElementById("form1").action="cart.php?action=add&p="+codigo;
	// alert("modificado al precio: "+document.getElementById("precio").value);
}

function TraerCombo(){
	combo=document.getElementById("segun").value;
	if(combo=="---"){
		
	}else{
		// alert(combo);
		RequestObject.open("GET","comboload.php?combo="+combo,true);
		RequestObject.send(null);
		RequestObject.onreadystatechange=cargar_combo;
	}
}

function cargar_combo(){
	if(RequestObject.readyState==4){
		hola=RequestObject.responseXML;
		// alert(RequestObject.responseText);
		v1=hola.getElementsByTagName("valor");
		t1=hola.getElementsByTagName("texto");
		maximo=t1.length;
		document.getElementById("producto_ppal").length=0;
			document.getElementById("producto_ppal").options[0] = new Option(t1[0].firstChild.nodeValue,v1[0].firstChild.nodeValue,"defaultChecked");
			alert(document.getElementById("producto_ppal").options[0].checked);
	}else{
	}
}

function cambio_sub_categoria(){
	categoria=document.getElementById("cboCategory").value;
	if(categoria==""){
		
	}else{
		RequestObject.open("GET","categorias.php?categoria="+categoria,true);
		RequestObject.send(null);
		RequestObject.onreadystatechange=mostrar_subs;
	}
}

function mostrar_subs(){
	comboSubs=document.getElementById("cboSubCat");
	if(RequestObject.readyState==4){
		// alert(RequestObject.responseText);
		comboSubs.length=0;
		epa=RequestObject.responseXML;
		valores=epa.getElementsByTagName("valor");
		textos=epa.getElementsByTagName("texto");
		maximo=valores.length;
		for(var j=0;j<maximo;j++){
			comboSubs.options[j] = new Option(textos[j].firstChild.nodeValue,valores[j].firstChild.nodeValue);
		}
	}
}

function cambiarProducto2(prod,resaltado,tabla,dato){
	RequestObject.open("GET","funciones/cambiosku.php?prod="+prod+"&tabla="+tabla+"&dato="+dato,true);	
	RequestObject.send(null);
	total=document.getElementById("maximo").value;
	// alert(resaltado);
		for(var h=1;h<total;h++){
			document.getElementById(h).style.background="#F6F5EA";
		}
		document.getElementById(resaltado).style.background="#006600";
		// alert(document.getElementById(resaltado).style.background);
	RequestObject.onreadystatechange=cargar_resultado;
}
/*function cargar_resultado2(){
	if(RequestObject.readyState==4){
		hola=RequestObject.responseXML;
		// alert(RequestObject.responseText);
		nombre=hola.getElementsByTagName("nombre");
		d1=hola.getElementsByTagName("descripcion");
		h1=hola.getElementsByTagName("hijos");
		p1=hola.getElementsByTagName("precios_hijos");
		c1=hola.getElementsByTagName("codigo");
		i1=hola.getElementsByTagName("imagen");
		maximo=h1.length;
		document.getElementById("form1").action="cart.php?action=add&p="+c1[0].firstChild.nodeValue;
		document.getElementById("ver_popup").href="javascript:MM_openBrWindow('zoom.php?id="+c1[0].firstChild.nodeValue+"','productos','status=yes,scrollbars=yes,width=500,height=500')";
		document.getElementById("enlace").href="javascript:MM_openBrWindow('zoom.php?id="+c1[0].firstChild.nodeValue+"','productos','status=yes,scrollbars=yes,width=500,height=500')";
		document.getElementById("imagen").src="cuadros.php?img="+i1[0].firstChild.nodeValue+"&anchura=300";
		// alert("cuadros.php?img="+i1[0].firstChild.nodeValue+"&anchura=300");
		document.getElementById("imagen").alt="images/product/"+nombre[0].firstChild.nodeValue;
		// document.getElementById("descripcion").innerHTML=d1[0].firstChild.nodeValue;
		// document.getElementById("titulo_prod").innerHTML=nombre[0].firstChild.nodeValue;
		// document.getElementById("precios").innerHTML="";
		for(var i=0;i<maximo;i++){
		// 	document.getElementById("precios").innerHTML +="<input name='precio_radio' type='radio' value=\""+p1[i].firstChild.nodeValue+"\" onClick=\"CambiarPrecio('"+p1[i].firstChild.nodeValue+"','"+c1[i].firstChild.nodeValue+"')\" checked=\"checked\" />"+h1[i].firstChild.nodeValue+" Bs. ";
		// 	document.getElementById("precios").innerHTML +=p1[i].firstChild.nodeValue+"<br>";
			
		}
	}else{
	}
}*/