
var http = null;

////////////////////////////////

function GetHttpObject()
{ 
 var o = null;

 if (window.XMLHttpRequest)
  {
   o = new XMLHttpRequest();
  }
 else if (window.ActiveXObject)
  {
   o = new ActiveXObject("Msxml2.XMLHTTP");

   if (o == null)
    {
     o = new ActiveXObject ("Microsoft.XMLHTTP");
    }
  }
 return o;
}  

////////////////////////////////

var vip_email;

function check_vip_login (email)
{
 var url="/cgi-bin/check_vip_login";

 if (email.length < 1)
  { 
   return false;
  }

 vip_email = email;


 if (email  == 'dolisting')
  {
   var thenewdate = new Date ();
   thenewdate.setTime (thenewdate.getTime() + (365 * 24 * 3600 * 1000));
   SetCookie ('email_address', vip_email, thenewdate, "/");

   return true;
  }


 http = GetHttpObject();

 if (http == null)
  {
   alert ("Browser does not support HTTP Request");
   return false;
  } 

 url = url + "?vip=" + email;
 http.onreadystatechange = receive_vip;
 http.open ("GET", url, true);
 http.send (null);
 return true;
} 

//////////////////////////////////

function receive_vip() 
{
 var thenewdate = new Date ();

 if (http == null) { return; }

 if ((http.readyState == 4) && (http.status == 200))
  { 
   thenewdate.setTime (thenewdate.getTime() + (365 * 24 * 3600 * 1000));

   if (http.responseText == "OK")   //// VIP is OK
    {
     SetCookie ('email_address', vip_email, thenewdate, "/");
    }
   else
    {
     SetCookie ('email_address', '', thenewdate, "/");
     alert ('Wrong VIP email address. You are NOT logged in');
    }
   window.location.reload();
  } 
} 

//////////////////////////////////

function submit_wlogin()
{
 if ((whole_login.email.value == '') ||
     (whole_login.password.value == '') ||
     (isEmail (whole_login.email.value) == false))
  {
   alert ('One of the required fields is empty or invalid.');
   return false;
  }

 ws_email = whole_login.email.value;

 http = GetHttpObject();

 if (http == null)
  {
   alert ("Browser does not support HTTP Request");
   return false;
  } 

 var url = "/cgi-bin/ws_login2";
 url = url + "?email=" + whole_login.email.value;
 url = url + "&password=" + whole_login.password.value;
 http.onreadystatechange = receive_wh_login;
 http.open ("GET", url, true);
 http.send (null);

 return true;
}


//////////////////////////////////

function receive_wh_login() 
{
 var thenewdate = new Date ();

 if (http == null) { return; }

 if ((http.readyState == 4) && (http.status == 200))
  { 
   thenewdate.setTime (thenewdate.getTime() + (365 * 24 * 3600 * 1000));

   if (http.responseText == "OK")   //// WH LOGIN is OK
    {
     SetCookie ('ws_email', ws_email, thenewdate, "/");
     var txt = "<FONT COLOR=GREEN><B>You are logged in (" + ws_email + ")</B></FONT>"
     document.getElementById("wholesale_login").innerHTML = txt;
     alert ('You are successfully logged in as a Wholesale Buyer.');
    }
   else
    {
     SetCookie ('ws_email', '', thenewdate, "/");
     document.getElementById("wholesale_login").innerHTML = "<FONT COLOR=GREEN>Wrong email address or password. You are NOT logged in</FONT>";
     alert ('Wrong email address or password. You are NOT logged in');
    }
  } 
} 

//////////////////////////////////

function receive_tracking() 
{
} 

//////////////////////////////////


function tracking_cookie()
{
 var c = GetCookie ('tracking');

 if (c == '') { return false; }

 http = GetHttpObject();

 if (http == null)
  {
   alert ("Browser does not support HTTP Request");
   return false;
  } 

 var url = "/cgi-bin/ctracking";
 http.onreadystatechange = receive_tracking;
 http.open ("GET", url, true);
 http.send (null);

 return true;
}


//////////////////////////////////

function email_friend_close() 
{
 document.getElementById("email_friend").innerHTML = "";
}

//////////////////////////////////

function receive_email_friend() 
{
 if (http == null) { return; }

 if ((http.readyState == 4) && (http.status == 200))
  { 
   if (http.responseText == "OK")
    {
     document.getElementById("email_friend").innerHTML = '<P CLASS="item_desc">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Email Sent.</P>';
    }
   else
    {
     document.getElementById("email_friend").innerHTML = '<P CLASS="item_desc">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sending Error.</P>';
    }
  } 
}

//////////////////////////////////

