//Declare local/global variables
var ie = (document.all) ? 1 : 0;
var ns = (document.layers) ? 1 : 0;

var _id;
var _noteType;
var _noteCategory;
var _agentId;
var _panelName;

// Used by DisplayNote and LoadNote functions
var NoteXHttp;
var NoteX, NoteY;

// Used by DisplayPricingGroup and PricingGroupHandler functions
var pgXHttp; // Pricing  Group XML
var PricingX = 0, PricingY = 0;
var _panelname;

// Variables used by 
var initialize = 0;
var Ex, Ey, topColor, subColor, ContentInfo;

var ContentWidth = 150;
if(ns){
	doc = "document."; 
	sty = "";
	Ex = "e.pageX"
	Ey = "e.pageY"
	window.captureEvents(Event.MOUSEMOVE)
	window.onmousemove=overhere
	
	topColor = "#808080"
	subColor = "#C0C0C0"
}
if(ie){
	doc = "document.all."; 
	sty = ".style";
	Ex = "event.x"
	Ey = "event.y"
	
	topColor = "#808080"
	subColor = "#C0C0C0"
}


function MoveToolTip(layerName, FromTop, FromLeft, e) {
	
	if(document.all) 
		var lyr = document.all[layerName];
	else 
		var lyr = document.getElementById(layerName);
		
	lyr.style.top = eval(FromTop) + ((ns) ? document.body.scrollTop : 0);	
	lyr.style.left = eval(FromLeft) + 15;
}

function ReplaceContent(layerName) {
	if(document.layers) {
		with(document.layers[layerName].document) 
		{ 
		   open(); 
		   write(ContentInfo); 
		   close(); 
		}
	}
	else {
		document.getElementById(layerName).innerHTML = ContentInfo;
	}	
}



function Activate(){initialize=1;}
function deActivate(){
	initialize=0;
	//eval(doc + "ToolTip" + sty + ".visibility = 'hidden'")
	if(document.layers)
		document.layers["ToolTip"].style.visibility = 'hidden';
	else		
		document.getElementById("ToolTip").style.visibility = 'hidden';
}


function overhere(e) {
	if(initialize) {
		MoveToolTip("ToolTip", Ey, Ex, e);
		//eval(doc + "ToolTip" + sty + ".visibility = 'visible'")
		if(document.layers)
			document.layers["ToolTip"].style.visibility = 'visible';
		else
			document.getElementById("ToolTip").style.visibility = 'visible';
	}	
	else {
		MoveToolTip("ToolTip", 0, 0)
		//eval(doc + "ToolTip" + sty + ".visibility = 'hidden'")
		if(document.layers)
			document.layers["ToolTip"].style.visibility = 'hidden';
		else		
			document.getElementById("ToolTip").style.visibility = 'hidden';
	}
}

function EnterContent(layerName, TTitle, TContent, TWidth){

	ContentInfo = '<table border="0" width="' + TWidth + '" cellspacing="0" cellpadding="0">'+
		'<tr><td width="100%" bgcolor="#000000">'+
		
		'<table border="0" width="100%" cellspacing="1" cellpadding="0">'+
		'<tr><td width="100%" bgcolor='+topColor+'>'+
		
		'<table border="0" width="90%" cellspacing="0" cellpadding="0" align="center">'+
		'<tr><td width="100%">'+
		
		'<font class="tooltiptitle">&nbsp;'+TTitle+'</font>'+
		
		'</td></tr>'+
		'</table>'+
		
		'</td></tr>'+
		
		'<tr><td width="100%" bgcolor='+subColor+'>'+
		
		'<table border="0" width="90%" cellpadding="0" cellspacing="1" align="center">'+
		
		'<tr><td width="100%">'+
		
		'<font class="tooltipcontent">'+TContent+'</font>'+
		
		'</td></tr>'+
		'</table>'+
		
		'</td></tr>'+
		'</table>'+
		
		'</td></tr>'+
		'</table>';
	
	
	ReplaceContent(layerName)

}
function ChangeWidth(val) {
	if(val !='' && !isNaN(val) && val>0) ContentWidth=val;
}


