﻿//declaro els objectes HTTPREQUEST
var http_upd_add_prod;
var http_upd_acc_prod;
var http_upd_acc_busc;
var http_upd_arbre;
var http_upd_precio;
var http_upd_image_prod;

function update_add_prod_div(id){
	vars = get_query("prod_form");
	dir = "../productos/inc_resp/upd_add_prod.php" + vars + "&cat=" + id;	
	http_upd_add_prod = getXmlHttpObject();
	http_upd_add_prod.open("GET", dir, true);
  http_upd_add_prod.onreadystatechange = handle_add_prod_div;
  http_upd_add_prod.send(null);
}

function handle_add_prod_div(){
	if (http_upd_add_prod.readyState == 4) {
		document.getElementById('add_prod').innerHTML = http_upd_add_prod.responseText;
	} 
}

function update_arbre_div(id){
	vars = get_query("prod_form");
	dir = "../productos/inc_resp/upd_arbre.php" + vars + "&cat=" + id;
	alert(dir);
	http_upd_arbre = getXmlHttpObject();
	http_upd_arbre.open("GET", dir, true);
  http_upd_arbre.onreadystatechange = handle_arbre_div;
  http_upd_arbre.send(null);
	
}

function handle_arbre_div(){
	if (http_upd_arbre.readyState == 4) {
		document.getElementById('arbre').innerHTML = http_upd_arbre.responseText;
	} 
}

function update_acc_prod_div(id){
	vars = get_query("prod_form");
	dir = "../includes/listado.php" + vars + "&cat=" + id;
	//alert(dir);
	http_upd_acc_prod = getXmlHttpObject();
	http_upd_acc_prod.open("GET", dir, true);	
  http_upd_acc_prod.onreadystatechange = handle_acc_prod_div;
  http_upd_acc_prod.send(null);
  
  //	muestro loading.
	document.getElementById('acc_prod').innerHTML = '<div id="loading" style="text-align:center;"></div>';
	
	var so = new SWFObject("../media/loading.swf", "flashmovie", "300", "30", "9", "#FFFFFF");
	so.addParam("wmode", "transparent");
	so.write("loading");
}

function handle_acc_prod_div(){
	if (http_upd_acc_prod.readyState == 4) {
		document.getElementById('acc_prod').innerHTML = http_upd_acc_prod.responseText;
		document.getElementById('busca_bot').disabled = false;
	}
}

function handle_validaProd(){
	if(http.readyState == 4){
		//alert(http.responseText);
		resp_back(http.responseText);
	}
}

function upd_precio_div(tipo_operacion,ididioma) {
	dir = "../includes/combo_precios.php?ididioma=" + ididioma + "&operacion_f=" + tipo_operacion;
	//alert(dir);
	http_upd_precio = getXmlHttpObject();
	http_upd_precio.open("GET", dir, true);
 	http_upd_precio.onreadystatechange = handle_precio_div;
 	http_upd_precio.send(null);
}
	
function handle_precio_div(){
	if (http_upd_precio.readyState == 4) {
		//alert(http_upd_precio.responseText);
		document.getElementById('combo_precios').innerHTML = http_upd_precio.responseText;
	} 
}

function update_acc_busc_div(id){
	vars = get_query("prod_form");
	dir = "../includes/buscador_res.php" + vars + "&cat=" + id;
	//alert(dir);
	http_upd_acc_busc = getXmlHttpObject();
	http_upd_acc_busc.open("GET", dir, true);
  http_upd_acc_busc.onreadystatechange = handle_acc_busc_div;
  http_upd_acc_busc.send(null);
}

function handle_acc_busc_div(){
	if (http_upd_acc_busc.readyState == 4) {
		document.getElementById('acc_busc').innerHTML = http_upd_acc_busc.responseText;
		document.getElementById('busca_bot').disabled = false;
	}
}

function upd_image_prod_div(id_image,prod) {
	dir = "../includes/image_prod.php?id_image=" + id_image + "&prod=" + prod;
	//alert(dir);
	http_upd_image_prod = getXmlHttpObject();
	http_upd_image_prod.open("GET", dir, true);
 	http_upd_image_prod.onreadystatechange = handle_image_prod_div;
 	http_upd_image_prod.send(null);
}
	
function handle_image_prod_div(){
	if (http_upd_image_prod.readyState == 4) {
		document.getElementById('image_prod').innerHTML = http_upd_image_prod.responseText;
	}
}


/*
FUNCIONS PER A CONTROLAR LA PUJADA DE IMATGES
*/

function valida_img(id_img, nom, w, h, w_th, h_th){
	var form = document.getElementById("prod_form");
	if(document.getElementById("f_" + nom).value != ""){
		vars = "?id_img=" + id_img;
		vars = vars + "&nom=" + nom;
		vars = vars + "&w=" + w;
		vars = vars + "&h=" + h;
		vars = vars + "&w_th=" + w_th;
		vars = vars + "&h_th=" + h_th;
		form.action = "../core/inc/uploadImg.php" + vars;
		form.target = "upload_file_frame";
		form.submit();
	}
}

