var discnt = 0;   // no default percent discount

var coupons = new Array (  // place to put coupon codes
  "ocfamily",                 // 1st coupon value - comma seperated
  "studio",                 // 2nd coupon value - add all you want
  "coup3"                  // 3rd coupon value
);
var coupdc  = new Array (  // place to put discounts for coupon vals
  15,
  15,
  0
);
var coupval = "(blanket)"; // what user entered as coupon code

function ChkCoup () {      // check user coupon entry
var i;
  discnt = 0;              // assume the worst
  for (i=0; i<coupons.length; i++) {
    if (coupval.toLowerCase() == coupons[i]) {
      discnt = coupdc[i];  // remember the discount amt
      alert ("Valid coupon number! \n\n" + discnt + "% discount now in effect.");
	CanvasForm.coupcode.value=coupval;
	PopForm.coupcode.value=coupval;
	NotecardsForm.coupcode.value=coupval;
	ReadForm(CanvasForm);
	ReadForm(PopForm);
	ReadForm(NotecardsForm);             
      return;
    }
  }
  alert ("'" + coupval + "'  is not a valid code!");
  discnt = 0
	ReadForm(CanvasForm);
	ReadForm(PopForm);
	ReadForm(NotecardsForm);               
}

function checkLength(form){
    if (form.description.value.length > 20){
        alert("Text too long. Must be 20 characters or less");
        return false;
    }
    return true;
}

function Require(obForm,szFields)
    {
      var fields = szFields.split(",")
      var szMissing= new Array();
      for (x=0;x<fields.length;x++) {
        if (obForm.elements[fields[x]].value.length==0) {
           szMissing[szMissing.length]=new String(fields[x]);
        }
      }
      if (szMissing.length) {
        alert("The field"+((szMissing.length>1)?"s ":" ")+szMissing.join(",")+" must be filled in first");
        return false;
      }
      if (obForm.name == "MyForm") return ShowProgress();
      return true;
    }

  function ValidateSize(obj,noteok, c8x10ok, c11x14ok)
    {
     if(obj.name=="cartview") return true;
     
     if(document.location.search=="")
	 	{
			alert("Please upload a file before adding any items to your cart.");
			return false;
		}
     if(noteok==0 && obj.name=="NotecardsForm" )
	 	{
			alert("The image you've sent us is too small for notecards.  Please send us a larger file.");
			return false;
		}	
     if(c11x14ok==0 && obj.name=="CanvasForm" )
	 	{
	 		
	 		pos = obj.Size.selectedIndex;
			if(String(obj.Size.options[pos].value).substring(0,5)=="11x14")
			{
				alert("The image you've sent us is too small for an 11 x 14 canvas print.  Please send us a larger file.");
				return false;
			}
		}		
     if(c8x10ok==0 && obj.name=="CanvasForm" )
	 	{
	 		
	 		pos = obj.Size.selectedIndex;
			if(String(obj.Size.options[pos].value).substring(0,4)=="8x10")
			{
				alert("The image you've sent us is too small for an 8 x 10 canvas print.  Please send us a larger file.");
				return false;
			}
		}						
	return true;
    }    

function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < .995) rnd = 1;  // for old Netscape browsers
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}


