﻿$(document).ready(function(){
	
	// add othercontact to an article
	$(".add-otherservice").bind('click', function() {
		
		var otherservice = $(".otherservice-base").clone().appendTo('#otherservice').attr("class", "otherservice-clone");	
		
   $(".del-otherservice").click(function() {
			if(window.confirm("¿Está seguro que eliminar el contacto seleccionado?")) {
				$(this).parent().parent().remove();
			}
			return false;
		});
		
		return false;
	});
	
	$(".del-otherservice").click(function() {
		if(window.confirm("¿Está seguro de eliminar la cotizacion?")) {
		$(this).parent().parent().remove();
		}
		return false;
	});
	
	
	$(".del-cotizaciones").click(function() {
		if(window.confirm("¿Está seguro de eliminar la cotizacion?")) {
			return true;
		}
		return false;
	});
	
	$(".del-legal").click(function() {
		if(window.confirm("¿Está seguro de eliminar el documento legal")) {
			return true;
		}
		return false;
	});
	
	$(".del-soporte").click(function() {
		if(window.confirm("¿Está seguro de eliminar el documento de soporte?")) {
			return true;
		}
		return false;
	});
	/***********************************************/
	$(".nadf").submit(function() {
		var salir = $("#salir");               
		  alert("Tus datos fueron grabados, regresa pronto a concluir tu registro");		   
    });
	
	$(".nad").submit(function() {
		 if(confirm("Al presionar el botón Concluir Inscripción ya no será posible hacer cambios después.")){
			   alert("Registro finalizado");
			 }
			
	});
	/***********************************************/
	if($("#flash").length) {
		var swf = new SWFObject($("#flash").attr("title"), "ply", "624", "282", "9", "#000");
		swf.addParam("wmode", "transparen");
		swf.write("flash");
		$("#flash").attr("title", "");
	}
	
	setTimeout("youtube()", 50);
	
	$("a[rel=gallery]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none'
	});
	
	$(".download").click(function() {
		var el = $(this);
		var expand = el.attr("href");
		if(expand=="none"){
		 	alert("Regístrese para obtener los beneficios de nuestra membrecía");
			return false;
		}else if(expand=="denied"){
			alert("No tiene permisos para abrir este archivo");
			return false;
		}else{
			return true;	
		}		
	});
	
	$(".validate").submit(function(){
		$send = true;
		$("input.required, select.required, textarea.required").each(function(){
			if($.trim($(this).val()) == "" && $send != false) {
				alert($(this).attr("title"));
				$(this).focus();
				$send = false;
			}
		});
		return $send;
	});	
	
	$(".validatefrm").submit(function(){
		var frm = $(this);
		var errors = 0;
		var password = $("#password");  
        var password2 = $("#password2");
		var email = $("#email_user");  
        var email_rep = $("#email_rep");
		
		frm.find("input").each(function(i) {
			var el = $(this);
			if(el.attr("req")=="true") {
				if(el.attr("type")=="text" || el.attr("type")=="password") {
					if($.trim(el.val())=="") {
						alert(el.attr("title"));
						el.focus();
						errors = 1;
						return false;
					}
				}
			}
			if($('#password').val() != $('#password2').val()) {
                       alert("Las contraseñas deben coincidir");
					   errors = 1;
					   return false;
	  
	                }
			if($('#email_user').val() != $('#email_rep').val()) {
                       alert("Los emails deben coincidir");
					   errors = 1;
					   return false;
	  
	                }		
			       
			if(el.attr("alt")!="") {
				if($.trim(el.val())!="" && el.attr("alt")=="email") {					
					if(!check_email(el.val())){
						alert('Escribe una direcci\u00f3n de correo v\u00e1lida');
						el.focus();
						errors = 1;
						return false;
					}	
				}				
			}
			
			
		});
		if(errors==1) {
			return false;
		}
		frm.find("select").each(function(i) {
			var el = $(this);
			if(el.attr("req")=="true") {
				if($.trim(el.val())=="0") {
					alert(el.attr("title"));
					el.focus();
					errors = 1;
					return false;
				}
			}
		});
		if(errors==1) {
			return false;
		}
	});	
	
	
	$("input.helper, textarea.helper").focus(function(){
		if($(this).val()==$(this).attr("title")){
			$(this).val("");
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).val($(this).attr("title"));
		}
	});
	
	var lh = $("#home .left").height();
	var rh = $("#home .right").height();
	var sh = $("#home .shop").height();
	var bh = $("#home .bottom").height();
	if(lh>rh) {
		$("#home .shop").height(sh+(lh-rh));
	} else if(rh>lh) {
		$("#home .bottom").height(bh+(rh-lh));
	}
	
});



function youtube(){
	$(".youtube").each(function(i){
		var video = $(this).attr("title");
		$(this).html('<object width="240" height="198"><param name="movie" value="'+video+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+video+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="240" height="198"></embed></object>');
		$(this).attr("title", "");
	});
}


	
/* validates an email address */
function check_email(emailStr) {
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
	    return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false
			}
		}
		return true
	}
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		return false
	}
	if (len<2) {
		alert(errStr)
		return false
	}
	return true;
}
