var reload_data;
var show_related;
var hide_related;


function show_submenu(id,param)
    {
    /*Hide other submenus*/
    jQuery(".category ul ul").removeClass("show_display");
    jQuery(".category ul ul").addClass("hide_display");
    jQuery(".category ul li span").removeClass("show_display");
    jQuery(".category ul li span").addClass("hide_display");
    jQuery(".category ul li a").removeClass("hide_display");
    jQuery(".category ul li a").addClass("show_display");
    jQuery(".category ul li").removeClass("active");

    /*Show submenu*/
    jQuery("#"+id).addClass("active");
    jQuery("#"+id+" ul").removeClass("hide_display");
    jQuery("#"+id+" ul").addClass("show_display");
    jQuery("#"+id+" span").removeClass("hide_display");
    jQuery("#"+id+" span").addClass("show_display");
    jQuery("#"+id+" a").removeClass("show_display");
    jQuery("#"+id+" a").addClass("hide_display");
    jQuery("#"+id+" ul li a").removeClass("hide_display");

    /*Update migas section*/
    jQuery("#bread").remove();
    jQuery("#detail").remove();
    jQuery("#breadProject").remove();
    /*jQuery("#breadProject").after("<span id='bread'> >"+ jQuery("#span_"+param).html()+"</span>")*/
    jQuery("#hrefBread").after("<a href='" + jQuery("#project").val() + "'id='breadProject'> " + jQuery("#literalProject").val()+ "</a><span id='bread'> > "+ jQuery("#span_"+param).html()+"</span>")

    jQuery.post
        (
        "/_ajax/Section.show"+jQuery("#url").val()+"&param1="+param+"&name="+jQuery("#span_"+param).html(),
        {},
        function(xml)
            {
            var html=xml.getElementsByTagName("body")[0].firstChild.nodeValue;
            jQuery("#content_section_389").html(html);
            }
        );
    }

function show_submenu_home(id){
     if(jQuery("#sub_"+id).hasClass("hide_display")==true){
         jQuery("#submenu ul li ul").removeClass("show_display");
         jQuery("#submenu ul li ul").addClass("hide_display");
         jQuery("#sub_"+id).removeClass("hide_display");
         jQuery("#sub_"+id).addClass("show_display");
     }else{
         jQuery("#submenu ul li ul").removeClass("show_display");
         jQuery("#submenu ul li ul").addClass("hide_display");
         jQuery("#sub_"+id).removeClass("show_display");
         jQuery("#sub_"+id).addClass("hide_display");
     }
}


function Next_product(param){
    jQuery.post(
               "/_ajax/Section.show"+jQuery("#urlArrow").val()+"&"+param,{},
               function(xml){
                      var html=xml.getElementsByTagName("body")[0].firstChild.nodeValue;
                      jQuery("#content_section_395").html(html);
                      jQuery("#link_mail_product").click(function (){
                              show_related();
                              put_title();
                              reload_data();
                      });

                      jQuery("#social_sites").click(function (){
                              show_related();
                              put_title_social();
                              reload_data_social();
                      });
               });

}

function Previous_product(param){
    jQuery.post(
               "/_ajax/Section.show"+jQuery("#urlArrow").val()+"&"+param,{},
               function(xml){
                      var html=xml.getElementsByTagName("body")[0].firstChild.nodeValue;
                      jQuery("#content_section_395").html(html);
                      jQuery("#link_mail_product").click(function (){
                            show_related();
                            put_title();
                            reload_data();
                      });

                      jQuery("#social_sites").click(function (){
                           show_related();
                           put_title_social();
                           reload_data_social();
                      });
               });
}

