var tree_state;
var tree_highlighted_node;
var user_type;
var buser_id;
var basket_lang;
var basket_ctry;
var basket_prod_N;
var id_nom_carpetas;
var id_carpeta_abierta;
var num_baskets;
var num_price_deliveries;
var num_quotations;
var num_orders;
var num_received_elements;
var saved_elements_open;
var fast_delivery_products_open;
var basket_showorderstatus = 0;

function borrar(){
	alert("Parece que funciona!!!");
}

function print_and_close(){
	window.print();
	window.close();
}

function set_cookie_form(lang, ctry){
	if (document.check_form.cookie_checkbox.checked){
		submit_cookie_form(lang, ctry, 'yes');
	}
	else{
		submit_cookie_form(lang, ctry, 'no');
	}
}

function submit_cookie_form(lang, ctry, set_cookie){
	document.cookie_form.lang.value = lang;
	document.cookie_form.ctry.value = ctry;
	document.cookie_form.set_cookie.value = set_cookie;
	document.cookie_form.submit();
}

function submit_template_form(page, tree_options, tree_state, tree_highlighted_node, box){
	document.template_form.tree_options.value = tree_options;
	document.template_form.tree_state.value = tree_state;
	document.template_form.tree_highlighted_node.value = tree_highlighted_node;
	document.template_form.box.value = box;
	document.template_form.action = page;
	document.template_form.submit();
}

function quick_contact(field, text){
	if (field == "message"){
		if (document.quick_contact_form.message.value == text){
			document.quick_contact_form.message.value = "";
		}
	}
	if (field == "email"){
		if (document.quick_contact_form.email.value == text){
			document.quick_contact_form.email.value = "";
		}
	}
}

function locations(field, zip_text){
	if (field == "zip_distributors"){
		if (document.locations_form.zip_distributors.value == zip_text){
			document.locations_form.zip_distributors.value = "";
		}
	}
}

function locations_v2(field_id, initial_value){
	var field = document.getElementById(field_id);
	
	if (field.value == initial_value){
		field.value = "";
	}
}

function check_quick_contact(message1, message2, message3){
	if (document.quick_contact_form.message.value == ""){
		alert(message1);
		return false;
	}
	
	if (document.quick_contact_form.email.value == ""){
		alert(message2);
		return false;
	}
	
	if (!validarEmail(document.quick_contact_form.email.value)){
		alert(message3);
		return false;
	}
	
	document.quick_contact_form.submit()
}

function load_european_locations_iframe(url){
	if (url == ""){
		document.getElementById("european_locations_layer").style.display = "none";
	}
	else{
		document.getElementById("european_locations_layer").style.display = "";
		document.getElementById("european_locations_iframe_id").src = url;
	}
}

function load_half_locations_iframe(span_id, url, iframe_id, oid){
	if (span_id == "distributors_layer"){
		document.getElementById("distributors_layer").style.display = "";
		document.locations_iframes_form.locations_search_field.value = "daso_zip_code";
		document.locations_iframes_form.locations_search_value.value = document.locations_form.zip_distributors.value;
		document.locations_iframes_form.folder_oid.value = oid;
	}
	
	if (span_id == "branch_offices_layer"){
		if (document.locations_form.free_field_branch_offices.value == ""){
			document.getElementById("branch_offices_layer").style.display = "none";
		}
		else{
			document.getElementById("branch_offices_layer").style.display = "";
			document.locations_iframes_form.locations_search_field.value = "daso_xml_field";
			document.locations_iframes_form.locations_search_value.value = document.locations_form.free_field_branch_offices.value;
			document.locations_iframes_form.folder_oid.value = oid;
		}
	}
	
	document.locations_iframes_form.action = url;
	document.locations_iframes_form.target = iframe_id;
	document.locations_iframes_form.submit();
}

function load_locations_iframe(span_id, url, iframe_id, oid){
	if (span_id == "distributors_layer"){
		document.getElementById("branch_offices_layer").style.display = "none";
		document.getElementById("distributors_layer").style.display = "";
		document.locations_form.free_field_branch_offices.selectedIndex = 0;
		document.locations_iframes_form.locations_search_field.value = "daso_zip_code";
		document.locations_iframes_form.locations_search_value.value = document.locations_form.zip_distributors.value;
		document.locations_iframes_form.folder_oid.value = oid;
	}
	
	if (span_id == "branch_offices_layer"){
		document.getElementById("distributors_layer").style.display = "none";
		if (document.locations_form.free_field_branch_offices.value == ""){
			document.getElementById("branch_offices_layer").style.display = "none";
		}
		else{
			document.getElementById("branch_offices_layer").style.display = "";
			document.locations_iframes_form.locations_search_field.value = "daso_xml_field";
			document.locations_iframes_form.locations_search_value.value = document.locations_form.free_field_branch_offices.value;
			document.locations_iframes_form.folder_oid.value = oid;
		}
	}
	
	document.locations_iframes_form.action = url;
	document.locations_iframes_form.target = iframe_id;
	document.locations_iframes_form.submit();
}

