function ajaxupdate(z){
	for(var i=0; i < z["length"];i++){
		var id=z[i]["id"];
		var value=z[i]["value"];
		e = document.getElementById(id);
		if (e) {
			if(id.match(/^checked/i)){
//				alert(e+ "is the span containing the check image");
				e.style.display='block';
			//}else if (e.hasAttribute("value")) {
			}else if (e.value != undefined) {
				updateField(id, value);
			//}else if (e.hasAttribute("text")) {
			}else if (e.text != undefined) {
				updateText(id, value);
			}else {
				updateBlock(id, value);
			}
		}
	}
}
function updateText(id, v){
//	alert("updateText : "+ id);
	var ele = document.getElementById(id);
	ele.text=v;
	flashText(id);
}
function updateField(id, v){
	alert("upateField : " + id );
	var ele = document.getElementById(id);
	ele.value=v;
	flashText(id);
}
function updateBlock(id, value){
	//	alert("updateBlock : " +id );
		var ele = document.getElementById(id);
		ele.innerHTML=value;
		flashBackground(id);
}
function flashBackground(id){
//	alert("flash background");
	e = document.getElementById(id);
	var bgcolor=e.style.backgroundColor;
try{
	if (bgcolor) {
		//var normal_color = e.style.backgroundColor;
		var normal_color = (RGBtoHEX(bgcolor));
	}else{
		var normal_color='f0f0f0';
	}
	
	}catch(f){
		alert(f);
		var normal_color='f0f0f0';
	}
//	alert("normal color: " + normal_color);
	var highlight_color='00AA00'; 
    if(id=="errors"){
        hightlight_color='FF0000';
    }
	colorFade(id, 'background', normal_color, highlight_color, 60, 30);
	setTimeout(function(){colorFade(id, 'background', highlight_color, normal_color, 30, 60)}, 2000);		
}
function RGBtoHEX(rgb){
	var hexvalue="";
	var vals = rgb.replace("rgb(","");
	var vals = vals.replace(")", "");
	var vals = vals.split(",");
	for (var k = 0; k < 3; k++) {
		vals[k] = vals[k] / 1;
		hexvalue  += vals[k].toString( 16 );
	}
	return hexvalue
}

function flashText(id){
	var normal_color='000000';
	var highlight_color='ffffff';
	colorFade(id, 'text', normal_color, highlight_color, 30, 30);
	setTimeout(function(){colorFade(id, 'text', highlight_color, normal_color, 60, 60)}, 2000);		
}

function NutraJaxCall(params, targets, serverFunction, tab){
		document.getElementById("ajax_loading").style.visibility="visible";
		setTimeout("NutraJaxTimeout()", 20000); 
		serverFunction(params, targets, tab, NutraJaxCallBack);
}		
function NutraJaxCallBack(z){
	var loadingDiv=	document.getElementById("ajax_loading");
	if (loadingDiv.style.visibility === "visible") {
		document.getElementById("ajax_loading").style.visibility = "hidden";
        if (!z["errors"]) {
            openTab(z['tab']);
        }
        
		ajaxupdate(z);
	}
	
}
function NutraJaxTimeout(){
	var loadingDiv=	document.getElementById("ajax_loading");
	if(loadingDiv.style.visibility==="visible"){
		loadingDiv.style.visibility="hidden";
		alert("server request timed out.  please try again");
		document.getElementById("shipto_button").disabled=false;
	}
}
	
function openTab(div_id){
		var divtab = document.getElementById(div_id);
		if (divtab){
			divtab.onclick = new Function('processClick(this)');
			divtab.style.background = "";
			processClick(divtab);
		}
}
function closeTab(div_id){
			var divtab = document.getElementById(div_id);
		if (divtab){
			divtab.onclick = '';
			divtab.style.background = "#aaaaaa";;
		}
}
