
function CCM_ChangeCurrency(currency, defaultcurrency){
	var currentHref = document.location.href;
	var newHref = document.location.href;
	var theCurrency = (currency-0);
	if(!CCM_ValidateCurrency(theCurrency)){
		theCurrency = defaultcurrency;
	}
	// no querystring
	if(currentHref.indexOf("?") < 0){
		newHref = currentHref + "?pricecat=" + theCurrency;
	}
	// no pricecat in querystring
	else if(currentHref.toLowerCase().indexOf("pricecat=") < 0){
		newHref = currentHref + "&pricecat=" + theCurrency;
	}
	// replace pricecat in querystring
	else{
		newHref = currentHref.replace(/pricecat\=[0-9]+/gi, "pricecat=" + theCurrency);
	}
	// redirect to update currency
	document.location.href = newHref;
}	
function CCM_ChangeLanguage(_lang){
	var currentHref = document.location.href;
	var newHref = document.location.href;
	var theLanguage = _lang;
	// no querystring
	if(currentHref.indexOf("?") < 0){
		newHref = currentHref + "?Lang=" + theLanguage;
	}
	// no pricecat in querystring
	else if(currentHref.toLowerCase().indexOf("lang=") < 0){
		newHref = currentHref + "&lang=" + theLanguage;
	}
	// replace pricecat in querystring
	else{
		newHref = currentHref.replace(/lang\=[A-Z]{2}\-[A-Z]{2}/gi, "Lang=" + theLanguage);
	}
	// redirect to update currency
	document.location.href = newHref;
}	

function CCM_ValidateCurrency(currency){
	if(currency > 0 && currency <= 9){
		return true;
	}else{
		return false;
	}
}

// dynamic css
function CCM_AppendStyleSheet(placeholder, src){
	// check for source css file
	if(!src){return;}
	// browser sniffer
	var I=(navigator.appName=="Microsoft Internet Explorer") && ((navigator.userAgent.toLowerCase().indexOf("Mac")!=-1) || (navigator.appVersion.toLowerCase().indexOf("Mac")!=-1));
	var A=(navigator.userAgent.toLowerCase().indexOf("safari")!=-1);
	// write out css file based on browser type
	if (I || A){
		document.write("<"+"link"+" rel='stylesheet' type='text/css' href='"+src+"'>");
	}else{
		var cssLinkElement = document.createElement("link");
		cssLinkElement.rel = "stylesheet";
		cssLinkElement.type = "text/css";
		cssLinkElement.href = src;
		document.getElementById(placeholder).appendChild(cssLinkElement);
	}
}

// radpanelbar collapse prevention
function CCM_PreventCollapse(item){
	if(item.Parent == item.ParentRadPanelbar){
		document.location.href = item.NavigateUrl
		return false;
	}
}

// radpanelbar expand prevention
function CCM_PreventExpand(item){
	if(item.Parent == item.ParentRadPanelbar){
		document.location.href = item.NavigateUrl
		return false;
	}
}


// Get Document Object
function CCM_getObj(name){
	if(document.getElementById){
		return document.getElementById(name);
	}else if(document.all){
		return document.all[name];
	}else if(document.layers && document.layers[name] != null){
		return CCM_getObjNN4(document, name);
	}else{
		return false;
	}
}

// Get Document Style Object
function CCM_getObjStyle(name){
	if(document.getElementById){
		return document.getElementById(name).style;
	}else if(document.all){
		return document.all[name].style;
	}else if(document.layers){
		return CCM_getObjNN4(document, name);
	}
}

// Get NN4 Document Object
function CCM_getObjNN4(obj, name){
	var x = obj.layers;
	var foundLayer;
	for(var i=0;i<x.length;i++){
		if(x[i].id == name){
			foundLayer = x[i];
		}else if (x[i].layers.length){
			var tmp = CCM_getObjNN4(x[i],name);
		}
		if(tmp){
			foundLayer = tmp;
		}
	}
	if(foundLayer){
		return foundLayer;
	}else{
		return false;
	}
}

