

function popup(filename,width,height)
{
result = window.open(filename, "popped1", "width="+ width + ", height="+height+", location=no, menubar=no, status=no, toolbar=no, scrollbars=yes, resizable=no");
if (result != null) html = "is not blocking";
else alert("Your Browser is blocking popups which is preventing a 3dCart window to appear.");
}

function popupsimple(filename,width,height)
{
var w = 480, h = 340;

if (document.all || document.layers) {
   w = screen.availWidth;
   h = screen.availHeight;
}

var leftPos = (w-width)/2, topPos = (h-height)/2;
result = window.open(filename, "popped1", "width="+ width + ", height="+height+",top=" + topPos + ",left=" + leftPos + ",location=no, menubar=no, status=no, toolbar=no, scrollbars=no, resizable=no");
if (result != null) html = "is not blocking";
else alert("Your Browser is blocking popups which is preventing a 3dCart window to appear.");
}

  var stocknum='';

function SearchAndReplace(Content, SearchFor, ReplaceWith) {

   var tmpContent = Content;
   var tmpBefore = new String();   
   var tmpAfter = new String();
   var tmpOutput = new String();
   var intBefore = 0;
   var intAfter = 0;

   if (SearchFor.length == 0)
      return;


   while (tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase()) > -1) {
   
      // Get all content before the match
      intBefore = tmpContent.toUpperCase().indexOf(SearchFor.toUpperCase());
      tmpBefore = tmpContent.substring(0, intBefore);
      tmpOutput = tmpOutput + tmpBefore;

      // Get the string to replace
      tmpOutput = tmpOutput + ReplaceWith;


      // Get the rest of the content after the match until
      // the next match or the end of the content
      intAfter = tmpContent.length - SearchFor.length + 1;
      tmpContent = tmpContent.substring(intBefore + SearchFor.length);

   }

   return tmpOutput + tmpContent;

}

function Len(str)
/***
IN: str - the string whose length we are interested in

RETVAL: The number of characters in the string
***/
{ return String(str).length; }


function Left(str, n)
/***
IN: str - the string we are LEFTing
n - the number of characters we want to return

RETVAL: n characters from the left side of the string
***/
{
if (n <= 0) // Invalid bound, return blank string
return "";
else if (n > String(str).length) // Invalid bound, return
return str; // entire string
else // Valid bound, return appropriate substring
return String(str).substring(0,n);
}


function Right(str, n)
/***
IN: str - the string we are RIGHTing
n - the number of characters we want to return

RETVAL: n characters from the right side of the string
***/
{
if (n <= 0) // Invalid bound, return blank string
return "";
else if (n > String(str).length) // Invalid bound, return
return str; // entire string
else { // Valid bound, return appropriate substring
var iLen = String(str).length;
return String(str).substring(iLen, iLen - n);
}
}


function Mid(str, start, len)
{
// Make sure start and len are within proper bounds
if (start < 0 || len < 0) return "";

var iEnd, iLen = String(str).length;
if (start + len > iLen)
iEnd = iLen;
else
iEnd = start + len;

return String(str).substring(start,iEnd);
}

function InStr(strSearch, charSearchFor)
{
for (i=0; i < Len(strSearch); i++)
{
if (charSearchFor == Mid(strSearch, i, 1))
{
return i;
}
}
return -1;
} 





