function changePhoto(main_photo_id,replace_src){
	
	var el = document.getElementById(main_photo_id);
	el.src = replace_src;
	
}

function launchSubPage(subpagehref){
	window.open(subpagehref,'subpage','left=20,top=20,width=500,height=450,toolbar=0,resizable=0,menu=0,scrollbars=0,status=0');
}

function product_init(){
	
	fillOptions(1);

}

function optionFilled(num){

	var producttype_attribute_id = attributes_array[num];
	
	var el = document.getElementById("option_"+producttype_attribute_id);
	
	//make sure something was selected
	if(el.options[el.selectedIndex].value!=''){
	
		//fill the next one
		fillOptions(num+1);
		
		var dd_value = el.options[el.selectedIndex].value.split("_");
		
		var producttype_attribute_option_id = dd_value[0];
		
		updatePrice();
		
		//find out if we need to change out the photo
		new Ajax.Request('/product_ajax.php', {method:'post', postBody:'action=getphoto&product_id='+product_id+'&producttype_attribute_id='+producttype_attribute_id+"&producttype_attribute_option_id="+producttype_attribute_option_id, onComplete:changePhotoAjax});
	
	}
	
}

function updatePrice(t){
	
	var att_price=0;
	
	//get all the prices from the selected boxes
	for(i=1;i<attributes_array.length;i++){
		
		var el = document.getElementById("option_"+attributes_array[i]);
		
		//make sure something was selected
		if(el.options[el.selectedIndex].value!=''){
			
			var dd_value = el.options[el.selectedIndex].value.split("_");
			
			if(dd_value[1]){
			
				att_price += Number(dd_value[1]);
				
			}
			
		}
		
	}
	
	var priceToUse = currencyFormatted(product_base_price + att_price);
	
	//update the html
	var el = document.getElementById('price');
	el.innerHTML = "$" + String(priceToUse);

	
}

function currencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}


function changePhotoAjax(t){
	
	if(t.responseText!=""){
		changePhoto('product_shot',t.responseText);
	}
	
}

function fillOptions(attribute_num){
	
	var producttype_attribute_id = attributes_array[attribute_num];
	
	var previous_selections = "";
	
	//get the previous selections
	for(i=1;i<attribute_num;i++){
		
		var el = document.getElementById("option_"+attributes_array[i]);
		
		var dd_value = el.options[el.selectedIndex].value.split("_");
		
		previous_selections += dd_value[0]+",";
		
	}
	
	//remove the last comma
	previous_selections = previous_selections.substr(0,previous_selections.length-1);
	
	new Ajax.Request('/product_ajax.php', {method:'post', postBody:'action=getattributeoptions&product_id='+product_id+'&attribute_num='+attribute_num+'&producttype_attribute_id='+producttype_attribute_id+"&previous_selections="+previous_selections, onComplete:renderOptions});

}

function clearddl(objSelect)

{

     for (var i = (objSelect.options.length-1); i >= 0; i--){
		 objSelect.remove(0);
     }

}

function renderOptions(t){
	
	var xmlDoc = t.responseXML;
	
	var xmlObj = xmlDoc.getElementsByTagName('options');
	
	var producttype_attribute_id = xmlObj[0].getAttribute("producttype_attribute_id");
	var attribute_num = xmlObj[0].getAttribute("attribute_num");
	var productype_attribute_name = xmlObj[0].getAttribute("producttype_attribute_name");
	
	var xmlItems = xmlDoc.getElementsByTagName('option');
	
	var el = document.getElementById("option_"+producttype_attribute_id);	
	
	clearddl(el);
	
	var objOption = new Option('Choose','');
	el.options[el.length] = objOption;
	
	for (var i=0;i<xmlItems.length;i++){
			
			var producttype_attribute_option_id = xmlItems[i].getAttribute("producttype_attribute_option_id");
			var producttype_attribute_option_name = xmlItems[i].getAttribute("producttype_attribute_option_name");
			var producttype_attribute_option_price = xmlItems[i].getAttribute("producttype_attribute_option_price");
			
			/*if(producttype_attribute_option_price=="0"){
				//var price = "";
				var price = "0";
			}else{
				
				if(producttype_attribute_option_price.substr(0,1)=="-"){
					var price = "(-$"+producttype_attribute_option_price.substr(1)+")"
				}else{
					var price = "(+$"+producttype_attribute_option_price+")"
				}
				
				var price = producttype_attribute_option_price;
			}*/
			
			var objOption = new Option(producttype_attribute_option_name,producttype_attribute_option_id+"_"+producttype_attribute_option_price);
			el.options[el.length] = objOption;
			
	}
		

	el.disabled = false;
	
	//disable the remaining fields
	for(i=(Number(attribute_num)+1);i<attributes_array.length;i++){
		
		var new_attribute_num = attributes_array[i]
		
		var el = document.getElementById('option_'+new_attribute_num);
		
		//disable it
		el.disabled = true;
		
		//clear it
		clearddl(el);
		
		//add a temporary option
		var objOption = new Option('Choose','');
		el.options[el.length] = objOption;
		
	}
	
}


function validateForm(){
	
	var error = "";

	//go through all the attribute options and make sure that they're set
	for(i=1;i<attributes_array.length;i++){
		
		var el = document.getElementById('option_'+attributes_array[i]);
		
		if(el.value==""){
			
			error = "Error: You must fill out all options.";
			
		}
		
	}
	
	//make sure quantity is not zero
	var el = document.getElementById('quantity');
	
	if(el.value == "" || Number(el.value)<1){
		error = "Error: Please fill out a quantity.";	
	}

	if(error!=""){
		alert(error);
		return false;
	}else{
		return true;
	}
	
}