/******************************/
/* Constellation JS Functions */
/******************************/

/* ------------------------- */
/*  Account                  */
/* ------------------------- */

var URL;
var t;

function DelayedRedirect(inURL)
{
    URL = inURL;
    t = setTimeout("doRedirect();",2000);
}

function doRedirect()
{
    window.location.href=URL;
}

function HandleLogin(e, text)
{
    return Handle(e, text, "ctl00_ContentPlaceHolder1_btnLogin");
}
function HandleProperties(e, text)
{
    return Handle(e, text, "ctl00_ContentPlaceHolder1_btnSave"); 
}
function HandleResetPassword(e, text)
{
    return Handle(e, text, "ctl00_ContentPlaceHolder1_btnResetPassword"); 
}
function HandleRequestPassword(e, text)
{
    return Handle(e, text, "ctl00_ContentPlaceHolder1_btnRequest"); 
}
function HandleRegister(e, text)
{
    return Handle(e, text, "ctl00_ContentPlaceHolder1_Button1"); 
}
function Handle(e, text, elementId) {
    var CharCode = e.keyCode? e.keyCode : e.charCode;
    if(CharCode == 13){ 
        document.getElementById(elementId).click();
        return false; 
    }else{
        return true;
    }
}
function HandleAccountKeyPress(e, text, elementId) {
    var CharCode = e.keyCode? e.keyCode : e.charCode;
    if(CharCode == 13){ 
        document.getElementById(elementId).click();
        return false; 
    }else{
        return true;
    }
}


/* ------------------------- */
/*  Search                   */
/* ------------------------- */

function HandleSearch(e, text)
{
    var CharCode = e.keyCode? e.keyCode : e.charCode;
    if(CharCode == 13){
        if(text != 'Search Site' && text != '')
        { 
            location.href='/search.aspx?SearchText=' + text; 
        }
        return false; 
    }else{
        return true;
    }
}

function HandleLogin(e, text)
{
    return Handle(e, text, "ctl00_ContentPlaceHolder1_btnLogin");
}
function HandleProperties(e, text)
{
    return Handle(e, text, "ctl00_ContentPlaceHolder1_btnSave"); 
}
function HandleResetPassword(e, text)
{
    return Handle(e, text, "ctl00_ContentPlaceHolder1_btnResetPassword"); 
}
function HandleRequestPassword(e, text)
{
    return Handle(e, text, "ctl00_ContentPlaceHolder1_btnRequest"); 
}
function HandleRegister(e, text)
{
    return Handle(e, text, "ctl00_ContentPlaceHolder1_Button1"); 
}
function Handle(e, text, elementId) {
    var CharCode = e.keyCode? e.keyCode : e.charCode;
    if(CharCode == 13){ 
        document.getElementById(elementId).click();
        return false; 
    }else{
        return true;
    }
}

function Redirect(url) {
    window.location = url;
}


function findPos(obj) {
	var curleft = curtop = 0;

    var UserBox = document.getElementById(obj);
    curleft = (document.documentElement.clientWidth / 2) - (UserBox.offsetWidth / 2);
    curtop = (document.documentElement.clientHeight / 2) - (UserBox.offsetHeight / 2) + document.documentElement.scrollTop;

	return [curleft,curtop];
}

function switchTabs(to) {
    
}


function mouseOverRatings(rating) {
    document.getElementById("hand1").src = (rating > 0) ?
        "/images/UserBox/hand1on.jpg" :
        "/images/UserBox/hand1off.jpg";
        
    document.getElementById("hand2").src = (rating > 1) ?
        "/images/UserBox/hand2on.jpg" :
        "/images/UserBox/hand2off.jpg";
        
    document.getElementById("hand3").src = (rating > 2) ?
        "/images/UserBox/hand3on.jpg" :
        "/images/UserBox/hand3off.jpg";
        
    document.getElementById("hand4").src = (rating > 3) ?
        "/images/UserBox/hand4on.jpg" :
        "/images/UserBox/hand4off.jpg";
        
    document.getElementById("hand5").src = (rating > 4) ?
        "/images/UserBox/hand5on.jpg" :
        "/images/UserBox/hand5off.jpg";
}
function mouseOutRatings() {
    document.getElementById("hand1").src = (document.getElementById("ctl00_ub_Rating").value > 0) ?
        "/images/UserBox/hand1on.jpg" :
        "/images/UserBox/hand1off.jpg";
        
    document.getElementById("hand2").src = (document.getElementById("ctl00_ub_Rating").value > 1) ?
        "/images/UserBox/hand2on.jpg" :
        "/images/UserBox/hand2off.jpg";
        
    document.getElementById("hand3").src = (document.getElementById("ctl00_ub_Rating").value > 2) ?
        "/images/UserBox/hand3on.jpg" :
        "/images/UserBox/hand3off.jpg";
        
    document.getElementById("hand4").src = (document.getElementById("ctl00_ub_Rating").value > 3) ?
        "/images/UserBox/hand4on.jpg" :
        "/images/UserBox/hand4off.jpg";
        
    document.getElementById("hand5").src = (document.getElementById("ctl00_ub_Rating").value > 4) ?
        "/images/UserBox/hand5on.jpg" :
        "/images/UserBox/hand5off.jpg";
}
function setRatings(rating) {
    document.getElementById("ctl00_ub_Rating").value = (document.getElementById("ctl00_ub_Rating").value == rating) ?
        0 :
        rating;
}


/* ------------------------- */
/*  XML Loading              */
/* ------------------------- */
// global flag
var isIE = false;