function validateValues(what,alerting) {
    var valid = true;
    var fieldnamemod= new String(); 
    var fieldvaluemod= new String();  
    var checkBoxes = false;
    var checkboxChecked = false;
    var price=0.0000;    
    var radioButtons = false;
    var radioChecked = false;
    var imagename= new Image;
    var itemid;
  
	stocknum='';
   

	if (document.add.std_price==null)
	{
	//'Item has no options.
	
	}
	else
	{


	price=document.add.std_price.value;
        //itemid=document.add.itemid.value;

    for (var i=0, j=what.elements.length; i<j; i++) {
        myType = what.elements[i].type;
        

        
        fieldnamemod=what.elements[i].name;
if (fieldnamemod!='recipientselect') 
{
        var field_array;
 
 		//alert(fieldnamemod);
 		// IMAGE dropdown's
 		//alert(fieldnamemod + fieldnamemod.indexOf("di_"));
 		if (fieldnamemod.indexOf("di_") > -1) 
 		{
 		
 		// Have its own image per drop down
 		SetSrc(what,"img_" + fieldnamemod,GetValue(what,"image_" + what.elements[i].options[what.elements[i].selectedIndex].value));
 		
 		// change main image
 		//SetSrc(what,"large",GetValue(what,"image_" + what.elements[i].options[what.elements[i].selectedIndex].value));
 		
 		}

        if (myType == 'radio') {
            radioButtons = true;
           if (what.elements[i].checked)
           {radioChecked = true;

           
           price=eval(price)+eval(GetValue(what,"price_" + what.elements[i].value));
           itemid=(itemid)+GetValue(what,"OptID_" + what.elements[i].value);


           stocknum=stocknum + what.elements[i].value + "#";
           }
        }
        if (myType == 'checkbox') {
            checkBoxes = true;
            if (what.elements[i].checked)
            {
            
            if (fieldnamemod>"")

	     price=eval(price)+eval(GetValue(what,"price_" + fieldnamemod));
	     itemid=(itemid)+GetValue(what,"OptID_" + fieldnamemod);           
            }
        }
        if (myType == 'hidden' || myType == 'password' || myType == 'text' || myType == 'textarea')
            if (what.elements[i].value == what.elements[i].defaultValue) valid = false;
        if (myType == 'select-one' || myType == 'select-multiple')
            {
            //if (what.elements[i].selectedIndex == 0) valid = false;
            
            if (what.elements[i].options[what.elements[i].selectedIndex].value>"")
            {
            //alert(what.elements[i].options[what.elements[i].selectedIndex].value + '::' + GetValue(what,"price_" + what.elements[i].options[what.elements[i].selectedIndex].value));

	    fieldvaluemod=what.elements[i].options[what.elements[i].selectedIndex].value; 
	    field_array=fieldvaluemod.split(":::");    
	    if (field_array[0]!=undefined){ stocknum=stocknum + field_array[1] + "#"}
              	    

	    if (GetValue(what,"price_" + what.elements[i].options[what.elements[i].selectedIndex].value)!=undefined)
		{
//alert((GetValue(what,"price_" + what.elements[i].options[what.elements[i].selectedIndex].value)));
	price=eval(price)+eval(GetValue(what,"price_" + what.elements[i].options[what.elements[i].selectedIndex].value));	
		}	
	    
	    
	    itemid=itemid+''+GetValue(what,"OptID_" + what.elements[i].options[what.elements[i].selectedIndex].value);

  
			}
			
			
            }
    }
}
    if ((checkBoxes && !checkboxChecked) || (radioButtons && !radioChecked)) valid = false;

    //if (!valid)
    //        alert('Form not completely filledxxx');

//	alert(price + 'a');
    	changeprice(price);
	changeid(itemid);

	changepricematrix(price);
	if (alerting==1)
	{	
	return true; //check_stock(what,stocknum);
	}
    //return valid;
    }
    
}


function changepricematrix(price)
{
var item_baseprice;
var discounted_price=0;
var item_optionprice=0;

item_baseprice=price;

//alert(item_baseprice);

if (typeof(price_divs)!='undefined')
{
item_pricedivs=price_divs.split(",");
item_pctarr=price_pct.split(",");
item_discarr=price_val.split(",");
item_plevel=price_mins.split(",");


if (price_optsdisc==0){
//item_optionprice=0; //item_baseprice-document.add.std_price.value;
item_optionprice=item_baseprice-document.add.std_price.value;
//alert(item_optionprice);
//item_baseprice=document.add.std_price.value;
item_baseprice=price-item_optionprice; //document.add.std_price.value;
//alert(item_baseprice);
}else
{
item_optionprice=0;
}



for (pricediv in item_pricedivs)
{
//alert(eval(GetValue(document.add,"qty-0")));

if (eval(item_plevel[pricediv])<=eval(GetValue(document.add,"qty-0")))
{

if (item_pctarr[pricediv]==1)
{
//alert(item_baseprice +'d');
discounted_price=item_baseprice-(item_baseprice*(item_discarr[pricediv]/100));
}
else
{
discounted_price=item_baseprice-item_discarr[pricediv];
}

//alert(discounted_price)


discounted_price=parseFloat(discounted_price+item_optionprice);
changecontent("price2",formatCurrency(discounted_price*eval(GetValue(document.add,"qty-0"))))

}

if (discounted_price==0) discounted_price=eval(item_baseprice)+eval(item_optionprice);

changecontent("price2",formatCurrency(discounted_price*eval(GetValue(document.add,"qty-0"))))


//alert(eval(GetValue(document.add,"qty-0")));
}



//alert("b");

}
else
{

}

}



