
//Advanced Email Check credit-
//By JavaScript Kit (http://www.javascriptkit.com)
//Over 200+ free scripts here!
var panelCorreo = new YAHOO.widget.Panel("ATP_correo",
	{ width:'500px', height:'350px', fixedcenter: true, constraintoviewport: false, underlay:"none", close:true, visible:false, draggable:false, modal:true
		}
	);
	
var myWaitCorreo = new YAHOO.tecnopyme.util.wait("Enviando su petición");
var atpCorreo = "";
var atpNombreCorreo = "";
var atpTelefonoCorreo = "";
var timeOutEnviarCorreo = null;
function scrollCorreo(){
	document.getElementById('scroll_datos').scrollTop = document.getElementById('scroll_datos').scrollHeight;
}

function ATPcheckemail(mail){
var str=mail;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		return true;
	else{
	  	var mensaje = "";
	  	mensaje +="¿Confirma que " + mail + " es su dirección de correo?";
		if (confirm(mensaje))
			return true;
		else
			return false;	
	}
}
function ATPMailWindow(ref, id) {
  var texto = "";
  var tid = "";  
  texto += "<form id='enviar_correo_info'>";
			texto += "<p><label class='left' for='ATP_from'>Correo: </label><input class='text' type='text' id='ATP_from' name='ATP_from'  style='width:200px;' value='" + unescape(atpCorreo) + "'></p>";
			texto += "<p><label class='left' for='ATP_nombre'>Nombre (*): </label><input class='text' type='text' id='ATP_nombre' name='ATP_nombre'  style='width:200px;' value='" + unescape(atpNombreCorreo) + "'></p>";
			texto += "<p><label class='left' for='ATP_tel'>Teléfono (*): </label><input class='text' type='text' id='ATP_tel' name='ATP_tel'  style='width:200px;' value='" + unescape(atpTelefonoCorreo) + "'></p>";
			texto += "<span>Asunto:</span> <span class='alt2'>Información sobre referencia " + ref + "</span>";
		texto += "<textarea  style='margin: 10px;padding: 10px;' rows=8 id='ATP_body" + tid + "' cols=48>Deseo recibir más información sobre este artículo</textarea>";
		texto += "<br /><a href='#' class='outlet' style='margin: 10px;' id='send_mail" + tid + "' name='send_mail' onclick=\"javascript:ATPSendMail('" + ref + "'," + id + "); return false;\"><img src='/css/img/enviar.gif' style='border:0' alt='enviar'></a>";
	texto += "</form>";
	return texto;
}
function ATPMailWindowContacto() {
  var texto = "";
  var tid = "";  
  texto += "<form id='enviar_correo_info'>";
			texto += "<p><label class='left' for='ATP_from'>Correo: </label><input class='text' type='text' id='ATP_from' name='ATP_from'  style='width:200px;' value='" + unescape(atpCorreo) + "'></p>";
			texto += "<p><label class='left' for='ATP_nombre'>Nombre (*): </label><input class='text' type='text' id='ATP_nombre' name='ATP_nombre'  style='width:200px;' value='" + unescape(atpNombreCorreo) + "'></p>";
			texto += "<p><label class='left' for='ATP_tel'>Teléfono (*): </label><input class='text' type='text' id='ATP_tel' name='ATP_tel'  style='width:200px;' value='" + unescape(atpTelefonoCorreo) + "'></p>";
			texto += "<span>Asunto:</span> <span class='alt2'>Solicitud de información</span>";
		texto += "<textarea  style='margin: 10px;padding: 10px;' rows=8 id='ATP_body" + tid + "' cols=48></textarea>";
		texto += "<br /><a href='#' class='outlet' style='margin: 10px;' id='send_mail" + tid + "' name='send_mail' onclick=\"javascript:ATPSendMail('', null); return false;\"><img src='/css/img/enviar.gif' style='border:0' alt='enviar'></a>";
	texto += "</form>";
	return texto;
}

function ATPSendMail(ref, id){
	var correo = document.getElementById("ATP_from").value;
	var cuerpo = document.getElementById("ATP_body").value;
	var tel = document.getElementById("ATP_tel").value;
	var nombre = document.getElementById("ATP_nombre").value;

	if (correo.length > 0){
	  	if (ATPcheckemail(correo) == false)
	  		return;
	}

	if (tel.length == 0){
		alert("Debe proporcionar un teléfono");
		return;
	}

	if (nombre.length == 0){
		alert("Debe proporcionar un nombre");
		return;
	}
	
	if (cuerpo.length == 0){
		alert("Debe proporcionar un asunto");
		return;
	}
	correo = escape (correo);
	cuerpo = escape (cuerpo);
	tel	= escape (tel);
	nombre	= escape (nombre);

	//Guardar en global
	atpCorreo = correo;
	atpNombreCorreo =nombre;
	atpTelefonoCorreo = tel;
	
	panelCorreo.hide();
	myWaitCorreo.show();
	endTimeOut(timeOutEnviarCorreo);
	timeOutEnviarCorreo = beginTimeOut(tiempoTimeOut);
 	var request = '/articulos/json_enviar_correo.php?ref=' + ref + '&from=' + correo + "&body=" + cuerpo + "&nombre=" + nombre + "&tel=" + tel + "&timeout="+timeOutEnviarCorreo;
	if (id)
		request += '&id=' + id;
 	//proccesRequestEnviarCorreo
	//prompt("S", request);
	// Create a new script object
	aObj = new JSONscriptRequest(request);
	// Build the script tag
	aObj.buildScriptTag();
	// Execute (add) the script tag
	aObj.addScriptTag();
}
function proccesRequestEnviarCorreo(jData){
var c;
  if (timeOutEnviarCorreo){
	if (jData != null) {
		if (jData.timeout != timeOutEnviarCorreo)
			return;
	}else{
	  	endTimeOut(timeOutEnviarCorreo);
	  	timeOutEnviarCorreo = null;
		return;
	}
	endTimeOut(timeOutEnviarCorreo);
	timeOutEnviarCorreo = null;
  }
  	myWaitCorreo.hide();
  	if (jData.resultado == 0)
	    c  = new YAHOO.tecnopyme.util.myAlert("Solicitud enviada con éxito");
    else
    	c  = new YAHOO.tecnopyme.util.myAlert("Se ha producido un error al procesar su solicitud");

}
//A esta función se llama desde el grid y desde los listados clásicos
function showMailWindow(ref, id){
	try{
			panel.hide();
	}catch(e){
				;
  }		
	panelCorreo.setHeader("Solicitud de información");
	var texto = "";
	texto += "<div style='width:460px; height:340px; height:*310px;' id='atp_mail'>"
	
	texto += "<div style='text-align:left;' id='caja_correo'>";
	texto += ATPMailWindow(ref, id);	
	texto += "</div>";
	texto +="</div>";

	//panelEspere.hide();
	panelCorreo.setBody(texto);
	panelCorreo.render(document.body);
	panelCorreo.show();

}

function showMailWindowContacto(){
	try{
			panel.hide();
	}catch(e){
				;
  }		
	panelCorreo.setHeader("Solicitud de información");
	var texto = "";
	texto += "<div style='width:460px; height:340px; height:*310px;' id='atp_mail'>"
	
	texto += "<div style='text-align:left;' id='caja_correo'>";
	texto += ATPMailWindowContacto();	
	texto += "</div>";
	texto +="</div>";

	//panelEspere.hide();
	panelCorreo.setBody(texto);
	panelCorreo.render(document.body);
	panelCorreo.show();

}
