function showinfo()
 {
  var elem = document.getElementById('info');
  elem.style.visibility='visible';
  repos(elem,250);
 }
function noinfo()
 {
  var elem = document.getElementById('info');
  elem.style.visibility='hidden';
 }
function repos(elem,wid)
 {
  elem.style.bottom = "5%";
  elem.style.right = "4px";
  elem.style.width = wid +"px";
 }
function fill(elem)
 {
  var docwidth
  //opera Netscape 6 Netscape 4x Mozilla 
  if (window.innerWidth)
   {
//    docwidth = window.innerWidth; 
   } 
  //IE Mozilla 
  else if (document.body.clientWidth)
   { 
    docwidth = document.body.clientWidth; 
    docheight = document.body.clientHeight; 
   }

  elem.style.width = docwidth - 256;
  //elem.style.height = docheight;
 }
function filldoc()
 {
  var ma=document.getElementById('main');
  fill(ma);
 }

function isInt(testStr)
 {
  var Digits = "0123456789";
  var isInt = true;
  for (i = 0; i < testStr.length && isInt == true; i++)
   {
    testChar = testStr.charAt(i);
    if (Digits.indexOf(testChar) == -1)
     {
      isInt = false;
     }
   }
  return isInt;
 }

function doRemove(item)
 {
  document.forms['removeForm'].elements['rmItem'].value = item;
  document.forms['removeForm'].submit();
  return false;
 }

function totUp(suffix)
 {
  eval('qty = document.getElementById("Qty_' + suffix + '");');
  eval('prea = document.getElementById("PrEa_' + suffix + '");');
  eval('prtot = document.getElementById("PrTot_' + suffix + '");');
  var total = document.getElementById('Total');
  var qval = parseInt(qty.value);
  if (qval < 0)
   {
    qval = 1;
   }
  qty.value = qval;
  var prtotold = parseFloat(prtot.value);
  prtot.value = dispDollars(qty.value * prea.value);
  total.value = dispDollars(parseFloat(total.value) - prtotold + parseFloat(prtot.value));
  datachange = true;
  var upd = document.getElementById('update');
  upd.style.visibility='visible';
 }

function chartUp(suffix,set)
 {
  eval('opt1 = document.getElementById("Opt1_' + suffix + '");');
  eval('opt2 = document.getElementById("Opt2_' + suffix + '");');
  eval('opt3 = document.getElementById("Opt3_' + suffix + '");');
  eval('opt4 = document.getElementById("Opt4_' + suffix + '");');
  eval('prea = document.getElementById("PrEa_' + suffix + '");');
  if (opt4)
   {
    eval('chartobj = document.getElementById("chart_' + set + '_' + opt1.value + 
      '_' + opt2.value + '_' + opt3.value + '_' + opt4.value + '");');
   }
  else if (opt3)
   {
    eval('chartobj = document.getElementById("chart_' + set + '_' + opt1.value + 
      '_' + opt2.value + '_' + opt3.value +'");');
   }
  else if (opt2)
   {
    eval('chartobj = document.getElementById("chart_' + set + '_' + opt1.value + 
      '_' + opt2.value + '");');
   }
  else if (opt1)
   {
    eval('chartobj = document.getElementById("chart_' + set + '_' + opt1.value +
      '");');
   }
  prea.value = chartobj.value;
  totUp(suffix);
 }

function dispDollars(val)
 {
  var ival = parseInt(val);
  var fval = parseFloat(val);
  if (ival != fval)
   {
    var cval = (fval - ival) * 100;
    var sval = new String(ival + '.' + substr(String('00'+ cval),-2));
   }
  else 
   {
    var sval = new String(ival + '.00');
   }
  return sval;
 }