// Flash Player Version Detection - Rev 1.5
// Detect Client Browser type
// Copyright(c) 2005-2006 Adobe Macromedia Software, LLC. All rights reserved.
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

function CCM_ControlVersion()
{
	var version;
	var axo;
	var e;

	// NOTE : new ActiveXObject(strFoo) throws an exception if strFoo isn't in the registry

	try {
		// version will be set for 7.X or greater players
		axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
		version = axo.GetVariable("$version");
	} catch (e) {
	}

	if (!version)
	{
		try {
			// version will be set for 6.X players only
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
			
			// installed player is some revision of 6.0
			// GetVariable("$version") crashes for versions 6.0.22 through 6.0.29,
			// so we have to be careful. 
			
			// default to the first public version
			version = "WIN 6,0,21,0";

			// throws if AllowScripAccess does not exist (introduced in 6.0r47)		
			axo.AllowScriptAccess = "always";

			// safe to call for 6.0r47 or greater
			version = axo.GetVariable("$version");

		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 4.X or 5.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = axo.GetVariable("$version");
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 3.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
			version = "WIN 3,0,18,0";
		} catch (e) {
		}
	}

	if (!version)
	{
		try {
			// version will be set for 2.X player
			axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
			version = "WIN 2,0,0,11";
		} catch (e) {
			version = -1;
		}
	}
	
	return version;
}

// JavaScript helper required to detect Flash Player PlugIn version information
function CCM_GetSwfVer(){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	var flashVer = -1;
	
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
			var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;			
			var descArray = flashDescription.split(" ");
			var tempArrayMajor = descArray[2].split(".");
			var versionMajor = tempArrayMajor[0];
			var versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
			var versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
			var flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	else if ( isIE && isWin && !isOpera ) {
		flashVer = CCM_ControlVersion();
	}	
	return flashVer;
}

// When called with reqMajorVer, reqMinorVer, reqRevision returns true if that version or greater is available
function CCM_DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision)
{
	versionStr = CCM_GetSwfVer();
	if (versionStr == -1 ) {
		return false;
	} else if (versionStr != 0) {
		if(isIE && isWin && !isOpera) {
			// Given "WIN 2,0,0,11"
			tempArray         = versionStr.split(" "); 	// ["WIN", "2,0,0,11"]
			tempString        = tempArray[1];			// "2,0,0,11"
			versionArray      = tempString.split(",");	// ['2', '0', '0', '11']
		} else {
			versionArray      = versionStr.split(".");
		}
		var versionMajor      = versionArray[0];
		var versionMinor      = versionArray[1];
		var versionRevision   = versionArray[2];

        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
		if (versionMajor > parseFloat(reqMajorVer)) {
			return true;
		} else if (versionMajor == parseFloat(reqMajorVer)) {
			if (versionMinor > parseFloat(reqMinorVer))
				return true;
			else if (versionMinor == parseFloat(reqMinorVer)) {
				if (versionRevision >= parseFloat(reqRevision))
					return true;
			}
		}
		return false;
	}
}

function CCM_AddExtension(src, ext)
{
  if (src.indexOf('?') != -1)
    return src.replace(/\?/, ext+'?'); 
  else
    return src + ext;
}

function CCM_Generateobj(objAttrs, params, embedAttrs) 
{ 
    var str = '';
    if (isIE && isWin && !isOpera)
    {
  		str += '<object ';
  		for (var i in objAttrs)
  			str += i + '="' + objAttrs[i] + '" ';
  		for (var i in params)
  			str += '><param name="' + i + '" value="' + params[i] + '" /> ';
  		str += '></object>';
    } else {
  		str += '<embed ';
  		for (var i in embedAttrs)
  			str += i + '="' + embedAttrs[i] + '" ';
  		str += '> </embed>';
    }

    document.write(str);
}

function CCM_RunContent(){
  var ret = 
    CCM_GetArgs
    (  arguments, ".swf", "movie", "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
     , "application/x-shockwave-flash"
    );
  CCM_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
}

