// Calendar.js.
//<script>

function ProcessCell(objCell)
{
	//var objCell = document.getElementById(strName);
	if (objCell.className == "TDBooked" ||  objCell.className == "TDEmptyStart")
		return false;

 
	var oLblPriceElement = document.all("lblPrice");
	var oLblTaxesElement = document.all("lblTaxes");
	var oLblTotalPriceElement = document.all("lblTotal");
	var oLblTotalPriceHeatElement = document.all("lblTotalHeat");
	var oLblNightStay = document.all("lblNightStay");
	var oLblCleaningFee = document.all("lblCleaningFee");
	var oLblRateCleaning = document.all("lblRateCleaning");
	
	var intCleaningThreshold = document.all("lblCleaningThreshold").innerHTML;
	var sCurrencySymbol = document.all("lblCurrencySymbol").innerHTML;
	var exch = Number(document.all("txtRate").innerHTML);
	var TotalNights = new Number(oLblNightStay.innerHTML);
	var tmpStr = new String(objCell.id);
	tmpStr = tmpStr.substr(tmpStr.lastIndexOf("_")+1);
	//alert(DateSelected);
	
	var tmpDate = new Date(tmpStr);

	

	var areDeleting = new Boolean();
	if(objCell.className == "TDAvailSelected" || objCell.className == "TDEnd_selected")
		areDeleting = true;
	else
		areDeleting = false;

	if(TotalNights == 0)
	{
		document.all("StayFrom").value = tmpStr;
		//document.all("StayFrom_tb").value = tmpStr;
		StayFrom_outer_VisibleDate = tmpStr;
		tmpDate.setDate(tmpDate.getDate() + 1);
		document.all("StayTo").value = getShortDate(tmpDate);
		//document.all("StayTo_tb").value = document.all("StayTo").value
		//StayTo_outer_VisibleDate = document.all("StayTo_tb").value;

	}
	else  // There are dates selected already...
	{
		var tmpVal = new Number();
		tmpVal = checkBeforeOrAfterSelected(tmpDate);
		if ((tmpVal == 0 )  && (TotalNights >=1 && !areDeleting)) // Exit and don't continue if this isn't next to selected date.
			return 0;
		if (tmpVal == 3 && areDeleting)
			return 0;
			
		var tmpStayFrom = new Date(document.all("StayTo").value)
		var tmpStayTo = new Date(document.all("StayFrom").value)
		if( (tmpDate > tmpStayTo || tmpDate < tmpStayFrom ))
		if(tmpDate > tmpStayTo)
		{
			//alert("Set End Date");
			tmpDate.setDate(tmpDate.getDate() + 1);
			document.getElementById("StayTo").value = getShortDate(tmpDate);
			//TODO: Add this back    document.getElementById("StayTo_tb").value = document.getElementById("StayTo").value
			//StayTo_outer_VisibleDate = document.all("StayTo").value;
			//alert("setting StayTo: " + document.getElementById("StayTo").value );
		}
		else
		{
			//alert("Set Start Date");
			document.getElementById("StayFrom").value = getShortDate(tmpDate);
			// TODO: Add this back    document.getElementById("StayFrom_tb").value = document.getElementById("StayFrom").value
			//StayFrom_outer_VisibleDate = document.all("StayFrom").value
			//alert("setting StayFrom: " + document.getElementById("StayFrom").value );

		}
	}

	if( areDeleting )
	{

//		oLblPriceElement.innerHTML = FormatCurrency(Number(FMClean(oLblPriceElement.innerHTML)) - Number(objCell.title.substr(objCell.title.lastIndexOf('$')+1)),1);
		oLblPriceElement.value = "$" + FormatCurrency(Number(FMClean(oLblPriceElement.value)) - Number(objCell.title.substr(objCell.title.lastIndexOf('$')+1)),1);

		oLblNightStay.innerHTML = TotalNights - 1;
		if(objCell.className == "TDAvailSelected")
			objCell.className = "TDAvail";
		if(objCell.className == "TDEnd_selected")	
			objCell.className = "TDEndEmpty";
		//objCell.style.backgroundColor = "";
	}
	else
	{
//		oLblPriceElement.innerHTML = FormatCurrency(Number(FMClean(oLblPriceElement.innerHTML)) + Number(objCell.title.substr(objCell.title.lastIndexOf('$')+1)),1);

		oLblPriceElement.value = "$" + FormatCurrency(Number(FMClean(oLblPriceElement.value)) + Number(objCell.title.substr(objCell.title.lastIndexOf('$')+1)),1);

		oLblNightStay.innerHTML = TotalNights + 1;
		if(objCell.className == "TDAvail")
			objCell.className = "TDAvailSelected";
		if(objCell.className == "TDEndEmpty")	
			objCell.className = "TDEnd_selected";

	}

	if(Number(oLblNightStay.innerHTML) >= intCleaningThreshold || Number(oLblNightStay.innerHTML) == 0 )
		oLblCleaningFee.value = "$0.00";
	else
		oLblCleaningFee.value = "$" + oLblRateCleaning.innerHTML;  // TODO: Make this one a dynamic variable


//	oLblTaxesElement.innerHTML = FormatCurrency(Math.round(Number(FMClean(oLblPriceElement.innerHTML)) * GetTaxRate(objCell.id) * 100) / 100,1);
	oLblTaxesElement.value = "$" + FormatCurrency(Math.round((Number(FMClean(oLblPriceElement.value))+ Number(FMClean(oLblCleaningFee.value))) * GetTaxRate(objCell.id) * 100) / 100,1);

//	oLblTotalPriceElement.innerHTML = FormatCurrency(Number(FMClean(oLblPriceElement.innerHTML)) + Number(FMClean(oLblTaxesElement.innerHTML)),1);

	oLblTotalPriceElement.value = "$" + FormatCurrency(Number(FMClean(oLblPriceElement.value)) + Number(FMClean(oLblCleaningFee.value)) + Number(FMClean(oLblTaxesElement.value)),1);

	if (Number(oLblNightStay.innerHTML) <= 7 && Number(oLblNightStay.innerHTML) > 0 )
		oLblTotalPriceHeatElement.value = "$" + FormatCurrency(Math.round((Number(lblRatePoolHeat.innerHTML) * (1 + GetTaxRate(objCell.id)) + Number(FMClean(oLblTotalPriceElement.value))) * 100) /100,1);
	else
		oLblTotalPriceHeatElement.value = "$" + FormatCurrency(Math.round(((Number(lblRatePoolHeat.innerHTML) / 7) * Number(FMClean(oLblNightStay.innerHTML)) * (1 + GetTaxRate(objCell.id)) + Number(FMClean(oLblTotalPriceElement.value))) * 100) /100,1);

	if (Number(document.all("txtRate").innerHTML) != 0)
	{
//		document.all("lblFPrice").innerHTML = sCurrencySymbol + " " + FormatCurrency(Math.round(Number(FMClean(oLblPriceElement.innerHTML)) * exch * 100)/100, 1 );
		document.all("lblFPrice").innerHTML = sCurrencySymbol + " " + FormatCurrency(Math.round(Number(FMClean(oLblPriceElement.value)) * exch * 100)/100, 1 );
		document.all("lblFCleaningFee").innerHTML = sCurrencySymbol + " " + FormatCurrency(Math.round(Number(FMClean(oLblCleaningFee.value)) * exch* 100)/100, 1 );

		document.all("lblFTaxes").innerHTML = sCurrencySymbol + " " + FormatCurrency(Math.round(Number(FMClean(oLblTaxesElement.value)) * exch* 100)/100, 1 );
		document.all("lblFTotal").innerHTML = sCurrencySymbol + " " + FormatCurrency(Math.round(Number(FMClean(oLblTotalPriceElement.value)) * exch * 100)/100 ,1);
		document.all("lblFTotalHeat").innerHTML = sCurrencySymbol + " " + FormatCurrency(Math.round(Number(FMClean(oLblTotalPriceHeatElement.value)) * exch * 100 )/100,1);
	}
	return false;
	//objCell.
}
function checkBeforeOrAfterSelected(tdDate)
{
	var tmpDate1 = new Date(tdDate);
	var tmpDate2 = new Date(tdDate);
	var rtnVal = new Number();
	rtnVal = 0;
	tmpDate1.setDate(tdDate.getDate() - 1)
	tmpDate2.setDate(tdDate.getDate() + 1); //Add one day, check if class is selected...
	if(checkIsSelected(tmpDate1))
		rtnVal = 1;
	if(checkIsSelected(tmpDate2))
	{
		if(rtnVal == 1)
			rtnVal = 3;
		else
			rtnVal = 2;
	}
	return rtnVal;

}

