// item_detail_size.js
/* msc 10/25/10 - fixed js errors */
/* ram 03/01/10 - added function for eye chart */

var colorList = new Array();
var sizeList = new Array();
var selList = new Array();
var groupCount = new Array();
var groupCount2 = new Array();
var orderLine = "";
var newWindow;
var sizeChartWindow;
var largePictureWindow;
var newWinShipTo;
var msg;

function fixArrays(productlist,hasColor,hasSize){
var form = document.getElementById("item_detail");
var iList = new String("" + productlist);
var startPos = 0;
var endPos = iList.indexOf(",");
var isGood = 0;
var keepGoing = 1;
var n = 0;
var curRec = new String();
// build size array
var howMany = (document.getElementsByName('howManyItems')) 
            ? parseInt(document.getElementsByName('howManyItems')[0].value)
            : 0;

for (var j = 0; j < howMany; j++){
 var sList = new String(document.getElementsByName("size_list"+(j+1))[0].value);
 var startPos = 0;
 var endPos = sList.indexOf(",");
 var isGood = 0;
 var keepGoing = 1;
 var k=0;
 sizeList[j] = new Array();
 while (keepGoing == 1){
  if (endPos == sList.length-1) keepGoing =0;
  if (endPos == -1){
   endPos = sList.length;
   keepGoing =0;
  }
  //put each into an array
  curRec = sList.substring(startPos, endPos);
  sizeList[j][k] = curRec;
  k++;
  if (keepGoing == 1){
   startPos = endPos +1;
   endPos = sList.indexOf(",", startPos);
  }
 }
 groupCount2[j] = k;
}

// build size select with size array
if (hasSize == "yes") {setSizeSelect(hasSizeN);
} else
if (hasColor == "yes") {setColorSelect(hasColorN);}
// build color array
for (var n = 0; n < howMany; n++){
 var iList = (document.getElementsByName("color_list"+(n+1)).length > 0 ) ? new String(document.getElementsByName("color_list"+(n+1))[0].value) : new String("");
 var startPos = 0;
 var endPos = iList.indexOf(",");
 var isGood = 0;
 var keepGoing = 1;
 var m=0;
 colorList[n] = new Array();
 while (keepGoing == 1){
  if (endPos == iList.length-1) keepGoing =0;
  if (endPos == -1){
   endPos = iList.length;
   keepGoing =0;
  }
  //put each into an array
  curRec = iList.substring(startPos, endPos);
  colorList[n][m] = curRec;
  m++;
  if (keepGoing == 1){
   startPos = endPos +1;
   endPos = iList.indexOf(",", startPos);
  }
 }
 groupCount[n] = m;
}
}

function sizeChanged(hasColor,hasColorN){
if (hasColor == "yes"){setColorSelect(hasColorN);}
}

function setSizeSelect(hasSizeN){
var form = document.item_detail;
var numSelected = 0;
var selectedItem = 0; //form.sel_item.selectedIndex;
form.sel_size.length=0;  //clear the options array
form.sel_size.options[0] = new Option(hasSizeN + " - Choose First", "");
for (var n = 0; n < groupCount2[selectedItem]; n++){
 var option1 =
 new Option(""+sizeList[selectedItem][n], ""+sizeList[selectedItem][n]);
 form.sel_size.options[n + 1]= option1;
 // mark as selected condition from gbs app never evaluates true here
 var deptString;
 deptString = 'All departments';
 if (deptString.indexOf(""+sizeList[selectedItem][n]) > -1 ){
  form.sel_size.options[n].selected=true;
  numSelected++;
 }
}
if (numSelected == 0) form.sel_size.options[0].selected=true;
}

function setColorSelect(hasColorN){
var form = document.item_detail;
var cForm = document.sizecolor;
var xForm = document.item_detail.color_list1;
var rForm = document.item_detail.color_codes;
var numSelected = 0;
var selectedItem = 0; //form.sel_item.selectedIndex;
if (typeof form.sel_size.selectedIndex == "number") {
 selectedItem = form.sel_size.selectedIndex - 1;
}
if(!Array.indexOf){
   Array.prototype.indexOf = function(obj){
      for(var i=0; i<this.length; i++){
         if(this[i]==obj){
            return i;
         }
      }
   return -1;
   }
}
form.sel_color.options.length = 0;
if (form.sel_size.type == 'hidden'){
form.sel_color.options[0] = new Option(hasColorN + " - Choose First", "");
}else{
form.sel_color.options[0] = new Option(hasColorN + " - Choose Second", "");
}
if (selectedItem >= 0) {
 var spArray = cForm.elements[(selectedItem)].value.split(",");
 var sprArray = rForm.value.split(",");
 var spxArray = xForm.value.split(",");
 for (var q=0;q<spArray.length;q++){
  var num1 = spxArray.indexOf(spArray[q]);
  var colnum = sprArray[num1];
  var option1 = new Option(colnum + " - " + spArray[q], spArray[q]);
  form.sel_color.options[q + 1] = option1;
 }
}
if (numSelected == 0) form.sel_color.options[0].selected=true;
}

