

/////////////////////////////////////////
// write out this:
// <link href="....css" rel="stylesheet">
/////////////////////////////////////////
var internet_explorer = false;
var netscape = false;
var stylesheet_filename;
if (navigator.appName.indexOf('Netscape') != -1) {
    netscape=true;
    stylesheet_filename = '/css/pleasantstays_ns.css';
} else {
    internet_explorer=true;
    stylesheet_filename = '/css/pleasantstays_ie.css';
}
document.write ('<link href="' + stylesheet_filename + '" rel="stylesheet">\n');




// decimal point character differs by language and culture
var decimalPointDelimiter = "."


// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}



/** Validates that a literal is a valid dollar figure.
 */
function validateDollarAmount(amount){

    if(isEmpty(amount)) return false;
    //////////////////////////////////////
    // CHECK FOR NON NUMERICS
    //////////////////////////////////////
    for(i = 0; i < amount.length; i++){
        ch = amount.charAt(i);
        if(ch == '.')
            continue;
        if(i == 0 && ch == '$'){
            // DO NOTHING.. ITS OK

        } else if(! isDigit(ch)) {
            alert("Invalid dollar amount.");
            return false;
        }
    }

    //////////////////////////////////////
    // CHECK FOR LEGAL DECIMAL POINT
    //////////////////////////////////////
    hasDecimal = amount.indexOf('.') > -1
    badDecimalLocatiion = (hasDecimal && ( (amount.indexOf('.')  != (amount.length - 3)) ||
                                           (amount.indexOf('.')  == 0)));
    if(badDecimalLocatiion){
        alert("Invalid decimal point location.");
        return false;
    }
    return true;
}


// Returns true if character c is a digit
// (0 .. 9).
function isDigit (c){
    return ((c >= "0") && (c <= "9"))
}



// isFloat (STRING s [, BOOLEAN emptyOK])
//
// True if string s is an unsigned floating point (real) number.
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, then call isFloat.
//
// Does not accept exponential notation.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.


// isFloat (STRING s)
//
// True if string s is an unsigned floating point (real) number.
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isInteger, then call isFloat.
//
// Does not accept exponential notation.
//

function isFloat (s){

    var i;
    var seenDecimalPoint = false;

    if (isEmpty(s))
        return false;

    if (s == decimalPointDelimiter) return false;

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);

        if ((c == decimalPointDelimiter) && !seenDecimalPoint) seenDecimalPoint = true;
        else if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}




// isSignedFloat (STRING s [, BOOLEAN emptyOK])
//
// True if string s is a signed or unsigned floating point
// (real) number. First character is allowed to be + or -.
//
// Also returns true for unsigned integers. If you wish
// to distinguish between integers and floating point numbers,
// first call isSignedInteger, then call isSignedFloat.
//
// Does not accept exponential notation.
// see comments of function isInteger.

function isSignedFloat (s){
    if (isEmpty(s))
        false;
    else {
        var startPos = 0;

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;
        return (isFloat(s.substring(startPos, s.length)))
    }
}