function CCM_GetArgs(args, ext, srcParamName, classid, mimeType){
  var ret = new Object();
  ret.embedAttrs = new Object();
  ret.params = new Object();
  ret.objAttrs = new Object();
  for (var i=0; i < args.length; i=i+2){
    var currArg = args[i].toLowerCase();    

    switch (currArg){	
      case "classid":
        break;
      case "pluginspage":
        ret.embedAttrs[args[i]] = args[i+1];
        break;
      case "src":
      case "movie":	
        args[i+1] = CCM_AddExtension(args[i+1], ext);
        ret.embedAttrs["src"] = args[i+1];
        ret.params[srcParamName] = args[i+1];
        break;
      case "onafterupdate":
      case "onbeforeupdate":
      case "onblur":
      case "oncellchange":
      case "onclick":
      case "ondblClick":
      case "ondrag":
      case "ondragend":
      case "ondragenter":
      case "ondragleave":
      case "ondragover":
      case "ondrop":
      case "onfinish":
      case "onfocus":
      case "onhelp":
      case "onmousedown":
      case "onmouseup":
      case "onmouseover":
      case "onmousemove":
      case "onmouseout":
      case "onkeypress":
      case "onkeydown":
      case "onkeyup":
      case "onload":
      case "onlosecapture":
      case "onpropertychange":
      case "onreadystatechange":
      case "onrowsdelete":
      case "onrowenter":
      case "onrowexit":
      case "onrowsinserted":
      case "onstart":
      case "onscroll":
      case "onbeforeeditfocus":
      case "onactivate":
      case "onbeforedeactivate":
      case "ondeactivate":
      case "type":
      case "codebase":
      case "id":
        ret.objAttrs[args[i]] = args[i+1];
        break;
      case "width":
      case "height":
      case "align":
      case "vspace": 
      case "hspace":
      case "class":
      case "title":
      case "accesskey":
      case "name":
      case "tabindex":
        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
        break;
      default:
        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
    }
  }
  ret.objAttrs["classid"] = classid;
  if (mimeType) ret.embedAttrs["type"] = mimeType;
  return ret;
}

/* Standard Attributes */

var ATTRIBUTES = new Array();
var LABELS = new Array();
var PRICE = new Array();
var DEFAULTPRICE = new Array();
var ADDTOCARTBTN = new Array();
var ATTRFLDS = new Array();
var ATTRLBLS = new Array();
var QTYFLDS = new Array();
var SETTINGS = new Array();
	
function loadAttribute1(productid){
	var ddlAttribute1 = $(ATTRFLDS[productid].attribute1);
	var ddlAttribute2 = $(ATTRFLDS[productid].attribute2);
	var ddlAttribute3 = $(ATTRFLDS[productid].attribute3);
	var ddlAttribute4 = $(ATTRFLDS[productid].attribute4);
	var lblAttribute1 = ATTRLBLS[productid].attribute1;
	var lblAttribute2 = ATTRLBLS[productid].attribute2;
	if(ddlAttribute1){
		ddlAttribute1.options.length = 0;
		ddlAttribute1.options[0] = new Option("", "");
		if(ddlAttribute2){
			ddlAttribute2.options.length = 0;
			ddlAttribute2.options[0] = new Option("", "");
		}
		if(ddlAttribute3){
			ddlAttribute3.options.length = 0;
			ddlAttribute3.options[0] = new Option("", "");
		}
		if(ddlAttribute4){
			ddlAttribute4.options.length = 0;
			ddlAttribute4.options[0] = new Option("", "");
		}
		if(ATTRIBUTES[productid].length > 0){
			var moreThan1Attribute = false;
			var optionIndexStart = 0;
			if(!SETTINGS[productid].oneandone){
				ddlAttribute1.options[0].text = replaceSubstring(SETTINGS[productid].select, "<replacementlabel>", lblAttribute1);
				optionIndexStart++;
			}else{
				var label = new Element('div');
				label.setStyle({
					textAlign: "left",
					overflow: "hidden", 
					fontSize: "14px",
					padding: "0 0 20px 0"
				});
				label = $(label);
				label.update(ATTRIBUTES[productid][0].originalvalue);
				ddlAttribute1.insert({before: label});
				$("UI_ProductAttributes" + productid).setStyle({
					height: "21px", 
					width: "210px", 
					overflow: "hidden", 
					border: "solid 1px #DCD1C3", 
					padding: 0,
					margin: "0 auto"
				});
			}
			//alert(ATTRIBUTES[productid][0].text);
			for(var a=0; a<=ATTRIBUTES[productid].length-1; a++){
				ddlAttribute1.options[(a+optionIndexStart)] = new Option(ATTRIBUTES[productid][a].text, ATTRIBUTES[productid][a].value);
				if(!moreThan1Attribute && ATTRIBUTES[productid][a].attribs.length > 0){
					moreThan1Attribute = true;
				}
			}
			if(moreThan1Attribute){
				//ddlAttribute1.observe("change", loadAttribute2);
				if(ddlAttribute2 && lblAttribute1){
					ddlAttribute2.options[0].text = replaceSubstring(replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute1), "<replacementlabel>", lblAttribute2), "<replacementlabel>", lblAttribute2);
				}
				if(ddlAttribute3 && lblAttribute1){
					ddlAttribute3.options[0].text = replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute1), "<replacementlabel>", lblAttribute2);
				}
				if(ddlAttribute4 && lblAttribute1){
					ddlAttribute4.options[0].text = replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute1), "<replacementlabel>", lblAttribute2);
				}
			}else{
				if(ddlAttribute2){
					ddlAttribute2.options[0].text = "";
				}
				if(ddlAttribute3){
					ddlAttribute3.options[0].text = "";
				}
				if(ddlAttribute4){
					ddlAttribute4.options[0].text = "";
				}
			}
		}
	}
}

