
var itsonview=false;
var httpObject = null;
var globalEditField='';
var inline_edit_allowed=true;

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}
function checkField(src,label)
{
	if (src.value.trim()=='')
	{
		alert('Please enter '+label);
		src.focus();
		return false;
	}
	else
		return true;
}
function openWindow(_url, _width, _height)
{
	_name = Math.round(Math.random()*10000);
	var myWindow = window.open (_url, _name, "resizable=yes,scrollbars=yes,status=1,location=0,width="+_width+",height="+_height);
	if (navigator.appName=='Netscape') 
		myWindow.focus(); 
}
function catchKey(e, form_name, skip_fields)
{
    var isIE = (navigator.userAgent.indexOf("MSIE") != -1);
    key = (isIE) ? key = event.keyCode : e.which;
    if (key == 13)
    {
		if (skip_fields)
			document.forms[form_name].submit();
		else if (document.forms[form_name].elements['login'].value!='' && document.forms[form_name].elements['password'].value!='')
			document.forms[form_name].submit();
    }
}
//
function show(divId)
{
	var id = document.getElementById(divId);
	id.style.display = 'inline';
}
//
function fnhide(divId)
{
	var id = document.getElementById(divId);
	id.style.display = 'none';
}
//
function getObj(n)
{
	return document.getElementById(n);
}
//
function hndMouseOver(fieldLabel)
{
	if (itsonview || (!inline_edit_allowed && fieldLabel!='status_id' && fieldLabel!='description'))
	{
		return;
	}
	var mouseArea='';
	mouseArea='mouse_'+ fieldLabel;
	show("crmspanid");
	divObj = getObj('crmspanid');
	crmy = findPosY(getObj(mouseArea));
	crmx = findPosX(getObj(mouseArea));
	divObj.setAttribute('onclick','handleEdit("'+fieldLabel+'");');
	divObj.style.left=(crmx+getObj(mouseArea).offsetWidth -divObj.offsetWidth)+"px";
	divObj.style.top=crmy+"px";
}
//
function handleEdit(fieldlabel)
{//alert(editArea);
	show('edit_'+fieldlabel);
	fnhide('view_'+fieldlabel);
	fnhide('crmspanid');
	itsonview=true;
	return false;
}
//
function hndCancel(fieldlabel)
{
	show('view_'+fieldlabel);
	fnhide('edit_'+fieldlabel);
	itsonview=false;
	return false;
}
//
function doAjaxSave(area, field, item_id)
{
	httpObject = getHTTPObject();
	if (httpObject != null) 
	{
		httpObject.open('POST', 'http://affiliate.testking.com/admin/crm/ajax.php?area='+area+'&field_name='+field+'&field_value='+document.getElementById(field).value+'&item_id='+item_id, true);
		httpObject.send(null);
		httpObject.onreadystatechange = setOutput;
		globalEditField=field;
	}
}
//
function setOutput()
{
	if(httpObject.readyState == 4)
	{
		ar=httpObject.responseText.split('{AND}');
		if (ar[0]=='0')
		{
			document.getElementById('view_'+globalEditField).innerHTML=ar[1];
			hndCancel(globalEditField);
			globalEditField='';
		}
		else
		{
//			alert(httpObject.responseText);
			alert('Error: '+ar[1]);
		}
	}
}
// Get the HTTP Object
function getHTTPObject()
{
	if (window.ActiveXObject) 
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) 
		return new XMLHttpRequest();
	else 
	{
		alert("Your browser does not support AJAX.");
		return null;
	}
} 
//
function findPosX(obj) 
{
	var curleft = 0;
	if (document.getElementById || document.all) 
	{
		while (obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} 
	else if (document.layers) 
		curleft += obj.x;
	return curleft;
}
//
function findPosY(obj) 
{
	var curtop = 0;
	if (document.getElementById || document.all) 
	{
		while (obj.offsetParent) 
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (document.layers)
		curtop += obj.y;
	return curtop;
}
