$(function() {   
  $('DIV.familia').mouseover(function() {  
       $(this).addClass("familia_hover").show("slow");  
  } )  
  $('DIV.familia').mouseout(function() {  
      $(this).removeClass("familia_hover").show("slow");  
  } )
})

$(document).ready(function(){
   	var options = { zoomWidth: 250, zoomHeight: 250 }
	$('.jqzoom').jqzoom(options);
});


function writeObjFlash (file, width, height, bgcolor, mode) { document.write ("<object codeBase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0 height=" + height + " width=" + width + " classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000><PARAM NAME='FlashVars' VALUE=''><PARAM NAME='Movie' VALUE='" + file + "'><PARAM NAME='Src' VALUE='" + file + "'><PARAM NAME='WMode' VALUE='" + mode + "'><PARAM NAME='Play' VALUE='-1'><PARAM NAME='Loop' VALUE='-1'><PARAM NAME='Quality' VALUE='High'><PARAM NAME='SAlign' VALUE=''><PARAM NAME='Menu' VALUE='0'><PARAM NAME='Base' VALUE=''><PARAM NAME='AllowScriptAccess' VALUE='0'><PARAM NAME='Scale' VALUE='ShowAll'><PARAM NAME='DeviceFont' VALUE='0'><PARAM NAME='EmbedMovie' VALUE='0'><PARAM NAME='BGColor' VALUE='" + bgcolor +"'><PARAM NAME='SWRemote' VALUE=''><PARAM NAME='MovieData' VALUE=''><PARAM NAME='SeamlessTabbing' VALUE='1'><PARAM NAME='Profile' VALUE='0'><PARAM NAME='ProfileAddress' VALUE=''><PARAM NAME='ProfilePort' VALUE='0'><embed src='" + file + "' quality=high pluginspage='http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash' type='application/x-shockwave-flash' width='" + width + "' height='" + height +"'  wmode= '"+mode+"'></embed></object>"); }

function popup(page, name, width, height) {	
	window.open(page, name, "width=" + width + ",height="+height+",menubar=no,scrollbars=yes");
}

function prueba_ajax(){
	try {
	ajax=nuevoAjax(); 
	ajax.open("GET", "ajax_prueba.htm",true);
		 
	ajax.onreadystatechange = function()
	{ 
		if (ajax.readyState==4)
		{ 
			
			alert(ajax.responseText);
		} 
	}
	
	ajax.send(null);
	}
	catch (e)
	{
		alert(e);
	}	
	
}

function nuevoAjax(){
	var xmlHttp

  try
  {
    //Firefox, Opera 8.0+, Safari
	alert("1");
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    //Internet Explorer
    try
    {
      xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	  alert("2");
    }
    catch(e)
    {
      try
      {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		alert("3");
      }
      catch(e)
      {
        alert("Your browser does not support AJAX!")
        return false;
      }
    }
  }
  return xmlHttp;
}

function change_tallas(server, id_color, tipo_tarifa){
	var id_talla = $('#talla_'+id_color).attr("value");
 	        var codigo_articulo_ = $("#codigo_articulo_"+id_color).attr("value")
 	        var articulo_color_ = $("#articulo_color_"+id_color).attr("value")
 	    var html = '';
 	        var html2 = ''; 
 	       
 	        html = $.ajax({
 	                type: "POST",
 	                url: server+"multi/get_cantidad_producto/" + codigo_articulo_ + "/" + id_color + "/" + id_talla + "/" + articulo_color_+"/"+tipo_tarifa,
 	                async: false
 	         }).responseText;
 	       
 	 //  alert(server+"/multi/get_precio_producto/" + codigo_articulo_ + "/" + id_color + "/" + id_talla + "/" + articulo_color_+"/"+tipo_tarifa);
 	        html2 = $.ajax({
 	                type: "POST",
 	                url: server+"multi/get_precio_producto/" + codigo_articulo_ + "/" + id_color + "/" + id_talla + "/" + articulo_color_+"/"+tipo_tarifa,
 	                async: false
 	         }).responseText;

 	        var newOptions = unescape(html);
 	        html = newOptions.replace(/\+/gi," ");		
 	        $("#div_cantidad_"+id_color).html(html);
			
			var newOptions2 = unescape(html2);
 	        html = newOptions2.replace(/\+/gi," ");
			html = html2;
			if(html2.split("&nbsp;&nbsp;&nbsp;").length > 1){
				html = html2.split("&nbsp;&nbsp;&nbsp;");
				html = 	html[1];
				$("#div_precio_"+id_color).css("color", "red");
			}
 	       
 	        
 	        $("#div_precio_"+id_color).html(html2 + " €");
 	        $("#precio_"+id_color).attr("value", html);
 	        ejecutar_suma("cantidad_" + id_color, "precio_" + id_color, "subtotal_" + id_color);
}

