var marca=false;


function enviar_presupuesto(){
	var enviar=true;
	var valor;
	var url="?op=enviar_email";
	var error="Tiene los siguientes errores:\r\n";
	if ( document.getElementById('calidades') != null)
	{
		var indice = document.getElementById('calidades').selectedIndex;
		 var valor = document.getElementById('calidades').options[indice].value;
		 var textito = document.getElementById('calidades').options[indice].text;
		if (valor == 0) {
			error+='  -Debe seleccionar calidades.\r\n';
			enviar=false;
		}
		url += '&calidades=' + textito;
	}
	

	
	if ( document.getElementById('algodon') != null && document.getElementById('calidades').selectedIndex == 3)
	{
		var indice = document.getElementById('algodon').selectedIndex;
		 var valor = document.getElementById('algodon').options[indice].value;
		 var textito = document.getElementById('algodon').options[indice].text;
		url += '&algodon=' + textito;
	}
	
		if ( document.getElementById('confeccion') != null)
	{
		var indice = document.getElementById('confeccion').selectedIndex;
		 var valor = document.getElementById('confeccion').options[indice].value;
		  var textito = document.getElementById('confeccion').options[indice].text;
		if (valor == 0) {
			error+='  -Debe seleccionar confecciones.\r\n';
			enviar=false;
		}
		url += '&confeccion=' + textito;
	}
	
			if ( document.getElementById('tipo') != null)
	{
		var indice = document.getElementById('tipo').selectedIndex;
		 var valor = document.getElementById('tipo').options[indice].value;
		  var textito = document.getElementById('tipo').options[indice].text;
		if (valor == 0) {
			error+='  -Debe seleccionar tipo.\r\n';
			enviar=false;
		}
		url += '&tipo=' + textito;
	}
	
	if ( document.getElementById('medida') != null)
	{
		var indice = document.getElementById('medida').selectedIndex;
		 var valor = document.getElementById('medida').options[indice].value;
		  var textito = document.getElementById('medida').options[indice].text;
		if (valor == 0) {
			error+='  -Debe seleccionar medida.\r\n';
			enviar=false;
		}
		url += '&tipo=' + textito;
	}

	if ( document.getElementById('ancho') != null)
	{
	   valor = document.getElementById('ancho').value;
		if (valor == "" || valor == "ancho") {
			error+='  -Debe escribir un ancho.\r\n';
			enviar=false;
		}
		url += '&ancho=' + valor;
	}
	
		if ( document.getElementById('largo') != null)
	{
	   valor = document.getElementById('largo').value;
		if (valor == "" || valor == "largo") {
			error+='  -Debe escribir un largo.\r\n';
			enviar=false;
		}
		url += '&largo=' + valor;
	}
	
	if ( document.getElementById('grosor') != null)
	{
	   valor = document.getElementById('grosor').value;
		if (valor == "" || valor == "grosor") {
			error+='  -Debe escribir un grosor.\r\n';
			enviar=false;
		}
		url += '&grosor=' + valor;
	}
	
		if ( document.getElementById('alto') != null)
	{
	   valor = document.getElementById('alto').value;
		if (valor == "" || valor == "alto") {
			error+='  -Debe escribir un alto.\r\n';
			enviar=false;
		}
		url += '&alto=' + valor;
	}

   valor = document.getElementById('nombre').value;
	if (valor == "" || valor == "Nombre...") {
		error+='  -Debe escribir un nombre.\r\n';
		enviar=false;
	}
	url += '&nombre=' + document.getElementById('nombre').value;

 	
	valor = document.getElementById('email').value;
	if (valor == "" || valor == 'Email...') {
		error+='  -Debe escribir un email.\r\n';
		enviar=false;
	}else{
	if (validar(document.getElementById('email').value)==false){
		error+='  -Debe escribir un email correcto.\r\n';
		enviar=false;
	}
	}
	url += '&email=' + document.getElementById('email').value;
	
	valor = document.getElementById('telefono').value;
	if (valor == '' || valor == 'Telefono...') {
		error+='  -Debe escribir un tel\u00e9fono.\r\n';
		enviar=false;
	}
	url += '&telefono=' + document.getElementById('telefono').value;
	url += '&obs=' + document.getElementById('obs').value;
	url += '&modelo=' + document.getElementById('modelo').value;
	if (enviar) {
		marca = true;
		/*alert(url);*/
		envio_mail('content_presupuesto', url, 'get');
		url="";
	}
	else {
		alert(error);
	}
}

function validar(valor){
    re=/^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$/
    if(!re.exec(valor))    {
        return false;
    }else{
        return true;
    }
}




//********************************************************************************************
//inicializamos la imagen
var imagen_loading='<div class="cortinilla_loading"></div><img class="image_loaging" src="img/loading.gif" alt="Cargando..."/>'; 
var enviado='<div id=\"enviado\" style="background-image: url(\'imgs/ok.jpg\'); background-repeat: no-repeat; padding-left: 21px; padding-right: 2px; font-size: 10px; margin-left: -18px; color: #666; margin-top: 10px;"><strong style="font-size: 11px;">Correo enviado.</strong><br />Gracias por su confianza, pronto nos pondremos en contacto con usted.<br /><br /><a onclick=\"javascript:volver()\">Volver al listado de productos.</a></div>'; 
//********************************************************************************************


function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Error.");
	}
}

function Ajax(id_div,url,method,miga_seccion,nombre_sub){
	marca = false;
	migas1(miga_seccion,nombre_sub);
	ajax = getXmlHttpRequestObject();
    if (ajax.readyState == 4 || ajax.readyState == 0){
		content = document.getElementById(id_div);
	 	content.innerHTML = '';
		ajax.open(method, url, true);
		ajax.onreadystatechange = getajaxresult;
		ajax.send(null);	
		return false;
	}
	
}


function envio_mail(id_div,url,method){
	
	ajax = getXmlHttpRequestObject();
    if (ajax.readyState == 4 || ajax.readyState == 0){
		content = document.getElementById(id_div);
	 	content.innerHTML = '';
		ajax.open(method, url, true);
		ajax.onreadystatechange = getajaxresult;
		ajax.send(null);	
		return false;
	}

}

function pepe()
{
	$("#content_presupuesto").css("display", "block");
	/*$("#enviado").fadeIn(1500);*/
	$("#enviado").css("display", "none");
}

function getajaxresult(){
	if (ajax.readyState == 4 && ajax.status == 200) {
		//var contenido=ajax.responseText.split("##");
		//alert(contenido[0]+'=='+id_div);
		//if (contenido[0] == id_div) {
		//content2 = document.getElementById(contenido[0]);
		//content2.innerHTML = '';
		//contentcontent2.innerHTML = ajax.responseText;
		//}
		//else {
		
		$("#content_presupuesto").css("display", "block");
		if (marca == false) {
			content.innerHTML = ajax.responseText;
		}else{
			content.innerHTML = enviado;
		}
	}
	else {
		content.innerHTML = imagen_loading;
	}
}


function show(id){
	
 if (document.getElementById('calidades').selectedIndex!=3){
	 document.getElementById(id).style.display="none";
 
 }else{
	document.getElementById(id).style.display="block";
 }
}	

function show1()
{
	 if (document.getElementById('tipo').selectedIndex !=0 ){
			$.ajax({
				type: "GET",
		        url: "catalogo.php",
				data: "si=1&opc=" + document.getElementById('tipo').selectedIndex,
			  	success: function(msg){
			    $("#materiales1").html(msg);},
			  dataType:"html",
			   complete:  function(){ $("#materiales1").show(1000);}
			 });
	 
	 }
}

