<!--
//script by http://www.yvoschaap.com

//XMLHttpRequest class function
function datosServidor() {
};
datosServidor.prototype.iniciar = function() {
	try {
		// Mozilla / Safari
		this._xh = new XMLHttpRequest();
	} catch (e) {
		// Explorer
		var _ieModelos = new Array(
		'MSXML2.XMLHTTP.5.0',
		'MSXML2.XMLHTTP.4.0',
		'MSXML2.XMLHTTP.3.0',
		'MSXML2.XMLHTTP',
		'Microsoft.XMLHTTP'
		);
		var success = false;
		for (var i=0;i < _ieModelos.length && !success; i++) {
			try {
				this._xh = new ActiveXObject(_ieModelos[i]);
				success = true;
			} catch (e) {
			}
		}
		if ( !success ) {
			return false;
		}
		return true;
	}
}

datosServidor.prototype.ocupado = function() {
	estadoActual = this._xh.readyState;
	return (estadoActual && (estadoActual < 4));
}

datosServidor.prototype.procesa = function() {
	if (this._xh.readyState == 4 && this._xh.status == 200) {
		this.procesado = true;
	}
}

datosServidor.prototype.enviar = function(urlget,datos) {
	if (!this._xh) {
		this.iniciar();
	}
	if (!this.ocupado()) {
		this._xh.open("GET",urlget,false);
		this._xh.send(datos);
		if (this._xh.readyState == 4 && this._xh.status == 200) {
			return this._xh.responseText;
		}
		
	}
	return false;
}


var urlBase = "includes/templates/quotations/update.php";
var urlBase_noscript = "includes/templates/quotations/update_noscript.php";
var urlBase_updatequotedb = "ajaxFetcher.php?filetoken=FILENAME_AJAX_UPDATE_QUOTATION_DB";
var formVars = "";
var changing = false;
var thisBlank = "";

function fieldEnter_rate(campo,evt,idfld) {
	currentId = idfld.substr(idfld.lastIndexOf("_")+1);
	evt = (evt) ? evt : window.event;
	if (evt.keyCode == 13 && campo.value!="" && campo.value!=0) {
		elem = document.getElementById( idfld );
		remotos = new datosServidor;
		
		if(idfld.substr(0,5) == "rate_"){
			passValue = (parseFloat(campo.value)).toFixed(2);
		}
		
		if(idfld.substr(0,4) == "qty_"){
			passValue = parseFloat(campo.value);
		}
		
		nt = remotos.enviar(urlBase_noscript + "?fieldname=" +encodeURI(elem.id)+ "&content="+encodeURI(passValue)+"&"+formVars,"");
		//remove glow
		noLight(elem);
		Spry.Utils.setInnerHTML(elem, nt);
		if(idfld.substr(0,5) == "rate_"){
		productQty = parseFloat(document.getElementById("qty_"+currentId).innerHTML);
		productRate = parseFloat(campo.value);
		newProductTotal = productQty * productRate;
		Spry.Utils.setInnerHTML("price_"+currentId, newProductTotal.toFixed(2));
		}
		if(idfld.substr(0,4) == "qty_"){
		updatePrice();
		productRate = parseFloat(document.getElementById("rate_"+currentId).innerHTML);
		productQty = parseFloat(campo.value);
		newProductTotal = productQty * productRate;
		Spry.Utils.setInnerHTML("price_"+currentId, newProductTotal.toFixed(2));
		}
		productid = document.getElementById("id_"+currentId).innerHTML;
		optionId = document.getElementById('attrib_'+currentId).value;
		updateQuoteDb("update",productid,"",optionId,productRate,productQty,newProductTotal.toFixed(2),"draft","",idfld);
		updateTotal();
		changing = false;
		return false;
	} else {
		return true;
	}


}

function fieldEnter(thisBlank,campo,evt,idfld) {
	evt = (evt) ? evt : window.event;
	//if (evt.keyCode == 13 && campo.value!="") {
	if (evt.keyCode == 13) {
		elem = document.getElementById( idfld );
		remotos = new datosServidor;
		nt = remotos.enviar(urlBase + "?thisBlank="+thisBlank+"&fieldname=" +encodeURI(elem.id)+ "&content="+encodeURI(campo.value)+"&"+formVars,"");
		//remove glow
		noLight(elem);
		Spry.Utils.setInnerHTML(elem, nt);
		//elem.innerHTML = nt;
		if(thisBlank=="yes" && campo.value!="")q_addrow("quoteTable");
		changing = false;
		return false;
	} else {
		return true;
	}


}