function GetValue(formx,name) {
//alert(name);
var i;
    for(i=0;i<formx.elements.length;i++) {

     
    if(formx.elements[i].name==name) {

	 if (formx.elements[i].type == 'select-one')
	{
	if (formx.elements[i].options==null) { return 999; }
//alert(eval(formx.elements[i].options[formx.elements[i].selectedIndex].text) + '|' + name);

	if (formx.elements[i].options[formx.elements[i].selectedIndex].value=='')
{
		return formx.elements[i].options[formx.elements[i].selectedIndex].text;
}
else
{
		return formx.elements[i].options[formx.elements[i].selectedIndex].value;
}

	}
	else
	{
        return formx.elements[i].value;
	}

      }
    } 
  }

function SetSrc(formx,sname,sourcename) {

imgs=document.getElementsByTagName('img');
 for(i=0;i<imgs.length;i++)
 {
// alert(imgs[i].name + "-" + sname);	
// test if the class 'roll' exists
  if(imgs[i].name==sname)
  {
  //alert("XX");
   imgs[i].src=sourcename;
   
  }
 }




  }


function recalculate()
{                    	 
}


function changecontent(fieldname,content1)
{
var txt=content1;

  if (document.getElementById)
  {
	if (document.getElementById(fieldname)!=null)
	{
     	document.getElementById(fieldname).innerHTML = txt;
	document.getElementById(fieldname).style.visibility = 'visible';
	}
  }
  else if (document.all)
  {
	if (document.all[fiendname]!=null)
	{
    document.all[fiendname].innerHTML = txt;
    document.all[fieldname].style.visibility = 'visible';
	}
  }
  else if (document.layers)
  {
	
	if (document.layers[fieldname]!=null)
	{
     document.layers[fieldname].document.open();
     document.layers[fieldname].document.write(txt);
     document.layers[fieldname].document.close();
     document.layers[fieldname].visibility = 'show';
	}
  }
}


function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}
function changeid(itemid)
{
var txt=itemid
changecontent('itemid',txt);
}


function changeprice(price)
{
var txt=formatCurrency(price);
//var txt=(price);
changecontent('price',txt);
}

function formatCurrency(num) {
return '$' + FormatNumber(num,4,true,false,true) +'';
num = num.toString().replace(/\$|\,/g,'');

if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));

num = Math.floor(num*100+0.50000000001);
cents = num%100;

num = Math.floor(num/100).toString();

if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
//alert(cents)
return (((sign)?'':'-') + '$' + num + '.' + cents);
}


function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}



function getSingleInputValue(obj,use_default,delimiter) {
	switch(obj.type){
		case 'radio': case 'checkbox': return(((use_default)?obj.defaultChecked:obj.checked)?obj.value:null);
		case 'text': case 'hidden': case 'textarea': return(use_default)?obj.defaultValue:obj.value;
		case 'password': return((use_default)?null:obj.value);
		case 'select-one':
			if (obj.options==null) { return null; }
			if(use_default){
				var o=obj.options;
				for(var i=0;i<o.length;i++){if(o[i].defaultSelected){return o[i].value;}}
				return o[0].value;
				}
			if (obj.selectedIndex<0){return null;}
			return(obj.options.length>0)?obj.options[obj.selectedIndex].value:null;
		case 'select-multiple': 
			if (obj.options==null) { return null; }
			var values=new Array();
			for(var i=0;i<obj.options.length;i++) {
				if((use_default&&obj.options[i].defaultSelected)||(!use_default&&obj.options[i].selected)) {
					values[values.length]=obj.options[i].value;
					}
				}
			return (values.length==0)?null:commifyArray(values,delimiter);
		}
	alert("FATAL ERROR: Field type "+obj.type+" is not supported for this function");
	return null;
	}




//-------------------------------------------------------------------
// getInputValue(input_object[,delimiter])
//   Get the value of any form input field
//   Multiple-select fields are returned as comma-separated values, or
//   delmited by the optional second argument
//   (Doesn't support input types: button,file,reset,submit)
//-------------------------------------------------------------------
function getInputValue(obj,delimiter) {
	var use_default=(arguments.length>2)?arguments[2]:false;
	if (isArray(obj) && (typeof(obj.type)=="undefined")) {
		var values=new Array();
		for(var i=0;i<obj.length;i++){
			var v=getSingleInputValue(obj[i],use_default,delimiter);
			if(v!=null){values[values.length]=v;}
			}
		return commifyArray(values,delimiter);
		}
	return getSingleInputValue(obj,use_default,delimiter);
	}

// isArray(obj)
// Returns true if the object is an array, else false
//-------------------------------------------------------------------
function isArray(obj){return(typeof(obj.length)=="undefined")?false:true;}