function loadAttribute2(productid){
	var ddlAttribute1 = $(ATTRFLDS[productid].attribute1);
	var ddlAttribute2 = $(ATTRFLDS[productid].attribute2);
	var ddlAttribute3 = $(ATTRFLDS[productid].attribute3);
	var ddlAttribute4 = $(ATTRFLDS[productid].attribute4);
	var lblAttribute1 = ATTRLBLS[productid].attribute1;
	var lblAttribute2 = ATTRLBLS[productid].attribute2;
	var lblAttribute3 = ATTRLBLS[productid].attribute3;
	var attrib1val = ddlAttribute1.options[ddlAttribute1.selectedIndex].value;
	if(ddlAttribute2 && lblAttribute1){
		ddlAttribute2.options.length = 0;
		ddlAttribute2.options[0] = new Option(replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute1), "<replacementlabel>", lblAttribute2), "");
		if(ddlAttribute3 && lblAttribute1){
			ddlAttribute3.options.length = 0;
			ddlAttribute3.options[0] = new Option(replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute1), "<replacementlabel>", lblAttribute2), "");
		}
		if(ddlAttribute4 && lblAttribute1){
			ddlAttribute4.options.length = 0;
			ddlAttribute4.options[0] = new Option(replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute1), "<replacementlabel>", lblAttribute2), "");
		}
		for(var a=0; a<=ATTRIBUTES[productid].length-1; a++){
			if(ATTRIBUTES[productid][a].value == attrib1val){
				if(ATTRIBUTES[productid][a].attribs.length > 0){
					var moreThan2Attribute = false;
					ddlAttribute2.options[0].text = replaceSubstring(SETTINGS[productid].select, "<replacementlabel>", lblAttribute2);
					for(var b=0; b<=ATTRIBUTES[productid][a].attribs.length-1; b++){
						ddlAttribute2.options[(b+1)] = new Option(ATTRIBUTES[productid][a].attribs[b].text, ATTRIBUTES[productid][a].attribs[b].value);
						if(!moreThan2Attribute && ATTRIBUTES[productid][a].attribs[b].attribs.length > 0){
							moreThan2Attribute = true;
						}
					}
					if(moreThan2Attribute){
						//ddlAttribute2.observe("change", loadAttribute3);
						if(ddlAttribute3 && lblAttribute2){
							ddlAttribute3.options[0].text = replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute2), "<replacementlabel>", lblAttribute3);
						}
						if(ddlAttribute4 && lblAttribute2){
							ddlAttribute4.options[0].text = replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute2), "<replacementlabel>", lblAttribute3);
						}
					}else{
						if(ddlAttribute3){
							ddlAttribute3.options[0].text = "";
						}
						if(ddlAttribute4){
							ddlAttribute4.options[0].text = "";
						}
					}
				}
			}
		}
	}
}