function reqFields() {
// validate email address
form = document.start_anywhere;
if (validEmailAddr(form.email)==false) return false;
return true;
}

function isPosInt(iVal) {
iStr = iVal.toString();
if(iStr.length==0){return false;}
for(var i=0;i<iStr.length;i++) {
 var oneCh = iStr.charAt(i);
 if(oneCh<"0"||oneCh>"9") {return false;}
}
return true;
}

function personalized( yesorno ) {
persEntered = yesorno;
}

function ShowSizeChart(site) {
if (site=="timeforme") {
 sizeChartWindow = window.open("chart.html", "sizeChartWindow",
  "width=675,height=677,left=0,top=10,resizable,scrollbars");
} else {
 sizeChartWindow = window.open("chart.html", "sizeChartWindow",
  "width=620,height=500,left=0,top=10,resizable,scrollbars");
} 
sizeChartWindow.focus();
}

function ShowSizeChart2(site) {
 sizeChartWindow = window.open("chart2.html", "sizeChartWindow",
  "width=500,height=500,left=0,top=10,resizable,scrollbars");
 sizeChartWindow.focus();
}

function ShowEyeChart(site) {
 sizeChartWindow = window.open("eye-chart.html", "sizeChartWindow",
  "width=700,height=700,left=0,top=10,resizable,scrollbars");
 sizeChartWindow.focus();
}

function UnloadWindows() {
if (newWindow) {newWindow.close();}
if (largePictureWindow) {largePictureWindow.close();}
if (sizeChartWindow) {sizeChartWindow.close();}
if (newWinShipTo) {newWinShipTo.close();}
if (answ) {answ.close();}
}

function contHelp() {
var vm = "If you choose a continuity option, we will automatically reorder " +
 "this product at the time intervals you choose. Just select how frequently " +
 "you would like to receive this product on a regular basis. From this " +
 "point on, AmeriMark will automatically order and ship the product to " +
 "your doorstep at the time interval selected.  When the order ships, we " +
 "will automatically bill your credit card.";
if (!answ || answ.closed) {
 vm = '<html><head><title>Continuity Help</title></head>' +
  '<body style="padding:5;">' + vm +
  '<br><br><div align="center">'+
  '<input type="button" value="Close" onClick="self.close();"></div>' +
  '</body></html>';
 answ = window.open("","","height=260,width=300,top=160,left=160");
 answ.document.write(vm);
 answ.document.close();
}
if (answ.focus) {answ.focus()};
}

        function defbodyLoad() {

                fixArrays('',hasColors,hasSizes);

                if (hasSizes == 'yes' && document.item_detail.selectedSize.value != "") {
                        document.item_detail.sel_size.selectedIndex =        document.item_detail.selectedSize.value;
                        sizeChanged(hasColors);
                }
                if (hasColors == 'yes' && document.item_detail.selectedColor.value != "") {
                        document.item_detail.sel_color.selectedIndex = document.item_detail.selectedColor.value;
                }
        }

        function presubmit(sizetitle,colortitle){

                jnk=CheckItem(sizetitle,colortitle);
                if ( jnk ) {
                        document.item_detail.submit();
                }
                return false;

        }

        function swatchPop(url){
                window.open(url,'swatchWin','toolbar=no,location=no,status=no,menubar=no,directories=no,copyhistory=no,scrollbars=no,width=355,height=345,top=300,left=400,screenY=300,screenX=400,resizable=no');
                return false;
        }

        function updatePrice(sizetitle,colortitle) {
                var selcol = (document.item_detail.sel_color.type=="select-one") ? document.item_detail.sel_color.selectedIndex : 0;
                var selsize = (document.item_detail.sel_size.type=="select-one") ? document.item_detail.sel_size.selectedIndex : 0;;

                if(selcol>0&&selsize>0){
                        if (CheckItem(sizetitle,colortitle)) {
                                 if (hasColors == 'yes') {
                                  document.item_detail.selectedColor.value = selcol;
                                 }
                                 if (hasSizes == 'yes') {
                                  document.item_detail.selectedSize.value = selsize;
                                 }
                                // document.getElementById('buynow').style.visibility='hidden';
                                document.getElementById('buynow').innerHTML = 'Calculating price ...';
                                document.item_detail.action = "item_detail.html";
                                document.item_detail.method = "POST";
                                document.item_detail.submitv.value="check";
                                document.item_detail.submit();
                        }
                }
        }

        function checkAvail(sizetitle,colortitle) {
                if (CheckItem(sizetitle,colortitle)) {
                        if (hasColors == 'yes') {
                                document.item_detail.selectedColor.value = document.item_detail.sel_color.selectedIndex;
                        }
                        if (hasSizes == 'yes') {
                                document.item_detail.selectedSize.value = document.item_detail.sel_size.selectedIndex;
                        }
                        document.item_detail.action = "item_detail.html";
                        document.item_detail.method = "POST";
                        document.item_detail.submitv.value="check";
                        return CheckItem(sizetitle,colortitle);
                        document.item_detail.submit();
                } else {
                        return false;
                }
				return true;
        }

        function EnterPers(thePage) {
                if(thePage!=''){
                        window.open(thePage ,'newWin','toolbar=no,location=no,status=yes,menubar=no,directories=no,copyhistory=no,scrollbars=yes,width=465,height=345,left=450,screenX=450,top=30,screenY=30,resizable=yes');
                }
                return false;
        }

