var aFormRequest = new Array();
var clickImage = false;
var iImageCont = 0;
var iImageTotal = 0;

var aImagesText = {};

function initForm () {
	
	if (document.getElementById("comentarios")) {
		aFormRequest['comentarios'] = new Array();
		aFormRequest['comentarios']['value'] = "(Describe qué servicios necesitas)";
		if (document.getElementById("comentarios").innerHTML == "(Describe qué servicios necesitas)") {
			aFormRequest['comentarios']['clicked'] = false;
		}
		else {
			aFormRequest['comentarios']['clicked'] = true;
			$('#comentarios').removeClass('off');
		}
	}
	
	// TODO Loads text by ajax
	// If no email company
	}


function formSubmitRequest (form) {
	
	$('#butget_btn').attr("disabled", "true"); 
	
	sErrorMessage = "";
	sErrorMessage2 = "";
	if ((form.comentarios.value == "(Describe qué servicios necesitas)") || (form.comentarios.value.replace(/^\s+|\s+$/gi, '').length < 3)) {
		sErrorMessage2 += "\n - El comentario debe tener 3 caracteres como mínimo";
	}
	
	if (sErrorMessage != "") {
		sErrorMessage = "Debes cambiar el valor de los campos:" + sErrorMessage;
	}
	
	if (form.nombre.value.length < 2) {
		sErrorMessage2 += "\n - El nombre debe tener 3 caracteres como mínimo";
	}
	if (!checkEmail (form.mail)) {
		sErrorMessage2 += "\n - El formato del E-mail es incorrecto";
	}
	if (!checkSelect (form.id_provincia)) {
		sErrorMessage2 += "\n - Selecciona una provincia";
	}
	if (!checkSelect (form.id_localidad)) {
		sErrorMessage2 += "\n - Selecciona la población";
	}
	if (!checkPhone(form.telefono)) {
		sErrorMessage2 += "\n - El número de teléfono introducido es incorrecto";
	}
	if (form.id_instalacion.value == -1) {
		sErrorMessage2 += "\n - Selecciona la sede a la que quieres enviar tu consulta.";
	}
	if (sErrorMessage2 != "") {
		sErrorMessage2 = "Se han encontrado los siguientes errores:" + sErrorMessage2;
	}
	
	if (sErrorMessage != "" || sErrorMessage2 != "") {
		
		$('#butget_btn').removeAttr("disabled");
		alert (sErrorMessage + sErrorMessage2);
	}
	else {
		submit_request ();
	}
 }


var clickImage = false;
$(function() {
	
	// Checks for phone marker
	if (document.location.hash == '#phone') {
		showtelef ();
	}
	
	});


function submit_request () {
	
	$.ajax({
		type: "POST",
		url: "/request/request/?com=211",
		data: $("#formRequest").serialize (),
		success: function(msg) {
			if (msg == 0) {
				alert ("ERROR");
				$('#butget_btn').removeAttr("disabled");
			}
			else {
				request_step_2 (msg);
				$('#butget_btn').removeAttr("disabled");
			}
		}
	});
}


function request_step_2 (iRequest) {
	
	var sNoForm = "";
	
	document.location.hash = '#header'; // Centers page on top // Hides selects under iframe problem
	if ($('#noForm').val() == 1) {  sNoForm = "&noForm=1"; }
	tb_show ("", "/request/request_run/?req=" + iRequest + sNoForm + "&KeepThis=true&TB_iframe=true&modal=true&height=500&width=704");
}

function OpenMap (head_id, bShowPhone){
	tb_show ("", "/company/head_quarter_map/?com=211&head_id="+head_id+"&show="+bShowPhone+"&KeepThis=true&TB_iframe=false&modal=true&height=500&width=705");
}

function request_step_3 (iRequest) {
	parent.document.getElementById('budgetbox_end').style.display = 'block';
	parent.document.getElementById('budgetbox_end').style.visibility = 'visible';
	parent.document.getElementById('budgetbox').style.display = 'none';
	parent.document.getElementById('budgetbox').style.visibility = 'hidden';
}


function gallery(a) {
	
	if (a >= iImageTotal) a = 0;
	iImageCont = a;
	
	thumbId = "img_" + a;
	
	// if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));

	// Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = $('#gallery a#'+thumbId);	
	
	// Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	// Hides the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	// Puts texts of image
	$('#pImageText').html ("<strong>"+aImagesText[a]['cmi_name']+"</strong>. " + aImagesText[a]['cmi_description']);
}

// calls an iframe to increase phone counter
function showtelef () {
	if ($("#divShowPhone") != null) {
		$("#divShowPhone").css("visibility", "visible");
		$("#divShowPhone").css("display", "block");
		$("#aShowPhone").css("visibility", "hidden");
		$("#aShowPhone").css("display", "none");
		
		ajaxShowPhone ();		
	}
}

function ajaxShowPhone (){ 
	$.ajax({
		type: "GET",
		url: "/company/increase_phone_views/?com=211"
	});
}


function setNameFocus () {
	document.location.href = document.location.pathname + document.location.search + "#budgetbox";
	$('#nombre').focus();
}

function card_navigation (query_string) {
	$.ajax({
		type: "GET",
		url: "/company/company_nav/?" + unescape (query_string),
		success: function(msg) {
			if (msg != "") {
				$('#nav_top').html (msg);
			}
		}
	});
}