/*
  This script file contains the posting methods for the ShopCenter jsp page

  CopyRight (C) SonicSpider 2007, All Rights Reserved
  Developed by John B. Moore

Code conventions
=========================================
function prefixes
   sv -  functions found in this file
var prefixes:
	 g_  - global to this file
         Local vars
	  _s    - string
	  _n    - number
	  _v   - anytype
	  _b   - boolean
	  _ar - array
	  _the - object

	 arg_ - argument param of that function
	 <none> local to that function
function suffixes:
       _s - returns string
       _n - returns number
       _b - returns boolean
       _r - returns array
       _v - returns anytype
       _obj - returns object reference
       <none> - returns nothing (void)

Dependencies;

Requires the following vars defined dynamically in the calling page

g_sEncodedURL
g_choiceType

*/

function postIt(arg_sAction, arg_sType , arg_sForm, arg_sVar1, arg_sVar2) {

       //var g_sEncodedURL = "";
       // var g_choiceType = "";

		// use to temp disable a post action
        if (arg_sVar2 == "nolink") {
           alert("Feature not available at this time!");
           return;
        }

        //alert("here");

        var _slocation = g_sEncodedURL+"?action="+arg_sAction+
                   "&type="+arg_sType+
                   "&form="+arg_sForm+
                   "&value1="+arg_sVar1+
                   "&value2="+arg_sVar2+
                   "&sourceform=shopcenter"+
                   "&path=IndexPath"+
                   "&pathindex=1";


        if (arg_sAction == "genlink") {

          if (arg_sType == "links") {
              popLinkWindow(_slocation, arg_sType, 825, 700);
          } else if (arg_sType == "help") {
              popLinkWindow(_slocation, arg_sType, 300, 500);
          } else if (arg_sType == "faqs") {
              popLinkWindow(_slocation, arg_sType, 400, 500);
          } else if (arg_sType == "about") {
              popLinkWindow(_slocation, arg_sType, 525, 475);
          }

        } else if (arg_sType == "view_prod") {

          // trap double click
          if (checkIsDblClick() == true) {
            resetDblClickCheck();
            return;
          }

          popLinkWindow(_slocation, arg_sType, 450, 650);

        } else if (arg_sType == "view_special") {

          // trap double click
          if (checkIsDblClick() == true) {
            resetDblClickCheck();
            return;
          }

          popLinkWindow(_slocation, arg_sType, 450, 450);

        } else {

            var _multiFormTag;
            // check for special form names used when there are multiple forms on a page
            if (eval("document.theForm"+arg_sVar1)) {
              _multiFormTag = arg_sVar2;
            } else {
              _multiFormTag = "";
            }


            //
            if (arg_sType == "search_prod") {

             _slocation = _slocation + "&search_value="+document.searchForm.search_value.value;

            } else if (arg_sType.indexOf("addcart") != -1) {

             // alert("add cart");

              var _qty = 0;

              if (document.getElementById("CART_QTY"+_multiFormTag)) {

                _qty = document.getElementById("CART_QTY"+_multiFormTag).value;
                if (_qty == "") {
                   alert("A quantity value is Required.");
                   return;
                }
              } else {
                // default quantity
                _qty = 1;
              }

               _slocation = _slocation + "&CART_QTY="+_qty;

              // check for existance of choicelist object
			  if (document.getElementById("choicelist"+_multiFormTag)) {
                //getvalue
                var _choicelist = document.getElementById("choicelist"+_multiFormTag).value;
                if (_choicelist != "") {
                  // comma delimited list
                  _slocation = _slocation + "&choicelist="+_choicelist;

                  var _arChoices = _choicelist.split(",");
                  //create matching comma delimited list
                  var _nCount = _arChoices.length;

                  var _sChoiceCodes = ""

                  for(var i = 0 ; i< _nCount ; i++){
					var _objChoice = document.getElementById(_arChoices[i]+"_"+_multiFormTag)
					if (_objChoice) {
						if (i == 0) {
						  _sChoiceCodes = _objChoice.options[_objChoice.selectedIndex].value;

						} else {
						  _sChoiceCodes = _sChoiceCodes+","+ _objChoice.options[_objChoice.selectedIndex].value;

						}
					}
                  }
                  _slocation = _slocation +  "&choicecodes="+_sChoiceCodes;


                }

              }


              if (g_choiceType == "field") {

                _slocation = _slocation + "&PROD_ID="+ arg_sVar1;


              } else if (g_choiceType == "radio") {

                var _arRadio = document.getElementById("PROD_ID"+_multiFormTag);
                var _sRadioChoice = "";
				if (_arRadio) {
					for (var i=0;i<_arRadio.length;i++) {
					  if (_arRadio[i].checked) {
						_sRadioChoice = _arRadio[i].value;
					  }
					}
				}

                if (_sRadioChoice == "") {
                  alert("Please Select an item to purchase.");
                  return;
                }

                _slocation = _slocation + "&PROD_ID="+ _sRadioChoice;


              } else if (g_choiceType == "check") {

                // to do....

              }


              // check for price override field..
              // used for products with multiple or variable prices

			  if (document.getElementById("UNIT_PRICE"+_multiFormTag) ) {

				  var _objPriceTag = document.getElementById("UNIT_PRICE"+_multiFormTag);
				  var _sNodeName = _objPriceTag.nodeName;

				  if (_sNodeName.toUpperCase() == "SELECT") {

					_slocation = _slocation + "&UNIT_PRICE=" + _objPriceTag.options[_objPriceTag.selectedIndex].value;

				  } else {

					 _slocation = _slocation +  "&UNIT_PRICE="+_objPriceTag.value;

				  }

			  }

              // check for name override name field..
              // used for products that are templates and are changed on the fly..
              if (document.getElementById("OVERRIDE_NAME"+_multiFormTag)) {

				   var _objNameTag = document.getElementById("OVERRIDE_NAME"+_multiFormTag);
				   var _sNodeName = _objNameTag.nodeName;

                   if (_sNodeName.toUpperCase() == "SELECT") {

					_slocation = _slocation + "&OVERRIDE_NAME=" + _objNameTag.options[_objNameTag.selectedIndex].value;

				  } else {

					 _slocation = _slocation + "&OVERRIDE_NAME="+_objNameTag.value;

				  }
              }


              if (arg_sType == "addcart_coup") {

                 var _objCoup = document.getElementById("COUP_ID"+_multiFormTag);

				 if (_objCoup) {

                    _slocation = _slocation + "&COUP_ID="+_objCoup.value;

				 }

              }



            } else if (arg_sType == "addfavorite") {

              _slocation = _slocation + "&CART_QTY="+arg_sVar2;

            } else if (arg_sType == "addspecial") {

              _slocation = _slocation + "&CART_QTY=1"

			  var _choicelist = "";
              // check for existance of choicelist object
			  if (document.getElementById("choicelist"+arg_sVar2)) {
                //getvalue
                _choicelist = document.getElementById("choicelist"+arg_sVar2).value;
                if (_choicelist != "") {
                  // comma delimited list
                  _slocation = _slocation + "&choicelist="+_choicelist;

                  var _arChoices = _choicelist.split(",");
                  //create matching comma delimited list
                  var _nCount = _arChoices.length;

                  var _sChoiceCodes = ""

                  for(var i = 0 ; i< _nCount ; i++){
					var _objChoice = document.getElementById(_arChoices[i]+"_"+arg_sVar2)
					if (_objChoice) {
						if (i == 0) {
						  _sChoiceCodes = _objChoice.options[_objChoice.selectedIndex].value;

						} else {
						  _sChoiceCodes = _sChoiceCodes+","+ _objChoice.options[_objChoice.selectedIndex].value;

						}
					}
                  }
                  _slocation = _slocation + "&choicecodes="+_sChoiceCodes;


                }

              }


            } else {

              // do nothing extra...

            }

            // trap double click
            if (checkIsDblClick() == true) {
                resetDblClickCheck();
                return;
            }

            document.location = _slocation;
       }
  }

