<!--



/*
Permite la inclusion de expresiones regulares en selectores. Como se usa?
http://james.padolsey.com/javascript/regex-selector-for-jquery/
*/

jQuery.expr[':'].regex = function(elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ? 
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels,'')
        },
        regexFlags = 'ig',
        regex = new RegExp(matchParams.join('').replace(/^\s+|\s+$/g,''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}


/*
$.swapImage(".swapImage"); 
$.swapImage(".swapImageDisjoint", true, true, "click");
*/

$(document).ready(function() {


	$.swapImage(".swapImage"); 

	$("#banner-mudanza").fancybox().trigger('click');


	$("ul#topnav li").hover(function() {
		$(this).find("span").show(); //Show the subnav

		if ($(this).find("span").length > 0) {
			$('#menu').addClass('menuAddBg');
		}
		$(this).children('a').addClass('menuAddColor');

	} , function() {

		$(this).find("span").hide(); 
		if ($(this).find("span").length > 0) {
			$('#menu').removeClass('menuAddBg');
		}
		$(this).children('a').removeClass('menuAddColor');
	});



  $('#productos h3').each(function() {
    var tis = $(this), state = false, answer = tis.next('div').hide().css('height','auto').hide();
    tis.click(function() {
      state = !state;
      //answer.slideToggle(state);
      answer.toggle(state);
      tis.toggleClass('active',state);
    });
  });






	$('.slideshow').cycle({
		fx: 'fade'
	});



	$('input.num').keydown(function(event) {
		//alert(event.keyCode);
		// Allow only backspace and delete, tab, arrows shift inicio fin
		if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 16 || event.keyCode == 35 || event.keyCode == 36 || (event.keyCode >= 37 && event.keyCode <= 40) ) {
			// let it happen, don't do anything
		} else {
			// Ensure that it is a number and stop the keypress
			if (event.keyCode < 48 || ( event.keyCode > 57 && event.keyCode < 96) || event.keyCode > 105 ) {
				event.preventDefault(); 
			}       
		}
	});


	$('input.numd').keyup(function () { 
		this.value = this.value.replace(/[^0-9\.]/g,'');
	});



	// reset calculadora de corte
	$("#select1 option:first").attr('selected','selected');
	$("#hcorte").attr('value','');
	$("#wcorte").attr('value','');
	$("#combinado").attr('checked','');
	$("#totalcortes").attr('value','');
	$("#areautilizada").attr('value','');
	$("#areadesperdiciada").attr('value','');



	$("#select1").change(function(){
		$.post("carga-select.asp",{ id:$(this).val() },function(data){$("#select2").html(data);})
		$("#totalcortes").attr('value','');
		$("#areautilizada").attr('value','');
		$("#areadesperdiciada").attr('value','');

		//limpiar los campos de resultados
	});


	$("#select2").change(function(){
		$("#totalcortes").attr('value','');
		$("#areautilizada").attr('value','');
		$("#areadesperdiciada").attr('value','');
	});


	$("#limpiar").click(function(){
		// reset calculadora de corte
		//$("#select1 option:first").attr('selected','selected');
		$("#hcorte").attr('value','');
		$("#wcorte").attr('value','');
		$("#combinado").attr('checked','');
		$("#totalcortes").attr('value','');
		$("#areautilizada").attr('value','');
		$("#areadesperdiciada").attr('value','');
		//$.post("carga-select.asp",{ id:0 },function(data){$("#select2").html(data);})
	});




	$("#calcular").click(function(){


		if ($.trim($('#select1').val()) ==0) {
			alert('Por favor ingrese seleccione un tipo de papel');
			$('#select1').focus();
			return false;
		}

		if ($.trim($('#select2').val()) =='') {
			alert('Por favor ingrese seleccione la medida del pliego ');
			$('#select2').focus();
			return false;
		}


		if ($.trim($('#wcorte').val()) =='') {
			alert('Por favor ingrese el ancho del corte que desea');
			$('#wcorte').focus();
			return false;
		}


		if ($.trim($('#hcorte').val()) =='') {
			alert('Por favor ingrese el alto del corte que desea');
			$('#hcorte').focus();
			return false;
		}

		var combinado;
		var wS1;
		var hS1;
		var wS2;
		var hS2;
		var totalcortes;
		var totalcortesenSobrante1;
		var totalcortesenSobrante2;
		var wCorte = $('#wcorte').val();
		var hCorte = $('#hcorte').val();


		if($('#combinado').is(':checked')){
			combinado = 1;
		}else{
			combinado = 0;
		}

		var mPliego = $('#select2').val();
		var arrPliego = mPliego.split('x');
		var wPliego = arrPliego[0];
		var hPliego = arrPliego[1];



		if (combinado == 1) {



			//wCorteTotal = ( wPliego - (wPliego % wCorte) ) / wCorte ;
			//hCorteTotal = ( hPliego - (hPliego % hCorte) ) / hCorte ;

			wCorteTotal = ( ( wPliego - ( ( (wPliego*100) % (wCorte*100) ) / 100 ) ) / wCorte ).toFixed(0);
			hCorteTotal = ( ( hPliego - ( ( (hPliego*100) % (hCorte*100) ) / 100 ) ) / hCorte ).toFixed(0);
			totalcortes = wCorteTotal * hCorteTotal;


			wSobrante = wPliego - ( wCorteTotal * wCorte);
			hSobrante = hPliego - ( hCorteTotal * hCorte);


			wS1 = hPliego;
			hS1 = wSobrante;
			totalcortesenSobrante1 = ( ( ( wS1 - ( ( (wS1*100) % (wCorte*100) ) / 100 ) ) / wCorte ).toFixed(0) ) * ( ( ( hS1 - ( ( (hS1*100) % (hCorte*100) ) / 100 ) ) / hCorte ).toFixed(0) );


			wS2 = hSobrante;
			hS2 = wPliego;
			totalcortesenSobrante2 = ( ( ( wS2 - ( ( (wS2*100) % (wCorte*100) ) / 100 ) ) / wCorte ).toFixed(0) ) * ( ( ( hS2 - ( ( (hS2*100) % (hCorte*100) ) / 100 ) ) / hCorte ).toFixed(0) );


			totalcortes = totalcortes + totalcortesenSobrante1 + totalcortesenSobrante2;



			//test pliego invertido

			wPliego = arrPliego[1];
			hPliego = arrPliego[0];


			wCorteTotal = ( ( wPliego - ( ( (wPliego*100) % (wCorte*100) ) / 100 ) ) / wCorte ).toFixed(0);
			hCorteTotal = ( ( hPliego - ( ( (hPliego*100) % (hCorte*100) ) / 100 ) ) / hCorte ).toFixed(0);
			totalcortesInvertido = wCorteTotal * hCorteTotal;


			wSobrante = wPliego - ( wCorteTotal * wCorte);
			hSobrante = hPliego - ( hCorteTotal * hCorte);


			wS1 = hPliego;
			hS1 = wSobrante;
			//totalcortesInvertidoenSobrante1 = ( ( wS1 - (wS1 % wCorte) ) / wCorte ) * ( ( hS1 - (hS1 % hCorte) ) / hCorte );
			totalcortesInvertidoenSobrante1 = ( ( ( wS1 - ( ( (wS1*100) % (wCorte*100) ) / 100 ) ) / wCorte ).toFixed(0) ) * ( ( ( hS1 - ( ( (hS1*100) % (hCorte*100) ) / 100 ) ) / hCorte ).toFixed(0) );


			wS2 = hSobrante;
			hS2 = wPliego;
			//totalcortesInvertidoenSobrante2 = ( ( wS2 - (wS2 % wCorte) ) / wCorte ) * ( ( hS2 - (hS2 % hCorte) ) / hCorte );
			totalcortesInvertidoenSobrante2 = ( ( ( wS2 - ( ( (wS2*100) % (wCorte*100) ) / 100 ) ) / wCorte ).toFixed(0) ) * ( ( ( hS2 - ( ( (hS2*100) % (hCorte*100) ) / 100 ) ) / hCorte ).toFixed(0) );


			totalcortesInvertido = totalcortesInvertido + totalcortesInvertidoenSobrante1 + totalcortesInvertidoenSobrante2;

			if ( totalcortesInvertido > totalcortes ) {
				totalcortes = totalcortesInvertido;
			}



		}else{



			wCorteTotal = ( ( wPliego - ( ( (wPliego*100) % (wCorte*100) ) / 100 ) ) / wCorte ).toFixed(0);
			hCorteTotal = ( ( hPliego - ( ( (hPliego*100) % (hCorte*100) ) / 100 ) ) / hCorte ).toFixed(0);

			totalcortes = wCorteTotal * hCorteTotal;


			
			wPliego = arrPliego[1];
			hPliego = arrPliego[0];


			wCorteTotal = ( ( wPliego - ( ( (wPliego*100) % (wCorte*100) ) / 100 ) ) / wCorte ).toFixed(0);
			hCorteTotal = ( ( hPliego - ( ( (hPliego*100) % (hCorte*100) ) / 100 ) ) / hCorte ).toFixed(0);

			totalcortesInvertido = wCorteTotal * hCorteTotal;



			if ( totalcortesInvertido > totalcortes ) {
				totalcortes = totalcortesInvertido;
			}
			


		}


		areautilizada = Math.round(((wCorte*hCorte)*totalcortes*100)/(wPliego*hPliego)*100)/100;
		areadesperdiciada = Math.round((100 - areautilizada)*100)/100;




		$('#totalcortes').val(totalcortes);
		$('#areautilizada').val(areautilizada + "%");	
		$('#areadesperdiciada').val(areadesperdiciada + "%");


	});






	$('a').focus(function() {
		$(this).blur();
	});




	$('.swapImageDisjoint').click( function() {
		$('#lafoto').attr('alt',$(this).attr('alt'));
		$('#lafoto').attr('title',$(this).attr('alt'));
	});




	$('.fancybox').fancybox({
		'transitionIn'	: 'none',
		'transitionOut'	: 'none',
		'autoScale' 	: false
	});



	// apply to all png images 
	$('.pngtrans').ifixpng(); 








	$('#formContacto').submit(function() {

		if ($.trim($('#inombre').val()) =='') {
			alert('Por favor ingrese el nombre');
			$('#inombre').focus();
			return false;
		}


		if ($.trim($('#iemail').val()) =='') {
			alert('Por favor ingrese un e-mail');
			$('#iemail').focus();
			return false;
		}else{

			var er_email = /^(?:[a-zA-Z0-9_'^&amp;/+-])+(?:\.(?:[a-zA-Z0-9_'^&amp;/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$/
			if(!er_email.test($('#iemail').attr('value'))){
				alert('Por favor ingrese un e-mail válido');
				$('#iemail').focus();
				return false;
			}

		}


		if ($.trim($('#icomentario').val()) =='') {
			alert('Por favor ingrese el comentario');
			$('#icomentario').focus();
			return false;
		}



		return true;

	});




	$('#formNewsltter').submit(function() {

		if ($.trim($('#inombre').val()) =='') {
			alert('Por favor ingrese el nombre');
			$('#inombre').focus();
			return false;
		}


		if ($.trim($('#iemail').val()) =='') {
			alert('Por favor ingrese un e-mail');
			$('#iemail').focus();
			return false;
		}else{

			var er_email = /^(?:[a-zA-Z0-9_'^&amp;/+-])+(?:\.(?:[a-zA-Z0-9_'^&amp;/+-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)$/
			if(!er_email.test($('#iemail').attr('value'))){
				alert('Por favor ingrese un e-mail válido');
				$('#iemail').focus();
				return false;
			}

		}


		if($('#inewsletter').is(':checked')){
			//combinado = 1;
		}else{
			alert('Debe tildar la opcion "Deseo recibir el newsletter" para enviar este formulario');
			return false;
		}


		return true;

	});













});


//-->
