/*global $, alert, document, Cufon, _gaq */
/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, newcap: true, immed: true */

function checkContact() {
	var m, lng, re_email, re_tel;

	m = '';
	lng = $('html').attr('lang');
	re_email = /^[A-Z0-9._%\-]+@[A-Z0-9.\-]+\.(?:[A-Z]{2}|aero|biz|cat|com|coop|edu|gov|info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel)$/i;
	re_tel = /^[0-9]{9,}$/;

	if (String($('#name').val()).length === 0) {
		m = (lng === 'es' ? 'Por favor, introduzca su nombre' : 'Please enter your full name');
		$('#name').focus();
	} else if (String($('#email').val()).length === 0) {
		m = (lng === 'es' ? 'Por favor, introduzca su dirección de correo electrónico' : 'Please enter your e-mail address');
		$('#email').focus();
	} else if (!re_email.test($('#email').val())) {
		m = (lng === 'es' ? 'La dirección de correo electrónico introducida no es válida' : 'The e-mail address is not valid');
		$('#email').focus();
	} else if ($('#phone').val() !== '' && !re_tel.test($('#phone').val())) {
		m = (lng === 'es' ? 'El teléfono introducido no es válido' : 'The phone number is not valid');
		$('#phone').focus();
	} else if (String($('#subject').val()).length === 0) {
		m = (lng === 'es' ? 'Por favor, introduzca el asunto de su consulta' : 'Please enter the subject');
		$('#subject').focus();
	} else if (String($('#message').val()).length === 0) {
		m = (lng === 'es' ? 'Por favor, introduzca su mensaje' : 'Please enter your message');
		$('#message').focus();
	}

	if (m !== '') {
		alert(m);
		return false;
	}

	$.ajax({
		async: true,
		cache: false,
		timeout: 10000,
		type: 'POST',
		url: '/apps/support/open.php',
		data: $('#fticket').serialize(),
		dataType: 'html',
		
		beforeSend: function () {
			var m;
			m = '<img id="loading" src="../common/img/loading.gif" alt="" />';
			$('#fticket input[type="submit"]').replaceWith(m);
			_gaq.push(['_trackPageview', '/JS/contact/beforeSend']);
		},
		success: function (data, textStatus, XMLHttpRequest) {
			var m, r;

			r = (data.indexOf('<p align="center" id="infomessage">Support ticket request created</p>') > 0);

			if (r) {
				_gaq.push(['_trackPageview', '/JS/contact/success']);
				m = '<img src="../common/img/icon-ok.png" alt="" />';
				m += (lng === 'es' ? 'Mensaje enviado con éxito' : 'Message successfully sent');
			} else {
				_gaq.push(['_trackPageview', '/JS/contact/formError']);
				m = '<img src="../common/img/icon-warning.png" alt="" />';
				m += '<span style="color:#f00">';
				m += (lng === 'es' ? 'Error al enviar su mensaje. Por favor, revise el formulario y vuelva a intentarlo' : 'Error sending your request. Please check the form and try it again');
				m += '</span>';
			}
			$('#fticket #loading').replaceWith(m);
		},
		error: function () {
			var m;
			m = '<img src="../common/img/icon-warning.png" alt="" />';
			m += '<span style="color:#f00">';
			m += (lng === 'es' ? 'Error al enviar el mensaje. Por favor, póngase en contacto con nosotros en <a href="mailto:info@knowgate.es">info@knowgate.es</a>' : 'Error sending the message. Please contact us at <a href="mailto:info@knowgate.es">info@knowgate.es</a>');
			m += '</span>';
			$('#fticket #loading').replaceWith(m);
			_gaq.push(['_trackPageview', '/JS/contact/ajaxError']);
		}
	});

	return false;
}

function showMenu() {
	var vis;

	vis = ($('#menu').css('display') === 'block');

	$('#st, #mn, #lg').toggle();

	if (vis) {
		$('#menu').slideUp();
	} else {
		$('#menu').slideDown();
	}	

}

$(document).ready(function () {
	Cufon.replace('h1');

	if ($('#menu').css('display') === 'none') {
		// Smartphones
		$('#logo-sup').append('<div class="blueButton" id="bMenu">Menu</div>');
		$('#bMenu').click(showMenu);
		$('#menu a.disabled').click(showMenu);
	}

	$('#fticket').submit(checkContact);

	$('a[href^="http://"]').attr('target', '_blank');
	$('a[href^="https://"]').attr('target', '_blank');

	$(window).konami(function(){
		$('#konami').fancybox({
			'padding'	: 0,
			'autoScale'	: false,
			'transitionIn'	: 'none',
			'transitionOut'	: 'none',
			'title'		: 'Know Gate S.L. &mdash; 2000-' + String(new Date().getFullYear()),
			'width'		: 249,
			'height'	: 372,
			'href'		: '../common/img/cabeza.png',
			'type'		: 'image'
		});
		$("#konami").trigger('click');

	});
});

