function buildState(selItem,country,initialMsg){
	for (i = $("f_state").length - 1; i>=0; i--) {
		$("f_state").options[i] = null; 
 	}
	
	if(country == ""){ country = $("f_country").options[$("f_country").selectedIndex].value; }

	if(country != "" && country != undefined){
		if(JSStates.length > 0){
			if(selItem == "" || selItem == undefined || selItem == 0) $("f_state").options[0] = new Option(initialMsg,0);
			j = 0;
			for(i=0;i < JSStates.length;i++){
				if(JSStates[i][2] == country){
					$("f_state").options[$("f_state").length] = new Option(JSStates[i][1],JSStates[i][0]);
					if(selItem == JSStates[i][0]) $("f_state").selectedIndex = j;
					j++;
				}
			}
		}
	} else{
		$("f_state").options[$("f_state").length] = new Option("n/a",0);
	}
}

var theCounter = 1000;
function addFormat(){
	var theHTML;
	theHTML = "<div id=\"formatItm" + theCounter + "\">";
	theHTML = theHTML + "<input name=\"wineFormatPrice_qty[]\" type=\"text\" size=\"4\" value=\"1\"> ";
	theHTML = theHTML + "<select name=\"wineFormatPrice_format[]\">";
		theHTML = theHTML + "<option selected>750ml</option>";
		theHTML = theHTML + "<option>500ml</option>";
		theHTML = theHTML + "<option>375ml</option>";
		theHTML = theHTML + "<option>Magnum</option>";
	theHTML = theHTML + "</select> ";
	theHTML = theHTML + "<input name=\"wineFormatPrice_price[]\" type=\"text\" size=\"6\ value=\"\">";
	theHTML = theHTML + " <a href=\"javascript:removeFormat(" + theCounter + ");\">Enlever</a></div>";
	$("priceFormat").innerHTML = $("priceFormat").innerHTML + theHTML;
	theCounter = theCounter + 1;
}

function removeFormat(toRemove){
	$("formatItm"+toRemove).innerHTML = "";
	$("formatItm"+toRemove).style.display = "none";
}

var funcStore = Array();
	
function swapEdit(obj,initialVal,fieldName,table,keyField,keyValue,className,rmString){
	if(rmString) fieldName = fieldName.replace(rmString,'');
	initialVal = initialVal.escapeHTML().replace(/\"/g,"&quot;");
	
	theHTML = "";
	theHTML = "<input size=\"50\" type=\"text\" class=\""+className+"\" name=\""+fieldName+"_value\" id=\""+fieldName+"_value\" value=\""+initialVal+"\">";
	
	obj.innerHTML = theHTML;
	$(fieldName+'_value').focus();
	funcStore[obj] = obj.onclick;
	obj.onclick = function(){ return null; };
	
	$(fieldName+'_value').onblur = function(){ swapRestore(obj,initialVal.escapeHTML(),$(fieldName+'_value').value.escapeHTML(),funcStore[obj],fieldName,table,keyField,keyValue); };
	
}

function swapRestore(obj,initialVal,newVal,objFunc,fieldName,table,keyField,keyValue){
	if(initialVal != newVal) ajaxRequest(table,fieldName,newVal,keyField,keyValue);
	theHTML = "";
	theHTML = newVal;
	obj.innerHTML = theHTML;
	obj.onclick = objFunc;
}

function ajaxRequest(table,field,value,keyField,keyValue){
	var url = '/cgi/sessions.php';
	new Ajax.Request(url, {   
		method: 'post',   
		parameters: 'a=ajaxUpdate&table='+ table +'&field='+ field +'&keyfield='+keyField+'&keyvalue='+ keyValue +'&value='+ value,
		onComplete: function(transport) {     
			if(transport.responseText == "ok") alert("Champ modifié");
			if(transport.responseText == "error") alert("erreur");
		} 
	});
} 