function load_locations_iframe_v2(span_id, url, iframe_id){
	if (span_id == "distributors_layer"){
		document.getElementById("branch_offices_layer").style.display = "none";
		document.getElementById("distributors_layer").style.display = "";
		document.locations_form.free_field_branch_offices.selectedIndex = 0;
		document.locations_iframes_form.locations_search_value.value = document.locations_form.zip_distributors.value;
	}
	
	if (span_id == "branch_offices_layer"){
		document.getElementById("distributors_layer").style.display = "none";
		if (document.locations_form.free_field_branch_offices.value == ""){
			document.getElementById("branch_offices_layer").style.display = "none";
		}
		else{
			document.getElementById("branch_offices_layer").style.display = "";
			document.locations_iframes_form.locations_search_value.value = document.locations_form.free_field_branch_offices.value;
		}
	}
	
	document.locations_iframes_form.action = url;
	document.locations_iframes_form.target = iframe_id;
	document.locations_iframes_form.submit();
}

function goto_locations_entity(search_field, search_value, folder_oid, url){
	document.locations_entity_form.search_field.value = search_field;
	document.locations_entity_form.search_value.value = search_value;
	document.locations_entity_form.folder_oid.value = folder_oid;
	document.locations_entity_form.action = url;
	document.locations_entity_form.submit();
}

function goto_locations_entity_v2(search_value, url){
	document.locations_entity_form.search_value.value = search_value;
	document.locations_entity_form.action = url;
	document.locations_entity_form.submit();
}

function show_hide_tr(tr_id){
	if (document.getElementById(tr_id).style.display == "none"){
		document.getElementById(tr_id).style.display = "";
	}
	else{
		document.getElementById(tr_id).style.display = "none";
	}
}

function check_email_page_form(message){
	var parameters = "?";
	
	if (document.email_page_form.email_from.value == "" || document.email_page_form.email_to.value == "" ||
		!validarEmail(document.email_page_form.email_from.value) || !validarEmail(document.email_page_form.email_to.value)){
		alert(message);
		return false;
	}
	else{
		document.email_page_form.url.value = parent.document.print_form.action;
		for (i = 0; i < parent.document.print_form.elements.length; i++){
			if (parent.document.print_form.elements[i].name != "print"){
				parameters = parameters + parent.document.print_form.elements[i].name + "=" + parent.document.print_form.elements[i].value + "&";
			}
		}
		parameters = parameters.substring(0, parameters.length - 1);
		while (parameters.indexOf(" ") != -1){
			parameters = parameters.replace(" ", "%20");
		}
		document.email_page_form.parameters.value = parameters;
		document.email_page_form.submit();
	}
}

function get_page_URL(field_id){
	var page_URL = parent.document.print_form.action;
	var parameters = "?";
	for (i = 0; i < parent.document.print_form.elements.length; i++){
		if (parent.document.print_form.elements[i].name != "print"){
			parameters = parameters + parent.document.print_form.elements[i].name + "=" + parent.document.print_form.elements[i].value + "&";
		}
	}
	parameters = parameters.substring(0, parameters.length - 1);
	while (parameters.indexOf(" ") != -1){
		parameters = parameters.replace(" ", "%20");
	}
	
	document.getElementById(field_id).value = page_URL + parameters;
}

function resize_iframe_and_close_layer(iframe_id, new_height, layer_id){
	parent.document.getElementById(iframe_id).height=new_height;
	setTimeout("parent.document.getElementById('" + layer_id + "').style.display = 'none'", 3000);
}

function subscribe_unsubscribe_mojo(lang, ctry){
	window.open('http://www.smcchile.cl/portal_chile/WebContent/corporative/modules/Newsletter/jsp/subscribe_unsubscribe.jsp?lang=' + lang + '&ctry=' + ctry, 'mojo_pop', 'left=200,top=200,width=500,height=400,resizable=0');
	//return false;
}