// global request and XML document objects
var req;

var xmlhttp;

function loadXMLDoc(url) {
    //alert(url);

	xmlhttp=null;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest) {
		xmlhttp=new XMLHttpRequest();
	}
	// code for IE
	else if (window.ActiveXObject){
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xmlhttp!=null) {
		xmlhttp.onreadystatechange = processReqChange;
		xmlhttp.open("GET", url, true);
		xmlhttp.send(null);
	} else {
		alert("Your browser does not support XMLHTTP.");
	}
}

// handle onreadystatechange event of req object
function processReqChange() {
    // only if req shows "loaded"
    if (xmlhttp.readyState == 4) {
        // only if "OK"
        if (xmlhttp.status == 200) {
            parseXMLDoc();
         } else {
            alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
         }
    }
}



function parseXMLDoc() {
    //var action = xmlhttp.responseXML.getElementsByTagName("action")[0].firstChild.data;
    var result = xmlhttp.responseXML.getElementsByTagName("result")[0].firstChild.data;
  
    alert(result);
    if(result.indexOf("remove") > 0) {
        window.location.reload();
    } else if(result == "Your rating/review has been saved.") {
        document.getElementById("ctl00_hdnReload").value = "1";
        document.getElementById("ctl00_con_btn").click();
    }
    //eval(action + "(result);");
}


/* ------------------------- */
/*  Itinerary                */
/* ------------------------- */
function newItinerary() {
    var name = prompt("What is your new itinerary's name?");
    if(name.length > 0 && name != null)
        window.location = "Itinerary.aspx?new=" + name;
}

function openTripBuilder() {
    document.getElementById('TripBuilderPopUp').style.display = 'block';
}

function openItineraryLoginBox() {
    var ItineraryLogin = document.getElementById("ItineraryLogIn");
    
    if(ItineraryLogin.style.display != "block") {
        ItineraryLogin.style.display = "block";
        coords = findPos("ItineraryLogIn");
        ItineraryLogin.style.top = (coords[1] + 15) + "px";
        ItineraryLogin.style.left = coords[0] + "px";
        
    } else {
        ItineraryLogin.style.display = "none";
    }
}


/* ------------------------- */
/*  User Box                 */
/* ------------------------- */
function openUserBox() {
    toggleUserBox2(null, null, document.getElementById("ctl00_ub_Type").value, document.getElementById("ctl00_ub_TypeID").value);
}
function toggleUserBox(button, type, area, id) {
    var UserBox = document.getElementById("UserBox");
    
    if(UserBox.style.display != "block") {
        UserBox.style.display = "block";
        coords = findPos("UserBox");
        UserBox.style.top = (coords[1] + 15) + "px";
        UserBox.style.left = coords[0] + "px";
        
    } else {
        UserBox.style.display = "none";
    }
        
    document.getElementById("ctl00_ub_Type").value = area;
    document.getElementById("ctl00_ub_TypeID").value = id;
    
    showReadBlock();
}
function toggleUserBox2(button, type, area, id) {
    if(document.getElementById("ReadReviews") != null && Reviews != null) {
        document.getElementById("ReadReviews").innerHTML = Reviews[id];
    }
    toggleUserBox(button, type, area, id);
}

function cancelRatingReview() {
    var UserBox = document.getElementById("UserBox");
    UserBox.style.display = "none";
}

function showWriteBlock() {
    var WriteBlock = document.getElementById("ub_WriteBlock");
    var ReadBlock = document.getElementById("ub_ReadBlock");
    var WriteReview = document.getElementById("ub_WriteReviews");
    var ViewReviews = document.getElementById("ub_ViewReviews");
    var SignUp = document.getElementById("ub_SignUp");
    
    ReadBlock.style.display = "none";
    if (SignUp != null) SignUp.style.display = "none";
    WriteBlock.style.display = "block";
    WriteReview.src = "/images/UserBox/WriteReviewOn.jpg";
    ViewReviews.src = "/images/UserBox/ViewReviewsOff.jpg";
}

function showReadBlock() {
    var WriteBlock = document.getElementById("ub_WriteBlock");
    var ReadBlock = document.getElementById("ub_ReadBlock");
    var WriteReview = document.getElementById("ub_WriteReviews");
    var ViewReviews = document.getElementById("ub_ViewReviews");
    var SignUp = document.getElementById("ub_SignUp");
    
    WriteBlock.style.display = "none";
    if (SignUp != null) SignUp.style.display = "none";
    ReadBlock.style.display = "block";
    WriteReview.src = "/images/UserBox/WriteReviewOff.jpg";
    ViewReviews.src = "/images/UserBox/ViewReviewsOn.jpg";
}

function showSignUp() {
    var LoginBlock = document.getElementById("ub_WriteBlock");
    var SignUp = document.getElementById("ub_SignUp");
    
    LoginBlock.style.display = "none";
    SignUp.style.display = "block";
}


/* ------------------------- */
/*  Info Box                 */
/* ------------------------- */

function closeInfoBox() {
    var InfoBox = document.getElementById("InfoBox");
    InfoBox.style.display = "none";
}
function toggleInfoBox() {
    var InfoBox = document.getElementById("InfoBox");
    
    if(InfoBox.style.display != "block") {
        InfoBox.style.display = "block";
        coords = findPos("InfoBox");
        InfoBox.style.top = (coords[1] + 15) + "px";
        InfoBox.style.left = coords[0] + "px";
        
    } else {
        InfoBox.style.display = "none";
    }
}