function loadAttribute3(productid){
	var ddlAttribute1 = $(ATTRFLDS[productid].attribute1);
	var ddlAttribute2 = $(ATTRFLDS[productid].attribute2);
	var ddlAttribute3 = $(ATTRFLDS[productid].attribute3);
	var ddlAttribute4 = $(ATTRFLDS[productid].attribute4);
	var lblAttribute2 = ATTRLBLS[productid].attribute2;
	var lblAttribute3 = ATTRLBLS[productid].attribute3;
	var lblAttribute4 = ATTRLBLS[productid].attribute4;
	if(ddlAttribute1 && ddlAttribute2 && lblAttribute2 && ddlAttribute3){
		var attrib1val = ddlAttribute1.options[ddlAttribute1.selectedIndex].value;
		var attrib2val = ddlAttribute2.options[ddlAttribute2.selectedIndex].value;
		ddlAttribute3.options.length = 0;
		ddlAttribute3.options[0] = new Option(replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute2), "<replacementlabel>", lblAttribute3), "");
		if(ddlAttribute4 && lblAttribute2){
			ddlAttribute4.options.length = 0;
			ddlAttribute4.options[0] = new Option(replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute2), "<replacementlabel>", lblAttribute3), "");
		}
		for(var a=0; a<=ATTRIBUTES[productid].length-1; a++){
			if(ATTRIBUTES[productid][a].value == attrib1val){
				if(ATTRIBUTES[productid][a].attribs.length > 0){
					for(var b=0; b<=ATTRIBUTES[productid][a].attribs.length-1; b++){
						if(ATTRIBUTES[productid][a].attribs[b].value == attrib2val){
							if(ATTRIBUTES[productid][a].attribs[b].attribs.length > 0){
								var moreThan3Attribute = false;
								ddlAttribute3.options[0].text = replaceSubstring(SETTINGS[productid].select, "<replacementlabel>", lblAttribute3);
								for(var c=0; c<=ATTRIBUTES[productid][a].attribs[b].attribs.length-1; c++){
									ddlAttribute3.options[(c+1)] = new Option(ATTRIBUTES[productid][a].attribs[b].attribs[c].text, ATTRIBUTES[productid][a].attribs[b].attribs[c].value);
									if(!moreThan3Attribute && ATTRIBUTES[productid][a].attribs[b].attribs[c].attribs.length > 0){
										moreThan3Attribute = true;
									}
								}
								if(moreThan3Attribute){
									//ddlAttribute3.observe("change", loadAttribute4);
									if(ddlAttribute4 && lblAttribute3){
										ddlAttribute4.options[0].text = replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute3), "<replacementlabel>", lblAttribute4);
									}
								}else{
									if(ddlAttribute4){
										ddlAttribute4.options[0].text = "";
									}
								}
							}
						}
					}
				}
			}
		}
	}
}