/*
FUNCTION:	ChangeImage
PURPOSE:	To provide a consistent approach to handling hover images or other reasons to replace an image source.
AUTHOR:		Harold Price
*/
function ChangeImage(imgName, imgSrc) {
	var img = (document.all)? document.all[imgName] : document.getElementById(imgName);
	img.src=imgSrc;
}
/*
FUNCTION:	AddToFavorites
PURPOSE:	Opens a user dialog window to Add the current page to the brownser favorites/bookmarks.
AUTHOR:		Harold Price
*/
function AddToFavorites() {
	window.external.addFavorite(window.document.location,  window.document.title);
}

// PURPOSE: Loop through nodes of an xml nodes and return an array of attributes 
function xmlAttributes(oNode){
	var tAttributes={};
	if(oNode.attributes) {
		for (var i=0; i<oNode.attributes.length; ++i)		
			tAttributes[oNode.attributes.item(i).name]=oNode.attributes.item(i).value;
	}
	return tAttributes;
}

// PURPOSE: Return array of nodes of an xml document
function xmlChildNodes(oParent,sChildName){
	var tNodes=[];
	var e=oParent.firstChild;
	while(e!=null){
		if(e.nodeName==sChildName) {
			tNodes.push(e);
		}
		e=e.nextSibling;
	}
	return tNodes;
}	

// PURPOSE: Return array of nodes of an xml document
// This is pretty much not used at all on our site
function AJAXRequest(xmlSRC, actionEvtHndler) {
	var xttpObj;
	if (window.XMLHttpRequest) { // Mozilla - based browser 
		xttpObj = new XMLHttpRequest(); 
		xttpObj.onreadystatechange = actionEvtHndler;	
		xttpObj.open("GET", xmlSRC, true);		
		xttpObj.send(null); 
		
	} else if (window.ActiveXObject) {  // to see if we are running in IE 
		xttpObj = new ActiveXObject("Msxml2.XMLHTTP");
		xttpObj.onreadystatechange = actionEvtHndler;	
		xttpObj.open("GET", xmlSRC, false);	
		xttpObj.send();   
	}
	return xttpObj;
}