function fieldBlur_rate(campo,idfld) {
	currentId = idfld.substr(idfld.lastIndexOf("_")+1);
	if (campo.value!="" && campo.value!=0) {
		elem = document.getElementById( idfld );
		remotos = new datosServidor;
		
		if(idfld.substr(0,5) == "rate_"){
			passValue = (parseFloat(campo.value)).toFixed(2);
		}
		
		if(idfld.substr(0,4) == "qty_"){
			passValue = parseFloat(campo.value);
		}
		
		nt = remotos.enviar(urlBase_noscript + "?fieldname=" +encodeURI(elem.id)+ "&content="+encodeURI(passValue)+"&"+formVars,"");
		//remove glow
		noLight(elem);
		Spry.Utils.setInnerHTML(elem, nt);
		if(idfld.substr(0,5) == "rate_"){
		var productQty = parseFloat(document.getElementById("qty_"+currentId).innerHTML);
		var productRate = parseFloat(campo.value);
		var newProductTotal = productQty * productRate;
		Spry.Utils.setInnerHTML("price_"+currentId, newProductTotal.toFixed(2));
		}
		if(idfld.substr(0,4) == "qty_"){
		var productRate = parseFloat(document.getElementById("rate_"+currentId).innerHTML);
		var productQty = parseFloat(campo.value);
		var newProductTotal = productQty * productRate;
		Spry.Utils.setInnerHTML("price_"+currentId, newProductTotal.toFixed(2));
		}
		updateTotal();
		changing = false;
		return false;
	} else {
		return true;
	}


}

function updateQuoteDb(dbAction,productId,productName,optionId,productRate,productQty,productTotal,entryStatus,oldProductId,fieldname,oldOptionId){
	var currentQuoteId = document.getElementById('quoteId').innerHTML;
	var attnElem = document.getElementById('kindAttn');
	if(attnElem != null){
	var attn = document.getElementById('kindAttn').value;
	} else {
	var attn = "";
	}
	remotos = new datosServidor;
		nn = remotos.enviar(urlBase_updatequotedb + "&currentQuoteId="+currentQuoteId+"&productId="+productId+"&productName="+productName+"&optionId="+optionId+"&productRate="+productRate+"&productQty="+productQty+"&productTotal="+productTotal+"&entryStatus="+entryStatus+"&dbAction="+dbAction+"&oldProductId="+oldProductId+"&oldOptionId="+oldOptionId+"&fieldname="+fieldname+"&attn="+attn,"");
		Spry.Utils.setInnerHTML("messages", nn);
}

function fieldBlur(thisBlank,campo,idfld,productid,updateflag) {
	//if (campo.value!="") {
		currentId = idfld.substr(idfld.lastIndexOf("_")+1);
		oldproductid = document.getElementById("id_"+currentId).innerHTML;
		elem = document.getElementById( idfld );
		remotos = new datosServidor;
		nt = remotos.enviar(urlBase + "?thisBlank="+thisBlank+"&fieldname=" +escape(elem.id)+ "&content="+escape(campo.value)+"&"+formVars,"");
		Spry.Utils.setInnerHTML(elem, nt);
		if(updateflag != "noupdate" && campo.value != ""){
			Spry.Utils.setInnerHTML("id_"+currentId, productid);
			//document.getElementById("rate_"+currentId).className = "editText";
			Spry.Utils.setInnerHTML("qty_"+currentId, "1");
			document.getElementById("qty_"+currentId).className = "editText";
			document.getElementById('attrib_'+currentId).style.display = 'none';
			currentOptionId = document.getElementById('attrib_'+currentId).value;
			ClearOptions(document.getElementById('attrib_'+currentId));
			AddToOptionList(document.getElementById('attrib_'+currentId), "", "-None-", "true");
			selectAttrib(productid, currentId);
			var calcPrice = parseFloat(document.getElementById('rate_'+currentId).innerHTML) * parseFloat(document.getElementById('qty_'+currentId).innerHTML);
			Spry.Utils.setInnerHTML("price_"+currentId, calcPrice.toFixed(2));
			optionId = document.getElementById('attrib_'+currentId).value;
			
			if(thisBlank == "yes"){
				updateQuoteDb("add",productid,campo.value,optionId,calcPrice,"1",calcPrice,"draft","",idfld);
			} else {
				updateQuoteDb("replace",productid,campo.value,optionId,calcPrice,"1",calcPrice,"draft",oldproductid,idfld,currentOptionId);
			}			
			
			updateTotal();
		}
		
		//elem.innerHTML = nt;
		//if(thisBlank=="yes" && campo.value!="")q_addrow("quoteTable");
		changing = false;
		return false;
	//}
}