function checkIsSelected(dtDate)
{
	var tmpString = String();
	var obTD;
	obTD = findTDByDate(dtDate);
	if (obTD != null) 
	{
		tmpString = obTD.className;
		if (tmpString.indexOf("elected",0) > 0 )
			return true;
		else
			return false;	
	}
	return false;
}

function getShortDate(dtDate)
{
	var tmpString = new String();
	tmpString = String(dtDate.getMonth()+1 + "/" + dtDate.getDate() + "/" + dtDate.getFullYear())
	return tmpString;	
}

//This function finds and returns the TD element, by the date given
function findTDByDate(theDate)
{
	var tmpString = String()
	tmpString = getShortDate(theDate);
	var ElementID = document.getElementsByName(tmpString);
	var oCollection = document.getElementsByTagName("TD");
	
	var cnt;
	for(cnt=0;cnt<oCollection.length;cnt++)
	{
		var tmpTD = oCollection(cnt)
		if (tmpTD.id != '')
		{
		if(tmpTD.id.indexOf(tmpString)>=0)
			return tmpTD;
		}
	}
}

function ToSelected( theDate, ID)
{
// TODO: adjust for selected -1 as displayed blocked days.
	var dtDate = new Date(theDate);
	
	var TDItem = findTDByDate(dtDate);
	if (TDItem != undefined)
	{
		// TODO: Select the cell for this one...
		//alert(TDItem.id)
		ProcessCell(TDItem);
	}
}


