var http=null;
var inline_arr = [
	{"span_email_id":"email_id"},
	{"span_mphone_id":"mphone_id"},
	{"span_profile_id":"profile_id"}
];
var link_arr = ["lnkEmail_id", "lnkMphone_id", "lnkProfile_id"];
var cancel_arr = ["span_email_cancel", "span_mphone_cancel", "span_profile_cancel"];
var orig_arr = ["orig_email", "orig_mphone", "orig_profile"];
var new_arr = ["new_email", "new_mphone", "new_profile"];
var error_arr = ["error_email", "error_mphone", "error_profile"];
var verify_arr = ["lnkVEmail", "lnkVMphone"];
var label_arr = [
	{}, {}, {"edit":"Edit Name"},
]; // to override the default link labels "Edit", "Save", "Cancel" etc.
var const_inline_error = "<br>There seems to be some problem updating your account. We recommend clicking the \"Refresh\" or \"Reload\" button on your browser's toolbar.";

function gbi(elId) {
	return document.getElementById(elId);
}


function Inline(idx, oName, elName, elId, dLt, cb) // Class Author: Rahul Batra @ 2008
{
	this.idx = idx; // index of element
	this.oName = oName; // name of object
	this.elName = elName; // id of element which is to be edited inline
	this.elId = elId; // id of element which is to be edited inline
	this.dLt = dLt; // display limit length after which "..." are appended
	this.cb = cb; // callback to execute after a successful edit


	this.inlineOn = function() { // method
		for (spanId in inline_arr[this.idx]) {
			ele1=gbi(spanId); ele2=gbi(inline_arr[this.idx][spanId]); ele3=gbi(cancel_arr[this.idx]);
			ele2.value = gbi(new_arr[this.idx]).value;
			ele1.style.display = 'none';
			ele2.style.display = ele3.style.display = '';
			ele2.select();
			ele2.focus();
		}
		lnk=gbi(link_arr[this.idx]);
		lnk.disabled = false;
		lnk.innerHTML = "Save";
		var fstr = this.oName + ".inlineOff()";
		lnk.onclick = function() {
			eval(fstr);return false;
		};
    if(gbi(verify_arr[this.idx]))
  		gbi(verify_arr[this.idx]).style.display = "none";
	};


	this.inlineOff = function() { // method
		for (spanId in inline_arr[this.idx]) {
			ele1=gbi(spanId); ele2=gbi(inline_arr[this.idx][spanId]); ele3=gbi(cancel_arr[this.idx]);
			if (this.dLt && String(ele2.value).length > this.dLt)
				ele1.innerHTML = String(ele2.value).substr(0, this.dLt) + "...";
			else
				ele1.innerHTML = ele2.value;
			ele1.style.display = '';
			ele2.style.display = ele3.style.display = 'none';
		}
		gbi(new_arr[this.idx]).value = ele2.value;
		lnk=gbi(link_arr[this.idx]);
		lnk.disabled = true;
		lnk.innerHTML = "Saving...";
//		lnk.onclick = ""; UNCOMMENT this line to prevent users from clicking multiple times on the same link, but do NOT uncommment if slow net connection

		if (http != null && http.readyState != 0 && http.readyState != 4)
			http.abort();
			try {
				http = new XMLHttpRequest();
			}
			catch (error) {
				try {
					http = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (error) {
					http = null;
					return false;
				}
			}
		var requestParams = this.elName + "=" + encodeURIComponent(gbi(this.elId).value) + "&id=" + encodeURIComponent(gbi('id').value) + (gbi("altresid") ? "&altresid=" + encodeURIComponent(gbi("altresid").value) : "") + "&rand=" + Math.random()*10;
		var fstr = this.oName + ".onreadystatechangeInline()";
		http.onreadystatechange = function() {
			eval(fstr); return true;
		};
		http.open("GET", gbi('ajax_url').value + "?" + requestParams, true);
		http.send(null);
	};


	this.inlineCancel = function() { // method
		for (spanId in inline_arr[this.idx]) {
			ele1=gbi(spanId); ele2=gbi(inline_arr[this.idx][spanId]); ele3=gbi(cancel_arr[this.idx]);
			if (this.dLt && String(gbi(orig_arr[this.idx]).value).length > this.dLt)
				ele1.innerHTML = String(gbi(orig_arr[this.idx]).value).substr(0, this.dLt) + "...";
			else
				ele1.innerHTML = String(gbi(orig_arr[this.idx]).value);
			ele1.style.display = '';
			ele2.style.display = ele3.style.display = 'none';
			gbi(error_arr[this.idx]).innerHTML = "";
		}
		gbi(new_arr[this.idx]).value = gbi(orig_arr[this.idx]).value;
		lnk=gbi(link_arr[this.idx]);
		lnk.disabled = false;
		lnk.innerHTML = label_arr[this.idx]["edit"] ? label_arr[this.idx]["edit"] : "Edit";
		var fstr = this.oName + ".inlineOn()";
		lnk.onclick = function() {
			eval(fstr);return false;
		};
	};


	this.onreadystatechangeInline = function() { // method
		if (http.readyState == 4) {
			try {
				if (http.status == 200) {
					if (http.responseText.charAt(0) == "\n") // to handle prob with symfony (extra \n char)
						var arr_response = http.responseText.substr(1).split("|X|");
					else
						var arr_response = http.responseText.split("|X|");

					if (arr_response[0] == "OK") {
						lnk=gbi(link_arr[this.idx]);
						lnk.disabled = false;
						lnk.innerHTML = label_arr[this.idx]["edit"] ? label_arr[this.idx]["edit"] : "Edit";
						var fstr = this.oName + ".inlineOn()";
						lnk.onclick = function() {
							eval(fstr); return false;
						};
						gbi(error_arr[this.idx]).innerHTML = "";
						for (spanId in inline_arr[this.idx]) {
							ele1=gbi(spanId); ele2=gbi(inline_arr[this.idx][spanId]);
							var oldVal = gbi(orig_arr[this.idx]).value;
							gbi(orig_arr[this.idx]).value = gbi(new_arr[this.idx]).value = ele1.title = arr_response[1];
							if (this.dLt && arr_response[1].length > this.dLt)
								ele1.innerHTML = arr_response[1].substr(0, this.dLt) + "...";
							else
								ele1.innerHTML = arr_response[1];
						}
						if (gbi("mod_dt")) {
							gbi("mod_dt").innerHTML = arr_response[2];
						}
						if (this.elName.indexOf("email") != -1 && gbi("error_verify"))
							gbi("error_verify").style.display = "none";

						if (this.cb)
							this.cb.call(null, oldVal, arr_response[1]);
            if(gbi(verify_arr[this.idx]))
      					gbi(verify_arr[this.idx]).style.display = "";
					}
					else
					{
						if (arr_response[1] == undefined || arr_response[1] == "") {
							arr_response[1] = const_inline_error;
						}
						gbi(error_arr[this.idx]).innerHTML = arr_response[1];
						this.inlineOn();
					}

				}
				else if (http.status == 302) {
					gbi(error_arr[this.idx]).innerHTML = const_inline_error;
					this.inlineOn();
				}
				else if (http.status != 0) { // IE returns a status code of 0 on some occasions, so ignore that case
					gbi(error_arr[this.idx]).innerHTML = "<br>There was an error performing the requested action: http.status";
					this.inlineOn();
				}
			}
			catch (error) {
			}
		}
		return true;
	};
	return this; // return object of Inline class
}

function trapKey(event, fcode) {
	if ((event && event.keyCode == 13)) { // enter key
		eval(fcode);
		return false;
	}

	return true;
}