//edit field created
function editBox(actual) {
	//alert(actual.nodeName+' '+changing);
	if(!changing){
		//width = widthEl(actual.id) + 20;
		width = widthEl(actual.id)-10;
		height =heightEl(actual.id) + 0;

		//if(height < 40){
		if(actual.innerHTML=="" || actual.innerHTML=="&nbsp;(click here to add product)"){
		thisBlank = "yes";
		actual.innerHTML = "";
		actual.style.color = "";
		}else{
		thisBlank = "no";
		}
		//if(width < 100)	width = 150;
		if(actual.id.substr(0,5) == "desc_"){
			suggestbox = "<input id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px; padding-left: 5px;\" maxlength=\"254\" type=\"text\" value=\"" + actual.innerHTML + "\" onfocus=\"highLight(this);\" onblur=\"noLight(this);\" />";
			
			Spry.Utils.setInnerHTML(actual, suggestbox);
			var options = {
	script: "ajaxfetcher.php?filetoken=FILENAME_AJAX_PRODUCTS_FETCH&xid="+Math.random()+"&",
			varname: "input",
			json: false,
			maxresults: 500,
			offsety: 0,
			timeout: 30000,
			delay: 0,
			shownoresults: true,
			cache: true,
			blank: thisBlank,
			triggerelem: document.getElementById(actual.id+'_field'),
			triggerid: actual.id
};
var as = new bsn.AutoSuggest(actual.id +'_field', options);
}
else {
if(thisBlank == "no"){
	suggestbox = "<input id=\""+ actual.id +"_field\" style=\"width: "+width+"px; height: "+height+"px;\" maxlength=\"254\" type=\"text\" value=\"" + actual.innerHTML + "\" onkeypress=\"return fieldEnter_rate(this,event,'" + actual.id + "')\" onfocus=\"highLight(this);\" onblur=\"noLight(this); return fieldBlur_rate(this,'" + actual.id + "');\" />";
			
	Spry.Utils.setInnerHTML(actual, suggestbox);
	}
	else{return;}
}
		changing = true;
	}
		actual.firstChild.focus();
}



//find all span tags with class editText and id as fieldname parsed to update script. add onclick function
function editbox_init(){
	if (!document.getElementsByTagName){ return; }
	var spans = document.getElementsByTagName("div");

	// loop through all span tags
	for (var i=0; i<spans.length; i++){
		var spn = spans[i];

        	if (((' '+spn.className+' ').indexOf("editText") != -1) && (spn.id)) {
			spn.onclick = function () { editBox(this); }
			spn.onfocus = function () { editBox(this); }
			spn.onmouseover = highlightme;
			spn.onmouseout = normalme;
			spn.style.cursor = "pointer";
			spn.title = "Click to edit";	
			spn.style.backgroundColor = "#eeeecc";
       		}

	}


}

function highlightme(event){
this.style.backgroundColor = "#ffffdd";
}

function normalme(event){
this.style.backgroundColor = "eeeecc";
}

//crossbrowser load function
function addEvent(elm, evType, fn, useCapture)
{
  if (elm.addEventListener){
    elm.addEventListener(evType, fn, useCapture);
    return true;
  } else if (elm.attachEvent){
    var r = elm.attachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Please upgrade your browser to use full functionality on this page");
  }
}

//get width of text element
function widthEl(span){

	if (document.layers){
	  w=document.layers[span].clip.width;
	} else if (document.all && !document.getElementById){
	  w=document.all[span].offsetWidth;
	} else if(document.getElementById){
	  w=document.getElementById(span).offsetWidth;
	}
return w;
}

//get height of text element
function heightEl(span){

	if (document.layers){
	  h=document.layers[span].clip.height;
	} else if (document.all && !document.getElementById){
	  h=document.all[span].offsetHeight;
	} else if(document.getElementById){
	  h=document.getElementById(span).offsetHeight;
	}
return h;
}

function highLight(span){
            //span.parentNode.style.border = "2px solid #D1FDCD";
            //span.parentNode.style.padding = "0";
            span.style.border = "none";          
			span.style.backgroundColor = "#efefdf";          
}

function noLight(span){
        //span.parentNode.style.border = "0px";
        //span.parentNode.style.padding = "2px";
        span.style.border = "0px";   


}

//sets post/get vars for update
function setVarsForm(vars){
	formVars  = vars;
}

addEvent(window, "load", editbox_init);
-->
