//Shopping cart Javascript functions

var MaxCartItems = 19;
var ReferrerLife = 1000 * 60 * 60 * 2;
var pageloc = self.location;
var storeID = 'GulfCondos';

//using ASCII values 192, 193, 194, 195, 196, 197 as separators
var sp1="À";
var sp2="Á";
var sp3="Â";
var sp4="Ã";
var sp5="Ä";
var sp6="Å";

function SaveReferrer()
{
CookieName = 'Referrer';
CookieValue = GetCookie(CookieName);
if(CookieValue == null){CookieValue = document.referrer;}
var expdate = new Date();
expdate.setDate(ReferrerLife);
SetCookie(CookieName,CookieValue,expdate);
}

function getCookieVal (offset)
{
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name)
{
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen)
  {
  var j = i + alen;
  if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
  i = document.cookie.indexOf(" ", i) + 1;
  if (i == 0) break;
  }
return null;
}

function SetCookie (name, value, expdate)
{
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = expdate;
var path = "/";
var domain = null;
var secure = false;

document.cookie = name + "=" + escape (value) +
  ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
  ((path == null) ? "" : ("; path=" + path)) +
  ((domain == null) ? "" : ("; domain=" + domain)) +
  ((secure == true) ? "; secure" : "");
}

function DeleteCookie(name)
{
var exp = new Date();
exp.setTime (exp.getTime() - 1);  // This cookie is history
var cval = GetCookie (name);
if (cval != null){document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + ";path=/";}
}

function AddItem(ID,Sdesc,Price,Qty,Weight,WeightUnit,Spship)
{
if (Qty <= 0)
  {
  rc = alert('The Quantity must be greater than 0');
  return false;
  }

//check for duplicate values in cookies

if (isduplicate(Sdesc))
  {
  alert('Information for that property has already been requested');
  return;
  }

if (confirm('Please confirm the addition of ' +Sdesc+ '\nto your list of properties to get information on.'))
  {
  for(NumItems = 0; NumItems<=MaxCartItems; NumItems++)
    {
    if(NumItems == MaxCartItems)
      {
      alert('Maximum of '+MaxCartItems+' different items can fit in the shopping cart, please go to the Checkout Counter and submit your order.  Then empty your cart and shop some more later.');
      }
    else
      {
      CookieName = storeID + NumItems;
      CookieValue = GetCookie(CookieName);
      if(CookieValue == null)
        {
        var cvalue = Sdesc;
        while (cvalue.indexOf("`") > -1) cvalue=cvalue.replace("`", "'");
        CookieValue = "["+ID+sp1+cvalue+sp2+Price+sp3+Qty+sp4+Weight+sp5+WeightUnit+sp6+Spship+"]";
        SetCookie(CookieName,CookieValue);
        break;
        }
      }
    }
  }
//  return true;
}

function FloatFormat(expr,decplaces)
{
var str = "" + Math.round(eval(expr) * Math.pow(10,decplaces));
while(str.length <= decplaces){str = "0" + str;}
var decpoint = str.length - decplaces;
return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}

