/*httpRequest*/

var xmlHttp = createXmlHttpRequestObject();

function createXmlHttpRequestObject(){
	var xmlHttp;
	
	try{
		xmlHttp = new XMLHttpRequest();
	}
	catch(e){
		var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
										"MSXML2.XMLHTTP.5.0",
										"MSXML2.XMLHTTP.4.0",
										"MSXML2.XMLHTTP.3.0",
										"MSXML2.XMLHTTP",
										"Microsoft.XMLHTTP");
		for(var i = 0; i < XmlHttpVersions.lenght && !xmlHttp; i++){
			try{
				xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
			}
			catch(e){}
		}
	}
	if(!xmlHttp){
		alert("Error al crear el objeto XMLHttpRequest");
	}
	else{
		return xmlHttp;
	}
}

/*Funciones Galeria*/

function requestGallery(galleryId){
	parameter = "id=" +  galleryId;
	if(xmlHttp){
		try{
			xmlHttp.open("POST","sideS/req_gallery.php",true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.onreadystatechange = galleryResponse;
			xmlHttp.send(parameter);
		}
		catch(e){
			alert("No se pudo establecer comunicación con el servidor" + e.toString());
		}
	}
}

function galleryResponse(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			try{
				galleryData();
			}
			catch(e){
				alert("Error al leer la respuesta" + e.toString());
			}
		}
		else{
			alert("Error al recibir la respuesta\n" + xmlHttp.statusText);
		}
	}
}

