<!--
/* jquanty3.js */
/* 
	The two functions are independent of each other...
	* pricrebreak  - is called from the htm form from the QTY input box.
	* fix_colornumber - is called from the is ready function and from the
	drop down color box on the html form. If the color changes or onChange then
	fix_colornumber is called.

	Also: These functions us function.js too. So that has to be included in the html
	web page along with this funtion.

	When isReady is called fix_colornumber is called from within that function to check
	if the color changed or not.

	function pricebreak ----
	Checks out price breaks and assign price 
	Uses function.js also for the makeArry function due to N2 

	In the html or htm form you set the following fields
	hidden  discount="1,19.95,2,19.00,3,18.00,4,17.00,-1,-1"
	hidden  maxqty ="5"  maximum the quantity can be
	hidden minqty = "1"   minimum the quantity can be

	fix_colornumber ----
	This version has the color position function named
	fix_colornumber. This version of the routine gets the 
	position of the color letter to be substituted into the
	part number. The position is sent here from the htm file
	which calls this routine.

	function removeleadingspaces ----
	This removes leading blank spaces in a string and returns string 

	function isANumber----
	This returns 1 if the string passed to it is a numbe or 0 if it is not...

	function whichform-----
	This fills global var n with form name if form name is found...
*/

var n;
var entry;
var price;
var debugtrail = 0;

/* *********************************************************************************
		On submit routine
************************************************************************************ */
function isReady(form,colorpicked,numberofitems){

if(debugtrail) {
		window.alert("IN FUNCTION isReady STOP1 \n colorpicked: " +colorpicked + "\n numberofitems: " + numberofitems);
}
	if ( (fix_colornumber(form, colorpicked,numberofitems)) == false){
	//if ( (fix_colornumber2(form)) == false){

			if(debugtrail) {
				window.alert("INS FUNCTION isReady STOP 2 \n SUBMISSION About to return false for submit Returned from fix_colornumber form"); 
		    		return false;
			}
	}
	if(debugtrail) {
			//.. make return false if you are testing and don't want it to send the form up...
			 window.alert("IN FUNCTION IsReady STOP3 \nSUBMISSION About to return TRUE for submit"); return true;
	}
 
 return true;
  
}
/* *************************************************************************
		remove leading spaces in string 
*************************************************************************** */
function removeleadingspaces(thestring) {
var thestring2 = thestring;
var thestringlen;
var returnstring;
var i = 0;

thestringlen = thestring2.length;
/* window.alert("String = " + thestring2+ "\nString Length= " + thestringlen);
return;
*/

   for (var i2=0; i2 < thestring2.length; i2++){
      if ( thestring2.charAt(i2) !=" " ){
          i = i2;
          break;
         }
   }
   thestringlen = thestring2.length;

   returnstring = thestring2.substring(i,thestringlen);

if(debugtrail) {
   		window.alert("IN Function Removeleadingspaces: \nReturn String = " + returnstring+ "\nReturn string length = " +thestringlen+"\nVariable i counter = " +i);
       	//return;
}

   return returnstring;
   
}
/* **************************************************************************** 
    find which form number has been sent up 
	Returns: n which is a global variable set above to form name..
**************************************************************************** */
function whichform(formname,noofforms){
var noofforms   = document.forms.length;
var formname1   = formname;

  for (var n1=0; n1 < noofforms; n1++) {
     if (document.forms[n1].name == formname1) {
       n = n1;   // ** sets global n to formname being used...
       break;
     }
  }
} 