function email_friend_fun() 
{
 var friend_email = email_friend_frm.recipient.value;
 var sender_email = email_friend_frm.sender_email.value;
 var sender_name  = email_friend_frm.sender_name.value;

 if (isEmail (friend_email) == false)
  {
   document.getElementById("email_friend").innerHTML = '<P CLASS="item_desc">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Friend\'s email is invalid or blank.</P>';
   return false;
  }

 if (isEmail (sender_email) == false)
  {
   document.getElementById("email_friend").innerHTML = '<P CLASS="item_desc">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sender\'s email is invalid or blank.</P>';
   return false;
  }

 http = GetHttpObject();

 if (http == null)
  {
   document.getElementById("email_friend").innerHTML = '<P CLASS="item_desc">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Browser doesn\'t support HTTP request.</P>';
   return false;
  } 

 var url = "/cgi-bin/email_item";
 url = url + "?sender_email=" + sender_email;
 url = url + "&recipient=" + friend_email;
 url = url + "&category=" + email_friend_frm.category.value;
 url = url + "&id=" + email_friend_frm.id.value;
 url = url + "&sender_name=" + sender_name;

 http.onreadystatechange = receive_email_friend;
 http.open ("GET", url, true);
 http.send (null);

 document.getElementById("email_friend").innerHTML = '<P CLASS="item_desc">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sending Email to ' + friend_email + ' ...</P>';

 return false;
}

//////////////////////////////////

function email_friend (category, item_id) 
{
 var sender_email = GetCookie ('email_address');

 if (sender_email == null) { sender_email = ''; }

 var txt = '<CENTER>' +

'<TABLE BORDER=0 BGCOLOR=GRAY CELLSPACING=0 CELLPADDING=1><TR><TD><TABLE BGCOLOR=WHITE CELLPADDING=0 CELLSPACING=0 BORDER=0 WIDTH="460"><TR><TD VALIGN=MIDDLE ALIGN=CENTER>' +

'<P CLASS="item_desc"><BR>' +
'Email a friend with this item: ' + item_id + ' - just complete the form' +
'<BR>and click the "Send Email" button below:' +
'<FORM NAME="email_friend_frm" METHOD="post" ACTION="" onSubmit="return email_friend_fun()">' +
'<TABLE CLASS="item_desc" BORDER=0 BGCOLOR=WHITE>' +
'<TR>' +
'<TD VALIGN=MIDDLE>Friend\'s Email Address:</TD>' +
'<TD VALIGN=MIDDLE><input type="text" name="recipient" size=30 maxlength=40></TD>' +
'</TR>' +
'<TR>' +
'<TD VALIGN=MIDDLE>Your Name:</TD>' +
'<TD VALIGN=MIDDLE><input type="text" name="sender_name" size=30 maxlength=40></TD>' +
'</TR>' +
'<TR>' +
'<TD VALIGN=MIDDLE>Your Email Address:</TD>' +
'<TD VALIGN=MIDDLE><input type="text" name="sender_email" value="' + sender_email + '"size=30 maxlength=40></TD>' +
'</TR>' +
'<TR>' +
'<TD ALIGN=LEFT>' +
'<BR>' +
'<input type="button" value="Cancel" onclick="javascript:email_friend_close()">' +
'</TD>' +
'<TD ALIGN=RIGHT>' +
'<BR>' +
'<input name="submit" type="submit" value="Send Email">' +
'</TD>' +
'</TR>' +
'</TABLE>' +
'<input type="hidden" name="category" value="' + category + '">' +
'<input type="hidden" name="id" value="' + item_id + '">' +
'</FORM>' +
'</P>' +

'</TD></TR></TABLE></TD></TR></TABLE>' +

'</CENTER>';

 document.getElementById("email_friend").innerHTML = txt;
} 

//////////////////////////////////

function receive_searchitem() 
{
 if (http == null) { return; }

 if ((http.readyState == 4) && (http.status == 200))
  {
   if (http.responseText == "NOT_FOUND")
    {
     alert ("Sorry, search item number only");
    }
   else
    {
     document.location.href = http.responseText;
    }
  } 
}

////////////////////////////////////

function searchbyitemid()
{
 var itemid = search1.itemid.value;

 if ((itemid == null) || (itemid == ''))
  {
   event.returnValue = false;
   return false;
  }


 http = GetHttpObject();

 if (http == null)
  {
   alert ("Browser does not support HTTP Request");
   return false;
  } 

 url = "/cgi-bin/search_item2" + "?itemid=" + itemid;

 http.onreadystatechange = receive_searchitem;
 http.open ("GET", url, true);
 http.send (null);

 event.returnValue = false;
 return false;
}