function product_info(show){
  jQuery(".p_over, .p_info").hide();
  jQuery(".p_" + show).show();
  jQuery("#span_over, #span_info").hide();
  jQuery("#span_" + show).show();
  jQuery("#link_over, #link_info").show();
  jQuery("#link_" + show).hide();
  jQuery("#li_over, #li_info").removeClass("active");
  jQuery("#li_" + show).addClass("active");
  return false;
}
/*
function product_info(data){
     if(data==1){
           jQuery("#li_info").removeClass("active")
           jQuery("#li_over").addClass("active");
           jQuery("#span_over").css("display","block");
           jQuery("#link_over").css("display","none");
           jQuery("#span_info").css("display","none");
           jQuery("#link_info").css("display","block");
           jQuery(".p_info").css("display","none");
           jQuery(".p_over").css("display","block");
     }
     if(data==2){
           jQuery("#li_over").removeClass("active")
           jQuery("#li_info").addClass("active");
           jQuery("#span_over").css("display","none");
           jQuery("#link_over").css("display","block");
           jQuery("#span_info").css("display","block");
           jQuery("#link_info").css("display","none");
           jQuery(".p_over").css("display","none");
           jQuery(".p_info").css("display","block");
     }
}
*/
jQuery(function(){

  jQuery(".link_mail_tip").click(function(){
    var TipId = jQuery(this).attr("id").split("_")[1];
    show_related();
    put_title_tip(jQuery("#title_mail_tip").val());
    reload_data_tip(TipId);
  });

  reload_data_tip = function(TipId){
    var rel_url=jQuery("#url_email").val();
    jQuery.post("/_ajax/Section.show"+rel_url, {}, function(xml){
      var html=xml.getElementsByTagName("body")[0].firstChild.nodeValue;
      jQuery("#mail_product_pop_contenido").html(html);
      event_submit_tip(TipId);
    });
  }

  share_form_validate = function(){
    jQuery("#err_mail_r, #err_mail_s").css("display","none");
    jQuery("#lb_err1, #lb_err2, #lb_err3, #lb_err4").remove();
    jQuery("#id_btn_send").attr("disabled","disabled");
    var err=0;
    if(jQuery("#recipient_mail").val()==''){
      err++;
      jQuery("#lb_err1").remove();
      jQuery("#recipient_mail").after("<img src='/imgfiles/back/img/error.gif' id='lb_err1' style='display:none;' class='err_field'>");
      jQuery("#lb_err1").fadeIn("slow");
    }
    if(jQuery("#recipient_mail").val()!=''){
      if (!isEmailAddress(jQuery("#recipient_mail").val())) {
        err++;
        jQuery("#err_mail_r").css("display","inline");
      }
    }
    if(jQuery("#sender_mail").val()==''){
      err++;
      jQuery("#lb_err2").remove();
      jQuery("#sender_mail").after("<img src='/imgfiles/back/img/error.gif' id='lb_err2' style='display:none;' class='err_field'>");
      jQuery("#lb_err2").fadeIn("slow");
    }
    if(jQuery("#sender_mail").val()!=''){
      if (!isEmailAddress(jQuery("#sender_mail").val())) {
        err++;
        jQuery("#err_mail_s").css("display","inline");
      }
    }
    if(jQuery("#sender_name").val()==''){
      err++;
      jQuery("#lb_err3").remove();
      jQuery("#sender_name").after("<img src='/imgfiles/back/img/error.gif' id='lb_err3' style='display:none;' class='err_field'>");
      jQuery("#lb_err3").fadeIn("slow");
    }
    if(jQuery("#message").val()==''){
      err++;
      jQuery("#lb_err4").remove();
      jQuery("#message").after("<img src='/imgfiles/back/img/error.gif' id='lb_err4' style='display:none;vertical-align:top;padding-top:5px;' class='err_field'>");
      jQuery("#lb_err4").fadeIn("slow");
    }
    return err;
  }

  event_submit_tip = function(TipId){
    jQuery("#id_btn_send").click(function (){
      var err = share_form_validate();
      if(err>0){
        jQuery("#id_btn_send").removeAttr("disabled");
        return false;
      }
      else{
        jQuery.ajax({
          type: "POST",
          async: false,
          url: "/front.send_mail_tip",
          cache: false,
          data:{
            TipId: TipId,
            recipient_name: jQuery("#recipient_name").val(),
            recipient_mail: jQuery("#recipient_mail").val(),
            sender_name: jQuery("#sender_name").val(),
            sender_mail: jQuery("#sender_mail").val(),
            message: jQuery("#message").val()
          },
          success: function(response){
            hide_related();
            return false;
          }
        });
      }
      return false;
    });
  }

  jQuery(".link_share_tip").click(function(){
    var TipId = jQuery(this).attr("id").split("_")[1];
    show_related();
    put_title_tip(jQuery("#title_share_tip").val());
    reload_data_social_tip(TipId);
  });

  reload_data_social_tip = function(TipId){
    var rel_url = jQuery("#url_social").val() + "&TipId=" + TipId;
    jQuery.post("/_ajax/Section.show"+rel_url, {}, function(xml){
      var html=xml.getElementsByTagName("body")[0].firstChild.nodeValue;
      jQuery("#mail_product_pop_contenido").html(html);
    });
  }



        jQuery("#link_mail_product").click(function (){
               show_related();
               put_title();
               reload_data();
        });

        jQuery("#social_sites").click(function (){
               show_related();
               put_title_social();
               reload_data_social();
        });


         show_related = function(){
		jQuery("#capa").height(jQuery("body").height() + 500);   
		jQuery("#capa").width(jQuery("body").width());
		var offset = window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop;
		var left=(jQuery("body").width() - 750)/2;
		jQuery("#email_product_popup").css({left: left, top: "100px"})
		jQuery("#capa").fadeIn("slow");
		jQuery("#email_product_popup").fadeIn("slow");
	}

  hide_related = function (){
    jQuery("#capa").fadeOut("slow");
    jQuery("#email_product_popup").hide("slow", function(){
      jQuery("#mail_form input, #mail_form textarea").val("");
    });
  }

	jQuery("#mail_product_pop_cerrar").click(function(){
		hide_related();
	})


        reload_data=function(){
		var rel_url=jQuery("#url_email").val();
		jQuery.post(
			"/_ajax/Section.show"+rel_url,
			{},
			function(xml){
				var html=xml.getElementsByTagName("body")[0].firstChild.nodeValue;
				jQuery("#mail_product_pop_contenido").html(html);
                                event_submit();
			}

		)
	}


        reload_data_social=function(){
		var rel_url=jQuery("#url_social").val();
		jQuery.post(
			"/_ajax/Section.show"+rel_url,
			{},
			function(xml){
				var html=xml.getElementsByTagName("body")[0].firstChild.nodeValue;
				jQuery("#mail_product_pop_contenido").html(html);
                                event_submit();
			}

		)
	}

	put_title=function(){
		var literal=jQuery("#title_mail_prod").val();
		jQuery("#mail_product_pop_titulo").html(literal);
	}

	put_title_tip=function(text){
//		var literal=jQuery("#title_mail_prod").val();
		jQuery("#mail_product_pop_titulo").html(text);
	}

        put_title_social=function (){
               var literal=jQuery("#title_share").val();
               jQuery("#mail_product_pop_titulo").html(literal);
        }

  event_submit = function(){
    jQuery("#id_btn_send").click(function (){
      var err = share_form_validate();
      if(err>0){
        jQuery("#id_btn_send").removeAttr("disabled");
        return false;
      }
      else{
        jQuery.ajax({
          type: "POST",
          async: false,
          url: "/front.send_mail_product",
          cache: false,
          data:{
            CategoryId: jQuery("#param1").val(),
            SubcategoryId: jQuery("#param2").val(),
            ProductId: jQuery("#param3").val(),
            ColorId: jQuery("#param4").val(),
            MaterialId: jQuery("#param5").val(),
            CollectionId: jQuery("#param6").val(),
            TypeId: jQuery("#param8").val(),
            recipient_name: jQuery("#recipient_name").val(),
            recipient_mail: jQuery("#recipient_mail").val(),
            sender_name: jQuery("#sender_name").val(),
            sender_mail: jQuery("#sender_mail").val(),
            message: jQuery("#message").val()
          },
          success: function(response){
            hide_related();
            return false;
          }
        });
      }
      return false;
    });
  }
});



       
function isEmailAddress(s) {
	var filter=/^[A-Za-z][A-Za-z0-9_.\-]*@[A-Za-z0-9_\-]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	if (filter.test(s)) return true;
	else return false;
}

