$(document).ready(function(){
	$('#slideshow').cycle('fade');

	$('#cotizar').click(function(){
		var newaction = 'cotizador/origen/'+$('#cot_origen').attr('value')+'/destino/'+$('#cot_destino').attr('value')+'/';
		if(checkForm('frm_cotizador'))
			self.location.href=newaction;
	});

	$('#sendcotizar').click(function(){
		var tipo = 1;
		var enviode = 1;
		if($('#cont_tipo2').attr('checked'))
			tipo = 2;
		if($('#cont_enviode2').attr('checked'))
			enviode = 2;
		var newaction = 'cotizador/origen/'+$('#cont_origen').attr('value')+'/destino/'+$('#cont_destino').attr('value')+'/cont_enviode/'+enviode+'/cont_peso/'+$('#cont_peso').attr('value')+'/cont_alto/'+$('#cont_alto').attr('value')+'/cont_largo/'+$('#cont_largo').attr('value')+'/cont_ancho/'+$('#cont_ancho').attr('value')+'/cont_tipo/'+tipo+'/';
		if(checkForm('cont_cotizador'))
			self.location.href=newaction;
	});

	$('#sendsucursales').click(function(){
		var newaction = 'sucursales/estado/'+$('#id_state').attr('value')+'/poblacion/'+$('#id_poblacion').attr('value')+'/';
		if(checkForm('sucursales'))
			self.location.href=newaction;
	});

	$('#rastreaguia').click(function(){
		if(checkForm('frm_rastreo'))
			self.location.href='rastreo/guia/'+$('#guia').attr('value')+'/';
	});

	$('#cont_cotizador #cont_enviode1').click(function(){
		$('#cotizador_options').fadeOut();
		$('#cont_cotizador #cont_peso').attr('required', 'false');
		$('#cont_cotizador #cont_alto').attr('required', 'false');
		$('#cont_cotizador #cont_largo').attr('required', 'false');
		$('#cont_cotizador #cont_ancho').attr('required', 'false');
	});
	$('#cont_cotizador #cont_enviode2').click(function(){
		$('#cotizador_options').fadeIn();
		$('#cont_cotizador #cont_peso').attr('required', 'true');
		$('#cont_cotizador #cont_alto').attr('required', 'true');
		$('#cont_cotizador #cont_largo').attr('required', 'true');
		$('#cont_cotizador #cont_ancho').attr('required', 'true');
	});

	$('#sendrastreo').click(function(){
		$('#cont_rastreo').submit();
	});

	$('ul#menu li img').mouseover(function(){
		var newsrc = $(this).attr('src').replace('_off', '_on');
		$(this).attr('src', newsrc);
	});
	$('ul#menu li img').mouseout(function(){
		var newsrc = $(this).attr('src').replace('_on', '_off');
		$(this).attr('src', newsrc);
	});
	
	$('ul#menu li ul').hide();
    $('ul#menu > li > a').hover(function(){
    		$(this).next("ul").show();
		}, function(){
			$(this).next("ul").hide(); 
	});

	$("ul#menu li ul").hover(function(){
    		$(this).show();
		}, function(){
			$(this).hide();
	});

	var pos = $('#content').offset();
	$('#states').css('left', (pos.left+370));

	$('#closecob').click(function(){
		$('#tabcobertura').fadeOut();
		$('#cob_id_state').attr('value', 0);
	});

	$('#cob_id_state').change(function(){
		loadSucursalesData($('#cob_id_state').attr('value'), 1);
	});

	var tit = $('#maintit').html();
	if(tit){
		if(tit.length>0){
			var newtit = "";
			for(i=0; i<tit.length; i++){
				if(tit.charAt(i)==" ")
					newtit += '<img src="img/let/space.png" width="3" height="11" alt="" border="0">';
				else if(tit.charAt(i)=="|")
					newtit += '<img src="img/let/slash.png" height="11" alt="" border="0">';
				else if(tit.charAt(i)=="á")
					newtit += '<img src="img/let/aa.png" height="11" alt="" border="0">';
				else if(tit.charAt(i)=="é")
					newtit += '<img src="img/let/ee.png" height="11" alt="" border="0">';
				else if(tit.charAt(i)=="í")
					newtit += '<img src="img/let/ii.png" height="11" alt="" border="0">';
				else if(tit.charAt(i)=="ó")
					newtit += '<img src="img/let/oo.png" height="11" alt="" border="0">';
				else if(tit.charAt(i)=="ú")
					newtit += '<img src="img/let/uu.png" height="11" alt="" border="0">';
				else if(tit.charAt(i)=="ñ")
					newtit += '<img src="img/let/nn.png" height="11" alt="" border="0">';
				else
					newtit += '<img src="img/let/'+tit.charAt(i)+'.png" height="11" alt="" border="0">';
			}
			$('#maintit').html(newtit);
		}
	}
});


function loadSucursales(id_state){
	$('#cob_id_state').attr('value', id_state);
	loadSucursalesData(id_state, 1);
}
function loadSucursalesData(id_state, page){
	$("#tabcob").load("/cobertura/action/loadSucursales/id_state/"+id_state+"/page/"+page+"/");
	$('#tabcobertura').fadeIn();
}

/*********** Form validator **********/
$(document).ready(function(){
	$('.checkFrm').submit(function(){
		return checkForm($(this).attr('id'));
	});
});

function checkForm(frmID){
		var frmError = false;
		$('#'+frmID+' input, #'+frmID+' textarea, #'+frmID+' select').each(function(){
			if($(this).attr('required') == 'true' && !frmError){
				if(this.tagName == 'INPUT'){
					if($(this).attr('value') == ''){
						$(this).addClass('required');
						alert($(this).attr('title'));
						$(this).focus();
						frmError = true;
					}
					if($(this).attr('reqtype') == 'email'){
						if(!check_email($(this).attr('value'))){
							$(this).addClass('required');
							alert("Enter a valid Email address");
							$(this).focus();
							frmError = true;
						}
					}
				}
				if(this.tagName == 'SELECT'){
					if($(this).attr('selectedIndex') == 0){
						$(this).addClass('required');
						alert($(this).attr('title'));
						$(this).focus();
						frmError = true;
					}
				}
				if(this.tagName == 'TEXTAREA'){
					if($(this).attr('value') == ''){
						$(this).addClass('required');
						alert($(this).attr('title'));
						$(this).focus();
						frmError = true;
					}
				}
			}
		});
		if(frmError)
			return false;
		else
			return true;
}

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;
}
