//JavaScript "Orden de Compra"
//Propiedad: 1998-2003. Net It Corp. México S.A. de C.V. 
//Autor: Martín Jiménez (Director General de Sistemas. Net It Corp.)
//Site: www.net-it.com.mx


function CKquantity(checkString) {
   strNewQuantity = "";

   for ( i = 0; i < checkString.length; i++ ) {
      ch = checkString.substring(i, i+1);
      if ( (ch >= "0" && ch <= "9") || (ch == '.') )
         strNewQuantity += ch;
   }

   if ( strNewQuantity.length < 1 )
      strNewQuantity = "1";

   return(strNewQuantity);
}


//---------------------------------------------------------------------
// FUNCION:      AddToCart                                              
// PARAMETROS:   Form Object                                            
// RETORNOS:     Cookie to user's browser, with prompt                  
// DESCRIPCION:  Adds a product to the user's shopping cart             
//---------------------------------------------------------------------
function AddToCart(thisForm) {
   iNumberOrdered = 0;
   iNumberOrdered = GetCookie("NumberOrdered");
   iNumberOrdered++;

   if ( iNumberOrdered > 12 )
      alert("Lo siento, pero su carrito está lleno.");
   else {
      if ( thisForm.ID_NUM == null )
         strID_NUM    = "";
      else
         strID_NUM    = thisForm.ID_NUM.value;
      
      if ( thisForm.QUANTITY == null )
         strQUANTITY  = "1";
      else
         strQUANTITY  = thisForm.QUANTITY.value;
      
      if ( thisForm.PRICE == null )
         strPRICE     = "0.00";
      else
         strPRICE     = thisForm.PRICE.value;
      
      if ( thisForm.NAME == null )
         strNAME      = "";
      else
         strNAME      = thisForm.NAME.value;
      
      if ( thisForm.SHIPPING == null )
         strSHIPPING  = "0.00";
      else
         strSHIPPING  = thisForm.SHIPPING.value;
      
      if ( thisForm.ADDITIONALINFO == null )
         strADDTLINFO = "";
      else
         strADDTLINFO = thisForm.ADDITIONALINFO.value;

      dbUpdatedOrder = strID_NUM    + "|" + 
                       strQUANTITY  + "|" +
                       strPRICE     + "|" +
                       strNAME      + "|" +
                       strSHIPPING  + "|" +
                       strADDTLINFO;

      strNewOrder = "Order." + iNumberOrdered;
      SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
      SetCookie("NumberOrdered", iNumberOrdered, null, "/");
      notice = strQUANTITY + " " + strNAME + " ha sido agregado al carro de compras.";
      alert(notice);
   }
}


//---------------------------------------------------------------------
// FUNCION:      getCookieVal                                           
// PARAMETROS:   offset                                                 
// RETORNOS:     URL unescaped Cookie Value                             
// DESCRIPCION:  Get a specific value from a cookie                     
//---------------------------------------------------------------------
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);

   if ( endstr == -1 )
      endstr = document.cookie.length;
   return(unescape(document.cookie.substring(offset, endstr)));
}


//---------------------------------------------------------------------
// FUNCION:      FixCookieDate                                          
// PARAMETROS:   date                                                   
// RETORNOS:     date                                                   
// DESCRIPCION:  Fixes cookie date, stores back in date                 
//---------------------------------------------------------------------
function FixCookieDate (date) {
   var base = new Date(0);
   var skew = base.getTime();

   date.setTime (date.getTime() - skew);
}


//---------------------------------------------------------------------
// FUNCION:      GetCookie                                             
// PARAMETROS:   Name                                                  
// RETORNOS:     Value in Cookie                                       
// DESCRIPCION:  Retrieves cookie from users browser                    
//---------------------------------------------------------------------
function GetCookie (name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;

   while ( i < clen ) {
      var j = i + alen;
      if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));
      i = document.cookie.indexOf(" ", i) + 1;
      if ( i == 0 ) break;
   }

   return(null);
}