function subscribe_unsubscribe_email(lang, ctry){
	//window.open('http://www.smcchile.cl/portal_chile/WebContent/corporative/modules/Newsletter/jsp/subscribe_unsubscribe_email.jsp?lang=' + lang + '&ctry=' + ctry, 'email_pop', 'left=200,top=200,width=500,height=400,resizable=0');
	window.open('https://www.smc.eu/portal_ssl/WebContent/corporative/modules/Newsletter/jsp/subscribe_unsubscribe_email.jsp?lang=' + lang + '&ctry=' + ctry, 'email_pop', 'left=200,top=200,width=500,height=400,resizable=0');
	//return false;
}

function quitar(item, msg1, msg2){
	if (item == "all"){
		alert(msg1);
	}
	else{
		alert(msg2);
	}
	document.formulario_quitar.borrar.value=item;
	document.formulario_quitar.submit();
}

function poner(item, msg1){
	alert(msg1);
	document.formulario_poner.aniadir.value=item;
	document.formulario_poner.submit();
}

function fix_amount(cat_action, cat_data){
	document.formulario.cat_action.value = cat_action;
	document.formulario.cat_data.value = cat_data;
	document.formulario.action = "http://www.smcchile.cl/portal_chile/CatOrdering/jsp/cat_ordering_fix_amount.jsp";
	document.formulario.submit();
}

function fix_amount_switzerland(cat_action, cat_data){
	document.formulario.cat_action.value = cat_action;
	document.formulario.cat_data.value = cat_data;
	document.formulario.action = "http://www.smcchile.cl/portal_chile/CatOrdering/jsp/cat_ordering_fix_amount_switzerland.jsp";
	document.formulario.submit();
}

function validarNumeroObligatorio(numero){
	if (numero!=""){
		var regExpNumeroObligatorio = /^\d+$/
		return regExpNumeroObligatorio.test(numero.replace(/(^ +| +$)/, ""));
	}
	else{
		return false;
	}
}

function validarEmail(email){
	if (email!=""){
		var regExpEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/
		return regExpEmail.test(email);
	}
	else{
		return true;
	}
}

function validarFecha(fecha){ 
   if (fecha){  
      fecha_correcta = fecha;
      if ((fecha.substr(2,1) == "/") && (fecha.substr(5,1) == "/")){      
         for (i=0; i<10; i++){	
            if (((fecha.substr(i,1)<"0") || (fecha.substr(i,1)>"9")) && (i != 2) && (i != 5)){
               fecha_correcta = "no";
               break;  
			}  
         }
	     if (fecha_correcta){ 
	        a = fecha.substr(6,4);
		    m = fecha.substr(3,2);
		    d = fecha.substr(0,2);
		    if((a < 1900) || (a > 2050) || (m < 1) || (m > 12) || (d < 1) || (d > 31))
		       fecha_correcta = "no";
		    else {
		       if((a%4 != 0) && (m == 2) && (d > 28))	   
		          fecha_correcta = "no";
			   else	{
		          if ((((m == 4) || (m == 6) || (m == 9) || (m==11)) && (d>30)) || ((m==2) && (d>29)))
			         fecha_correcta = "no";	      				  	 
			   }
		    }
         }
      }		    			
	  else
	     fecha_correcta = "no";
	  if (fecha_correcta == "no"){
	  	return false;
	  }
	  else{
	  	return true;
	  }
   }
}

function validar(msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11){
	var ok = true;
	var msg = msg1;
	if (document.formulario.company.value == ""){
		msg = msg + msg2;
		ok = false;
	}
	if (document.formulario.surname.value == ""){
		msg = msg + msg3;
		ok = false;
	}
	if (document.formulario.name.value == ""){
		msg = msg + msg4;
		ok = false;
	}
	if (document.formulario.address.value == ""){
		msg = msg + msg5;
		ok = false;
	}
	if (document.formulario.zip.value == ""){
		msg = msg + msg6;
		ok = false;
	}
	if (document.formulario.city.value == ""){
		msg = msg + msg7;
		ok = false;
	}
	if (document.formulario.country.value == ""){
		msg = msg + msg8;
		ok = false;
	}
	if (document.formulario.email.value == ""){
		msg = msg + msg9;
		ok = false;
	}
	if (ok){
		if (validarEmail(document.formulario.email.value)){
			for (var i=1; i<=document.formulario.cat_total.value; i++){
				if (!validarNumeroObligatorio(eval("document.formulario.cat_" + i + "_amount.value"))){
					alert(msg11);
					return false;
				}
			}
			document.formulario.submit();
		}
		else{
			alert(msg10);
			return false;
		}
	}
	else{
		alert (msg);
		return false;
	}
}