function initValidation (id_form)
{
		
	var correcto = true;	
	// campos requeridos
	  $("#"+id_form).find(".required:visible").each(function(i) {
		id = $(this).attr("id");
	   valor=$(this).attr("value");
	   
	   if (valor==undefined || valor == ''){
	   	   mark_field(id,'required');
		   correcto = false;		
	   }else 
	   		unmark_field(id,'required');
     
	   });
	  
	// Aceptar condiciones		
	if($("#"+id_form + "#accept_cond").attr("value") != undefined && $("#"+id_form + "#accept_cond:checked").val() == null){		
		mark_field('accept_cond','required');
	   correcto = false;
	}else{
		unmark_field('accept_cond','required');
	}
	
	
	// Check extension file
	$("#"+id_form).find(".check_ext:visible").each(function(i) {
		id = $(this).attr("id");
	    valor=$(this).attr("value");
		extension = (valor.substring(valor.lastIndexOf("."))).toLowerCase(); 
		extension = extension.substring(1); 
		valid_exts = $("#check_ext").attr("value");
		valid_exts = valid_exts.split("|");
		
		comprueba = false;
		for (var i = 0; i < valid_exts.length && comprueba == false; i++) {
			if(valid_exts[i] == extension) comprueba = true;
		}
		
		if(comprueba == false){
			mark_field(id,'check_ext');
		   correcto = false;
		}else{
			unmark_field(id,'check_ext');
		}
	});
	
	  
	// campos password iguales	  
	pasw = $("#password").attr("value");		
	repasw = $("#repassword").attr("value");
	
	if(pasw != undefined && pasw != ''){
		if (pasw == repasw){
   			unmark_field('password','match_password');  		
	   }else{ 
   		   mark_field('password','match_password');
		   correcto = false;
	   }  	
	}
	
	// fecha nacimiento required
	var fecha_nac_any = $("#fecha_nac_any").attr("value");		
	var fecha_nac_mes = $("#fecha_nac_mes").attr("value");
	var fecha_nac_dia = $("#fecha_nac_dia").attr("value");
	
	if(fecha_nac_any != undefined && fecha_nac_any != ''){			
		if (fecha_nac_mes != undefined && fecha_nac_mes != ''){			
   			unmark_field('fecha_nac','required');  		
		}
		if (fecha_nac_dia != undefined && fecha_nac_dia != ''){
   			unmark_field('fecha_nac','required'); 
	   }
	   
	   if (fecha_nac_mes == undefined || fecha_nac_mes == ''){ 
   		   mark_field('fecha_nac','required');
		   correcto = false;	    		
	   }
	   if(fecha_nac_dia == undefined || fecha_nac_dia == ''){ 
   		   mark_field('fecha_nac','required');
		   correcto = false;
	   }
	}
	   
	  
	 
	// campos email
	$("#"+id_form).find(".valid_email:visible").each(function(i) {	
		id = $(this).attr("id");
	   valor=$(this).attr("value");
	  
	   if (valor!=undefined && valor !='') {
	  if (!valor.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i)){
	  	mark_field(id,'valid_email');
		correcto = false;
	  }else unmark_field(id,'valid_email');
	   }
	   });
	   
	 // min length
	 $("#"+id_form).find(".min_length:visible").each(function(i) {		
		id = $(this).attr("id");
	   valor=$(this).attr("value");
	  if (valor!=undefined && valor!='') {
	  if (valor.length<6){
	  	mark_field(id,'min_length');
		correcto = false;
	  }
	   else unmark_field(id,'min_length');
	  }
	   });

	 $("#"+id_form).find(".minimum:visible").each(function(i) {			 
		id = $(this).attr("id");
	   valor=$(this).attr("value");
	  if (valor!=undefined && valor !='') {
	  	id = $(this).attr("id");
	  	fieldcomp = id.substring(0, id.indexOf('_') );
	  	
	  	valor2=$("#"+fieldcomp).attr("value");
	  
	  	if (parseInt(valor)>parseInt(valor2)){
	  	correcto = false;
	  	mark_field(id,'minimum');
		}
	   else unmark_field(id,'minimum');
	  }
	   }); 	  
	 
	 return correcto;
}

function show_div(){

//$('#tel_me').show(); 

document.getElementById('tel_me').style.display='block';


	
}

function mark_field (id, type)
{	
	$("#"+id).css("background-color", "#ffffcc");	
	$("#error_"+ id + "_" + type).show();	
}

function unmark_field (id, type)
{	
	$("#"+id).css("background-color", "#FFFFFF");
	$("#error_"+ id + "_" + type).hide();
	//$("../",id).remove("span");
}

function confirm_text(text){
	if(confirm(text)){
		return true;
	}else return false;
}

function update_provinces(server, name_field, provincia_selected){
	var id_country = $("#"+name_field +"_pais").attr("value");	
	var html = $.ajax({
		type: "POST",
  		url: server+"/multi/update_provinces/" + id_country + "/" + name_field + "/" + provincia_selected,
  		async: false
	}).responseText;
	
	if(html != ''){
		var newOptions = unescape(html);
		newOptions = newOptions.replace(/\+/gi," ");
		
		$("#div_"+name_field+"_provinces").html(newOptions);
		$("#tr_"+name_field+"_provinces").show();		
	}else{
		$("#tr_"+name_field+"_provinces").hide();
	}
}

function marcar_sorprender(server, id_lang){
	var html = $.ajax({
		type: "POST",
  		url: server+"multi/marcar_sorprender",
  		async: false
	}).responseText;

	$("#btn_quiero_sorprender").hide();
	$("#btn_quiero_sorprender_marcado").show();

	if(html == "ok") document.location.href = id_lang+'/cesta';
}

function doPost (uri, field_name, field_value)
{
	idForm=Math.round(Math.random()*10);
	
	htmlForm = "<form id='dyn_form' action='"+uri+"' method='POST'>";
	htmlForm+= "<input type='hidden' name='"+field_name+"' value='"+field_value+"' >";
	htmlForm+= "</form>";
	
	$("body").append(htmlForm);
	$("#dyn_form").submit();
}