
//Ajax Request - Begin
function doRequest(where,post,succes,fail) {
  this.succes  = function(result) { succes(result.responseText); }
  this.fail    = function(result) { fail(result.responseText); }
  YAHOO.util.Connect.asyncRequest('POST',where,{success:this.succes,failure:this.fail},post);       
}
//Ajax Request - End
var lastLockedGuid = '';

function findPos(obj) {
    var nleft = 0;
    var ntop = 0;
    if (obj.offsetParent) {
        nleft = obj.offsetLeft
        ntop = obj.offsetTop
        while (obj = obj.offsetParent) {
            nleft += obj.offsetLeft
            ntop += obj.offsetTop
        }
    }
    return [nleft,ntop];
}




function showCallHint(ev,guid,id,img,title,call,record,module,id2,chi) {
	//lock_unlock_Position();
	//if(lastLockedGuid == guid) return;
	var div = document.getElementById('hint_div_'+guid);
	
	if(div.lockPosition == true && div.ec_call_record == record) return;
	else lock_unlock_Position();
	div.ec_call_record = record;
		
	div = ShowAdditionalDetails(title, div, call, module); 
	div.ec_number = call;
	div.ec_record = record;
	div.ec_module = module;
	div.ec_id = id2;
	div.ec_name = title;
	div.ec_chi = chi;
	
	if(!div.ec_number) {
		document.getElementById("EcmCallsStatusPhoneIcon").style.display = "none";
	} else {
		document.getElementById("EcmCallsStatusPhoneIcon").style.display = "";
	}	
	
	if(!div.ec_module || !div.ec_id) {
		document.getElementById("EcmCalls_search_img").style.display = "none";
		document.getElementById("EcmCalls_view_detail_img").style.display = "none";
	} else {
		document.getElementById("EcmCalls_view_detail_img").style.display = "";
		document.getElementById("EcmCalls_search_img").style.display = "";
	}
	
	if(chi && chi != '') {
		var chi_span = document.getElementById('EcmCallsCallHintIcon_'+chi);
		if(chi_span) {	
			document.getElementById("EcmCallsStatusPhoneIcon").innerHTML = chi_span.innerHTML;
		}
	}
	
	if(!div) return;
	//if(div.parentNode !== document.body) { div.parentNode_ = div.parentNode; document.body.appendChild(div); }
	
	div.style.display = '';
	

	
/*	if(typeof(img) != "undefined") {
		var position = findPos(img);
		div.style.left = position[0]-div.offsetWidth;
		div.style .top = position[1];
	} else {
	*/
		ev = ev || window.event;
		var x = 0; var y = 0;
		if(ev.pageX || ev.pageY) {
			x = ev.pageX;
			y = ev.pageY;
		} else {
			x = ev.clientX + document.body.scrollLeft - document.body.clientLeft,
			y = ev.clientY + document.body.scrollTop  - document.body.clientTop
	    }


	var ws = getWindowVisibleSize();
	var sc = getScrollXY();
	var container = document.getElementById("EcmCallsAdditionalDetailsContainer");
	var ofw = x-sc[0]+div.offsetWidth+5;

	if(typeof(img) != "undefined" || ofw > ws[0] ) {
		x = x-10-div.offsetWidth;
		document.getElementById("EcmCallsAdditionalInfoTopButtons").style.cssFloat = "right";
		document.getElementById("EcmCallsAdditionalInfoTopButtons").style.styleFloat = "right";	
	} else {
		x = x+10;
		document.getElementById("EcmCallsAdditionalInfoTopButtons").style.cssFloat = "left";
		document.getElementById("EcmCallsAdditionalInfoTopButtons").style.styleFloat = "left";	
    }

	var table1 = document.getElementById("EcmCallsAdditionalDetailsHeader");
	var table1td = document.getElementById("EcmCallsAdditionalDetailsHeaderTD");
	var table1div = document.getElementById("EcmCallsAdditionalDetailsHeaderDIV");	
	var table2 = document.getElementById("EcmCallsAdditionalDetailsFooter");
	var table2td = document.getElementById("EcmCallsAdditionalDetailsFooterTD");
	var table2div = document.getElementById("EcmCallsAdditionalDetails_description");

	if((y-sc[1]+div.offsetHeight+5) > ws[1]) {
		if(table1) container.appendChild(table1);
		table1.className = "olFgClass";
//		table1td.className = "olFgClass";
//		table1div.className = "olFontClass";
		table2.className = "olCgClass";
//		table2td.className = "olCgClass";
//		table2div.className = "olCapFontClass";
		y = y + 5 - div.offsetHeight;
	} else {
		if(table2) container.appendChild(table2);
		table2.className = "olFgClass";
//		table2td.className = "olFgClass";
//		table2div.className = "olFontClass";
		table1.className = "olCgClass";
//		table1td.className = "olCgClass";
//		table1div.className = "olCapFontClass";
		y  = y - 5;		
	}
	
	div.style.left = x;
	div.style.top  = y;	

	
//		var tds = div.getElementsByTagName('td');
//		for(var i=0; i<tds.length; i++) tds[i].style.backgroundColor = div.style.backgroundColor;

}

