
var color_obj;

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;    
    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;
	
		if (document.getElementById("itemid")!=null)
			itemid=document.add.itemid.value;
		else
			itemid="";
		
		
		for (var i=0, j=what.elements.length; i<j; i++) 
		{
			myType = what.elements[i].type;
			if (myType != null && myType != undefined)
			{
				fieldnamemod=what.elements[i].name;
				if (fieldnamemod!='recipientselect' && fieldnamemod!='qty-0')
				{
					var field_array;
				
 					// 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));

                        //1==2: don't count the price, because it'll be considered below when the dropdown option is parsed.
						if (1==2 && 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] + "#"}
							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 (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].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] + "#"}
							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;
		changeprice(price);
		changeid(itemid);
		if (alerting==1)
		{	
			var d = document;

			$("#availability").hide();
			$("#loading_availability").show();
			
			stat = check_stock(what,stocknum);
			var value = $("#"+color_obj).attr("value");
			var tmp = value.split(":::");
  			var opt = "color-"+tmp[1];
  			var name = $("#"+opt).html();
  			
  			if(stat != false)
  			{
			  //alert(stat);
			  if ($(".pushbutton[rel="+value+"]").length <= 0)
			  {
			  var color_link = d.createElement("a");
			  $(color_link).addClass("pushbutton");
			  $(color_link).addClass("pushbutton-color");
			  $(color_link).attr("href","javascript:void(0);");
			  $(color_link).attr("rel",value);
			  $(color_link).html(name);
			  
			  $("#color").append(color_link);
			  }
			}
			
  		}
		//return valid;
    } 
}



function changeid(itemid)
{
	var txt=itemid
	changecontent('product_id',txt);
}


function GetValue(formx,name) {
//alert(name);
var i;
    for(i=0;i<formx.elements.length;i++) {
      if(formx.elements[i].name==name) {
        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 changeprice(price)
{
var txt=formatCurrency(price);
changecontent('price',txt);
}

function formatCurrency(intNumber) {

	var currencySymbol;	//This variable gets the value of the currency_symbol variable declared on listing_x page template...
	var decimalPlaces;  //This variable gets the value of the prod_decimal_places variable declared on listing_x page template...
	try
	{
		currencySymbol = currency_symbol;
	}
	catch (err)
	{
		currencySymbol = '$';	//If there's no variable declared, uses '$' as default.
	}
	try
	{
		decimalPlaces = parseInt(prod_decimal_places);
		if (decimalPlaces == 0)
			decimalPlaces = 2;
	}
	catch (err)
	{
		decimalPlaces = 2;	//If there's no variable declared, uses '$' as default.
	}	
	
	intNumber = parseFloat(intNumber);	//Remove zeros on the right side
	intNumber = intNumber.toString();

	if (decimalPlaces > 2)
	{
		if (decimalPlaces - (intNumber.length - (intNumber.indexOf(".") + 1)) > 0)
			decimalPlaces = intNumber.length - (intNumber.indexOf(".") + 1);
		if (decimalPlaces < 2) 
			decimalPlaces = 2;
	}
	
	intNumber = parseFloat(intNumber); //Convert to float again so the method toFixed can be used
		
	//New version
	return currencySymbol + intNumber.toFixed(decimalPlaces);
	

	//Old version
	intNumber = intNumber.toString().replace(/\$|\,/g,'');
	if(isNaN(intNumber))
		intNumber = "0";
	sign = (intNumber == (intNumber = Math.abs(intNumber)));
	intNumber = Math.floor(intNumber*100+0.50000000001);
	cents = intNumber%100;
	intNumber = Math.floor(intNumber/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((intNumber.length-(1+i))/3); i++)
	intNumber = intNumber.substring(0,intNumber.length-(4*i+3))+','+
	intNumber.substring(intNumber.length-(4*i+3));
	return (((sign)?'':'-') + currencySymbol + intNumber + '.' + cents);
}