function FromSelected(theDate, ID)
{
	var dtDate = new Date(theDate);

	var TDItem = findTDByDate(dtDate);
	if (TDItem != undefined)
	{
		// TODO: Select the cell for this one...
		//alert(TDItem.id)
		ProcessCell(TDItem);
	
	}
}

function GetTaxRate(TheDate)
{
	st = new String();
	st = TheDate

	st = st.substr( st.indexOf("_",0)+1)
	d = new Date(st);
	dNewTaxes = new Date("7/1/2004");
	if (d >= dNewTaxes )
	{
		return .13; // as of July 1, new tax rate.
	}
	else
	{
		return .12;
	}
}
function GetDecimalDelimiter(nCountryCode)
{

       var sRet='';

       switch (nCountryCode)
       {
            case 3:   
                           sRet = '#';
                           break;
            case 2:                      
                           sRet = ',';
                           break;
            default:
                           sRet = '.';
                           break;
        }
      return sRet;
}
function GetCommaDelimiter(nCountryCode)
{
       var sRet='';
       switch (nCountryCode)
       {
            case 3:                      
                           sRet = '*';
                           break;
            case 2:        
                           sRet = ',';
                           break;
            default:
                           sRet = ',';
                           break;
        }
      return sRet;
}
function FormatClean(num)
{
     var sVal='';
     var nVal = num.length;
     var sChar='';    
   try
   {
       for(i=0;i<nVal;i++)
      {
         sChar = num.charAt(i);
         nChar = sChar.charCodeAt(0);
         if ((nChar >=48) && (nChar <=57))  { sVal += num.charAt(i);   }
      }
   }
    catch (exception) { AlertError("Format Clean",e); }
    return sVal;
}
function FMClean(num) // preserves the "."
{
     var sVal='';
     var nVal = num.length;
     var sChar='';
   try
   {
       for(i=0;i<nVal;i++)
      {
         sChar = num.charAt(i);
         nChar = sChar.charCodeAt(0);
         if (((nChar >=48) && (nChar <=57)) || nChar == 46 )  { sVal += num.charAt(i);   }
      }
   }
    catch (exception) { AlertError("Format Clean",e); }
    return sVal;
}
function FormatCurrency(num,nCountryCode)
{       
        var sVal='';
        var minus='';
        var Decimal='';
        var strNum = num.toString();
		//alert("in: " + strNum);
        Decimal = GetDecimalDelimiter(nCountryCode);
        if (strNum.lastIndexOf("-") == 0) { minus='-'; }
        if (strNum.lastIndexOf(Decimal) < 0) { strNum = strNum + '.00'; }
        num = FormatClean(num);
        sVal = minus + FormatDollar(strNum,GetCommaDelimiter(nCountryCode)) + GetDecimalDelimiter(nCountryCode) + FormatCents(strNum); 
		//alert( strNum + ' to ' + sVal );
		//alert("out: " + sVal);
        return sVal;
}

function FormatNumber(num,nCountryCode)
{       
        var sVal='';
        var minus='';
        var CommaDelimiter='';

        try 
       {

           CommaDelimiter = GetCommaDelimiter(nCountryCode);
			commaDelimiter = "";
           if (num.lastIndexOf("-") == 0) { minus='-'; }
           num = FormatClean(num);
           num = parseInt(num);
           var samount = new String(num);
           for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
          {
             samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
           }
        }
         catch (exception) { AlertError("Format Number",e); }
        return minus + samount;
}

function FormatCents(amount)
{
     var cents = '';
      try
      {
//		if (amount.lastIndexOf(".") -  == 0)
			//alert ("in cents:" + amount)
           //amount = parseInt(amount);
           var samount = new String(amount);
          	if (samount.lastIndexOf(".") < 0)
          	{
          		samount = samount + ".";
          	}
			while(samount.length - samount.lastIndexOf(".") <= 2)
			{
			 samount = samount + "0";
			}

           //if (samount.length == 0) { return '00'; }
           //if (samount.length == 1) { return '0' + samount; }
           //if (samount.length == 2) { return samount; }
         
           cents =  samount.substring(samount.length -2);
			//alert("out cents:" + cents );
      }
      catch (exception) { AlertError("Format Cents",e); }
      return cents;
}

function FormatDollar(amount,CommaDelimiter)
{
   try 
   {

        amount = parseInt(amount);
        var samount = new String(amount);
        if (samount.length <= 3) { return samount; }  
        //samount =  samount.substring(0,samount.length -2);
		//alert("in dollar:" + amount + " subs: " + samount)
        for (var i = 0; i < Math.floor((samount.length-(1+i))/3); i++)
        {
           samount = samount.substring(0,samount.length-(4*i+3)) + CommaDelimiter + samount.substring(samount.length-(4*i+3));
         }
		//alert("out dollar:" + samount)

   }
    catch (exception) { AlertError("Format Comma",e); }
    return samount;
}

 function AlertError(MethodName,e)
 {
            if (e.description == null) { alert(MethodName + " Exception: " + e.message); }
            else {  alert(MethodName + " Exception: " + e.description); }
 }
 