function ShowCart()
{
var isEmpty=true;
totprice = 0;
itemlist = 0;
document.writeln('<form>');
document.writeln('<table border=2 cellspacing=3 cellpadding=5>');
document.writeln('<tr><td align=center><font size=4><b>Property Name</b></font></td><td align=center><font size=4><b>Action</b></font></td></tr>');
for(NumItems = 0; NumItems<MaxCartItems; NumItems++)
  {
  CookieName = storeID +NumItems;
  CookieValue = GetCookie(CookieName);
  if(CookieValue != null)
    {
    for(var i=0;i<=CookieValue.length;i++)
      {
      if (CookieValue.substring(i,i+1) == '[') {itemstart = i+1;}
      else if (CookieValue.substring(i,i+1) == sp1)
        {
        isEmpty=false;
        ID = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp2)
        {
        Sdesc = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp3)
        {
        Price = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp4)
        {
        itemend = i;
        Qty = CookieValue.substring(itemstart, itemend);
        ItemTotal = 0;
        ItemTotal = (eval(Price*Qty));
        temptotal = ItemTotal * 100;
        totprice = totprice + ItemTotal;
        itemlist=itemlist+1;
        FItemTotal = FloatFormat(ItemTotal,2);
        document.writeln('<tr><td>'+Sdesc+'<input name=Qty'+NumItems+' type=hidden></td><td><a href="javascript:RemoveItem('+NumItems+')">Remove</a></td></tr>');
        }
      else if (CookieValue.substring(i,i+1) == sp5)
        {
        Weight = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp6)
        {
        WeightUnit = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == ']')
        {
        Spship = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      }
    }
  }
document.writeln('</table>');
if (isEmpty==true)
  {
  document.write('<br><table width=375><tr><td align=center>You have not selected any condos to receive information about.  ');
  document.write('Please go to the <a href="http://www.gulfcondos.com/condos/condolist.asp">Condos page</a> and select from the list there.</td></tr></table>');
  }
document.writeln('</form>');
}

function QtyChange(itemnum,Qty)
{
CookieName = storeID +itemnum;
CookieValue = GetCookie(CookieName);
for(var i=0;i<=CookieValue.length;i++)
  {
  if (CookieValue.substring(i,i+1) == '['){itemstart = i+1;}
  else if (CookieValue.substring(i,i+1) == sp1)
    {
    ID = CookieValue.substring(itemstart, i);
    itemstart = i+1;
    }
  else if (CookieValue.substring(i,i+1) == sp2)
    {
    Sdesc = CookieValue.substring(itemstart, i);
    itemstart = i+1;
    }
  else if (CookieValue.substring(i,i+1) == sp3)
    {
    Price = CookieValue.substring(itemstart, i);
    itemstart = i+1;
    }
//CookieValue = "["+ID+sp1+Sdesc+sp2+Price+sp3+Qty+sp4+Weight+sp5+WeightUnit+sp6+Spship+"]";
  else if (CookieValue.substring(i,i+1) == sp4)
    {
    OldQty = CookieValue.substring(itemstart, itemend);
    itemstart = i+1;
    }
  else if (CookieValue.substring(i,i+1) == sp5)
    {
    Weight = CookieValue.substring(itemstart, i);
    itemstart = i+1;
    }
  else if (CookieValue.substring(i,i+1) == sp6)
    {
    WeightUnit = CookieValue.substring(itemstart, i);
    itemstart = i+1;
    }
  else if (CookieValue.substring(i,i+1) == ']')
    {
    Spship = CookieValue.substring(itemstart, i);
    itemstart = i+1;
    }
  }

NewCookieValue = "["+ID+sp1+Sdesc+sp2+Price+sp3+Qty+sp4+Weight+sp5+WeightUnit+sp6+Spship+"]";
if(Qty <= 0){DeleteCookie(CookieName);}
else{SetCookie(CookieName,NewCookieValue);}
self.location = pageloc;
}

function RemoveItem(itemnum)
{
CookieName = storeID +itemnum;
DeleteCookie(CookieName);
self.location = pageloc;
}

function EmptyCart()
  {
  if(confirm('Are you sure you want to delete the list of properties?'))
    {
    for(NumItems=0; NumItems<MaxCartItems; NumItems++)
      {
      CookieName = storeID + NumItems;
      DeleteCookie(CookieName);
      }
    self.location = pageloc;
    }
  }

function Dollarize(expr)
  {
  return "$" + format(expr,2);
  }


rsize = 10;		// rounding size
oz_factor = 1;		// ounces to ounces
lbs_factor = 0.0625;		// ounces to pounds
mg_factor = 28349.5;		// ounces to miligrams
g_factor = 28.3495;		// ounces to grams
kg_factor = .0283495;	// ounces to kilograms
var UnitSize;	// weight number being converted
var OldUnit;	// converting from
var NewUnit;	// converting to
var NewUnitSize;	// new weight in new unit

function ConvertUnit(UnitSize,OldUnit,NewUnit)
{
// possible units
// oz
// lbs
// mg
// g
// kg

// if not oz, convert to oz first

NewUnitSize = 0;
TmpUnitSize = 0;

if(OldUnit != 'oz')
  {
  if(OldUnit == 'lbs'){TmpUnitSize = UnitSize / lbs_factor;}
  else if(OldUnit == 'mg'){TmpUnitSize = UnitSize / mg_factor;}
  else if(OldUnit == 'g'){TmpUnitSize = UnitSize / g_factor;}
  else if(OldUnit == 'kg'){TmpUnitSize = UnitSize / kg_factor;}
  }
else
  {
  // if oz, keep as is
  TmpUnitSize = UnitSize;
  }

// since TmpUnitSize is now oz, convert to NewUnit;
if(NewUnit == 'oz'){NewUnitSize = TmpUnitSize;}
else if(NewUnit == 'lbs'){NewUnitSize = TmpUnitSize * lbs_factor;}
else if(NewUnit == 'mg'){NewUnitSize = TmpUnitSize * mg_factor;}
else if(NewUnit == 'g'){NewUnitSize = TmpUnitSize * g_factor;}
else if(NewUnit == 'kg'){NewUnitSize = TmpUnitSize * kg_factor;}
}

function ShowInvoice()
{
totprice = 0;
itemlist = 0;
TotQty = 0;
TotWeight = 0;
FinalTotWeight = 0;
icount = 0;

document.writeln('<table border=2 cellspacing=3 cellpadding=5>');
document.writeln('<tr><td align=center><b>Property Name</b></td><td align=center><b>Comments</b></td></tr>');

for(NumItems = 0; NumItems<MaxCartItems; NumItems++)
  {
  CookieName = storeID + NumItems;
  CookieValue = GetCookie(CookieName);

  if(CookieValue != null)
    {
    for(var i = 0;i <= CookieValue.length; i++)
      {
      if (CookieValue.substring(i,i+1) == '['){itemstart = i+1;}
      else if (CookieValue.substring(i,i+1) == sp1)
        {
        ID = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp2)
        {
        Sdesc = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        TotQty=1;
        }
      else if (CookieValue.substring(i,i+1) == sp3)
        {
        Price = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp4)
        {
        Qty = CookieValue.substring(itemstart, i);
        itemstart=i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp5)
        {
        Weight = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp6)
        {
        WeightUnit = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == ']')
        {
        Spship = CookieValue.substring(itemstart, i);
        itemstart = i+1;

        icount++;
        document.write('<input type=hidden name="ID' + icount + '" value="' + Sdesc + '">');

        //get autoresponder email address
        document.write('<input type=hidden name="responder' + icount + '" value="' + getcondoresponder(Sdesc) + '">');

        document.write('<input type=hidden name="condoname' + icount + '" value="' + Sdesc + '">');

        document.writeln('<tr><td>' + Sdesc + '</td><td><input type=text name="Comments' + icount + '" size=10></td></tr>');
        }
      }
    }
  }

document.write('<input type=hidden name="responderqty" value="' + icount + '">');
//if no items are put in shopping cart, go to the viewcart page
if (TotQty==0)
  {
  alert('There are no properties requested.\nPlease go to a Condos page and select the desired property first.');
  location.replace('viewcondolist.htm');
  }

document.writeln('</table>');
}

function isduplicate(oneitem)
{
var Sdesc='', ID='';
for(NumItems = 0; NumItems<MaxCartItems; NumItems++)
  {
  CookieName = storeID + NumItems;
  CookieValue = GetCookie(CookieName);

  if(CookieValue==null) break;
  else
    {
    for(var i = 0;i <= CookieValue.length; i++)
      {
      if (CookieValue.substring(i,i+1) == '['){itemstart = i+1;}
      else if (CookieValue.substring(i,i+1) == sp1)
        {
        ID = CookieValue.substring(itemstart, i);
        itemstart = i+1;
        }
      else if (CookieValue.substring(i,i+1) == sp2)
        {
        var cvalue = CookieValue.substring(itemstart, i)
        while (cvalue.indexOf("'") > -1) cvalue=cvalue.replace("'", "`");
        Sdesc = cvalue;
        break;
        }
      }
    }
  if (Sdesc==oneitem) return true;
  }
return false;
}

function clearCookie()
{
for(var ni=0; ni<MaxCartItems; ni++)
  {DeleteCookie(storeID + ni);}
}

function moreinfo(propertyname)
{
AddItem('',propertyname,'0','1','0','0','0');
}

var bcount=0;
function infobutton(condoname)
{
var buttonsize='lg';
if (location.href.indexOf('condolist.htm') > -1 || location.href.indexOf('condolist.asp') > -1) buttonsize='';
while (condoname.indexOf("'") > -1)
  {
  condoname=condoname.replace("'", '`');
  }
var cstring = '<a href="javascript:moreinfo(\'' + condoname + '\')" ';
cstring += 'onmouseover="window.status=\'Get more information on ' + condoname + '\'; ';
cstring += 'switchpic(\'cartbutton' + bcount + '\',';
cstring += '\'http://www.gulfcondos.com/images/requestinfo' + buttonsize + '-1.gif\'); ';
cstring += 'return true" ';
cstring += 'onmouseout="window.status=\'\'; ';
cstring += 'switchpic(\'cartbutton' + bcount + '\',';
cstring += '\'http://www.gulfcondos.com/images/requestinfo' + buttonsize + '-0.gif\'); ';
cstring += 'return true" ';
cstring += '>';
cstring += '<img src="http://www.gulfcondos.com/images/requestinfo' + buttonsize + '-0.gif" border=0 name="cartbutton' + bcount + '">';
cstring += '</a>';
document.write(cstring);
if (buttonsize!='')
  {
  document.write('<br><br>');
  showviewbutton();
  }
bcount++;
}

function showviewbutton()
{
var buttonname='b-viewcondolist-', mouseover='1', mouseout='0';
var bstring = '';
bstring += '<a href="http://www.gulfcondos.com/viewcondolist.htm" ';
bstring += 'onmouseover="switchpic(\'viewcondolist\',';
bstring += '\'http://www.gulfcondos.com/images/' + buttonname + mouseover + '.gif\'); ';
bstring += 'window.status=\'View Selected Condo List\'; ';
bstring += 'return true" ';
bstring += 'onmouseout="switchpic(\'viewcondolist\',';
bstring += '\'http://www.gulfcondos.com/images/' + buttonname + mouseout + '.gif\'); ';
bstring += 'window.status=\'\'; ';
bstring += 'return true" ';
bstring += '>';
bstring += '<img src="http://www.gulfcondos.com/images/' + buttonname + mouseout + '.gif" border=0 name="viewcondolist">';
bstring += '</a>';
document.write(bstring);
}

function getcondoindex(condoname)
{
//check 2 arrays and return name of array and index
for (var ci=0;ci<orange.length;ci++)
  {
  if (orange[ci].name.toLowerCase()==condoname.toLowerCase()) return ('orange' + ci);
  }

for (var ci=0;ci<gulf.length;ci++)
  {
  if (gulf[ci].name.toLowerCase()==condoname.toLowerCase()) return ('gulf' + ci);
  }

for (var ci=0;ci<info.length;ci++)
  {
  if (info[ci].name.toLowerCase()==condoname.toLowerCase()) return ('info' + ci);
  }

return -1;
}

function getcondoresponder(condoname)
{
var condoID = getcondoindex(condoname), condoindex;

if (condoID==-1)
  {
  //for test purposes

  alert('Condo not found:' + condoname);
  return '';
  }
/*
else if (condoID.indexOf('orange') > -1)
  {
  condoindex=condoID.substr(6) * 1;
  if (orange[condoindex].email!='') return orange[condoindex].email;
  }
else if (condoID.indexOf('gulf') > -1)
  {
  condoindex=condoID.substr(4) * 1;
  if (gulf[condoindex].email!='') return gulf[condoindex].email;
  }
*/

return 'condos@gulfcondos.com';
}

var imagefs = new Image();  imagefs.src = "b-faqsheet-1.gif";
function switchpic(img_name,img_src)  {document[img_name].src=img_src}