function show_search_advance(){
        if(jQuery("#section_advance").hasClass("hide_display")==true){
              jQuery("#section_advance").removeClass("hide_display");
              jQuery("#section_advance").addClass("show_display");
        }else{
              jQuery("#input_color").val("");
              jQuery("#input_material").val("");
              jQuery("#sel_cat").val("");
              jQuery("#sel_subcat option[value!='']").remove();
              jQuery("#chk_residential").removeAttr("checked");
              jQuery("#chk_contract").removeAttr("checked");
              jQuery("#section_advance").removeClass("show_display");
              jQuery("#section_advance").addClass("hide_display");
        }
}

function update_lis_search(){
    jQuery.post(
               "/_ajax/Section.show"+jQuery("#url").val()+"&name="+jQuery("#search_input").val()+"&idcat="+jQuery("#sel_cat").val()+"&idsubcat="+jQuery("#sel_subcat").val()+"&color="+jQuery("#input_color").val()+"&material="+jQuery("#input_material").val()+"&contract="+jQuery("#chk_contract:checked").val()+"&residential="+jQuery("#chk_residential:checked").val(),{},
               function(xml){
                      var html=xml.getElementsByTagName("body")[0].firstChild.nodeValue;
                      jQuery("#content_section_443").html(html);
               });
}

function update_list_cat(idcat,idsubcat){
    jQuery.post(
               "/_ajax/Section.show"+jQuery("#url").val()+"&idsubcat="+idsubcat+"&idcat="+idcat,{},
               function(xml){
                      var html=xml.getElementsByTagName("body")[0].firstChild.nodeValue;
                      jQuery("#content_section_443").html(html);
               });
}

function typeCategory(id){
    if(id==1){
      var residential=1;
    }else{
      var contract=1;
    } 
    jQuery.post(
               "/_ajax/Section.show"+jQuery("#url").val()+"&name="+jQuery("#search_input").val()+"&idcat="+jQuery("#sel_cat").val()+"&idsubcat="+jQuery("#sel_subcat").val()+"&color="+jQuery("#input_color").val()+"&material="+jQuery("#input_material").val()+"&contract="+contract+"&residential="+residential,{},
               function(xml){
                      var html=xml.getElementsByTagName("body")[0].firstChild.nodeValue;
                      jQuery("#content_section_443").html(html);
               });
}



/* ------ form inputs management ------ */
function load_default(input, text){
  if(input.value == text) input.value = '';
}

function reload_default(input, text){
  if(input.value == '') input.value = text;
}

function erase_default(input, text){
  if(jQuery("#" + input).val() == text) jQuery("#" + input).val("");
}