function indiceCatalogo(language, country){
	if (language == 'de' && country == 'DE'){
		window.location = "http://www.smcchile.cl/portal_chile/WebContent/local/DE/Pg_Produktuebersicht/index.jsp?tree_options=tree_products.js&box=box_locations.htm";
	}
	else if (language == 'de' && country == 'CH'){
		window.location = "http://www.smcchile.cl/portal_chile/WebContent/local/CH/cat_ordering/jsp/index91024.jsp";
	}
	else{
		window.location = "http://www.smc.eu";
	}
}

function goToIndex(language, country){
	if (language == 'de' && country == 'DE'){
		window.location = "http://www.smc-pneumatik.de";
	}
	else if (language == 'de' && country == 'CH'){
		window.location = "http://www.smc.ch";
	}
	else{
		window.location = "http://www.smc.eu";
	}
}

function fix_values(){
	document.form_hidden.criteria.value = document.form_body.s_criterias.value;
	if (document.form_body.s_criterias.value == "images"){
		alert("Falta...");
	}
	else if (document.form_body.s_criterias.value == "all"){
		document.form_hidden.action = "http://www.smcchile.cl/portal_chile/WebContent/corporative/modules/NewProducts/jsp/new_products.jsp";
	}
	else {
		document.form_hidden.action = "http://www.smcchile.cl/portal_chile/WebContent/corporative/modules/NewProducts/jsp/select.jsp";
	}
	document.form_hidden.submit();
}

function goto_value(){
	document.form_hidden.criteria.value = document.form_body.s_criterias.value;
	document.form_hidden.value.value = document.form_body.s_values.value;
	document.form_hidden.action = "http://www.smcchile.cl/portal_chile/WebContent/corporative/modules/NewProducts/jsp/new_products.jsp";
	document.form_hidden.submit();	
}

function loadOpportunities(obj){
	document.hidden_form.country_offer.value = obj.value;
	document.hidden_form.submit();
}

function validate_search_string(msg){
	if (document.simple_search_form.search_string.value == ""){
		alert(msg);
	}
	else{
		document.simple_search_form.submit();
	}
}

function validate_advanced_search_string(msg){
	if (document.advanced_search_form.search_all_of.value == "" && document.advanced_search_form.search_phrase.value == "" &&
		document.advanced_search_form.search_some_of.value == "" && document.advanced_search_form.search_none_of.value == "" &&
		document.advanced_search_form.language.value == "" && document.advanced_search_form.file_format.value == ""){
		alert(msg);
		return false;
	}
	
	document.advanced_search_form.submit();
}

function set_search_none_of(){
	if (document.advanced_search_form.search_all_of.value == "" && document.advanced_search_form.search_phrase.value == "" &&
		document.advanced_search_form.search_some_of.value == ""){
		document.advanced_search_form.search_none_of.value = "";
		document.advanced_search_form.search_none_of.readOnly = "readonly";
		document.getElementById("search_none_of_id").style.background = "#D3D3D3";
		return false;
	}
	else{
		document.advanced_search_form.search_none_of.readOnly = "";
		document.getElementById("search_none_of_id").style.background = "#FFFFFF";
		return false;
	}
}

function reset_advanced_search_form(){
	document.advanced_search_form.search_none_of.value = "";
	document.advanced_search_form.search_none_of.readOnly = "readonly";
	document.getElementById("search_none_of_id").style.background = "#D3D3D3";
	document.advanced_search_form.reset();
}

/*
function set_query_language(lang){
	document.redo_search_form.language.value = lang;
	document.redo_search_form.submit();
}


function set_imm_search_value(imm_search_value){
	document.imm_search_value_form.imm_search_value.value = imm_search_value;
	document.imm_search_value_form.submit();
}
*/

function set_pagination(current_row, current_page){
	document.redo_search_form.current_row.value = current_row;
	document.redo_search_form.current_page.value = current_page;
	document.redo_search_form.submit();
}

function swap_image(img_obj, img_src){
	img_obj.src = img_src;
}

/*
We need to add a new style "img_class" for the opacity in the styles.css
function change_image_style(img_obj, img_class){
	img_obj.className = img_class;
}
*/

function addLoadWebEvent(func){
	var oldonload = window.onload;
	
	if (typeof window.onload != 'function'){
		window.onload = func;
	}
	else{
		window.onload = function(){
			oldonload();
			func();
		}
	}
}
