
// CPRealtime 2.0 Modification 1

// Misc global functions

var loginRequired = false;

// -------------------------------------------------------------------
function popup(img,h,w,title){

    var sWidth = screen.width / 2 - w;

     var win = window.open("","graphics","top=100,left=" + sWidth + ",scrollbars=0,resizable=1");
     win.resizeTo(w,h + 100);
     win.document.open("text/html", "replace");
     win.document.write("<html><head><title>"+title+"</title></head><body topmargin=0 leftmargin=0 marginwidth=0 marginheight=0>");
     win.document.write("<img src="+img+" width="+w+" height="+h+"><br>");
     win.document.write("<form><div align=center><input type=button name=Close value=\"Close Window\" onclick=window.close()></div></form>");
     win.document.write("</body></html>");
     win.status = "Zoom Image of " + title;
     win.focus();
}


// -------------------------------------------------------------------
function reorderWindow(cartid){
    var sWidth = (screen.width / 2 - 150);
    window.open('../reorder.php?session_id=' + cartid + '&add=1','reorder','width=600,height=550,resizable,scrollbars,left=250');
}

// -------------------------------------------------------------------
function getCookie(name){

     // Simply retreives a cookie

var cname = name + "=";
var dc = document.cookie;
     if (dc.length > 0) {
          begin = dc.indexOf(cname);
          if (begin != -1) {
               begin += cname.length;
               end = dc.indexOf(";", begin);
               if (end == -1) end = dc.length;
               return unescape(dc.substring(begin, end));
          }
     }
return null;
}

// -------------------------------------------------------------------
function testCookie(){

var loginCookie = getCookie('login');

     if(loginCookie == null){
          alert("It appears that you do not have cookies turned on in your browser? \n\n" +
                "This web site uses temporary cookies for tracking your purchases " +
                "and you must have cookies enabled to be able to shop on our web site.\n\n" +
                "These temporary cookies are not stored on your computer and will be deleted " +
                "from your browser cache when you close your browser.");
     }
     else{
          if(loginRequired){
               if((loginCookie == null)||(loginCookie == "false")){
                    //document.location = "./cgi-bin/store.php";
               }
          }
     }
}

// -------------------------------------------------------------------
function checkLogin(){

var Login = getCookie('login');

     if((loginRequired)&&(document.title == "Please Login...")){
          var STDIN = unescape(location.search.replace(/\+/g,' '));
          STDIN = STDIN.substring(1).split('&');
          for(i=0; i < STDIN.length; i++){
               STDIN[i] = STDIN[i].split('=');
          }
          var login = STDIN[0][1];
          if(login == "no"){
               alert("Invalid Login. Please Try Again...");
          }
     }

     if((loginRequired)&&(document.title != "Please Login...")){
          if(Login == null){
               document.location = "cgi-bin/store.php";
          }
          if((Login != null)&&(Login != "true")){
               document.location = "cgi-bin/store.php";
          }
     }
}

// -------------------------------------------------------------------
function welcomeUser(){

var username = getCookie('username');

          if(username != null){
               document.write("<H4><br>Welcome \n" + unescape(username) + "!</H4>");
          }
          else{
               sessionInfo = getCookie('SESSION_VARS');
               if(sessionInfo != null){
                    pairs = new Array();
                    pairs = sessionInfo.split("&");
                    for(i=0;i<pairs.length;i++){
                         var Values = pairs[i].split("=");
                         if(Values[0] == "USER_NAME"){
                              username = Values[1];
                         break;
                         }
                    }
                    document.write("<H4><br>Welcome " + unescape(username) + "!</H4>");
               }
          }
          if(username != null){
               document.title = "Welcome " + unescape(username) + "!";
          }
}

// -------------------------------------------------------------------
function formReset(){
     for(i = 0; i < document.forms[0].elements.length; i++){
          var inType = document.forms[0].elements[i].type;
          if(inType == "text"){
               document.forms[0].elements[i].value = "";
          }
     }
}

// -------------------------------------------------------------------
function qtyCheck(form){

var qtySelected = parseInt(0);

     for(i = 0; i < form.elements.length; i++){
          var fieldName = form.elements[i].name;
          if(fieldName.substring(0,8) == "QUANTITY"){
               var fieldValue = parseInt(form.elements[i].value);
               if(fieldValue > 0){
                    qtySelected += parseInt(fieldValue);

                        // Check availability

                    var id = fieldName.substring(9,fieldName.length);
                    var availFld = "available|" + id;
                    if(eval(form.elements[availFld])){
                        var qtyAvailable = parseInt(form.elements[availFld].value);
                        if(fieldValue > qtyAvailable){
                            var nameFld = "name|" + id;
                            var productName = form.elements[nameFld].value

                            var pattern = /\s{2,}/g;
                            var pName = productName.replace(pattern,"");

                            if(qtyAvailable <= 0){
                                alert("Sorry, the '" + pName + "' is not available at this time.");
                            }
                            else{
                                alert("There are only " + qtyAvailable + " of the '" + pName + "' available.\n\n" +
                                      "Please reduce the quantity so it is equal to or fewer than " + qtyAvailable + " item(s).");
                            }
                            form.elements[i].value = "";
                            form.elements[i].focus();
                            return false;
                        }
                    }
               }
          }
     }

     if(qtySelected == 0){
          alert("You have not entered a quantity of an item to add to the cart.");
          return false;
     }
return true;
}
// -------------------------------------------------------------------
function GoTo(thisURL) {
     alert(thisURL);
     location = thisURL;
}
// -------------------------------------------------------------------
function OpenWindow(thisURL) {
     var x = 650;
     var y = 350;
     var gWin = window.open(thisURL,'GraphicWindow',
     'width='+x+',height='+y+',resizable=1,scrollbars=1');
}

