Array.prototype.remove=function(s){
	if(this.indexOf(s) != -1) 
		this.splice(i, 1);
}

String.prototype.ltrim = function() {	
	var re = /\s*((\S+\s*)*)/;
	return this.replace(re, "$1");
}

String.prototype.rtrim = function() {	
	var re = /((\s*\S+)*)\s*/;
	return this.replace(re, "$1");
	
}

String.prototype.trim = function() {	
	return this.ltrim().rtrim();	
}

function getProductOption()
{
	var a = document.getElementById("productAttributes");
	
	if (!a)
		return null;
	
	var s = a.getElementsByTagName("select");
	
	if (!s) 
		return null;
		
	if(s.length < 2)	
		return null;
	
	if (s[2].options.length == 0)
		return null;
	
	return s[2];	
}

function getAssociatedLabel(e)
{
	var labels = document.getElementsByTagName('label');
    for (var i = 0; i < labels.length; i++) {
      if (labels[i].htmlFor != '') {
         var elem = document.getElementById(labels[i].htmlFor);
         if (elem && e == elem) {
			return labels[i];
		 }
      }
    }
	return null;
}

function changeProductSizeAndInceam(s, value)
{
	var i;
	for(i =0; i<s.length;i++) {
		if (s.options[i].value == value) {
			s.selectedIndex = i;
			break;
		}
	}
}

function changeProductSize()
{
	var s = getProductOption();
	
	if (!s)
		return;
		
	var s1 = document.getElementById("attrib-size");
	
	if (!s1)
		return;
		
	var s2 = document.getElementById("attrib-inseam");
	
	if(!s2)
		return;
	
	if (s1.options.selectedIndex < 0)
		return;
	
	while(s2.options.length >0)
		s2.remove(s2.options.length - 1);
	
	
	
	var t;
	var t1 = s1.options[s1.options.selectedIndex].text;
	
	var i;
	var j;
	var found;
	for(i=0; i<s.options.length;i++) {
		t = s.options[i].text;
		t2 = t.substring(t.lastIndexOf("/") + 1, t.length).trim();
		t = t.substring(0, t.lastIndexOf("/")).trim();
		
		if (t == t1) {
			found = false;
			for(j=0; j<s2.options.length;j++) {
				if (s2.options[j].text == t) {
					found = true;
					break;
				}				
			}
			
			if (!found) { 
			  s2.options[s2.options.length] = new Option(t2, s.options[i].value, false, false);
			}
		}		
	}
	
	if (s2.options.length < 0)
		return;
	
	s2.selectedIndex = 0;
	
	changeProductSizeAndInceam(s, s2.options[0].value);
}

function changeProductInseam()
{
	var s = getProductOption();
	
	if (!s)
		return;
		
	var s1 = document.getElementById("attrib-size");
	
	if (!s1)
		return;
		
	var s2 = document.getElementById("attrib-inseam");
	
	if(!s2)
		return;
		
	if (s1.options.selectedIndex < 0)
		return;
		
	changeProductSizeAndInceam(s, s2.options[s2.selectedIndex].value);	
}

function initProductSizeAndInseam()
{
	var s = getProductOption();
	
	if (!s)
		return;
		
	var s1 = document.getElementById("attrib-size");
	
	if (!s1)
		return;
		
	var s2 = document.getElementById("attrib-inseam");
	
	if(!s2)
		return;	

	var l = getAssociatedLabel(s);
	var l1 = getAssociatedLabel(s1);
	var l2 = getAssociatedLabel(s2);

	var ltext = l.innerHTML.split("/");
	if (ltext.length !=2)
		return;
		
	l1.innerHTML = ltext[0] + ":";
	l2.innerHTML = ltext[1] + ":";

	var t;
	var t1;
	
	var i;
	var j;
	
	var found;
	for(i=0; i<s.options.length;i++) {
		t = s.options[i].text;
		t1 = t.substring(0, t.lastIndexOf("/")).trim();
		 
		found = false;
		for(j=0; j<s1.options.length;j++) {
			if (s1.options[j].text == t1) {
				found = true;
				break;
			}				
		}
			
		if (!found) { 
			s1.options[s1.options.length] = new Option(t1, s.options[i].value, false, false);
		}		
	}
	
	s1.selectedIndex = 0;
	
	changeProductSize();
	changeProductInseam();
}

function getProductOption2()
{
	var a = document.getElementById("productAttributes");
	
	if (!a)
		return null;
	
	var s = a.getElementsByTagName("select");
	
	if (!s) 
		return null;
		
	if(s.length < 4)	
		return null;
	
	if (s[3].options.length == 0)
		return null;
	
	return s[3];	
}

function getProductWash()
{
	var w = document.getElementById("productAdditionalImages2");
	if (!w)
		return;
	
	var f = w.getElementsByTagName("fieldset");
	
	if (!f) 
		return null;

	if(f.length == 0)	
		return null;

	return f[0];	
}

function initProductWash()
{
	var s = getProductOption2();
	
	if (!s)
		return;
	
	var w = getProductWash();
	
	if(!w)
		return;
		
	var t = "";
	var i;
	for(i=0; i<s.options.length;i++) {
		t += '<div style="padding-right: 5px;" class="additionalImages centeredContent back"> <img title=" " alt=" " src="/store/images/products/wash-';
		t += s.options[i].text.trim().toLowerCase().replace(" ", "-");
		t += '.jpg"/> </div>';				
	}

	if (s.options.length > 0) {
		t += '<br class="clearBoth"/>';
		
		w.innerHTML += t;
		
		var p = document.getElementById("productAdditionalImages2");
		
		p.style.visibility = "inherit";
		p.style.display = "block";			
	}	
}