/* ****************************************************************************
		My function added...To check for a valid number even a is 0 allowed...
		Returns: 1 if a number is found even if is a 0... or 0 if no number found
****************************************************************************** */
function isANumber(number) {
  answer =1;
  for (var i=0; i < number.length; i++) {
       if ( (number.charAt(i) != " " )  && (!parseFloat(number.charAt(i)) ) &&
         ( (number.charAt(i) !="0") ) ) {
                  answer = 0;
 	    break;
	    }
       }
        return answer;
} 
/* ****************************************************************************
	    	Check the quantity 
		DISPLAYS: A window alert with the proper message based on wrong quantity put in..
****************************************************************************** */
function checkquantity(qty, formname, minallowed, maxallowed) {
    var noofforms   = document.forms.length;
    var f           = formname;
  //var n           = 0;

   whichform(f);  

   //for (var n1=0; n1 < noofforms; n1++) {
   //  if (document.forms[n1].name == f) {
   //    n = n1;
   //    break;
   //    }
   //}

   //  if ( !(isANumber(qty)) ) {
   //      window.alert("You must enter a number greater than 0.");
   //      return;
   //    }

   if ( (maxallowed >= 1) && (qty > maxallowed) )  {
         document.forms[n].QTY.value = maxallowed;
         window.alert("Sorry, you can only order a maximum of " + maxallowed) ;
         return;
         }

   if ( (minallowed > 1) && (qty < minallowed) )  {
         document.forms[n].QTY.value = minallowed;
         window.alert("Sorry, you must order a minumum quantity of " + minallowed) ;
         return;
         }

  return;
}
/* *****************************************************************************
	fix_colornumber2

 	The colorpos is in the htm form in hidden var COLORPOS. 
	Set this	equal to the position of the color string.

   	The drop down selection box in the html form has the color choices
   	sending up the letter for the color.

   	A substitution of this letter is placed into the SKU number from
   	the html form.


******************************************************************************** */
function fix_colornumber(formname, colorpicked,numberofitems) {
//function fix_colornumber2(formname, colorpicked, skuno2, itemname) {
//function fix_colornumber2(formname) {

if(debugtrail) {
	window.alert("IN Funtion fix_colornumber Stop1\n Formname: " + formname + " Color picked " + colorpicked);
	//return;
}

var noofforms   = document.forms.length;
var f           = formname;
var p           = formname;
var n           = 0;
// var colorlist   = new makeArray("W","C","Y","L","R","V","O","E","P","A","G","I");
// var noofcolors  = 8;

//whichform(f);  // Call this func first to get form number

for (var n1=0; n1 < noofforms; n1++) {
     if (document.forms[n1].name == p) {
       n = n1;
       break;
     }
  }

/* 
var colorpos     = document.forms[n].COLORPOS.value - 1; 
var skuno        = document.forms[n].SKU2.value;
var origcolor    = document.forms[n].COLORCHOICE.value;
var noofitems    = document.forms[n].NOOFITEMS.value;
var itemname     = document.forms[n].ITEMNAMEORIG.value;
*/

if(debugtrail) {
		window.alert("IN Funtion fix_colornumber Stop2\n  p = " + p + " Color picked " + colorpicked);
}

var colorpos      = formname.elements["COLORPOS"].value - 1; //allow for 0 pos
var skuno          = formname.elements["SKU2"].value;

//var colorpos      = formname.COLORPOS.value - 1; //allow for 0 pos
//var skuno          = formname.SKU2.value;


if(debugtrail) {
	window.alert("IN Funtion fix_colornumber Stop3 \n Variables have been set colorpos: " + colorpos + "\nSKU2= " + skuno +"\n Color picked " + colorpicked);
}

//var origcolor      = formname.COLORCHOICE.value;
var origcolor      = colorpicked;
var noofitems	= numberofitems;

//var noofitems    = formname.elements["NOOFITEMS"].value;
//var noofitems    = formname.NOOFITEMS.value;


var itemname    = formname.elements["ITEMNAMEORIG"].value;
//var itemname    = formname.ITEMNAMEORIG.value;

var skulength    = skuno.length;
var resultvalue  ;
var resultvalue2 ;
var resultName   ;
var pricelength  = 0;

var newcolor         		= origcolor.substring(0,1);
var newcolorlen      	= origcolor.length;
var colordescription 	= origcolor.substring(1,newcolorlen);


if(debugtrail) {
	window.alert("IN Funtion fix_colornumber Stop4\n noofitems: " + noofitems +"\nITEMNAMEORIG = " +itemname);
}

// Find the particular form item to use in this case SKU...

/*
for (var i=0; i < document.forms[n].elements.length; i++) {
     resultName  = document.forms[n].elements[i].name;
     resultValue = document.forms[n].elements[i].value;
     if (resultName == "SKU2"){
         resultvalue2 = document.forms[n].elements[i].value;
         break;
         }
}
*/


//resultvalue2 = skuno;



for (var i=0; i < formname.elements.length; i++) {
     resultName = formname.elements[i].name;
     resultValue = formname.elements[i].value;
     if (resultName == "SKU2"){
         		resultvalue2 = formname.elements[i].value;
         		break;
         }
}


if (noofitems > 0) {
	//   var pricelist     = new makeArray(noofitems);
   	var itemnamesplit = new makeArray(2);
   
  	 itemnamesplit     = itemname.split(":");
   	var newitemname   = itemnamesplit[1];

 	//  pricelist = skuno.split(":");   // split prices into array...
 	//  pricelength = pricelist.length;

   	var newskulen = 0;
   	var newsku = new makeArray(noofitems);
   	newsku = resultvalue2.split(":");

   	var newsku2 = newsku[itemname.substring(0,1)-1];   
   
 	// window.alert("got here" + newsku2); return;
	 //window.alert("got here" + newsku2); 

   	var newsku3 = removeleadingspaces(newsku2);
  	// window.alert("got here" + newsku2 + "\nNew Skuk 3 = " +newsku3); return;
  	newskulen = newsku2.length;
   	newsku2 = newsku3;
}



/*
  window.alert("IN Funtion fix_colornumber Stop6\nTest: colopos= " + colorpos+"\nskuno=" +skuno+ 
   "\nnewcolor= "+newcolor+
   "\nSkulenth=" +skulength+ 
   "\nItemname= " +itemname+ 
   "\nResultvalue2= " +resultvalue2+ 
   "\nResultName = " +resultName+ 
   "\nPricelist = " +pricelist[0]+ 
   "\nPricelist Length= " +pricelength+
   "\nnewsku2 = " +newsku2
   );

  // return;

  return true;
*/



// 2nd - Get the left side of part no and right side of part no.
   var leftpartno  = newsku2.substring(0,colorpos);
   var rightpartno = newsku2.substring(colorpos+1, newskulen);

// 3rd - Make the new partno combining left,color, and right nos.
   var newnumber   = leftpartno + newcolor + rightpartno; 
   
// 4th - Now place the new partno back into the html form.

// document.forms[n].SKU.value    = newnumber;


/*
   document.forms[n].NEWSKU.value = newnumber; // Display it.
   document.forms[n].ITEMNAME.value = newitemname + " " + colordescription; // Display it.
*/


   formname.NEWSKU.value = newnumber; // Display it.
   formname.SKU.value = newnumber;
   formname.NOOFITEMS.value = 0;
   formname.ITEMNAME.value = newitemname + " " + colordescription; // Display it.


if(debugtrail) {

  window.alert("IN Funtion fix_colornumber Stop5\n Test: colorpos= " + colorpos+"\nskuno=" +skuno+ 
   "\nOriginal Color pos Value = " +colorpos+
   "\nOriginalcolor Value = " +origcolor+
   "\nOriginal noofitems Value = " +noofitems+
   "\nOriginal skulength Value = " +skulength+
   "\nOriginalcolor Value = " +origcolor+
   "\nOriginal color description Value = " +colordescription+
   "\nnewcolor= "+newcolor+
   "\nnewcolerlen Value = " +newcolorlen+
   "\nSkulenth=" +skulength+ 
   "\nItemname= " +itemname+ 
   "\nResultvalue2= " +resultvalue2+ 
   "\nResultName = " +resultName+ 
   "\nNewsku length = " +newskulen+
   "\n==============================" +
   "\nThis will be replaced - newsku2 = " +newsku2+
   "\nLeft side is leftpartno =" +leftpartno+
   "\nRight side is rightpartno = " +rightpartno+
   "\nCombined Final Part No or Newnumber = " +newnumber+
   "\nNew form NEWSKU value = " +formname.NEWSKU.value+
   "\nNew  form SKU value = " +formname.SKU.value+
   "\nNew form NOOFITEMS value = " +formname.NOOFITEMS.value+
   "\nNew form ITEMNAME value  = " +formname.ITEMNAME.value

   );
}

//formname.reset();

//  return false;

//formname.reset();



// 5th - Done, now return from the function..

return;

}
/* *************************************************************************************
 	The colorpos is in the htm form in hidden var COLORPOS. 
	Set this	equal to the position of the color string.

   	The drop down selection box in the html form has the color choices
   	sending up the letter for the color.

   	A substitution of this letter is placed into the SKU number from
   	the html form.

***************************************************************************************** */
// function fix_colornumber(formname, colorpicked, skuno2,itemname) {

