var http = false;
if(navigator.appName == "Microsoft Internet Explorer") {
	try {
		http = new ActiveXObject("MSXML2.XMLHTTP.3.0");
	} catch (e) {
		try {
			http = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			try {
				http = new XMLHttpRequest();
			} catch(err) {
				http = false;
				alert("Please upgrade your web browser to view this site");
			}
		}
	}
} else {
  http = new XMLHttpRequest();
}

errorArray  = new Array() ;

/* Error Object to hold all possible errors */
errorArray = new Object();

/* Erros for departure date */
errorArray.depart_invalid_date = "" ;
errorArray.depart_past_date = "" ;
errorArray.depart_in3days_date = "" ;
errorArray.depart_beyond_date = "" ;

/* Errors of Nights */
errorArray.night_invalid = "" ;
errorArray.night_toolong = "" ;
errorArray.night_tooshort = "";

/* Errors of return date */
errorArray.return_invalid_date = "" ;
errorArray.return_in3days_date = "" ;
errorArray.return_beyond_date = "" ;
errorArray.return_beforedepart = "" ;

/* Total traveler and Age Errors */
errorArray.travelers_total_invalid = "";
errorArray.travelers_toomany = "";
errorArray.traverlers_child_age_invalid = "" ;

/* Children Ages */
errorArray.children_age_invalid = "" ;
errorArray.children_tooold  = "" ;
errorArray.lapchild = "" ;

/* Misc */
errorArray.airport_invalid = "" ;
errorArray.destination_invalid = "" ;
/**
 * This function gets an element name and validates the content of the element
 * @param {Object} element
 * @return {boolean} true if any error happened otherwise false
 */
function validateInput(element) {

    /* String representation of all errors */
    var errorStr = "" ;

    /* String for alerts */
    var errorStr_alert = "" ;

    /* Output of the function. true if any error happened*/
    var error = validateElement(element);

    /* Create a displayable representation of errors */
    for(var err in errorArray) {
        if (errorArray[err].length > 0) {
            errorStr += "<li><span>"+errorArray[err]+"</span></li>";
            errorStr_alert += errorArray[err]+"\n" ;
        }
    }
    /* Show the error block if any error happened */
    if (errorStr.length > 0) {
        $("booking_error").update("<ul style='list-style-type: disc;'>"+errorStr+"</ul>");
        $("booking_error").show();
//				alert(errorStr_alert);
    } else {
        $("booking_error").hide();
    }

    return error ;
}
/**
 * This function validates the element itself and returns the result
 * @param {Object} element
 */
function validateElement(element) {
    var error = false ;
    if (element == 'departDate') {
        error = validateDepartDate();
    } else if (element == 'numNights') {
        error = validateNights();
    } else if (element == 'returnDate') {
        error = validateReturnDate();
    } else if (element == 'totalTravelers') {
        error = printAgeInputs();
    } else if (element == 'infant') {
        checkForInfants();
    }

    if (error) {
        setErrorBorder(element);
    }
    else {
        resetErrorBorder(element);
    }
    return error ;
}
/**
 * This function puts a red border around error source element
 * @param {Object} elementID
 */
function setErrorBorder(elementID) {
    $(elementID).setStyle({
        'background-color': '#FFCCCC'
    });
}
/**
 * Removes the error border when error resolved
 * @param {Object} elementID
 */
function resetErrorBorder(elementID) {
    if ($(elementID)) {
        $(elementID).setStyle({
            'background-color': ''
        });
    }
}

function toggleById(id) {
    var b = document.getElementById(id);
    var i = document.getElementById(id+"_img");
    // var i = $("#"+id+"_img") ;
    if ( b.style.display=='none' ) {
        b.style.display='';
        i.src="/images/common/minus_blue.gif";
    } else {
        b.style.display='none';
        i.src="/images/common/plus_blue.gif";
    }
}