function doValidateUser()
 {
  fail = 0;
  var fields = new Array("newuname","fname","lname","newpass","passconf","city","state","zip","addr1");
  for (fieldnum in fields)
   {
    thisField = fields[fieldnum];
    if (document.forms['custlogin'].elements[thisField].value == '')
     {
      document.getElementById(thisField).style.fontWeight = 'bold';
      document.getElementById(thisField).style.color = '#ff4444';
      fail = 1;
     }
    else
     {
      document.getElementById(thisField).style.fontWeight = 'normal';
      document.getElementById(thisField).style.color = '#000000';
     }
   }
  if ( document.forms['custlogin'].elements['phonearea'].value == '' ||
       document.forms['custlogin'].elements['phonearea'].value.length != 3 ||
       !isInt(document.forms['custlogin'].elements['phonearea'].value) ||
       document.forms['custlogin'].elements['phoneex'].value == '' ||
       document.forms['custlogin'].elements['phoneex'].value.length != 3 ||
       !isInt(document.forms['custlogin'].elements['phoneex'].value) ||
       document.forms['custlogin'].elements['phonestn'].value == '' ||
       document.forms['custlogin'].elements['phonestn'].value.length != 4 ||
       !isInt(document.forms['custlogin'].elements['phonestn'].value)
     ) // Test phone numbers for proper size and content
   {
    document.getElementById('phone').style.fontWeight = 'bold';
    document.getElementById('phone').style.color = '#ff4444';
    fail = 1;
   }
  else
   {
    document.getElementById('phone').style.fontWeight = 'normal';
    document.getElementById('phone').style.color = '#000000';
   }

  if (
       document.forms['custlogin'].elements['zip'].value.length != 5 ||
       !isInt(document.forms['custlogin'].elements['zip'].value) ||
       (document.forms['custlogin'].elements['zip2'].value != '' &&
        (document.forms['custlogin'].elements['zip2'].value.length != 4 ||
         !isInt(document.forms['custlogin'].elements['zip2'].value)
       ))
     )  // Test Zip code for proper size and content
   {
    document.getElementById('zip').style.fontWeight = 'bold';
    document.getElementById('zip').style.color = '#ff4444';
    fail = 1;
   }
  else
   {
    document.getElementById('zip').style.fontWeight = 'normal';
    document.getElementById('zip').style.color = '#000000';
   }
  if ( document.forms['custlogin'].elements['newpass'].value !=
       document.forms['custlogin'].elements['passconf'].value) // Test password equality
   {
    document.getElementById('newpass').style.fontWeight = 'bold';
    document.getElementById('newpass').style.color = '#ff4444';
    document.getElementById('passconf').style.fontWeight = 'bold';
    document.getElementById('passconf').style.color = '#ff4444';
    fail = 1;
   }
  else
   {
    document.getElementById('newpass').style.fontWeight = 'normal';
    document.getElementById('newpass').style.color = '#000000';
    document.getElementById('passconf').style.fontWeight = 'normal';
    document.getElementById('passconf').style.color = '#000000';
   }
  if (document.forms['custlogin'].elements['newuname'].indexOf('@') == -1) // Test for "valid" email
   {
    document.getElementById('newuname').style.fontWeight = 'bold';
    document.getElementById('newuname').style.color = '#ff4444';
    fail = 1;
   }
  else
   {
    document.getElementById('newuname').style.fontWeight = 'normal';
    document.getElementById('newuname').style.color = '#000000';
   }
  if (fail == 1)
   {
    alert("Registration data invalid.  Missing or incorrect fields are in bold red.");
    return false;
   }
  return true;
 }