function lock_unlock_Position(guid,img) {
	if(lastLockedGuid != '') { var tmp = lastLockedGuid; lastLockedGuid = ''; lock_unlock_Position(tmp); } 
	if(typeof(guid) == "undefined") return;
	var div = document.getElementById('EcmCallsAdditionalDetails');
	if(div) {
		var div_guid = document.getElementById('hint_div_'+guid);
		if(div_guid && img) div_guid.imgNote = img;
		if(typeof(div_guid.lockPosition) == "undefined") div_guid.lockPosition = false;
		div_guid.lockPosition = !div_guid.lockPosition;
		if(div_guid.lockPosition) {
			if(typeof(div_guid.old_note) == "undefined") {
				var ttt = div_guid.getElementsByTagName('textarea');
				if(ttt[0])
					div_guid.old_note = ttt[0].value; 
				lastLockedGuid = guid; 
			} 
		}
		else {  
			hideCallHint(null,guid); lastLockedGuid = ''; div_guid.ec_call_record = ''; 
		}	
	}
}

function show_hide_Position(guid,img) {
	var div = document.getElementById('EcmCallsAdditionalDetails');
	var div_guid = document.getElementById('hint_div_'+guid);
	if(div) {
		if(div.style.display == "" && div_guid.lockPosition == true) {
			div.style.display = "none";	
		} else if(div.style.display == "none") {
			div.style.display = "";	
		}
		if(div_guid) {
			if(typeof(div_guid.lockPosition) == "undefined" || div_guid.lockPosition == false) {
				lock_unlock_Position(guid, img);
			}
		}
	}
}

function hideCallHint(ev,guid,id,img) {
	var div = document.getElementById('EcmCallsAdditionalDetails');
	var div_guid = document.getElementById('hint_div_'+guid);

		if(div && div_guid && div_guid.lockPosition != true) {
			div = ShowAdditionalDetails("");
			//if(div.parentNode === document.body) div.parentNode_.appendChild(div);// = div.parentNode; document.body.appendChild(div); }
			div.style.display = 'none';
		}
}

function saveNote(guid,id) {
	var div = document.getElementById('hint_div_'+guid);
	var note = div.getElementsByTagName('textarea')[0].value;
	var guid = guid;
	if(note != div.old_note)
		doRequest(
			'index.php',
			'&to_pdf=1&module=EcmCalls&action=AjaxSave&record='+id+'&note='+note,
			function(result) { 
				lock_unlock_Position(); 
				var div = document.getElementById('hint_div_'+guid);
				var cell = div.parentNode_;//.parentNode;
				div.old_note = note;
				if(note == '') img = "modules/EcmCalls/images/hint_grey.gif"; else img = "modules/EcmCalls/images/hint_black.gif";
				if(div.imgNote) div.imgNote.src = img;
				//alert(cell);
				//cell.getElementsByTagName('img')[0].src = img;				var img = '';
			},
			function(result) {}
		);
	else lock_unlock_Position();
}

function cancelNote(guid) {
	var div = document.getElementById('hint_div_'+guid);
	if(div) {
		div.getElementsByTagName('textarea')[0].value = div.old_note;
		lock_unlock_Position();
	}
}

function getWindowVisibleSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}