function ReadForm (obj1) { // process selects
var i,j,amt=0,des="",obj,pos,tok,val,op1a="",op1b="",op2a="",op2b="",itmn="";
var ary = new Array ();

//obj1.item_number.value = getsession();
if (obj1.baseamt) amt  = obj1.baseamt.value*1.0;  // base amount
if (obj1.basedes) des  = obj1.basedes.value;  // base description
if (obj1.baseon0) op1a = obj1.baseon0.value;  // base options
if (obj1.baseos0) op1b = obj1.baseos0.value;
if (obj1.baseon1) op2a = obj1.baseon1.value;
if (obj1.baseos1) op2b = obj1.baseos1.value;
if (obj1.baseitn) itmn = obj1.baseitn.value;
  

  for (i=0; i<obj1.length; i++) {     // run entire form
    obj = obj1.elements[i];           // a form element
    //alert(obj.type + " " + obj.name);
    if ((obj.type == "text" || obj.type == "textarea") && obj.name != "tot"  && obj.name != "quantity" 
            ){ 
      val = obj.value;            // get the data
            
      //if (tst == true &&          // only on final submit
      //    val == "") {            // force an entry
      //  alert ("Enter data for " + obj.name);
      //  return false;
      //}
      tag = obj.name.substring (obj.name.length-2);  // get flag
	  //alert(tag + " " + obj.type + " " + obj.name);      
      if      (tag == "1a") op1a = Trim(op1a + " " + val);
      else if (tag == "1b") op1b = Trim(op1b + " " + val);
      else if (tag == "2a") op2a = Trim(op2a + " " + val);
      else if (tag == "2b") op2b = Trim(op2b + " " + val);
      else if (des.length == 0) des = Trim(val);
      else des = Trim(des + ", " + val);
      
       //coupon 
       //alert(discnt);
		//if (discnt > 0) amt = Dollar (amt - (amt * discnt/100.0));
		//if (des.length == 0) des = des + ", " + discnt + "% dis, COUP = " + coupval;
		//end coupon   
    }
    //if (obj.type == "select-one" ) {
    if (obj.type == "select-one") {   // just get selects
      if (obj.name == "quantity" ||   // don't mess with these
          obj.name == "amount") continue;    
	
      pos = obj.selectedIndex;        // which option selected
      val = obj.options[pos].value;   // selected value
      ary = val.split (" ");          // break apart
      
      if (val=="badsize") 
		{
			alert("Please select a valid print size.");
			return false;
		}
	
      for (j=0; j<ary.length; j++) {  // look at all items
// first we do single character tokens...
        if (ary[j].length < 2) continue;
        tok = ary[j].substring (0,1); // first character
        val = ary[j].substring (1);   // get data
        if (tok == "@") amt = val * 1.0;
        if (tok == "+") amt = amt + val*1.0;
        if (tok == "%") amt = amt + (amt * val/100.0);
        if (tok == "#") {             // record item number
          //if (obj1.item_number) obj1.item_number.value = val;
          //if (obj1.item_number) obj1.item_number.value = Math.floor(Math.random()*12345677);
          ary[j] = "";                // zap this array element
         
        }
// Now we do 3-character tokens...
        if (ary[j].length < 4) continue;
        tok = ary[j].substring (0,3); // first 3 chars
        val = ary[j].substring (3);   // get data
        if (tok == "s1=") {           // value for shipping
          if (obj1.shipping)  obj1.shipping.value  = val;
          ary[j] = "";                // clear it out
        }
        if (tok == "s2=") {           // value for shipping2
          if (obj1.shipping2) obj1.shipping2.value = val;
          ary[j] = "";                // clear it out
          
        }
      }
      val = ary.join (" ");           // rebuild val with what's left

      if (obj.name == "on0" ||        // let these go where they want
          obj.name == "os0" ||
          obj.name == "on1" ||
          obj.name == "os1") continue;

      tag = obj.name.substring (obj.name.length-2);  // get flag
      if      (tag == "1a") op1a = Trim(op1a + " " + val); // stuff data
      else if (tag == "1b") op1b = Trim(op1b + " " + val);
      else if (tag == "2a") op2a = Trim(op2a + " " + val);
      else if (tag == "2b") op2b = Trim(op2b + " " + val);
      else if (tag == "3i") itmn = Trim(itmn + " " + val);
      else if (des.length == 0) des = Trim(val);
      else des = Trim(des + ", " + val);
      
             //coupon 
       //alert(discnt);
		//end coupon  
    }
  }
if (op1b=="") op1b="BLANK";
if (op2b=="") op2b="BLANK";
// Now summarize stuff we just processed, above
  //if (!IsNumeric(obj1.quantity.value)) obj1.quantity.value="1";
  if (op1a.length > 0) obj1.on0.value = op1a;  // stuff it away
  if (op1b.length > 0) obj1.os0.value = op1b;
  if (obj1.on1){
	if (op2a.length > 0) obj1.on1.value = op2a;
	if (op2b.length > 0) obj1.os1.value = op2b;
    }
  //if (itmn.length > 0) Math.floor(Math.random()*12345677); //obj1.item_number.value = itmn;
  obj1.item_name.value = des;// + " #" + Math.floor(Math.random()*12345677);
  //tempamt=amt;
  //tempamt*=obj1.quantity.value;

  if (String(des).substring(0,4)=="Note"){
	if (Trim(NotecardsForm.returnaddress_1b.value).length > 0) {
		amt+=10;
		//tempamt+=10;
		}
	if (Trim(NotecardsForm.personalizednote_2b.value).length > 0) {
		amt+=10;
		//tempamt+=10;
		}
	}
  //obj1.amount.value = Dollar (amt);
  //if (obj1.tot) obj1.tot.value = "$" + Dollar (tempamt);
	if (discnt > 0) amt = Dollar (amt - (amt * discnt/100.0));
	if (des.length == 0) des = des + ", " + discnt + "% dis, COUP = " + coupval;
  
  obj1.amount.value = Dollar (amt);
  if (obj1.tot) obj1.tot.value = "$" + Dollar (amt);//*obj1.quantity.value);  
  
 }

function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function


function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}