//---------------------------------------------------------------------
// FUNCION:      SetCookie                                              
// PARAMETROS:   name, value, expiration date, path, domain, security   
// RETORNOS:     Null                                                   
// DESCRIPCION:  Stores a cookie in the users browser                   
//---------------------------------------------------------------------
function SetCookie (name,value,expires,path,domain,secure) {
   document.cookie = name + "=" + escape (value) +
                     ((expires) ? "; expires=" + expires.toGMTString() : "") +
                     ((path) ? "; path=" + path : "") +
                     ((domain) ? "; domain=" + domain : "") +
                     ((secure) ? "; secure" : "");
}


//---------------------------------------------------------------------
// FUNCION:      DeleteCookie                                           
// PARAMETROS:   Cookie name, path, domain                              
// RETORNOS:     null                                                   
// DESCRIPCION:  Removes a cookie from users browser.                   
//---------------------------------------------------------------------
function DeleteCookie (name,path,domain) {
   if ( GetCookie(name) ) {
      document.cookie = name + "=" +
                        ((path) ? "; path=" + path : "") +
                        ((domain) ? "; domain=" + domain : "") +
                        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}


//---------------------------------------------------------------------
// FUNCION:      MoneyFormat                                            
// PARAMETROS:   Number to be formatted                                 
// RETORNOS:     Formatted Number                                       
// DESCRIPCION:  Reformats Dollar Amount to #.## format                 
//---------------------------------------------------------------------
function moneyFormat(input) {
   var dollars = Math.floor(input);
   var tmp = new String(input);

   for ( var decimalAt = 0; decimalAt < tmp.length; decimalAt++ ) {
      if ( tmp.charAt(decimalAt)=="." )
         break;
   }

   var cents  = "" + Math.round(input * 100);
   cents = cents.substring(cents.length-2, cents.length)
           dollars += ((tmp.charAt(decimalAt+2)=="9")&&(cents=="00"))? 1 : 0;

   if ( cents == "0" )
      cents = "00";

   return(dollars);
}


//---------------------------------------------------------------------
// FUNCION:      RemoveFromCart                                         
// PARAMETROS:   Order Number to Remove                                 
// RETORNOS:     Null                                                   
// DESCRIPCION:  Removes an item from a users shopping cart             
//---------------------------------------------------------------------
function RemoveFromCart(RemOrder) {
   if ( confirm("Cliquee 'Aceptar' para eliminar este producto de su lista.") ) {
      NumberOrdered = GetCookie("NumberOrdered");
      for ( i=RemOrder; i < NumberOrdered; i++ ) {
         NewOrder1 = "Order." + (i+1);
         NewOrder2 = "Order." + (i);
         database = GetCookie(NewOrder1);
         SetCookie (NewOrder2, database, null, "/");
      }
      NewOrder = "Order." + NumberOrdered;
      SetCookie ("NumberOrdered", NumberOrdered-1, null, "/");
      DeleteCookie(NewOrder, "/");
      location.href=location.href;
   }
}


//---------------------------------------------------------------------
// FUNCION:      GetFromCart                                            
// PARAMETROS:   Null                                                   
// RETORNOS:     Product Table Written to Document                      
// DESCRIPCION:  Draws current cart product table on HTML page          
//---------------------------------------------------------------------
function GetFromCart( fShipping ) {
   if( fShipping )
      WriteToForm( true, fShipping );
   else
      WriteToForm( true, 0 );
}


//---------------------------------------------------------------------
// FUNCION:      WriteToForm                                            
// PARAMETROS:   Null                                                   
// RETORNOS:     Product hidden fields Written to Document              
// DESCRIPCION:  Draws current cart product hidden fields on HTML form  
//               if bDisplay == true, shows cart output as HTML table   
//---------------------------------------------------------------------
function WriteToForm( bDisplay, fShipping ) {
   iNumberOrdered = 0;
   fTotal         = 0;
   strTotal       = "";
   strShipping    = "";
   strOutput      = "";
   iNumberOrdered = GetCookie("NumberOrdered");

   if ( bDisplay )
      strOutput = "<TABLE CLASS=\"nopcart\" CELLSPACING=5><TR>" +
                  "<TD CLASS=\"nopheader\"><b>NO. PARTE</b></TH>" +
                  "<TD CLASS=\"nopheader\"><b>NOMBRE DEL PRODUCTO</b></TH>" +				  
                  "<TD CLASS=\"nopheader\"><b>UNIDADES</b></TH>" +
                  "<TD CLASS=\"nopheader\"><b>PRECIO</b></TH>" +
                  "<TD CLASS=\"nopheader\"><b>ELIMINAR</b></TH></TR>";

   for ( i = 1; i <= iNumberOrdered; i++ ) {
      NewOrder = "Order." + i;
      database = "";
      database = GetCookie(NewOrder);

      Token0 = database.indexOf("|", 0);
      Token1 = database.indexOf("|", Token0+1);
      Token2 = database.indexOf("|", Token1+1);
      Token3 = database.indexOf("|", Token2+1);
      Token4 = database.indexOf("|", Token3+1);

      fields = new Array;
      fields[0] = database.substring( 0, Token0 );
      fields[1] = database.substring( Token0+1, Token1 );
      fields[2] = database.substring( Token1+1, Token2 );
      fields[3] = database.substring( Token2+1, Token3 );
      fields[4] = database.substring( Token3+1, Token4 );
      fields[5] = database.substring( Token4+1, database.length );

      fTotal     += (parseInt(fields[1]) * parseFloat(fields[2]) );
      strTotal    = moneyFormat(fTotal);

      if ( bDisplay ) {
         strOutput += "<TR><TD CLASS=\"nopentry\">"  + fields[0] + " " + "</TD>";

         if ( fields[5] == "" )
            strOutput += "<TD CLASS=\"nopentry\">"  + fields[3] + " " + "</TD>";
         else
            strOutput += "<TD CLASS=\"nopentry\" ALIGN=CENTER>"  + fields[3] + " - <I>"+ fields[5] + " " + "</I></TD>";

         strOutput += "<TD CLASS=\"nopentry\" ALIGN=CENTER>"  + fields[1] + "</TD>" +
                      "<TD CLASS=\"nopentry\" ALIGN=CENTER>$" +" " + moneyFormat(fields[2]) + " " + "/ U.S.D.</TD>";
         strOutput += "<TD CLASS=\"nopentry\" ALIGN=CENTER><input type=button value=\"  Eliminar  \" onClick=\"RemoveFromCart("+i+")\" class=\"nopbutton\"></TD></TR>";
      }

      strOutput += "<input type=hidden name=\"ID_"       + i + "\" value=\"" + fields[0] + "\">";
      strOutput += "<input type=hidden name=\"QUANTITY_" + i + "\" value=\"" + fields[1] + "\">";
      strOutput += "<input type=hidden name=\"PRICE_"    + i + "\" value=\"" + fields[2] + "\">";
      strOutput += "<input type=hidden name=\"NAME_"     + i + "\" value=\"" + fields[3] + "\">";
      strOutput += "<input type=hidden name=\"ADDTLINFO_"+ i + "\" value=\"" + fields[5] + "\">";
   }

   if ( bDisplay ) {
      strOutput += "<TR><TD CLASS=\"noptotal\" COLSPAN=3><B>TOTAL DE SU COMPRA:</B></TD>";
      strOutput += "<TD CLASS=\"noptotal\" COLSPAN=2><B>$" + " " + moneyFormat((fTotal + fShipping)) + " " + "/ U.S.D." + "</B></TD>";
      strOutput += "</TR>";
      strOutput += "</TABLE>";
      strOutput += "<input type=hidden name=\"TOTAL\"    value=\"$" + moneyFormat((fTotal + fShipping)) + "\">";
   }

   document.write(strOutput);
   document.close();
}