function fix_colornumber_old(formname) {
var noofforms   = document.forms.length;
var f           = formname;
var p           = formname;
var n           = 0;

var thename = formname.elements[3].name;
var thecolorchoicevalue = formname.elements["COLORCHOICE"].value;

// var colorlist   = new makeArray("W","C","Y","L","R","V","O","E","P","A","G","I");
// var noofcolors  = 8;

//whichform(f);  // Call this func first to get form number

for (var n1=0; n1 < noofforms; n1++) {
     if (document.forms[n1].name == p) {
       n = n1;
       break;
     }
  }

/* 
var colorpos     = document.forms[n].COLORPOS.value - 1; 
var skuno        = document.forms[n].SKU2.value;
var origcolor    = document.forms[n].COLORCHOICE.value;
var noofitems    = document.forms[n].NOOFITEMS.value;
var itemname     = document.forms[n].ITEMNAMEORIG.value;
*/

var colorpos      = formname.COLORPOS.value - 1; //allow for 0 pos
var skuno          = formname.SKU2.value;
var origcolor      = formname.COLORCHOICE.value;
var noofitems    = formname.NOOFITEMS.value;
var itemname    = formname.ITEMNAMEORIG.value;

var skulength    = skuno.length;
var resultvalue  ;
var resultvalue2 ;
var resultName   ;
var pricelength  = 0;

var newcolor         = origcolor.substring(0,1);
var newcolorlen      = origcolor.length;
var colordescription = origcolor.substring(1,newcolorlen);


// Find the particular form item to use in this case SKU...

/*
for (var i=0; i < document.forms[n].elements.length; i++) {
     resultName  = document.forms[n].elements[i].name;
     resultValue = document.forms[n].elements[i].value;
     if (resultName == "SKU2"){
         resultvalue2 = document.forms[n].elements[i].value;
         break;
         }
}
*/


for (var i=0; i < formname.elements.length; i++) {
     resultName = formname.elements[i].name;
     resultValue = formname.elements[i].value;
     if (resultName == "SKU2"){
         	resultvalue2 = formname.elements[i].value;
         	break;
         }
}

if (noofitems > 0) {
//   var pricelist     = new makeArray(noofitems);
   var itemnamesplit = new makeArray(2);
   
   itemnamesplit     = itemname.split(":");
   var newitemname   = itemnamesplit[1];

 //  pricelist = skuno.split(":");   // split prices into array...
 //  pricelength = pricelist.length;

   var newskulen = 0;
   var newsku = new makeArray(noofitems);
   newsku = resultvalue2.split(":");

   var newsku2 = newsku[itemname.substring(0,1)-1];   
   
 // window.alert("got here" + newsku2); return;
	 //window.alert("got here" + newsku2); 

   var newsku3 = removeleadingspaces(newsku2);
  // window.alert("got here" + newsku2 + "\nNew Skuk 3 = " +newsku3); return;
   newskulen = newsku2.length;
   newsku2 = newsku3;
}



/*
  window.alert("Test: colopos= " + colorpos+"\nskuno=" +skuno+ 
   "\nnewcolor= "+newcolor+
   "\nSkulenth=" +skulength+ 
   "\nItemname= " +itemname+ 
   "\nResultvalue2= " +resultvalue2+ 
   "\nResultName = " +resultName+ 
   "\nPricelist = " +pricelist[0]+ 
   "\nPricelist Length= " +pricelength+
   "\nnewsku2 = " +newsku2
   );

  // return;

  return true;
*/



// 2nd - Get the left side of part no and right side of part no.
   var leftpartno  = newsku2.substring(0,colorpos);
   var rightpartno = newsku2.substring(colorpos+1, newskulen);

// 3rd - Make the new partno combining left,color, and right nos.
   var newnumber   = leftpartno + newcolor + rightpartno; 
   
// 4th - Now place the new partno back into the html form.

// document.forms[n].SKU.value    = newnumber;


/*
   document.forms[n].NEWSKU.value = newnumber; // Display it.
   document.forms[n].ITEMNAME.value = newitemname + " " + colordescription; // Display it.
*/


   formname.NEWSKU.value = newnumber; // Display it.
   formname.SKU.value = newnumber;
   formname.NOOFITEMS.value = 0;
   formname.ITEMNAME.value = newitemname + " " + colordescription; // Display it.



  window.alert("Form Name = " + thename + "\nThe name value: " +thecolorchoicevalue + "\nNo of forms: " + noofforms + "\n" + "Test: colorpos= " + colorpos+"\nskuno=" +skuno+ 
   "\nOriginal Color pos Value = " +colorpos+
   "\nOriginalcolor Value = " +origcolor+
   "\nOriginal noofitems Value = " +noofitems+
   "\nOriginal skulength Value = " +skulength+
   "\nOriginalcolor Value = " +origcolor+
   "\nOriginal color description Value = " +colordescription+
   "\nnewcolor= "+newcolor+
   "\nnewcolerlen Value = " +newcolorlen+
   "\nSkulenth=" +skulength+ 
   "\nItemname= " +itemname+ 
   "\nResultvalue2= " +resultvalue2+ 
   "\nResultName = " +resultName+ 
   "\nNewsku length = " +newskulen+
   "\n==============================" +
   "\nThis will be replaced - newsku2 = " +newsku2+
   "\nLeft side is leftpartno =" +leftpartno+
   "\nRight side is rightpartno = " +rightpartno+
   "\nCombined Final Part No= " +newnumber+
   "\nNew NEWSKU value = " +formname.NEWSKU.value+
   "\nNew  SKU value = " +formname.SKU.value+
   "\nNew  NOOFITEMS value = " +formname.NOOFITEMS.value+
   "\nNew  ITEMNAME value  = " +formname.ITEMNAME.value

   );
//formname.reset();

  return false;

//formname.reset();



// 5th - Done, now return from the function..

return false;

}
/* ***************************************************************************
  PRICEBREAKS get the discount and the qty from the form and assign price break
***************************************************************************** */
function pricebreak(quantity, formname, check) {
  var noofforms   = document.forms.length;
  var qty         = parseFloat(quantity);
  var qty3        = quantity;
  this.name       = formname;
  var  p          = formname;
  var  n          = 0;
  //  var discstring  = document.NumberForm.discount.value;
  //  var maxallowed  = document.NumberForm.maxqty.value;
  //  var minallowed  = document.NumberForm.minqty.value;  
  // var disclist = new Array();


//  whichform(formname,noofforms);

for (var n1=0; n1 < noofforms; n1++) {
     if (document.forms[n1].name == p) {
       n = n1;
       break;
     }
  }

/*
  window.alert("Test: Form name=" + formname + " Global Var n= " + n) ;
  return;
*/

  var discstring  = document.forms[n].discount.value;
  var maxallowed  = document.forms[n].maxqty.value;
  var minallowed  = document.forms[n].minqty.value; 
  var qty2        = parseInt(document.forms[n].qty);
  var noofbreaks  = parseInt(discstring);
  var disclist    = new makeArray( (noofbreaks*2));


  price     = document.forms[n].PRICE.value;  //..Global price
  // price  = document.NumberForm.PRICE.value;


  if ( !(isANumber(qty3)) ) {
      document.forms[n].QTY.value = minallowed;
      window.alert("You must enter a number greater then 0 - no spaces or letters.");
      return;
    }

  if ( qty < 1 || qty3==' ' || qty3== null || qty3<=0) {
        document.forms[n].QTY.value = minallowed;    
        window.alert("You must enter a numeric quantity greater than 0...") ;
        //document.forms[n].QTY.focus();
        return;
   }

 // ** check must first be set by the htm file...
 if (check=='c'){  // Using check variable to make different checks...

   if ( (maxallowed >= 1) && (qty > maxallowed) )  {
        document.forms[n].QTY.value = maxallowed;
        window.alert("Sorry, you can only order a maximum of " + maxallowed) ;
        return;
   }
   if ( (minallowed > 1) && (qty < minallowed) )  {
        document.forms[n].QTY.value = minallowed;
        window.alert("Sorry, you must order a minumum quantity of " + minallowed) ;
        return;
   }

  return;
 } // ** end of check = c

  if ( (maxallowed > 1) && (qty > maxallowed) )  {
       document.forms[n].QTY.value = maxallowed;
        window.alert("Sorry, you can only order a maximum of " + maxallowed) ;
        return;
   }

  if ( (minallowed > 1) && (qty < minallowed) )  {
       document.forms[n].QTY.value = minallowed;
        window.alert("Sorry, you must order a minumum quantity of " + minallowed) ;
        return;
   }


  // ** Split price break at divider ","
  if (discstring) {  
     disclist = discstring.split(",");
    // window.alert("TEST LIST" + disclist);
    // return;
  }
  else { return; }

  if (!disclist ) {  // ...list of price breaks...
        return ; 
       }
  else {
         var i          = 2;
         var i2         = 0;
         var nextprice  = 0;
         i2             = noofbreaks;

         while (i2) {  // .. Loop through number of price breaks..

           nextprice = disclist[i+2];

           if (((qty >= disclist[i])  && (qty < nextprice))  ||
              ((qty>= disclist[i]) && (nextprice == -1))  ) {

               price = disclist[i+1];

               //  window.alert("TEST INSIDE LIST" + disclist+"\nDISC LIST 1:= "+disclist[1]+"\nPRICE= "+price+"\nQTY= "+qty+"\nDISC LIST I= "+disclist[i]+"\nDISC LIST i + 1= "+disclist[i+1]);
               //  return;

               //  document.NumberForm.PRICE.value = price;
               //  document.NumberForm.DISPTOTAL.value = price;

               //.. Now Set the html forms new values..
               document.forms[n].PRICE.value     = price;
               document.forms[n].DISPTOTAL.value = price;
               return;         
           }

           else {
	      i+= 2;
	      i2--;
           }

        } //.. end while i2

       // window.alert("TEST LIST" + disclist+"\nDISC LIST 1:= "+disclist[1]+"\nPRICE=           "+price+"\nQTY= "+qty+"\nDISC LIST I= "+disclist[i]+"\nDISC LIST i + 1= "+disclist[i+1]);
       // return;

       price = disclist[1];   // .. set base price again.  
       document.forms[n].DISPTOTAL.value = price;  // .. display price html
       document.forms[n].PRICE.value     = price;  
   }
}


// -->