function SendToFriend(thePage) {
if(thePage!=''){
 window.open(thePage ,'newWin',
 'toolbar=no,location=no,status=yes,menubar=no,directories=no,copyhistory=no,scrollbars=yes,width=565,height=535,left=400,screenX=400,top=30,screenY=30,resizable=yes');
 }
 // return false;
}

function ScentInfo(thePage) {
if(thePage!=''){
 window.open(thePage ,'newWin',
'toolbar=no,location=no,status=yes,menubar=no,directories=no,copyhistory=no,scrollbars=no,width=565,height=565,left=400,screenX=400,top=30,screenY=30,resizable=no');
 }
  // return false;
}
  

function ShowLargePicture(vr_test) {
                if (vr_test > "") {
                        largePictureWindow = window.open("large_picture.html", "largePictureWindow", "width=400,height=500,left=0,top=30,resizable,scrollbars");
                        largePictureWindow.focus();
                }
        }

function CheckItem(sizetitle,colortitle){
var di=document.item_detail;

if (di.sel_qty.value == "0" ||
 !isPosInt(di.sel_qty.value) ){
 alert("You must order at least 1 item.");
 di.sel_qty.select();
 return false;
} else if (parseInt(di.sel_qty.value) > 99){
 alert("Not allowed to order quantities greater than 99.");
 di.sel_qty.select();
 return false;
} else if ( (parseInt(di.sel_qty.value) > 1) &&
   (di.continuity) && di.continuity.selectedIndex > 0 ) {
 alert("Can order only quantities of 1 when ordering a continuity item.");
 di.sel_qty.select();
 return false;
} else if (hasSizes == 'yes' &&
  di.sel_size.selectedIndex <= 0) {
 if (di.sel_size.selectedIndex == 0) {
  alert("Please select a "+sizetitle+".");
  di.sel_size.focus();
  return false;
 }
 else {
  alert("Error occured. If problem persists, try refreshing page.");
  di.sel_size.focus();
  return false;
 }
} else if (hasColors == 'yes' &&
  di.sel_color.selectedIndex <= 0) {
 if (di.sel_color.selectedIndex == 0) {
  alert("Please select a "+colortitle+".");
  di.sel_color.focus();
  return false;
 }
 else {
  alert("Error occured. If problem persists, try refreshing page.");
  di.sel_color.focus();
  return false;
 }
} else if (persReq == 'yes' && persEntered == 'no') {
 alert("Personalization is required for this item.");
 // di.personalize.focus();
 return false;
/* added check to be sure we have a color & size value */
} else if
  (hasColors == 'yes' &&
(di.sel_color.options[di.sel_color.selectedIndex].value == null ||
di.sel_color.options[di.sel_color.selectedIndex].value == "")) {
 msg = "Error selecting a "+colortitle+", try again.";
 alert(msg);
 di.sel_color.focus();
 return false;
} else if (hasSizes == 'yes' &&
(di.sel_size.options[di.sel_size.selectedIndex].value == null ||
di.sel_size.options[di.sel_size.selectedIndex].value == "")) {
 msg = "Error selecting a "+sizetitle+", try again.";
 alert(msg);
 di.sel_size.focus();
 return false;
} else {
 di.persentered.value = persEntered;
 return true;
}
}