function recive_img(id_img, nom, path, w, h, w_th, h_th){
	var form = document.getElementById("prod_form");
	document.getElementById(nom).value = path;
	new_html = "";
	new_html = new_html + "<img src=\"../dades/img/temp/thumbs/" + path + "\">\n";
	new_html = new_html + "<input class=\"textos\" type=\"button\" value=\"Cambiar imagen\"";
	new_html = new_html + " onclick=\"change_img(" + id_img + ",'" + nom + "'," + w + "," + h + "," + w_th + "," + h_th + ");\">";
	
  document.getElementById("image_field_" + id_img).innerHTML = new_html; 
}

function change_img(id_img, nom, w, h, w_th, h_th){
	
	new_html = "";
	new_html = new_html + "<input type=\"file\" name=\"f_" + nom + "\" id=\"f_" + nom +  "\" class=\"textos\" />";
	new_html = new_html + "<input class=\"textos\" type=\"button\" value=\"Subir imagen\"";
	new_html = new_html + " onclick=\"valida_img(" + id_img + ",'" + nom + "'," + w + "," + h + "," + w_th + "," + h_th + ");\">";
  
  document.getElementById("image_field_" + id_img).innerHTML = new_html; 
}

/*
FUNCIONS PER A CONTROLAR LA PUJADA DE VIDEOS
*/

function valida_video(id_video, nom){
	form = document.getElementById("prod_form");
	if(document.getElementById("f_" + nom).value != ""){
		vars = "?id_video=" + id_video;
		vars = vars + "&nom=" + nom;
		form.action = "../core/inc/uploadVideo.php" + vars;
		form.target = "upload_file_frame";
		form.submit();
	}
}

function recive_video(id_video, nom, path){
	form = document.getElementById("prod_form");
	document.getElementById(nom).value = path;
	new_html = "";
	new_html = new_html + "<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\" width=\"305\" height=\"255\" id=\"reproductor\" align=\"middle\">";
	new_html = new_html + "<param name=\"allowScriptAccess\" value=\"sameDomain\" />";
	new_html = new_html + "<param name=\"movie\" value=\"../core/media/rep_video.swf?video_path=../../dades/video/temp/" + path + "\" />";
	new_html = new_html + "<param name=\"quality\" value=\"high\" />";
	new_html = new_html + "<param name=\"bgcolor\" value=\"#ffffff\" />";
	new_html = new_html + "<embed src=\"../core/media/rep_video.swf?video_path=../../dades/video/temp/" + path + "\" quality=\"high\" bgcolor=\"#ffffff\" width=\"305\" height=\"255\" name=\"reproductor\" align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" />";
	new_html = new_html + "</object>";
	new_html = new_html + "<input class=\"textos\" type=\"button\" value=\"Cambiar video\"";
	new_html = new_html + " onclick=\"change_video(" + id_video + ",'" + nom + "');\">";
	
  document.getElementById("video_field_" + id_video).innerHTML = new_html; 
}

function change_video(id_video, nom){
	
	new_html = "";
	new_html = new_html + "<input type=\"file\" name=\"f_" + nom + "\" id=\"f_" + nom +  "\" class=\"textos\" />";
	new_html = new_html + "<input class=\"textos\" type=\"button\" value=\"Subir video\"";
	new_html = new_html + " onclick=\"valida_video(" + id_video + ",'" + nom + "');\">";
  document.getElementById("video_field_" + id_video).innerHTML = new_html; 
}


/******************************************/
/* FUNCIONS PER CONTROLAR ELS DICCIONARIS */
/******************************************/

function valida_combos(formulari){
	//alert("valida combos...");
	if(document.getElementById(formulari).alquiler_compra.value == ""){
		alert("Debe rellenar el campo Operacion");
		return false;
	}
	if(document.getElementById(formulari).combo_pais.value == ""){
		alert("Debe rellenar el campo Pais");
		return false;
	}
	if(document.getElementById(formulari).combo_zona.value == ""){
		alert("Debe rellenar el campo Zona");
		return false;
	}
	if(document.getElementById(formulari).combo_tipo.value == ""){
		alert("Debe rellenar el campo Tipo");
		return false;
	}
	
	return true;
}

function upd_combo(){
	dir = "../productos/inc_resp/resp_upd_combos.php";
	sendQuerystring(dir, handle_upd_combo);
}

function handle_upd_combo(){
	if (http.readyState == 4) {
		document.getElementById('combos').innerHTML = http.responseText;
	}	
}

function popup_combos(combo){
	theURL = "../productos/admin_combos.php?combo=" + combo;
	winName = "Diccionarios";
	
	w = 550;
	h = 400;
	
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	
	features = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes';
	
	win = window.open(theURL,winName,features);
	
}