/*
FUNCTION:	DisplayNote
PURPOSE:	Makes the call to the XML generator and Hooks up with the handler.
AUTHOR:		Harold Price
*/
function DisplayNote(noteTypeCode, noteCategoryCode, id, agentId, panelName, e) {
	var xmlFile = "/Resources/Notes.cfm?Code=" + noteTypeCode + "&CatCode=" + noteCategoryCode + "&id=" + id + "&agentid=" + agentId;
	
	_id = id;
	_noteType = noteTypeCode;
	_noteCategory = noteCategoryCode;
	_agentId = agentId;
	_panelName = panelName;
	
	//window.document.style.cursor = 'hourglass';
	
	if(ie){ NoteX = event.clientX; NoteY = event.clientY; }	
	if(ns){ NoteX = e.pageX;NoteY = e.pageY;}
	
	if (window.XMLHttpRequest) { // Mozilla - based browser 
		NoteXHttp = new XMLHttpRequest(); 
		NoteXHttp.onreadystatechange = LoadNote;
		NoteXHttp.open("GET", xmlFile, true);	
		NoteXHttp.send(null); 
		
	} else if (window.ActiveXObject) {  // to see if we are running in IE 
		NoteXHttp = new ActiveXObject("Msxml2.XMLHTTP");
		NoteXHttp.onreadystatechange = LoadNote;
		NoteXHttp.open("GET", xmlFile, false);	
		NoteXHttp.send();   
	}
}
/*
FUNCTION:	LoadNote
PURPOSE:	Displays a new layer, from an XML document, containing the notes for a specific object. Can be used anywhere by anyone.
AUTHOR:		Harold Price
*/
function LoadNote() {		
	if (NoteXHttp.readyState==4){
		var arrNodes=Array();			
		if (window.XMLHttpRequest)
			arrNodes = xmlChildNodes(NoteXHttp.responseXML.documentElement, "note");
		else 
			arrNodes = NoteXHttp.responseXML.selectNodes("//note");		
		var pnlContainer = document.getElementById(_panelName);	
		pnlContainer.style.display = 'none';
		if(arrNodes.length >0) 
		{
						
			var attrib;
			var noteId;
			var authorId;
			var authorName;
			var createdDateTime;
			var invoiceno;
			var pmtrefid;
			var csinvpaymentid;
			var noteText;	
			
			var htmlNotes = '<table width="100%" cellpadding="1" cellspacing="1" border="0" bgcolor="#FFFFCD">';	
			htmlNotes += '<tr bgcolor="silver"><td class="small" valign="top" style="font-family:Arial, Helvetica, sans-serif; font-size: 14pt; font-weight: bold;"><b>' + arrNodes.length + ((arrNodes.length==1) ? ' Note':' Notes') + '</b></td>';
			htmlNotes += '<td class="small" valign="top" align="right"><b><a href="javascript:CloseNote();">Close</a></b></td></tr>';
			
				
			for(var i=0; i<arrNodes.length; i++) {
				attrib = xmlAttributes(arrNodes[i]);
				
				
				noteId = attrib['id'];
				authorId = attrib['authorid'];
				authorName = attrib['authorname'];
				createdDateTime = attrib['created'];
				invoiceno = ((typeof(attrib['invoiceno'])!= 'undefined')?attrib['invoiceno']:'');
				pmtrefid = ((typeof(attrib['pmtrefid'])!= 'undefined')?attrib['pmtrefid']:'');
				csinvpaymentid = ((typeof(attrib['csinvpaymentid'])!= 'undefined')?attrib['csinvpaymentid']:'');
				
				
				if(!document.all) 
					noteText = arrNodes[i].firstChild.nodeValue;
				else
					noteText = arrNodes[i].text;	
					
				
				htmlNotes += '<tr><td class="small" valign="top" rowspan="2">' + noteText.replace("'", "\'") + '</td><td class="small" align="right">' + authorName + '</td></tr><tr><td class="small" align="right">' + createdDateTime + '</td></tr>';
				htmlNotes += '<tr><td class="small" style="border-top: 1px solid gray;" colspan="2"><img src="/images/blank.gif" height="1" width="1" border=0></td></tr>';
								
			}	
				
			
			htmlNotes += '<tr><td class="small" align="center" colspan="2"><a href="##" onclick="javascript:OpenNoteForm(\'' + _noteType + '\', \'' + _noteCategory + '\', \'' + _id + '\');CloseNote();">Click Here to Add a New Note for this Item.</td></tr>';
			htmlNotes += '<' + '/table>';	
			

			var pnlText = document.createElement ("div");
			pnlText.innerHTML = htmlNotes;	
			
			/*
			var pnlCloser = document.createElement ("div");
			var pnlHeader = document.createElement ("div");
			
			pnlCloser.style.width = '80';
			pnlCloser.style.position = 'relative';
			pnlCloser.style.float = 'right';
			pnlCloser.style.marginRight = '10px';
			pnlCloser.style.backgroundColor = '#c0c0c0';
			
			pnlCloser.style.fontFamily = 'Arial, Helvetica, sans-serif';
			pnlCloser.style.fontWeight = 'bold';
			pnlCloser.style.fontSize = '10px';
			
			pnlHeader.style.width = '200';
			pnlHeader.style.fontFamily = 'Arial, Helvetica, sans-serif';
			pnlHeader.style.fontWeight = 'bold';
			pnlHeader.style.fontSize = '14px';
			pnlHeader.style.backgroundColor = '#c0c0c0';
			
			
			pnlCloser.innerHTML = '<a href="javascript:CloseNote();">Close</a>';
			
			pnlHeader.innerHTML = arrNodes.length + ((arrNodes.length==1) ? ' Note':' Notes');			
			*/
			
			pnlContainer.innerHTML = '';			
			
			// Add Header and Text to Panel
			//pnlContainer.appendChild (pnlCloser);
			//pnlContainer.appendChild (pnlHeader);
			pnlContainer.appendChild (pnlText);			
			
			/*htmlNotes += '<div id="NoteCloser" style="position:relative; margin-right:10px; float:right;background-color: ; font-family:Arial, Helvetica, sans-serif bold; font-size:10px;"><a href="##" onclick="javascript:CloseNote();">Close</a></div>';
			*/


			
			// Move panel to correct location
			if(ie) pnlContainer.style.top = NoteY + document.body.scrollTop;
			if(ns) pnlContainer.style.top = NoteY;			
			pnlContainer.style.left = NoteX + 15;			
			
			// UnHide until finished
			pnlContainer.style.display = 'block';			
					
		}
		else {
				window.status='No Notes found.';
		}
	}
} 

/*
FUNCTION:	LoadNote
PURPOSE:	Hides the panel layer if it was created.
AUTHOR:		Harold Price
*/
function CloseNote() {
	document.getElementById(_panelName).style.display='none';
}

