﻿// --------------------- Funzioni Adv ---------------------------
function MaintainSession()
{
    var http = null;
		
	if(typeof XMLHttpRequest != 'undefined')
	{
		try
		{
			http = new XMLHttpRequest();
		}
		catch (e) { http = null; }
	}
	else
	{
		try
		{
			http = new ActiveXObject("Msxml2.XMLHTTP") ;
		}
		catch (e)
		{
			try
			{
				http = new ActiveXObject("Microsoft.XMLHTTP") ;
			}
			catch (e) {	http = null; }
		}
	}
	
	if (http)
	{
    
     http.open("GET", "/MaintainSession.aspx",true);
     http.send(null)
     
     setTimeout(MaintainSession, 30000);
    }
}

function goToSearchResults(controlid)
{
    var errors = new Array();
    
    if (CheckSelect(controlid + '_ddlDestinations','-1'))
        errors.push('Destinazione');
        
    if (IsEmpty(controlid + '_txtDate'))
        errors.push('Data di partenza');
        
    if (OpenAlertError(errors))
    {
        document.forms['searchForm'].destinationID.value = $('[id$=ddlDestinations]').val();
        document.forms['searchForm'].childDestinationID.value = $('[id$=ddlLocalities]').val();
        document.forms['searchForm'].departureDate.value = $('[id$=txtDate]').val();
        document.forms['searchForm'].duration.value = -1;
        document.forms['searchForm'].submit();
    }
}

function populateLocalities()
{
    if ($('[id$=ddlDestinations]').val() == '-1')
    {
        $('[id$=ddlLocalities]').empty();  
        $('[id$=ddlLocalities]').append($('<option></option>').text('Selezionare una destinazione').val('-1'));                      
    }
    else
    {
        var o = new SearchEngine();
        o.getDestinations($('[id$=ddlDestinations]').val(), 'it-IT', populateLocalities_callBack);
    }
}

function populateLocalities_callBack(response)
{
    $('[id$=ddlLocalities]').empty();
    $$('[id$=ddlLocalities]').append($('<option></option>').text('Tutte le località').val('-1'));
    if (response.error)
        alert(response.error.message);
    else
    {
        $.each(response.result, function(o) {
            $$('[id$=ddlLocalities]').append($('<option></option>').text(this[0]).val(this[1]));
        });
    }
}

function goToPage(i)
{
	$$('hdnPage').val(i);	
	__doPostBack('lnkPagination','');
}

function goToPackage(url, duration, departureDate, destinationID, parentDestinationID)
{
    document.forms['searchForm'].destinationID.value = parentDestinationID;
    document.forms['searchForm'].childDestinationID.value = destinationID;
    document.forms['searchForm'].departureDate.value = departureDate;
    document.forms['searchForm'].duration.value = -1;
    document.forms['searchForm'].action = url;
    document.forms['searchForm'].submit();    
}

function GoToPackageFromSearch(frmAction, duration, departureDate, destinationID)
{ 
    document.forms["searchForm"].destinationID.value = destinationID;
    document.forms["searchForm"].duration.value = duration;
    document.forms["searchForm"].departureDate.value = departureDate;
    document.forms["searchForm"].action = frmAction;   
    document.forms["searchForm"].submit();
}

function popUpGallery(theURL) 
{
  var winWidth = 1000;
  var winHeight = 550;
  var screenWidth = ( screen.width - winWidth ) / 2;
  var screenHeight = ( screen.height - winHeight ) / 2;
  
  window.open(theURL,"popupGalleryWindow","screenX=" + screenWidth + ",screenY=" + screenHeight + ",left=" + screenWidth + ",top=" + screenHeight + ",width=" + winWidth + ",height=" + winHeight + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,copyhistory=no");
}

function popupGenerico(url) 
{
    window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=640,height=400,screenX=250,screenY=250,top=400,left=420')
}

function openPopup(url, width)
{
    return window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=' + width)    
}

function checkCallMeBack()
{
	
	if(document.getElementById("txtCMBFirstName").value == "")
	{
		alert("Inserire il Nome");
		return false;
	}
	
	if(document.getElementById("txtCMBLastName").value == "")
	{
		alert("Inserire il Cognome");
		return false;
	}
	
	if(document.getElementById("txtCMBMobile").value == "" && document.getElementById("txtCMBPhone").value == "")
	{
		alert("Inserire un numero di telefono Cellulare o Fisso");
		return false;
	}
			
	if(!IsValidHomeNumber("txtCMBMobile") && document.getElementById("txtCMBMobile").value != "")
	{
		alert("Inserire solo cifre nel campo Cellulare");
		return false;
	}
	
	if(!IsValidHomeNumber("txtCMBPhone") && document.getElementById("txtCMBPhone").value != "")
	{
		alert("Inserire solo cifre nel campo Telefono");
		return false;
	}
	
	if(document.getElementById("txtCMBEmail").value == "")
	{
		alert("Inserire un indirizzo E-mail");
		return false;
	}
	
	if(!IsValidEmail("txtCMBEmail"))
	{
		alert("Inserire una E-mail valida");
		return false;
	}
	
	if(!document.getElementById("cbxCMBPrivacy").checked)
	{
		alert("Per procedere è necessario acconsentire al trattamento dei dati personali.");
		return false;
	}
	
	return true;
}

function sendCallMeBack()
{
	if(checkCallMeBack())
		__doPostBack('lnkCallMeBack','');
}

function CreateDdl(appendToObj, ddlId, ddlClass, optionsArray, optionValueSelected, createEmptyOption, emptyOptionValue, emptyOptionText, onChangeFunction)
{
	// Creo la Select 
	newSelect = document.createElement("select");
	
	// Imposto il suo Id
	newSelect.setAttribute("id",ddlId);
	newSelect.setAttribute("name",ddlId);
	
	// Se ha una classe la assegno
	if(ddlClass != "")
		newSelect.className = ddlClass;
		
	// Se ha un "onchange" lo assegno
	if(onChangeFunction != "")
		newSelect.onchange = new Function(onChangeFunction);
	
	if(createEmptyOption)
	{
		newOption = document.createElement("option");
		newOptionText = document.createTextNode(emptyOptionText);
		newOption.appendChild(newOptionText);
		newOption.setAttribute("value", emptyOptionValue);
		newSelect.appendChild(newOption);
	}
	
	if(optionsArray.length > 0)
	{
		// Creo le Option
		var ddlOptions = new Array();
		ddlOptions = optionsArray.split("#");
			
		for (var i = 0; i < ddlOptions.length; i++)
		{
			var optionValue = ddlOptions[i].split("@");
			
			newOption = document.createElement("option");
			newOptionText = document.createTextNode(optionValue[0]);
			newOption.appendChild(newOptionText);
			newOption.setAttribute("value", optionValue[1]);
			if(optionValue[1] == optionValueSelected)
				newOption.setAttribute("selected", "true");
			
			newSelect.appendChild(newOption);
		}
	}
	
	appendToObj.appendChild(newSelect);
}

// Funzione per la concatenazione delle stringhe
function StringBuilder()
{
	this._strings = [];
	this.append = function(s) { this._strings.push(s); };
	this.toString = function() { return this._strings.join(''); };
}

function GoToAnchor(anchorName)
{
	location.href = "#" + anchorName;
}