function loadAttribute4(productid){
	var ddlAttribute1 = $(ATTRFLDS[productid].attribute1);
	var ddlAttribute2 = $(ATTRFLDS[productid].attribute2);
	var ddlAttribute3 = $(ATTRFLDS[productid].attribute3);
	var ddlAttribute4 = $(ATTRFLDS[productid].attribute4);
	var lblAttribute3 = ATTRLBLS[productid].attribute3;
	var lblAttribute4 = ATTRLBLS[productid].attribute4;
	var attrib1val = ddlAttribute1.options[ddlAttribute1.selectedIndex].value;
	var attrib2val = ddlAttribute2.options[ddlAttribute2.selectedIndex].value;
	var attrib3val = ddlAttribute3.options[ddlAttribute3.selectedIndex].value;
	if(ddlAttribute4){
		ddlAttribute4.options.length = 0;
		ddlAttribute4.options[0] = new Option(replaceSubstring(replaceSubstring(SETTINGS[productid].selectprevious, "<replacementpreviouslabel>", lblAttribute3), "<replacementlabel>", lblAttribute4), "");
		for(var a=0; a<=ATTRIBUTES[productid].length-1; a++){
			if(ATTRIBUTES[productid][a].value == attrib1val){
				if(ATTRIBUTES[productid][a].attribs.length > 0){
					for(var b=0; b<=ATTRIBUTES[productid][a].attribs.length-1; b++){
						if(ATTRIBUTES[productid][a].attribs[b].value == attrib2val){
							if(ATTRIBUTES[productid][a].attribs[b].attribs.length > 0){
								for(var c=0; c<=ATTRIBUTES[productid][a].attribs[b].attribs.length-1; c++){
									if(ATTRIBUTES[productid][a].attribs[b].attribs[c].value == attrib3val){
										if(ATTRIBUTES[productid][a].attribs[b].attribs[c].attribs.length > 0){
											ddlAttribute4.options[0].text = replaceSubstring(SETTINGS[productid].select, "<replacementlabel>", lblAttribute4);
											for(var d=0; d<=ATTRIBUTES[productid][a].attribs[b].attribs[c].attribs.length-1; d++){
												ddlAttribute4.options[(d+1)] = new Option(ATTRIBUTES[productid][a].attribs[b].attribs[c].attribs[d].text, ATTRIBUTES[productid][a].attribs[b].attribs[c].attribs[d].value);
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
}
	
function loadPrice(what, productid, a1index, a2index, a3index, a4index){
	var newprice = null;
	var outofstock = false;
		//alert(outofstock)
	try{
		if(a4index){
			newprice = ATTRIBUTES[productid][(a1index-1)].attribs[(a2index-1)].attribs[(a3index-1)].attribs[(a4index-1)].price;
			outofstock = ATTRIBUTES[productid][(a1index-1)].attribs[(a2index-1)].attribs[(a3index-1)].attribs[(a4index-1)].soldout;
		}else if(a3index){
			newprice = ATTRIBUTES[productid][(a1index-1)].attribs[(a2index-1)].attribs[(a3index-1)].price;
			outofstock = ATTRIBUTES[productid][(a1index-1)].attribs[(a2index-1)].attribs[(a3index-1)].soldout;
		}else if(a2index){
			newprice = ATTRIBUTES[productid][(a1index-1)].attribs[(a2index-1)].price;
			outofstock = ATTRIBUTES[productid][(a1index-1)].attribs[(a2index-1)].soldout;
		}else if(a1index){
			newprice = ATTRIBUTES[productid][(a1index-1)].price;
			outofstock = ATTRIBUTES[productid][(a1index-1)].soldout;
		}
	}catch(x){
		// do nothing on error
	}
	if($(QTYFLDS[productid][0]) && $(QTYFLDS[productid][2])){
		//alert(outofstock)
		if(outofstock){
			$(QTYFLDS[productid][0]).hide();
			$(QTYFLDS[productid][2]).show();
		}else{
			$(QTYFLDS[productid][2]).hide();
			$(QTYFLDS[productid][0]).show();
		}
	}
	if(!newprice || newprice == ""){
		newprice = DEFAULTPRICE[productid];
		$(QTYFLDS[productid][2]).hide();
		$(QTYFLDS[productid][0]).show();
		$(QTYFLDS[productid][1]).disable();
		$(QTYFLDS[productid][1]).value = 0;
	}else{
		showHideQuantity(what, QTYFLDS[productid][0], QTYFLDS[productid][2], QTYFLDS[productid][1]);
	}
	if($(PRICE[productid])){
		var defaultPriceTag = DEFAULTPRICE[productid];
		var defaultPriceTagLowered = defaultPriceTag.toLowerCase();
		var newpriceLowered = newprice.toLowerCase();
		if(newpriceLowered.indexOf('<span class=currency>') > 0){
			$(PRICE[productid]).innerHTML = newprice;
		}else{
			var startPosOfCurrencySpan = defaultPriceTagLowered.indexOf('<span class=currency>');
			var currencySpan = "";
			if(startPosOfCurrencySpan > 0){
				var endPosOfCurrencySpan = defaultPriceTagLowered.indexOf('</span>', startPosOfCurrencySpan) + 7;
				currencySpan = defaultPriceTag.substring(startPosOfCurrencySpan, endPosOfCurrencySpan);
			}
			$(PRICE[productid]).innerHTML = '<span class="DynamicPrice">' + newprice + currencySpan + '</span>';
		}
	}
}

function showHideQuantity(what, quantityid, outofstockid, quantityfldid){
	if(what.selectedIndex > 0){
		$(quantityfldid).enable();
		var val = what.options[what.selectedIndex].value;
		if(val == ""){
			$(quantityid).hide();
			$(outofstockid).show();
			$(quantityfldid).value = 0;
		}else{
			$(outofstockid).hide();
			$(quantityid).show();
			if(($(quantityfldid).value-0) == 0){
				$(quantityfldid).value = 1;
			}
		}
	}else{
		$(outofstockid).hide();
		$(quantityid).show();
		$(quantityfldid).disable();
		$(quantityfldid).value = 0;
	}
}

function checkQuantitiesAndAttributes(sender, args){
	// TODO: how do I get the product id here to check the right fields?
	//alert("Checking Quantities and Attributes...");
	args.IsValid = true;
}

function checkQuantity(sender, args){
	args.IsValid = true;
}

function checkAttribute1(sender, args){
	args.IsValid = true;
}

function checkAttribute2(sender, args){
	args.IsValid = true;
}

function checkAttribute3(sender, args){
	args.IsValid = true;
}

function checkAttribute4(sender, args){
	args.IsValid = true;
}

/*** Swatch ***/

function ProductSwatches(_productid, _image, _thumb, _settings){

	this.ProductID = _productid;
	this.SwatchWidth = _image.Width;
	this.SwatchHeight = _image.Height;
	this.SwatchThumbWidth = _thumb.Width;
	this.SwatchThumbHeight = _thumb.Height;
	this.ShowName = _settings.ShowName;
	this.ColorNameID = _settings.ColorNameID;
	this.SwatchesID = _settings.SwatchesID;
	if(_settings.OnSwatchLoad != null){
		this.OnSwatchLoadFunction = _settings.OnSwatchLoad.bind(this);
	}
	if(_settings.OnSwatchClick != null){
		this.OnSwatchClickFunction = _settings.OnSwatchClick.bind(this);
	}
	if(_settings.OnSwatchMouseOver != null){
		this.OnSwatchMouseOverFunction = _settings.OnSwatchMouseOver.bind(this);
	}
	if(_settings.OnSwatchMouseOut != null){
		this.OnSwatchMouseOutFunction = _settings.OnSwatchMouseOut.bind(this);
	}

	// defaults
	this.Swatches = new Array();
	this.ActualSwatches = new Array();
	this.SelectColour = "";
	this.CurrentSwatch = -1;

}

ProductSwatches.prototype.addSwatch = function(_swatch){
	this.Swatches.push(_swatch);
}

ProductSwatches.prototype.loadSwatches = function(){

	/*****************
	/** BUILDS THE FOLLOWING STRUCTURE

		<div class="UI_Swatch">
			<a href="javascript:void(0);" onclick="PopupProduct(1, 'swatches', 0);" onmouseover="writeSwatchName('Swatch Image 1');">
				<img src="/images/quiltsetc_swatch_image.jpg" width="30" height="30" border="0" alt="Swatch Image 1" />
			</a>
		</div>
		
	********************/
	
	// empty the satches div
	$(this.SwatchesID + this.ProductID).update('');
	
	var spnColorName = $(this.ColorNameID + this.ProductID);
	if(!this.ShowName && spnColorName){
		spnColorName.hide();
	}
	
	for(i=0; i<=this.Swatches.length-1; i++){
		
		var currentSwatch = this.Swatches[i];
	
		//alert(this.Swatches[i][1]);
	
		if(currentSwatch.ThumbURL != ""){
		
			this.ActualSwatches.push(currentSwatch);
			
			var div = new Element('div', {
				className: "Swatch"
			});
			div = $(div);
			
			var a = new Element('a', {
				href: "javascript:void(0);", 
				id: "ProductSwatch_" + i
			});
			a.observe('click', this.swatchClick.bind(this));
			a.observe('mouseover', this.swatchMouseOver.bind(this));
			a.observe('mouseout', this.swatchMouseOut.bind(this));
			a = $(a);
			div.appendChild(a);
			
			var img = new Element('img', {
				src: currentSwatch.ThumbURL, 
				width: this.SwatchThumbWidth, 
				height: this.SwatchThumbHeight, 
				alt: currentSwatch.SwatchName, 
				border: "0"
			});
			img = $(img);
			a.appendChild(img);
			
			$(this.SwatchesID + this.ProductID).appendChild(div);
			
			var spnColorName = $(this.ColorNameID + this.ProductID);
			if(this.ShowName && spnColorName){
				this.SelectColour = spnColorName.innerHTML;
			}
			
		}
		
	}
	
	try{
		if(typeof(this.OnSwatchLoadFunction) === 'function'){
			this.OnSwatchLoadFunction();
		}
	}catch(x){
		alert(x);
		// do nothing on error
	}
	
}

ProductSwatches.prototype.swatchClick = function(event){
	try{
		if(typeof(this.OnSwatchClickFunction) === 'function'){
			this.OnSwatchClickFunction(event);
		}else{
			this.popupSwatch(event);
		}
	}catch(x){
		this.popupSwatch(event);
	}
}

ProductSwatches.prototype.swatchMouseOver = function(event){
	try{
		if(typeof(this.OnSwatchMouseOverFunction) === 'function'){
			this.OnSwatchMouseOverFunction(event);
		}else{
			this.writeSwatchName(event);
		}
	}catch(x){
		this.writeSwatchName(event);
	}
}

ProductSwatches.prototype.swatchMouseOut = function(event){
	try{
		if(typeof(this.OnSwatchMouseOutFunction) === 'function'){
			this.OnSwatchMouseOutFunction(event);
		}else{
			this.emptySwatchName(event);
		}
	}catch(x){
		this.emptySwatchName(event);
	}
}

ProductSwatches.prototype.getIdFromElement = function(_elem){
	var parents = _elem.ancestors();
	var parent = parents[0];
	var arrId = parent.id.split("_");
	var index = (arrId[1] - 0);
	return index;
}

ProductSwatches.prototype.popupSwatch = function(event){
	var elem = Event.element(event);
	var index = this.getIdFromElement(elem);
	var width = (this.SwatchWidth + 50);
	var height = (this.SwatchHeight + 100);
	var newWindow = window.open("/popup_Swatch.aspx?productid=" + this.ProductID + "&imageindex=" + index,'Swatch_Popup','width=' + width + ',height=' + height + ',scrollbars=yes,menubar=no,toolbar=no,location=no,directories=no,resizable=yes,top=100,left=100')
	self.name = 'swatchWindow'
	newWindow.focus()
	return false;
}

ProductSwatches.prototype.writeSwatchName = function(event){
	var elem = Event.element(event);
	var index = this.getIdFromElement(elem);
	var name = this.Swatches[index].SwatchName;
	var spnColorName = $(this.ColorNameID + this.ProductID);
	if(this.ShowName && spnColorName){
		spnColorName.innerHTML = name;
	}
}

ProductSwatches.prototype.emptySwatchName = function(event){
	var spnColorName = $(this.ColorNameID + this.ProductID);
	if(this.ShowName && spnColorName){
		spnColorName.innerHTML = this.SelectColour;
	}
}

ProductSwatches.prototype.resetSwatches = function(){
	this.Swatches = new Array();
	this.ActualSwatches = new Array();
	this.SelectColour = "";
	this.CurrentSwatch = -1;
}

