function getElementsByClassName(classname, node)  {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function isbuilder(){
	var url = window.location.href;
	var urlarray = url.split("?");

	if (urlarray[1] == "build"){
		var build = document.getElementById('builder-add');
		
		var newform = document.createElement('form');
		newform.method = "post";
		newform.action = "http://p4.hostingprod.com/@hydroponics-garden.com/builder.php";
		newform.onsubmit = "getoptions();"
		newform.id = "builder-form";
		
		var input1 = document.createElement('input');
		input1.name = "row";
		input1.value = icat; //var icat is defined in the RTML template for the item page
		input1.type = "hidden";
		
		var input2 = document.createElement('input');
		input2.name = "name";
		input2.value = iname; //var iname is defined in the RTML template for the item page
		input2.type = "hidden";
		
		var input3 = document.createElement('input');
		input3.name = "price";
		input3.value = iprice;
		input3.type = "hidden";
		
		var input4 = document.createElement('input');
		input4.name = "image";
		input4.value = ipic;
		input4.type = "hidden";
		
		var input5 = document.createElement('input');
		input5.name = "id";
		input5.value = iid;
		input5.type = "hidden";
		
		var input6 = document.createElement('input');
		input6.name = "action";
		input6.value = "additem";
		input6.type = "hidden";
		
		var input7 = document.createElement('input');
		input7.name = "Add to Builder";
		input7.value = "Add to Builder";
		input7.src = "http://lib.store.yahoo.net/lib/yhst-34708634314647/atb.gif";
		input7.type = "image";
		
		newform.appendChild(input1);
		newform.appendChild(input2);
		newform.appendChild(input3);
		newform.appendChild(input4);
		newform.appendChild(input5);
		newform.appendChild(input6);

		build.appendChild(newform);
		
		var optrows = getElementsByClassName('options');
		var opts = "";
		
		for (var x=0; x<optrows.length; x++){
			var th = optrows[x].getElementsByTagName('th');
			var optname = th[0].innerHTML;
			optname = optname.replace(":","");
			var select = optrows[x].getElementsByTagName('select');
			var optval = select[0].value;
			
			opts += optname + "," + optval;
			if (x != (optrows.length - 1)){
				opts += "|";
			}
		}
		
		var oinput = document.createElement('input');
		oinput.name = "options";
		oinput.value = opts;
		oinput.type = "hidden";
		oinput.id = "optinput";
		newform.appendChild(oinput);
		
		newform.appendChild(input7);
	}
}

function getoptions(){
	var optrows = getElementsByClassName('options');
	var opts = "";
	
	for (var x=0; x<optrows.length; x++){
		var th = optrows[x].getElementsByTagName('th');
		var optname = th[0].innerHTML;
		optname = optname.replace(":","");
		var select = optrows[x].getElementsByTagName('select');
		var optval = select[0].value;
		
		opts += optname + "," + optval;
		if (x != (optrows.length - 1)){
			opts += "|";
		}
	}
	
	var theinput = document.getElementById('optinput');
	theinput.value = opts;
}

isbuilder();

