function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}

function ajax_object(){
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
		return ajaxRequest;
	} 
	catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
			return ajaxRequest;
		} 
		catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
				return ajaxRequest;
			} 
			catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
}

function ajaxRecommendFriend(form){
	form.disabled = true;
	ao = ajax_object();
	ao.onreadystatechange = function(){
		if(ao.readyState == 4){
			document.getElementById("recommend_friend").innerHTML = ao.responseText;
			form.disabled = false;
		}
	}
	ao.open("GET", "/ajax/ajax_recommend_friend.php?product_id="+form.product_id.value+"&email="+form.email.value+"&your_name="+form.your_name.value+"&personal_message="+form.personal_message.value, true);
	ao.send(null);
}

function updateProductOptions(option_name,selected_id){
	
	$.getJSON('/ajax/product_options.ajax.php',{option_id:option_name,selected_id:selected_id }, function(data) {
	
	if (data['base_price'] == ""){
		data['base_price'] = "0.00";
	}
	if (data['base_price'] != ""){
		$("#base_price").html(data['base_price']);
	}
	
	if (data['extra_price'] == ""){
		data['extra_price'] = "0.00";
	}
	if (data['extra_price'] != ""){
		$("#extra_price").html(data['extra_price']);
	}
	
	if (data['total_price'] == ""){
		data['total_price'] = "0.00";
	}
	if (data['total_price'] != ""){
		$("#total_price").html(data['total_price']);
	}
	
	if (data['delivery_price'] == ""){
		data['delivery_price'] = "0.00";
	}
	if (data['delivery_price'] != ""){
		$("#delivery_price").html(data['delivery_price']);
	}
	if (data['variation_id'] != ""){
		$("#variation_id").val(data['variation_id']);
	}
	if (data['delivery_type_id'] != ""){
		$("#delivery_type_id").val(data['delivery_type_id']);
	}
	
	if(data['next_option'] != ""){
		// alert(data['options'][0]);
		option_label = data['next_option'].replace("id","name");
		$("#"+data['next_option']).removeAttr('disabled'); 
		$("#"+data['next_option']).html("<option value='Please Select'>Please Select</option>"); // remove previous options
		
		for (x = 0; x < data['options'].length; x++){
			appendVal = "<option value='"+data['options'][x][data['next_option']]+"'";
			if (data['options'].length == 1){
				appendVal = appendVal + " selected='selected'";
			}
			appendVal = appendVal + ">"+data['options'][x][option_label]+"</option>";
			$("#"+data['next_option']).append(appendVal);
			if (data['options'].length == 1){
				updateProductOptions(data['next_option'],data['options'][x][data['next_option']])
			}
			//$("#"+data['next_option']).append("<option value='"+data['options'][x][data['next_option']]+"'");
			//if (data['options'].length == 1){
				//$("#"+data['next_option']).append(" selected='selected'");
			//}
			//$("#"+data['next_option']).append(">"+data['options'][x][option_label]+"</option>");
		}
		
	  }
	});
}
