var cookieName = 'o';
var oO = new Array();
var oS;
var cP = '';
var cnt = 0;
_init_order();

function _init_order() {  
  var cookieValue = getCookie(cookieName);
  if ( cookieValue ) {
     eval(cookieValue);
  }
}

function show_cookie() {
  var cookieValue = getCookie(cookieName);
  if ( cookieValue ) {
     eval(cookieValue);
  }
  var tmpStr = '';
  for ( i=0; i<oO.length; i++) {
     if ( oO[i] && oO[i] > 0 ) {
          tmpStr += 'Product '+i+' = '+oO[i]+"\n";
     }	  
  } 
  alert( tmpStr );	 	
}

function add_item() {
       if ( document.getElementById('orderAmount') ) {
	  var validInt = 0;
	  if ( document.getElementById('orderAmount').value ) {
	     validInt = validInteger( document.getElementById('orderAmount').value);
             if ( !validInt ) {
	        alert( 'Enter whole numbers only' );
		return false;
             }
	  }
	  else {
	        alert( 'Enter an amount' );
		return false;	  
	  }
	  var action = 'added';
          if ( parseInt(document.getElementById('orderAmount').value ) < 1 ) {
	     action = 'removed';
          }
	  oO[cP] = document.getElementById('orderAmount').value;
	  store_order();
	  hideOrderForm();
	  document.getElementById('orderAmount').value = '';
	  var productInfo = '';
	  if ( document.getElementById('product'+cP ) ) {
	     var innerHTML = document.getElementById('product'+cP ).innerHTML;
	     innerHTML = innerHTML.replace(/\<.+?\>/g, '');
     	     productInfo = "\n"+"-----------------------------\n"+innerHTML+"\n";
	     productInfo += 'Quantity -' +oO[cP]+"\n-----------------------------\n";
          }
	if ( confirm("The product has been added to your Quotation Request Form.\nClick OK to view the form or CANCEL to continue shopping.") ) {
	     document.location = '/site/quotes/quotation_request_form/';
	  }	  
       }
}

function add_product(prodID) {
	 if ( cP && cP != prodID ) {
	    hideOrderForm();
	 }
	 cP = prodID;
	 document.getElementById('btnProd'+prodID).style.display='none';
	 if ( !document.getElementById('orderFormPopup') ) {
	   var myPopUp = document.createElement("div");
	   myPopUp.id = "orderFormPopup";
	   myPopUp.className = "orderFormPopup";

	   text1 = document.createTextNode("Qty ");
	   myPopUp.appendChild(text1);

	   inputText = document.createElement("input");
           inputText.setAttribute("type",'text');
           inputText.id = 'orderAmount';
           inputText.maxLength = '5';
           inputText.className = 'orderAmount';
	   inputText.value = '';
	   if ( oO[cP] && oO[cP]>0) {
	      inputText.value = oO[cP];
	   }

           inputText.name = 'orderAmount';
           myPopUp.appendChild(inputText);

       	   submitButton = document.createElement("input");
	   submitButton.setAttribute("type",'button');
	   submitButton.setAttribute("id",'orderAddBtn');
	   submitButton.setAttribute("name",'orderAddBtn');
	   submitButton.className = 'btnOrderFrm';
	   submitButton.setAttribute("value",'Add');
	   submitButton.onclick = Function("add_item(document);");
	   myPopUp.appendChild(submitButton);		  


       	   submitButton = document.createElement("input");
	   submitButton.setAttribute("type",'button');
	   submitButton.setAttribute("id",'orderCancelBtn');
	   submitButton.setAttribute("name",'orderCancelBtn');
	   submitButton.className = 'btnOrderFrm';
	   submitButton.setAttribute("value",'Cancel');
	   submitButton.onclick = Function("hideOrderForm(document);");
	   myPopUp.appendChild(submitButton);		  

	   document.getElementById('frmProd'+prodID).appendChild(myPopUp);
	 }
	 else {
	   if ( oO[cP] ) {
	      if ( oO[cP]>0) {
	      	 document.getElementById('orderAmount').value = oO[cP];
	      }
	      else {
	      	  document.getElementById('orderAmount').value =''; 
	      }
	   }
           else {
              document.getElementById('orderAmount').value ='';
           } 
	   document.getElementById('frmProd'+prodID).appendChild(document.getElementById('orderFormPopup'));
	   document.getElementById('orderFormPopup').style.display='block';   
	 }	      
}

function hideOrderForm() {
  document.getElementById('orderFormPopup').style.display='none';
  document.getElementById('btnProd'+cP).style.display='block';
}

function remove_item() {
   if ( oO[cP] )  {
      oO.splice(cP,1);
   }
}

function store_order() {	
	oS = new JSSerializer();
	
	//Prefs
	oS.Prefs.SmartIndent = true;
	oS.Prefs.ShowLineBreaks = true;
	oS.Prefs.ShowTypes =	true;
	
	//Types
	oS.Types.UseNull = true;
	oS.Types.UseUndefined = true;
	oS.Types.UseArray = true;
	oS.Types.UseObject =	true;
	oS.Types.UseBoolean = true;
	oS.Types.UseDate = true;
	oS.Types.UseError = true;
	oS.Types.UseFunction = true;
	oS.Types.UseNumber =	true;
	oS.Types.UseRegExp =	true;
	oS.Types.UseString =	true;
	oS.Types.UseUserDefined = true;
	oS.Types.UseObjectsForUserDefined =	true;
	
	//Rules
	oS.CheckInfiniteLoops = true;
	oS.MaxDepth = 3;
	
	oS.Serialize(oO);
	
	var cookieStr = oS.GetJSString('oO');
	setCookie(cookieName, cookieStr, '','/');
	
}

function validInteger(inputStr) {
   var myRegExp = /\D/;
   if ( myRegExp.test(inputStr)  ) { 
      return false;
   }

   return true;
}

function init_quote_form() {
  
  for ( i=0; i<oO.length; i++) {
     if ( oO[i] && oO[i] > 0 ) {

          if ( document.getElementById('prod'+parseInt(i)) ) {
             cnt++; 
	     document.getElementById( 'prod'+parseInt(i)).value = oO[i];
	  }

          if ( document.getElementById('row'+parseInt(i)) ) {
             document.getElementById('row'+parseInt(i)).className = '';
          }
     }	  
    
  } 
  if ( cnt == 0 ) {
     alert('No products have been selected');
  }

}