////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
// Activity Scripts:
// Used to invoke contextual actions.
////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////

    function viewPropertyDetailsWithId(propertyId){
        document.MAINFORM.propId.value = propertyId;
        document.MAINFORM.activity.value = 'VIEW_PROPERTY_DETAILS_PAGE';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }

    function viewPropertyDetails(){
        document.MAINFORM.activity.value = 'VIEW_PROPERTY_DETAILS_PAGE';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }

    function updateProperty(){
        document.MAINFORM.activity.value = 'UPDATE_PROPERTY';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }


    function unListProperty(){
        document.MAINFORM.activity.value = 'UNLIST_PROPERTY';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }

    function removeProperty(){
        document.MAINFORM.activity.value = 'REMOVE_PROPERTY';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }

    function listProperty(){
        document.MAINFORM.activity.value = 'LIST_PROPERTY';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }


    function placeReservationForProperty(){
        document.MAINFORM.activity.value = 'CREATE_MANAGER_RESERVATION';
        document.MAINFORM.action.value = 'PICKDATES';
        submitForm();
    }

    function viewAvailability(){
        document.MAINFORM.activity.value = 'VIEW_PROPERTY_AVAILABILITY';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }

    function viewRatesGroup(ratesGroup){
        document.MAINFORM.activity.value = 'UPDATE_RATES';
        document.MAINFORM.action.value = 'INIT';
        document.MAINFORM.ratesGroupId.value = ratesGroup;
        submitForm();
    }

    function viewAllRates(){
        document.MAINFORM.activity.value = 'UPDATE_RATES';
        document.MAINFORM.action.value = 'VIEW_ALL_RATES';
        submitForm();
    }




    function viewPropertyReservations(){
        document.MAINFORM.activity.value = 'VIEW_PROPERTY_RESERVATIONS';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }

    function viewPropertyTravelerView(){
        document.MAINFORM.activity.value = 'VIEW_TRAVELER_PROPERTY_VIEW';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }

    //////////////////////////////////////////////////////////////////////
    // ACCOUNT LINKS
    //////////////////////////////////////////////////////////////////////
    function viewAccountDetailsWithId(accountId){
        document.MAINFORM.acntId.value = accountId;
        document.MAINFORM.activity.value = 'VIEW_ACCOUNT_DETAILS_PAGE';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function viewAccountDetails(){
        document.MAINFORM.activity.value = 'VIEW_ACCOUNT_PROPERTIES_PAGE';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function viewAccountPropertyList(){
        document.MAINFORM.activity.value = 'VIEW_ACCOUNT_PROPERTIES_PAGE';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }

    function updateAccountPreferences(){
        document.MAINFORM.activity.value = 'UPDATE_ACCOUNT_PREFERENCES';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function addProperty(){
        document.MAINFORM.activity.value = 'CREATE_PROPERTY';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }


    //////////////////////////////////////////////////////////////////////
    // USER LINKS
    //////////////////////////////////////////////////////////////////////
    function editUser(){
        document.MAINFORM.activity.value = 'UPDATE_USER';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function viewUserAccounts(){
        document.MAINFORM.activity.value = 'VIEW_ACCOUNTS_PAGE';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    //////////////////////////////////////////////////////////////////////
    // RESERVATION LINKS
    //////////////////////////////////////////////////////////////////////
    function acceptReservation(){
        document.MAINFORM.activity.value = 'HOLD_RESERVATION';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function addReservationCharge(){
        document.MAINFORM.activity.value = 'UPDATE_RESERVATION_CHARGES';
        document.MAINFORM.action.value = 'INIT';
        document.MAINFORM.chargeType.value = 'CHARGE';
        submitForm();
    }
    function addReservationDiscount(){
        document.MAINFORM.activity.value = 'UPDATE_RESERVATION_CHARGES';
        document.MAINFORM.action.value = 'INIT';
        document.MAINFORM.chargeType.value = 'DISCOUNT';
        submitForm();
    }
    function cancelReservation(){
        document.MAINFORM.activity.value = 'CANCEL_RESERVATION';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function completeReservation(){
        document.MAINFORM.activity.value = 'COMPLETE_RESERVATION';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function confirmReservation(){
        document.MAINFORM.activity.value = 'CONFIRM_RESERVATION';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function denyReservation(){
        document.MAINFORM.activity.value = 'DENY_RESERVATION';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function holdReservation(){
        document.MAINFORM.activity.value = 'HOLD_RESERVATION';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function manageReservations(){
        document.MAINFORM.activity.value = 'VIEW_ACTIVE_RESERVATIONS';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function receiveReservationPayment(){
        document.MAINFORM.activity.value = 'APPLY_RESERVATION_PAYMENT';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function reserveReservation(){
        document.MAINFORM.activity.value = 'RESERVE_RESERVATION';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function sendRefund(){
        document.MAINFORM.activity.value = 'SEND_REFUND';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function switchReservationProperty(){
        document.MAINFORM.activity.value = 'SWITCH_RESERVATION_PROPERTY';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function updateReservationCharges(){
        document.MAINFORM.activity.value = 'UPDATE_RESERVATION_CHARGES';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function updateReservationDates(){
        document.MAINFORM.activity.value = 'UPDATE_RESERVATION_DATES';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function viewReservationDetails(){
        document.MAINFORM.activity.value = 'VIEW_RESERVATION_DETAILS_PAGE';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function viewReservationDetailsWithId(reservationId){
        document.MAINFORM.resId.value = reservationId;
        document.MAINFORM.activity.value = 'VIEW_RESERVATION_DETAILS_PAGE';
        document.MAINFORM.action.value = 'INIT';
        submitForm();
    }
    function viewUser(userId){
        document.MAINFORM.activity.value = 'VIEW_USER';
        document.MAINFORM.action.value = 'INIT';
        document.MAINFORM.usrId.value = usrId;
        submitForm();
    }
////////////////////////////////////////////////////////////////////
// Date Validation Code
////////////////////////////////////////////////////////////////////
function dateValidation(alertValue){

          var checkValue = true;
          var yearLimit = true;
          var emptySearch = false;
		  var dt = new Date();


          //loop through from elements
          for(i=0; i<document.MAINFORM.length; i++){

            //looking for textarea with names that contain date
            if (document.MAINFORM[i].type == "text" && (document.MAINFORM[i].name).indexOf("Date") != -1 && document.MAINFORM[i].value != "") {
              document.MAINFORM[i].style.color = "Black";

              str = document.MAINFORM[i].value;
              var arrayDate = str.split("/"); //split date into month, day, year

                d = arrayDate[1];
                if (d == null || d == " " || d.length > 2 || d.length == 0) {
                    checkValue = false;
                } else if (d.length == 1){
                    d = d.replace("1", "01");
                    d = d.replace("2", "02");
                    d = d.replace("3", "03");
                    d = d.replace("4", "04");
                    d = d.replace("5", "05");
                    d = d.replace("6", "06");
                    d = d.replace("7", "07");
                    d = d.replace("8", "08");
                    d = d.replace("9", "09");
                }

                m = arrayDate[0];
                if (m == null || m == " " || m.length > 2 || m.length == 0) {
                    checkValue = false;
                } else if (m < 1 || m > 12) {
                    checkValue = false;
                } else if (m.length == 1 && checkValue != false) {
                    m = m.replace("1", "01");
                    m = m.replace("2", "02");
                    m = m.replace("3", "03");
                    m = m.replace("4", "04");
                    m = m.replace("5", "05");
                    m = m.replace("6", "06");
                    m = m.replace("7", "07");
                    m = m.replace("8", "08");
                    m = m.replace("9", "09");
                }

                y = arrayDate[2];
                if (y != null && y != "" && y.length != 0 && checkValue != false) {

                    currentCentury=dt.getFullYear();

                    //2-digit year, need to append the correct century
                    if (y.length == 2) {
                        y = ((currentCentury.toString()).substring(0,2))+y;
                        checkValue = true;
                    //4-digit year, do nothing
                    } else if (y.length == 4) {
                      checkValue = true;
                    //anything else would be wrong (0, 1, 3, or greater than 4 digits)
                    } else {
                      checkValue = false;
                    }

                    if (y > (currentCentury+2)) {
                        yearLimit = false;
                    }

                } else {
                    checkValue = false;
                }

              //validate the actual date entered is correct, if so then reformat it
              if (checkValue == true && d != null && d != "" && m != null && m != "" && y != null && y != "") {

                var newDate = m+"/"+d+"/"+y;
                    if (validateUSDate(newDate) == false) {
                        checkValue = false;
                        break;
                    } else { // date value is good, set it for processing
                        document.MAINFORM[i].value = newDate;
                    }
              } else {
                break;
              }

            }
          }

            if (alertValue == true) {
                if (yearLimit == false) {
                    alertValue = false;
                    checkValue = false;
                    alert ("The data range specified exceeds the available calendar range. \n\n Please enter dates between today's date and December 31, "+(currentCentury+2)+".");
                }
            }

            //check the switch value if false then popup message and set focus
            if (checkValue == false) {
                if (alertValue == true) {
                    document.MAINFORM[i].style.color = "Red";
                    document.MAINFORM[i].focus();
                    alert ("Please verify the date entered is correct and using the proper format.");
                }
                return false;
            } else {
                return true;
            }

  }

  function validateUSDate( strValue ) {
    /************************************************
    DESCRIPTION: Validates that a string contains only
    valid dates with 2 digit month, 2 digit day,
    4 digit year. Date separator can be ., -, or /.
    Uses combination of regular expressions and
    string parsing to validate date.
    Ex. mm/dd/yyyy or mm-dd-yyyy or mm.dd.yyyy

    PARAMETERS:
    strValue - String to be tested for validity

    RETURNS:
    True if valid, otherwise false.

    REMARKS:
    Avoids some of the limitations of the Date.parse()
    method such as the date separator character.
*************************************************/
  var objRegExp = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/

  //check to see if in correct format
  if(!objRegExp.test(strValue)) {
    return false;
  } else{
    var strSeparator = strValue.substring(2,3) //find date separator
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = arrayDate[1];

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0) {
        return true; //found in lookup table, good date
      }
    }

    //check for February
    var intYear = (arrayDate[2]);
    var intMonth = (arrayDate[0]);
    if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0){
      return true; //Feb. had valid number of days
    }
  }
  return false; //any other values, bad date
}
////////////////////////////////////////////////////////////////////
// Date Validation Code
////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
// Date Parsing Functions
////////////////////////////////////////////////////////////////////


//Parses through a date and assigns day, month and year
    function getCheckDate(fieldName) {
        var inDay;
        var inMonth;
        var inYear;
        var inDate = fieldName.value;
        inMonth = inDate.substr(0,2);
        //alert('Day: ' + inDay);
        inDay = inDate.substr(3,2);
        //alert('Month: ' + inMonth);
        inYear = inDate.substr(6,4);
        //alert('Year: ' + inYear);
    }

    //Parses through a date and returns month
    function getCheckDay(fieldName) {
        var inDay;
        var inDate = fieldName.value;
        inDay = inDate.substr(3,2);
        //alert('Month: ' + inMonth);
        return inMonth;
    }

    //Parses through a date and returns month
    function getCheckMonth(fieldName) {
        var inMonth;
        var inDate = fieldName.value;
        inMonth = inDate.substr(0,2);
        //alert('Month: ' + inMonth);
        return inMonth;
    }

    //Parses through a date and returns year
    function getCheckYear(fieldName) {
        var inYear;
        var inDate = fieldName.value;
        inYear = inDate.substr(6,4);
        //alert('Year: ' + inYear);
        return inYear;
    }


////////////////////////////////////////////////////////////////////
// Date Parsing Functions
////////////////////////////////////////////////////////////////////


function goToPageViaSelect(select){
    var page = select.options[select.selectedIndex].value;
    select.selectedIndex = 0;
    if(hasContent(page)){
        document.location = page;
    }
}


function highlightRow(row){
    //alert('highlight row:' + row);
    ctrlPressed  = event.ctrlKey;

    if(!row || !ctrlPressed){
        return;
    }

    if(row.tagName == "TD" || row.tagName == "td"){
        row = row.parentNode;
    }

    var i = 0;
    var child;
    for(i = 0; i < row.childNodes.length; i++){
        child = row.childNodes[i];
        if(child && (child.tagName == "TD" || child.tagName == "td")){

            if('lightblue' == child.style.backgroundColor){
                child.style.backgroundColor='';
            }
            else{
                child.style.backgroundColor='lightblue';
            }
        }
    }
}



function findProperty(search, location){


    if(location){
    	searchDropdownForSelection('neighborhood');
    	if(locationId == null || locationId == "blank")
    		searchDropdownForSelection('city');
    	if(locationId == null || locationId == "blank")
    		searchDropdownForSelection('region');
    	if(locationId == null || locationId == "blank")
    		searchDropdownForSelection('state');
    	if(locationId == null || locationId == "blank")
    		searchDropdownForSelection('country');
    	if(locationId == null || locationId == "blank")
    		locationId = null;

    	document.MAINFORM.locationId.value = locationId;
    }



    document.MAINFORM.activity.value = 'FIND_PROPERTY';
    if(search){
        document.MAINFORM.action.value = 'SEARCH';
        document.MAINFORM.findPropertyRunReport.value = 'true';

    } else {
        document.MAINFORM.action.value = '';
        document.MAINFORM.findPropertyRunReport.value = 'false';

    }
    submitForm();
}