/*
FUNCTION:	OpenNoteForm
PURPOSE:	Opens an new dialog window which will ask for note information.
AUTHOR:		Harold Price
*/
function OpenNoteForm(typeCode, categoryCode, Id) {
	var noteURL = '/AddNote.cfm?Add=true';
	if(typeCode != '') noteURL += '&type=' + typeCode;
	if(categoryCode != '') noteURL += '&category=' + categoryCode;	
	
	switch(typeCode) {
	case 'INV': 
		noteURL += '&InvoiceNo=' + Id;break;
	case 'CNT': 
		noteURL += '&ContactId=' + Id;break;
	case 'AGT': 
		noteURL += '&AgentId=' + Id;break;
	case 'PMT': 
		noteURL += '&PaymentRef=' + Id;break;
	case 'CRD': 
		noteURL += '&CreditId=' + Id;break;
	case 'TR': 
		noteURL += '&TourId=' + Id;break;
	case 'PON': 
		noteURL += '&POId=' + Id;break;
	case 'ADJ': 
		noteURL += '&AdjustmentId=' + Id;break;
	}
	
	var win = window.open(noteURL, 'AddNote', 'width=250,height=350,status=0,statusbar=0,scrollbars=0,resizing=1,resizable=1');
}


function DisplayPricingGroup(pricingGroup, panelName, e) {
		
	if(pricingGroup == '' || pricingGroup == 0) return false;
	_panelname = panelName;
	
	if (!e) var e = window.event||window.Event;
	
	if(typeof(e.pageX) != 'undefined'){PricingX = e.pageX;PricingY = e.pageY;}
	else if(typeof(e.clientX) != 'undefined'){PricingX = e.clientX + document.body.scrollLeft;PricingY = e.clientY + document.body.scrollTop;}
	else{PricingX = document.body.scrollLeft;PricingY = document.body.scrollTop;}
	
	if((pricingGroup == '' || pricingGroup == 0) && (agentId == '' || agentId == 0)) return false;
	
	
	var xmlFile = "/Resources/ProductOptions.cfm";
		xmlFile += '?pricingid=' + pricingGroup;
		
	var pgXHttp = new RemoteProcedure(xmlFile, PricingGroupHandler, null);
		
}

