// JavaScript Document
function checkDestinationNumber()
{
	var error = "";
	var number = document.getElementById("checknumber_form").phone_number;
	var destination = document.getElementById("checknumber_form").destination_number;
	if(number.value == "")
	{
		error = "Por favor, introduce un número.";
		number.style.borderColor = error_color;
		alert(error);
	}
	else
	{
		//if(number.value.charAt(0) == "+")
		//{
			var substringNumber = number.value.substring("1",number.value.length);
			if(!onlyNumbers(substringNumber))
			{
				error = "El número es incorrecto. Recuerda introducir sólo números y utilizar el 00 o el + para indicar el número internacional.";
				number.style.borderColor = error_color;
				alert(error);
			}
		//}
		//else
		//	
			if((!onlyNumbers(number.value)) && (number.value.charAt(0) != "+") && (error==""))
			{
				error = "El número es incorrecto. Recuerda introducir sólo números y utilizar el 00 o el + para indicar el número internacional.";
				number.style.borderColor = error_color;
				alert(error);
			}
		//}
	}
	if(error == ""){
		pageTracker._trackEvent("Inicio", "Puedo llamar");
		destination.value = formatInternationalNumber(number.value);
		createDialog();
	}
}


function createDialog(){
	DIALOG_WIDTH = 340;
	DIALOG_HEIGHT = 240;
	var div = openDialogBrown();
	div.innerHTML = "<iframe id='validador' name='validador' scrolling='no' border='0' frameborder='0' width='340' height='238' src='validador.html'></iframe>";
}

function destinationSuccess(){
	window.location.replace("validador.html");
	closeDialog();
}

function fieldDefaultValue(event,idform,fieldname,defvalue)
{
	var form = document.getElementById(idform);
	if(form != null)
	{
		for(var i=0; i<form.elements.length; i++)
		{
			if(form.elements[i].name == fieldname)
				var element = form.elements[i];
		}
		if(event == "onblur")
		{
			if(element.value == "")
				element.value = defvalue;
		}
		if(event == "onfocus")
		{
			if(element.value == defvalue)
				element.value = "";
		}
	}
}
	
var DIALOG_WIDTH = 560;
var DIALOG_HEIGHT = 340;
	
function openDialogBrown()
{
	//veilscreen
	var div_veilscreen = document.createElement("div");
	div_veilscreen.setAttribute("id","veilscreen");
	div_veilscreen.setAttribute("className", "veilscreen");
	div_veilscreen.setAttribute("class", "veilscreen");
	document.body.appendChild(div_veilscreen);
	
	//dialog
	var div = document.createElement("div");
	div.setAttribute("id","dialog");
	div.setAttribute("className", "dialog_brown");
	div.setAttribute("class", "dialog_brown");
	div.style.top = Math.floor((document.documentElement.clientHeight-DIALOG_HEIGHT)/2) + "px";
	div.style.left = Math.floor((document.documentElement.clientWidth-DIALOG_WIDTH)/2) + "px";
	div.style.height = DIALOG_HEIGHT + "px";
	div.style.width = DIALOG_WIDTH + "px";
	document.body.appendChild(div);
	//div_veilscreen.scrollIntoView(true);
	//document.body.style.overflow = "hidden";
	return div;
}

function closeDialogInternational()
{
	var div_veilscreen = top.document.getElementById("veilscreen");
	top.document.body.removeChild(div_veilscreen);
	var div = top.document.getElementById("dialog");
	top.document.body.removeChild(div);
	top.document.body.style.overflow = "auto";
}

function openDialog()
{
	//veilscreen
	var div_veilscreen = document.createElement("div");
	div_veilscreen.setAttribute("id","veilscreen");
	div_veilscreen.setAttribute("className", "veilscreen");
	div_veilscreen.setAttribute("class", "veilscreen");
	document.body.appendChild(div_veilscreen);
	//dialog
	var div = document.createElement("div");
	div.setAttribute("id","dialog");
	div.setAttribute("className", "dialog_white");
	div.setAttribute("class", "dialog_white");
	div.style.top = Math.floor((document.documentElement.clientHeight-DIALOG_HEIGHT)/2) + "px";
	div.style.left = Math.floor((document.documentElement.clientWidth-DIALOG_WIDTH)/2) + "px";
	div.style.height = DIALOG_HEIGHT + "px";
	div.style.width = DIALOG_WIDTH + "px";
	document.body.appendChild(div);
	//document.body.style.overflow = "hidden";
	//div_veilscreen.scrollIntoView(true);
	return div;
}

function closeDialog()
{
	var div_veilscreen = document.getElementById("veilscreen");
	document.body.removeChild(div_veilscreen);
	var div = document.getElementById("dialog");
	div.innerHTML = "";
	document.body.removeChild(div);
	//document.body.style.overflow = "auto";
}

function formatInternationalNumber(number)
{
    var international = false;
    if(number.charAt(0) == '+')
    {
        number = number.substring(1,number.length);
        international = true;
    }
    if(number.substring(0,2) == "00")
    {
        number = number.substring(2,number.length);
        international = true;
    }
    if(!international)
        number = "34" + number;
    return number;
}

function onlyNumbers(str)
{
	var ok = true;
	for(var i=0; i<str.length; i++)
	{
		var currentChar = str.charAt(i);
		var intValue = parseInt(currentChar);
		if (isNaN(intValue))
			ok = false;
	} 
	return ok;
}

var error_color = "red";
var correct_color = "silver";

function resetFormErrors(form)
{
	for (var i=0;i<form.length;i++)
	  form.elements[i].style.borderColor = correct_color;
}

function open_df(url,tag, bwindow) {
	pageTracker._trackEvent("Inicio", tag);	
	if (bwindow) { open(url); }
	else document.location=url;
}

function open_df_tag(url,tag1, tag2, bwindow) {
	pageTracker._trackEvent(tag1, tag2);	
	if (bwindow) { open(url); }
	else document.location=url;
}


function submitToSearch(evt){
	var k=evt.keyCode||evt.which;
	if (k==13) checkDestinationNumber();
	return k!=13;
}