// Generic XMLHttpRequest object generator
function newHttpRequest()
{
 var xmlHttp = null;
 try
  {
   // Firefox, Opera 8.0+, Safari
   xmlHttp = new XMLHttpRequest();
  }
 catch (e)
  {
   // Internet Explorer
   try
    {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
   catch (e)
    {
     try
      {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
     catch (e)
      {
       // xmlHttp is still null - nothing to see here.
      }
    }
  }
 return xmlHttp;
}

// Constructor for generic AJAX client
function ajaxClient() {};

// Add methods and properties as array
ajaxClient.prototype = {
    url: null,

    // Instance of XMLHttpRequest
    xmlhttp: null,

    // Generic Pass-back parameters
    passback: function() {},

    // Used to make sure multiple calls are not placed
    // with the same client object while another in progress
    callinprogress: false,

    // The user defined handler - see MyHandler below
    userhandler: null,

    init: function(url) {
        this.url = url;
        this.xmlhttp = newHttpRequest();
    },

    // handler argument is a user defined object to be called
    asyncGET: function (handler) {

        // Prevent multiple calls
        if (this.xmlhttp == null) {
            throw "Your browser does not support this function.";
        };

        // Prevent multiple calls
        if (this.callinprogress) {
            throw "Duplicate request!";
        };

        this.userhandler = handler;

        // Open an async request - third argument makes it async
        this.xmlhttp.open('GET',this.url,true);

        // Have to assign "this" to a variable - not sure why can't use directly
        var self = this;

        // Assign a closure to the onreadystatechange callback
        this.xmlhttp.onreadystatechange = function() {
            self.stateChangeCallback(self);
        }

        // Send the request
        this.xmlhttp.send(null);
    },

    stateChangeCallback: function(client) {
        switch (client.xmlhttp.readyState) {

            // Request not yet made
            case 1:
                try {
                    client.userhandler.onInit();
                } catch (e) { /* Handler method not defined */ }
            break;

            // Contact established with server but nothing downloaded yet
            case 2:
                try {
                    // Check for HTTP status 200
                    if ( client.xmlhttp.status != 200 ) {
                        client.userhandler.onError(
                            client.xmlhttp.status,
                            client.xmlhttp.statusText
                            );

                        // Abort the request
                        client.xmlhttp.abort();

                        // Call no longer in progress
                        client.callinprogress = false;
                    }
                } catch (e) {
                    /* Handler method not defined */
                }
            break;

            // Called multiple while downloading in progress
            case 3:
                // Notify user handler of download progress
                try {
                    // Get the total content length
                    // -useful to work out how much has been downloaded
                    try {
                        var contentLength = 
                            client.xmlhttp.getResponseHeader("Content-Length");
                    } catch (e) {
                        var contentLength = NaN;
                    } 

                    // Call the progress handler with what we've got
                    client.userhandler.onProgress(
                        client.xmlhttp.responseText,
                        contentLength
                    );

                } catch (e) { /* Handler method not defined */ }
            break;

            // Download complete
            case 4:
                try {
                    client.userhandler.onLoad(client.xmlhttp.responseText,client.passback);
                } catch (e) {
                    /* Handler method not defined */
                } finally {
                    // Call no longer in progress
                    client.xmlhttp.callinprogress = false;
                }
            break;
        }
    }
}

// A user defined handler to response to the XMLHTTPRequest
var rateItemHandlers = {
    onInit: function() {
    return;
        // Nothing for now
    },
    onError: function(status,statusText) {
        // Nothing for now
	return;
    },
    onProgress: function(responseText,length) {
        // Nothing for now
	return;
    },
    onLoad: function(result,cparams) {
        // TODO - Update Rating display (permanent rating display)
	var ctarget = document.getElementById("rating_"+cparams.gallery+"_"+cparams.itorder);
	var newHTML = '<img src="/graphics/ilikeit20.png" alt="' + result + '" title="' + result + '" border="0">';
	ctarget.innerHTML=newHTML;
    }
};

// For generic Ratings
function rateItem(galid,itorder,rating) {
    // Modify this to some page
    //alert("Rating Gallery: " + galid + ", Item: " + itorder + ", Rating: " + rating);
    var url = "/rating-results.php?gallery="+galid+"&item="+itorder+"&rating="+rating;
    var client = new ajaxClient();
    client.init(url);
    client.passback.gallery=galid;
    client.passback.itorder=itorder;
    try {
        client.asyncGET(rateItemHandlers);
    } catch (e) {
        alert(e);
    }
}

// For Recommends
function recommendItem(galid,itorder) {
    // Modify this to some page
    //alert("Rating Gallery: " + galid + ", Item: " + itorder + ", Rating: " + rating);
    var url = "/recommend-results.php?gallery="+galid+"&item="+itorder;
    var client = new ajaxClient();
    client.init(url);
    client.passback.gallery=galid;
    client.passback.itorder=itorder;
    try {
        client.asyncGET(rateItemHandlers);
    } catch (e) {
        alert(e);
    }
}

