jQuery.noConflict();

jQuery(document).ready(function($) {
	$("label.overlabel").overlabel();
	//comment out this call which was setting alternate row background colors to grey
	//applyStyles($);
});

function applyStyles($) {

	$(".dataTable").each(function(i) {
		$(this).find("tbody tr:odd").css('background-color', '#EEE');
	})
	$(".rich-table").each(function(i) {
		$(this).find("tr.rich-table-firstrow:odd").css('background-color', '#EEE');
	})

	$(".command-sourceZZZ").find(".command-target").hide().end().hover(
		function() {
			$(this).find(".command-target").show();
		},
		function() {
			$(this).find(".command-target").hide();
		}
	);
}

function afterAjaxRequest() {
	applyStyles(jQuery);
}

function showLinkInMsg(_msg) {

	if (_msg != null && _msg.indexOf('&lt;') != -1) {
			return _msg.replaceAll('&lt;','<').replaceAll('&gt;','>');
	} else {
		return _msg;
	}
}


function escapes(_document,bIsIframe) {

	if(!_document) _document=document;
	/*
	tgs2 = _document.getElementsByTagName('script');
	//alert(tgs2.length);
	if (!tgs2) return;
	for (var i=0;i<tgs2.length;i++) {
		//alert(tgs2[i].src);
		//alert(tgs2[i].innerHTML);
		//alert(getOuterHTML(tgs2[i]));
		//if (tgs2[i].innerHTML.indexOf('doubleclick') != -1) {
		//if (tgs2[i].innerHTML.indexOf('toto') != -1) {
		if (tgs2[i].src.indexOf('doubleclick') != -1) {
			//alert(tgs2[i].outerHTML);
			//alert(tgs2[i].innerHTML);
			//alert(getOuterHTML(tgs2[i]));
			//alert(tgs2[i].src);
			//alert(tgs2[i].language);
		}
	}
	*/

	tgs = _document.getElementsByTagName('label');
	if (!tgs) return;
	for (var i=0;i<tgs.length;i++) {
		//alert(tgs[i].innerHTML);
		if (tgs[i].innerHTML.indexOf('/&lt;') != -1) {
			tgs[i].innerHTML = tgs[i].innerHTML.replaceAll('/&lt;','<').replaceAll('&gt;/','>');
		}
	}

	/*tgs = _document.getElementsByTagName('td');
	if (!tgs) return;
	for (var i=0;i<tgs.length;i++) {
		//alert(tgs[i].innerHTML);
		if (tgs[i].innerHTML.indexOf('&lt;') != -1) {
			tgs[i].innerHTML = tgs[i].innerHTML.replaceAll('&lt;','<').replaceAll('&gt;','>');
		}
	}*/

}

$.fn.replaceAll = function(from, to) {
//String.prototype.replaceAll = function(from, to) {
	var i = this.indexOf(from);
	var c = this;
	while (i > -1) {
      c = c.replace(from, to);
      i = c.indexOf(from);
    }
	return c;
}

function getOuterHTML(object) {
	var element;
	if (!object) return null;
	element = document.createElement("div");
	element.appendChild(object.cloneNode(true));
	return element.innerHTML;
}

function startUp() {
	escapes();
}

function startDown() {
}

function get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if (!start)
      if (name != document.cookie.substring(0,name.length)) return null;
    if (start == -1)
        return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

//will comment out the counter code for now
function textCounter(field, countfield, maxlimit) {

	if (field.value.length > maxlimit) // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
/*	else
	{
	   var cnt = document.getElementById("mainform:" + countfield);
	   cnt.value = maxlimit - field.value.length;
	}
*/
}

/** Standard popup window open
 * The first parameter will be the new URL.
 * Subsequent parameters will be (if supplied) the width and height.
 * If not specified the width and height will be 955 and 755 respectively.
 */
function openWin()
{
	var url, width=955, height=750, screenX=0, screenY=0, toolbar="no", menubar="no", scrollbars="yes", resizable="yes";
	var args = arguments;

	url = args[0];
	if (args.length>1)
	{
		width=args[1];
	}
	if (args.length>2)
	{
		height=args[2];
	}

	window.open (url, '_blank', 'width='+width+',height='+height+',screenX='+screenX+',screenY='+screenY+',scrollbars='+scrollbars+',toolbar='+toolbar+',menubar='+menubar+',resizable='+resizable);

	return false;
}


window.onload = startUp;
window.onunload = startDown;

/** The next functions are used for the selection of listings in the search results page. */

/** Checks or unchecks all checkboxes passed by name. */
function toggleCheckboxes(checkItFldObj, form) {
	if (checkItFldObj.checked) {
		checkAll(form);
    } else {
        uncheckAll(form);
    }
}

/** Checks to see if at least on checkbox is checked. */
function someChecked(form) {

		list = form.elements;
        for (i = 0; i < list.length; i++) {
            if (list[i].checked == true ) {
              return true;
           	}
        }
        alert('No check-boxes have been checked.');
        return false;

}

/** Checks all checkboxes. */
function checkAll(form) {
		list = form.elements;
        for (i = 0; i < list.length; i++) {
            list[i].checked = true ;
        }

}

/** Unchecks all fields. */
function uncheckAll(field) {
    if (!field.length) {
        field.checked = false;
    } else {
        for (i = 0; i < field.length; i++) {
            field[i].checked = false ;
        }
    }
}





/* Unchecks the 'Select All' checkbox if an item is unchecked and to check to Select All if all elements are checked. */
function toggleSelectAllBox(checkItFldObj,fldObj) {

	if ((fldObj.checked == false) && (checkItFldObj.checked == true)) {
		checkItFldObj.checked = false;
	}

    if ((fldObj.checked == true) && (checkItFldObj.checked == false)) {
		//list = document.getElementsByName("chkbxFldName");
		list = document.forms['searchResults'].elements;

    	count = 0;
    	for (i = 0; i < list.length; i++) {
			if(list[i].checked) {
	    		count = count + 1;
			}
    	}

    	if(count == list.length) {
    	alert(count);alert(list.length);
        	checkItFldObj.checked = true;
    	}
	}
}


/* Problems exist when a conversation is terminated and user hits
 BACK button and navigates to a page that requires a conversation
*/
function checkConversationCookie()
{
 	var cookie = get_Cookie("conversation");
	if (cookie == null) {
	  window.location.replace("/public/home.jsf");
	}
}