function galleryData(){
	var xmlResponse = xmlHttp.responseXML;
	if(!xmlResponse && !xmlResponse.documentElement){
		throw("Inválida estructura del XML:\n" + xmlHttp.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if(rootNodeName == "parsererror"){
		throw("Estructura inválida del XML:");
	}
	
	xmlRoot = xmlResponse.documentElement;
	idProduct = xmlRoot.getElementsByTagName("id");
	codeProduct = xmlRoot.getElementsByTagName("code");
	detProduct = xmlRoot.getElementsByTagName("detail");
	imgProduct = xmlRoot.getElementsByTagName("image");
	
	if(idProduct.length < 0){
		prodGallery = "<h1>No hay elementos para mostrar</h1>";
		divGallery = document.getElementById("galeria");
		divGallery.innerHTML = prodGallery;	
	}
	else{
	var colsGallery;
	prodGallery = "<table id='show_gallery'>\n <tbody>\n";
	prodGallery += "<tr>\n";
	var counter = 1;
	
	for(i=0; i<idProduct.length; i++){
		totalImgs = idProduct.length - 1;
		colsGallery = counter * 2;
		prodGallery += "<td class='img_product'>";
		prodGallery += "<button class='product_btn' onclick='showDetails("+ idProduct.item(i).firstChild.data +")'>";
		prodGallery += "<img src='catalogo/thumb/" + imgProduct.item(i).firstChild.data + "' alt='" + detProduct.item(i).firstChild.data +"'/></button><p>"+ codeProduct.item(i).firstChild.data +"</p></td>";
		if(counter == 3){
			prodGallery += "</tr><tr>";
			counter = 0;
			
		}
		if(i == totalImgs){
			prodGallery += "</tr>";
		}
		counter++;
	}
	prodGallery += "</tbody></table>";
	divGallery = document.getElementById("galeria");
	divGallery.innerHTML = prodGallery;
	}
}

function showDetails(idProduct){
	$("div#content_dsp").slideUp();
	$("div#foot_dsp").css({top:0});	
	product = "id=" + idProduct;
	if(xmlHttp){
		try{
			xmlHttp.open("POST","sideS/req_details.php",true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.onreadystatechange = detailsResponse;
			xmlHttp.send(product);
		}
		catch(e){
			alert("No se pudo establecer comunicación con el servidor" + e.toString());
		}
	}
}

function detailsResponse(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			try{
				detailsData();
			}
			catch(e){
				alert("Error al leer la respuesta" + e.toString());
			}
		}
		else{
			alert("Error al recibir la respuesta\n" + xmlHttp.statusText);
		}
	}
}

function detailsData(){
	var xmlResponse = xmlHttp.responseXML;
	if(!xmlResponse && !xmlResponse.documentElement){
		throw("Inválida estructura del XML:\n" + xmlHttp.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if(rootNodeName == "parsererror"){
		throw("Estructura inválida del XML:");
	}
	$("div#cont_clip").load("content/clip.htm",function(){
		$(this).slideDown(1000);
		loadDataClip(xmlResponse);
	});
        
	}

function loadDataClip(xmlResponse){	
	xmlDetails = xmlResponse.documentElement;
	idDetail = xmlDetails.getElementsByTagName("id_d");
	codeDetail = xmlDetails.getElementsByTagName("code_d");
	detailDetail = xmlDetails.getElementsByTagName("detail_d");
	sizeDetail = xmlDetails.getElementsByTagName("size_d");
	colorDetail = xmlDetails.getElementsByTagName("color_d");
	imageDetail = xmlDetails.getElementsByTagName("image_d");
	miniDetail = xmlDetails.getElementsByTagName("min_d");
	
	cellId = $("input#id_price");
	cellCode = $("td#info_clave");
	cellDet = $("td#info_det");
	cellSize = $("td#info_med");
	cellColor = $("td#info_color");
	cellImage = $("img#info_image");
	cellPrice = $("select#scale_price");
	 
	cellId.value = idDetail.item(0).firstChild.data;
	cellCode.html(codeDetail.item(0).firstChild.data);
	cellDet.html(detailDetail.item(0).firstChild.data);
	cellSize.html(sizeDetail.item(0).firstChild.data);
	cellColor.html(colorDetail.item(0).firstChild.data);
	cellImage.attr("src","catalogo/large/" + imageDetail.item(0).firstChild.data);
	miniSell = miniDetail.item(0).firstChild.data;
	
	if(miniSell == 25){
		for(i = 0; i < 4; i++){
			if(i == 0){
				stringOption = "25 - 125";
			}
			if(i == 1){
				stringOption = "125 - 250";
			}
			if(i == 2){
				stringOption = "250 - 500";
			}
			if(i == 3){
				stringOption = "500 o +";
			}
			var objOption = document.createElement("option");
			objOption.value = i + 1;
			objOption.innerHTML = stringOption;
			cellPrice.append(objOption);
		}
	}
	else if(miniSell == 100){
		for(i = 0; i < 4; i++){
			if(i == 0){
				stringOption = "100";
			}
			if(i == 1){
				stringOption = "101 - 250";
			}
			if(i == 2){
				stringOption = "251 - 500";
			}
			if(i == 3){
				stringOption = "501 o +";
			}
			var objOption = document.createElement("option");
			objOption.value = i + 1;
			objOption.innerHTML = stringOption;
			cellPrice.append(objOption);
		}
	}
	else if(miniSell == 250){
		for(i = 0; i < 4; i++){
			if(i == 0){
				stringOption = "250";
			}
			if(i == 1){
				stringOption = "251 - 500";
			}
			if(i == 2){
				stringOption = "501 - 1000";
			}
			if(i == 3){
				stringOption = "1001 a 2500 o +";
			}
			var objOption = document.createElement("option");
			objOption.value = i + 1;
			objOption.innerHTML = stringOption;
			cellPrice.append(objOption);
		}
	}
	else if(miniSell == 500){
		for(i = 0; i < 4; i++){
			if(i == 0){
				stringOption = "500";
			}
			if(i == 1){
				stringOption = "501 - 1000";
			}
			if(i == 2){
				stringOption = "1001 - 2000";
			}
			if(i == 3){
				stringOption = "2001 a 5000 o +";
			}
			var objOption = document.createElement("option");
			objOption.value = i + 1;
			objOption.innerHTML = stringOption;
			cellPrice.append(objOption);
		}
	}
}

function showPrice(){
	id_price = cellId.value;
	parameter_price = "id=" + id_price;
	if(xmlHttp){
		try{
			xmlHttp.open("POST","sideS/req_prices.php",true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.onreadystatechange = pricesResponse;
			xmlHttp.send(parameter_price);
		}
		catch(e){
			alert("No se pudo establecer comunicación con el servidor" + e.toString());
		}
	}
}

function pricesResponse(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			try{
				pricesData();
			}
			catch(e){
				alert("Error al leer la respuesta" + e.toString());
			}
		}
		else{
			alert("Error al recibir la respuesta\n" + xmlHttp.statusText);
		}
	}
}

function pricesData(){
	var xmlResponse = xmlHttp.responseXML;
	if(!xmlResponse && !xmlResponse.documentElement){
		throw("Inválida estructura del XML:\n" + xmlHttp.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if(rootNodeName == "parsererror"){
		throw("Estructura inválida del XML:");
	}
	
	var priceRes;
	objSelect = document.getElementById("scale_price");
	var indx_price = objSelect.selectedIndex;
	var num_price = objSelect.options[indx_price].value;
	xmlPrices = xmlResponse.documentElement;
	price1 = xmlPrices.getElementsByTagName("cost1");
	price2 = xmlPrices.getElementsByTagName("cost2");
	price3 = xmlPrices.getElementsByTagName("cost3");
	price4 = xmlPrices.getElementsByTagName("cost4");
	
	if(num_price == 1){
		priceRes = price1.item(0).firstChild.data;
	}
	else if(num_price == 2){
		priceRes = price2.item(0).firstChild.data;
	}	
	else if(num_price == 3){
		priceRes = price3.item(0).firstChild.data;
	}
	else if(num_price == 4){
		priceRes = price4.item(0).firstChild.data;
	}
	else{
		priceRes = "Precio";
	}
	
	textPrice = document.getElementById("price_text");
	textPrice.innerHTML = "$ " + priceRes + " pza.";
}

/*Search Engine*/

function searchProduct(){
	searchString = document.getElementById("search_field").value;
	searchParameter = "search=" + searchString;
	if(xmlHttp){
		try{
			xmlHttp.open("POST","sideS/search_pdto.php",true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.onreadystatechange = searchResponse;
			xmlHttp.send(searchParameter);
		}
		catch(e){
			alert("No se pudo establecer comunicación con el servidor" + e.toString());
		}
	}
}

function searchResponse(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			try{
				searchData();
			}
			catch(e){
				alert("Error al leer la respuesta" + e.toString());
			}
		}
		else{
			alert("Error al recibir la respuesta\n" + xmlHttp.statusText);
		}
	}
}

function searchData(){
	var xmlResponse = xmlHttp.responseXML;
	if(!xmlResponse && !xmlResponse.documentElement){
		throw("Inválida estructura del XML:\n" + xmlHttp.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if(rootNodeName == "parsererror"){
		throw("Estructura inválida del XML:");
	}
	
	xmlRoot = xmlResponse.documentElement;
	idProduct = xmlRoot.getElementsByTagName("id");
	codeProduct = xmlRoot.getElementsByTagName("code");
	detProduct = xmlRoot.getElementsByTagName("detail");
	imgProduct = xmlRoot.getElementsByTagName("image");
	
	if(idProduct.length < 0){
		prodGallery = "<h1>No hay elementos para mostrar</h1>";
		divGallery = document.getElementById("galeria");
		divGallery.innerHTML = prodGallery;	
	}
	else{
	var colsGallery;
	prodGallery = "<table id='show_gallery'>\n <tbody>\n";
	prodGallery += "<tr>\n";
	var counter = 1;
	
	for(i=0; i<idProduct.length; i++){
		totalImgs = idProduct.length - 1;
		colsGallery = counter * 2;
		prodGallery += "<td class='img_product'>";
		prodGallery += "<button class='product_btn' onclick='showDetails("+ idProduct.item(i).firstChild.data +")'>";
		prodGallery += "<img src='catalogo/thumb/" + imgProduct.item(i).firstChild.data + "' alt='" + detProduct.item(i).firstChild.data +"'/></button><p>"+ codeProduct.item(i).firstChild.data +"</p></td>";
		if(counter == 3){
			prodGallery += "</tr><tr>";
			counter = 0;
			
		}
		if(i == totalImgs){
			prodGallery += "</tr>";
		}
		counter++;
	}
	prodGallery += "</tbody></table>";
	divGallery = document.getElementById("galeria");
	divGallery.innerHTML = prodGallery;
	}
}

/*Forma Contacto*/

function valContact(origin){
	nombreContacto = origin.nombre_contacto.value;
	telContacto = origin.tel_contacto.value;
	mailContacto = origin.mail_contacto.value;
	responseMsj = document.getElementById("state_msj");
	
	if(nombreContacto == ""){
		responseMsj.innerHTML = "Ingresa tu Nombre";
		return false;
	}
	else if(mailContacto == ""){
		responseMsj.innerHTML = "Ingresa una Dirección de Correo";
		return false;
	}
	else if(mailContacto.indexOf("@") == -1){
		responseMsj.innerHTML = "Ingresa un Correo Válido";
		return false;
	}
	else{
		return true;
	}
}

function sendMessage(forma){
	contactName = forma.nombre_contacto.value;
	contactCo = forma.empresa_contacto.value;
	contactTel = forma.tel_contacto.value;
	contactMail = forma.mail_contacto.value;
	contactMess = forma.msg_contacto.value;
	
	parameterContact = "name=" + contactName + "&com=" + contactCo + "&tel=" + contactTel + "&mail=" + contactMail + "&message=" + contactMess;
	
	if(xmlHttp){
		try{
			xmlHttp.open("POST","sideS/send_mail.php",true);
			xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlHttp.onreadystatechange = messageResponse;
			xmlHttp.send(parameterContact);
		}
		catch(e){
			alert("No se pudo establecer comunicación con el servidor" + e.toString());
		}
	}
}

function messageResponse(){
	if(xmlHttp.readyState == 4){
		if(xmlHttp.status == 200){
			try{
				printResult();
			}
			catch(e){
				alert("Error al leer la respuesta" + e.toString());
			}
		}
		else{
			alert("Error al recibir la respuesta\n" + xmlHttp.statusText);
		}
	}
}

function printResult(){
	var xmlResponse = xmlHttp.responseXML;
	if(!xmlResponse && !xmlResponse.documentElement){
		throw("Inválida estructura del XML:\n" + xmlHttp.responseText);
	}
	var rootNodeName = xmlResponse.documentElement.nodeName;
	if(rootNodeName == "parsererror"){
		throw("Estructura inválida del XML:");
	}
	
	msjPrint = document.getElementById("state_msj");
	xmlContactResponse = xmlResponse.documentElement;
	responseMail = xmlContactResponse.getElementsByTagName("response");
	
	if(responseMail == 0){
		msjPrint.innerHTML = "Su mensaje no ha podido ser enviado!";
	}
	else{
		msjPrint.innerHTML = "Su mensaje ha sido enviado!";
		cleanFormContact();
	}
}

function cleanFormContact(){
	document.contacto.nombre_contacto.value = "";
	document.contacto.empresa_contacto.value = "";
	document.contacto.tel_contacto.value = "";
	document.contacto.mail_contacto.value = "";
	document.contacto.msg_contacto.value = "";
}

/*Comportamiento*/

$(document).ready(function(){
	var slide = false;
	$("div#section_site").load("content/alfa.htm",function(){
		$(this).fadeIn(2000);
	});
	$("div#content_dsp").hide();
	$("div#head_dsp,div#foot_dsp").click(function(){
		if(!slide){
			$("div#content_dsp").slideDown();
			$("div#foot_dsp").css({top:-3});
			slide = true;
		}
		else{
			$("div#content_dsp").slideUp();
			$("div#foot_dsp").css({top:0});
			slide = false;
		}
	});
	
	
	$("li#alfa_btn,li#alfabtn_foot").click(function(){
		if(slide){
			$("div#content_dsp").slideUp();
			$("div#foot_dsp").css({top:0});
			slide = false;	
		}
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/alfa.htm",function(){
			$(this).fadeIn(2000);
			});	
		});	
	});
	
	$("li#contacto_btn,li#contactobtn_foot").click(function(){
		if(slide){
			$("div#content_dsp").slideUp();
			$("div#foot_dsp").css({top:0});
			slide = false;	
		}
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/contacto.htm",function(){
			$(this).fadeIn(2000);
			});	
		});
	});	
	
	$("li#servicios_btn,li#serviciosbtn_foot").click(function(){
		if(slide){
			$("div#content_dsp").slideUp();
			$("div#foot_dsp").css({top:0});
			slide = false;	
		}
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/servicios.htm",function(){
			$(this).fadeIn(2000);
			});	
		});
	});	
	
	$("li#boli_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(1);
			});	
		});
	});
	
	$("li#calc_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(2);
			});	
		});
	});
	
	$("li#compu_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(3);
			});	
		});
	});
	
	$("li#herra_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(4);
			});	
		});
	});
	
	$("li#llavme_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(5);
			});	
		});
	});
	
	$("li#llavpl_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(6);
			});	
		});
	});
	
	$("li#male_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(7);
			});	
		});
	});
	
	$("li#nino_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(8);
			});	
		});
	});
	
	$("li#ofi_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(9);
			});	
		});
	});
	
	$("li#pers_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(10);
			});	
		});
	});
	
	$("li#rel_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(12);
			});	
		});
	});
	
	$("li#reg_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(11);
			});	
		});
	});
	
	$("li#var_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			requestGallery(13);
			});	
		});
	});
	
	$("input#search_btn").click(function(){
		$("div#content_dsp").slideUp();
		slide = false;
		$("div#foot_dsp").css({top:0});
		$("div#section_site").empty().slideUp(1000,function(){
			$("div#section_site").load("content/galeria.htm",function(){
			$("div#section_site").fadeIn(2000);
			searchProduct()
			});	
		});
	});
	
	
});