/*
FUNCTION:	PricingGroupHandler
PURPOSE:	Generates a new layer containing a list of production options retrieve from an ajax call
AUTHOR:		Harold Price
*/
function PricingGroupHandler() {	
	if (typeof(pgXHttp) != 'undefined' && pgXHttp.readyState==4)
	{
		var arrNodes=Array();		
		if (window.XMLHttpRequest)
			arrNodes = xmlChildNodes(pgXHttp.responseXML.documentElement, "productoption");
		else 
			arrNodes = pgXHttp.responseXML.selectNodes("//productoption");
		if(arrNodes.length == 0) {	
			alert('Sorry, no pricing options were found for this account.')
		}
		else {

			var strHTML = '';			
			/*
			var strHTML = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
			strHTML += '<html xmlns="http://www.w3.org/1999/xhtml">';
			strHTML += '<head>';
			strHTML += '<title>Obeo Product Option Pricing</title>';
			strHTML += '<link href="http://www.360house.com/stylesheet.css" rel="stylesheet" type="text/css" />';
			strHTML += '</head>';
			strHTML += '<body>';
			*/
			strHTML += '<div align="left"><img src="http://www.obeo.com/Themes/Solutions/en/Media/Images/SolutionsHeader.gif" border=0 alt="Product Option Pricing" />';
			strHTML += '<table border="0" cellspacing="0" cellpadding="0">';
			strHTML += '<tr><td width="5" height="5"><img src="/images/obeo/FrameContainerTopLeftCorner.gif" alt="TopLeftCorner" width="5" height="5"></td><td height="5" style="background-image: url(/images/obeo/FrameContainerTopBackground.gif); background-position:left; background-repeat:repeat-x;"><img src="/images/blank.gif" alt="TopBackground" width="1" height="5"></td><td width="5" height="5"><img src="/images/obeo/FrameContainerTopRightCorner.gif" alt="TopRightCorner" width="5" height="5"></td></tr>';
			
			strHTML += '<tr><td width="5" style="background-image: url(/images/obeo/FrameContainerLeftBackground.gif); background-position:top; background-repeat: repeat-y;"><img src="/images/blank.gif" alt="LeftBackground" width="5" height="1"></td><td valign="top"><table width="100%" bgcolor="#FFFFFF">';
			for(var i=0; i<arrNodes.length; i++) {
				var attrib = xmlAttributes(arrNodes[i]);
				OptionName = attrib['name'];
				RetailPrice = attrib['retailprice'];
				
				strHTML += '<tr><td class="small" align="left" nowrap="nowrap">' + OptionName.replace("'", "\'") + '</td>';
				strHTML += '<td class="small" align="right" nowrap="nowrap">' + RetailPrice + '</td></tr>';
			}
			
			strHTML += '</table></td><td width="5" style="background-image: url(/images/obeo/FrameContainerRightBackground.gif); background-position:top; background-repeat: repeat-y;"><img src="/images/blank.gif" alt="RightBackground" width="5" height="1"></td></tr>';
			strHTML += '<tr><td width="5" height="5"><img src="/images/obeo/FrameContainerBottomLeftCorner.gif" alt="BottomLeftCorner" width="5" height="5"></td><td height="5" style="background-image: url(/images/obeo/FrameContainerBottomBackground.gif); background-position:left; background-repeat:repeat-x;"><img src="/images/blank.gif" alt="BottomBackground" width="1" height="5"></td><td width="5" height="5"><img src="/images/obeo/FrameContainerBottomRightCorner.gif" alt="BottomRightCorner" width="5" height="5"></td></tr></table><div class=smallgray align="right">x,y=' + PricingX  + ',' + PricingY + '</div></div>';
			
			//strHTML += '</body></html>';
			
			try {} catch(er) {
			}
			
			var pnlText = document.getElementById(_panelname);
			pnlText.innerHTML = strHTML;
			
			if(ie) 
				pnlText.style.top = PricingY + document.body.scrollTop-15;
			else if(ns) 
				pnlText.style.top = PricingY-32;			
			else
				pnlText.style.top = document.body.scrollTop;
			pnlText.style.left = PricingX + 15;
			pnlText.style.display = 'block';
		}
	}
} 
// AJAX SCripts
var iRemoteProcedure=0;
var tRemoteProcedures={};
function RemoteProcedure(sUrl,fnOnLoad,fnOnPatience){
	this.req=null;
	this.fnOnLoad=null;
	this.id=++iRemoteProcedure;
	tRemoteProcedures[this.id]=this;
	this.timer=null;
	this.call(sUrl,fnOnLoad,fnOnPatience);
}
RemoteProcedure.prototype.call=function(url,fnOnLoad,fnOnPatience){
	this.fnOnLoad=fnOnLoad;
	this.fnOnPatience=fnOnPatience;
	this.timer=window.setTimeout('tRemoteProcedures['+this.id+'].showPatience()',500);
	var sRand=(url.indexOf('?')==-1?'?':'&')+'Rand='+Math.random();
	if (window.XMLHttpRequest){
		this.req=new XMLHttpRequest();
		this.req.onreadystatechange=new Function('tRemoteProcedures['+this.id+'].processReqChange()');
		this.req.open("GET",url+sRand,true);
		this.req.send(null);
	} else if (window.ActiveXObject){
		this.req=new ActiveXObject("Microsoft.XMLHTTP");
		if (this.req){
			this.req.onreadystatechange=new Function('tRemoteProcedures['+this.id+'].processReqChange()');
			this.req.open("GET",url+sRand,false);
			this.req.send();
		}
	}
}
RemoteProcedure.prototype.showPatience=function(){
	this.clearTimeout();
	if(this.fnOnPatience)
		this.fnOnPatience();
}
RemoteProcedure.prototype.clearTimeout=function(){
	if (this.timer!=null){
		window.clearTimeout(this.timer);
		this.timer=null;
	}
}
RemoteProcedure.prototype.processReqChange=function(){
	if (this.req.readyState==4){ // 4 means "loaded"
		this.clearTimeout();
		if (this.req.status==200){ // 200 means "OK"
			if(this.fnOnLoad)
				this.fnOnLoad(this.req.responseXML);
		}
		else
			alert("Error " + this.req.status + "\n" + this.req.statusText + "\n\n" + this.req.responseText.replace(/\r\n/g,''));
	}
}