// The following variables are set by the GCMWebPage and should be available to all functions:
//	SiteSearchURL	- Example: /tools/search/search.aspx
//	SiteFacebookURL	- Example: http://www.gcm.com/general/facebook.asp
var timeoutId;
var attempts = 0;

function changeHoverImage(newImageType) {
	var startPos = event.srcElement.src.lastIndexOf('_');
	var endPos = event.srcElement.src.lastIndexOf('.');
	var length = event.srcElement.src.length;
	
	event.srcElement.src = event.srcElement.src.substring(0, startPos+1) + newImageType + event.srcElement.src.substring(endPos, length);
}

function adjustIFrameSize(id)
{
	//This function should be called by a resource embedded within another page (i.e. a page in an iframe)
	var myIFrame = document.getElementById(id);
	var loaded;
	if (myIFrame)
	{
	
		loaded = (myIFrame.contentDocument && myIFrame.contentDocument.defaultView) ?
			myIFrame.contentDocument.defaultView.loaded : ((myIFrame.Document) ?
			(myIFrame.Document.readyState == "complete") : false);
		if (!loaded){
			if (attempts++ < 5){
				timeoutId = setTimeout("adjustIFrameSize('" + id + "')",500);
			} else {
				attempts = 0;
			}
		} else {
			//if not already stored, store the initial size of the layer. We will use
			//this as the size to which the panel will reduce before being set to the
			//larger size if necessary. This is really to improve user experience when
			//the iframe content is actually smaller than the initial size of the iframe.
			//Rather than have it shrink to a small size and then maybe expand, we will
			//store the initial size. If the content is smaller, it won't reduce the
			//size any smaller than the initial default.
			if (myIFrame.contentDocument &&	myIFrame.contentDocument.body.offsetHeight)
			{
				myIFrame.height = myIFrame.contentDocument.body.offsetHeight;
			}
			else if (myIFrame.Document && myIFrame.Document.body.scrollHeight)
			{
				if (!myIFrame.initialHeight) myIFrame.initialHeight = myIFrame.height;
				myIFrame.height = myIFrame.initialHeight;
				myIFrame.height = myIFrame.Document.body.scrollHeight;
			}
			attempts = 0
		}
	}						
}
function ShowToolDialog(toolDialogName)
{
	var toolsLayer = document.all["toolsLayer"];
	var toolLayer = document.all[toolDialogName];

	if (toolsLayer != undefined && toolLayer != undefined)
	{
		if (toolsLayer.style.display == "") //toolsDialog is already visible
		{
			if (toolLayer.style.display == "")
			{
				//requested tool is already visible (so hide it). Also,
				//hide the main layer
				toolLayer.style.display = "none";
				toolsLayer.style.display = "none";
			} else {
				//requested tool is not visible so make sure all others are hidden first and then make the requested one visible.
				for (i=0;i<toolsLayer.children.length;i++)
				{
					toolsLayer.children[i].style.display = "none";
				}
				toolLayer.style.display = "";
				//The tool layer should now be visible so we would like 
				//to move the cursor to the first available input box.
				SelectFirstInput(toolLayer);
			}
		} else {
			toolsLayer.style.display = "";
			toolLayer.style.display = "";
			SelectFirstInput(toolLayer);
		}
	}
}
function SelectFirstInput(obj)
{
	for (i=0;i<obj.all.length;i++)
	{
		if (obj.all[i].tagName == "INPUT")
		{
			obj.all[i].select();
			break;
		}
	}

}

function ClickButtonOnEnter(buttonName,evt)
{
	evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (charCode == 13 || charCode == 3)
	{
		var btn = document.all[buttonName];
		if (typeof(btn) != undefined)
		{
			btn.click();
		}
		return false;
	}
	return true;
}

function LaunchSiteSearch(fieldname)
{
	var textbox = document.all[fieldname];
	var searchString = "";
	if (typeof(textbox) != undefined && typeof(SiteSearchURL) != undefined)
	{
		searchString = (textbox.value == "" ? "" : "?search=" + textbox.value);;
		location = SiteSearchURL + searchString;
	} else {
		window.status = "Unable to launch site search at this time.";
	}
	return false;
}

function LaunchFacebookSearch(fieldname)
{
	var faceTextbox = document.all[fieldname];
	var searchString = "";
	if (typeof(faceTextbox) != undefined && typeof(SiteFacebkURL) != undefined)
	{
		searchString = (faceTextbox.value == "" ? "" : "?u=" + faceTextbox.value);;
		void window.open(SiteFacebkURL + searchString,'Facebook','toolbar=no,width=700,height=650,directories=no,status=no,scrollbars=yes,resizable=yes,menubar=no');
	} else {
		window.status = "Unable to launch facebook search.";
	}
	return false;
}

function preLoadImage(image){
	var imageTmp=new Image();
	imageTmp.src= image;
}

function changeimage(dups,menuframe,changeto){
	if (dups>0){
		for (var i=0;i<dups;i++){
			document.images[menuframe + i].src= changeto;
		}
	} else {
		img = eval('document.images.' + menuframe);
		if (typeof img != 'undefined'){
			img.src = changeto;
		}
	}
}

function GetElementFocus(objectname)
{
	try {
	var object = document.all[objectname];
	if (typeof(object) != undefined) object.focus();
	} catch (e){}
	return true;
}

var openWinParms = "resizable,scrollbars,width=780,height=700";

function openWin(url,name) {
	popupWin = window.open(url.replace(/ /g,"%20"),"opennav",openWinParms);
}
function openPrint(url,name) {
    printWin = parent.window.open(url.replace(/ /g,"%20"),"opennavp","menubar,scrollbars,resizable,width=600,height=500");
}

function confirmNav(url,text){
if (confirm(text)) {
	if (url!=''){ location.replace(url); }
	return true;
	}
}

function chkChanges(url){
	//This function checks if there are any unsubmitted changes on this page.
	//This relies on changes being flagged by other actions in the page by setting the unSavedChanges variable to True.
	if (typeof unSavedChanges == "undefined"){ unSavedChanges = false }
	if (unSavedChanges == true){
		if (confirm("You have unsaved changes! \nPress CANCEL to stop and save your changes or OK to continue and lose changes")) {
			if (url==''){window.close();}else{location.replace(url)};
		return true;
		}
	} else {
		if (url==''){window.close();}else{location.replace(url)};
		return true;
	}
}

//***Standard Functions used across the site for form validation***//
function chkFldSelected (formItem, name) {
	//We are checking that a field is not blank
	if (formItem.value == '') { 
		alert("\nYou must specify a " + name + ".\n\nThank You");
		return false;
	}
	return true;
}

function chkSelected (formItem, name) {
	//We are checking that an item on a listbox is selected
	if (formItem.type == "select-one") {
		if (formItem.selectedIndex == 0) { 
			alert("\nPlease select a " + name + " from the list.\n\nThank You");
			return false;
		}
	}
	return true;
}

function chkImgSelected(strCollection,intId,formField,strPrompt,bolPrompt){
	var maxCount = eval(strCollection + "_max");
	formField.value = -1;
	for (var i=0;i<maxCount;i++){
		imgName = eval('document.images.' + strCollection + '_' + intId + '_' + i + '.src');
		if (imgName){
			if (imgName.search(/_on.gif/i)>0) {
				formField.value = i;
			}
		}
	}
	if (formField.value == -1 && imgName.search(/_grey/i)==-1 && bolPrompt) {
		alert("\nPlease select a " + strPrompt + ".\n\nThank You");
		return false;
	}
	return true;
}

function chkListSelected(formField,strPrompt,bolPrompt){
	var strChecked = false;
	//We are checking that one of a collection of checkboxes is checked
	for (var i=0;i<formField.length;i++){
		if (formField[i].checked){
			strChecked = true;
		}
	}
	if (strChecked != true && bolPrompt){
		alert("\nPlease select a " + strPrompt + ".\n\nThank You");
		return false;
	}
	return true;
}

//***End - Form Validation Functions ***//
	
function openPopUp(url,name,parms) {
    //if (url.indexOf("?") == -1) {
    //	url +=  "?"
    //} else {
    //	url +=  "&"
    //}
    //d = new Date();
    //url += "ts=" + d.getTime();
    popupWin = parent.window.open(url.replace(/ /g,"%20"),name,parms);
    return 0;
}

function isEmail(str) {
	// are regular expressions supported?
	var supported = 0;
	if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	}
	if (!supported){
		if ((str.indexOf(".") > 2) && (str.indexOf("@") > 0)){
			return true;
		} else {
			alert("\nYou must specify a valid EMail Address (i.e. in the format: user@domain).\n\nThank You");
			return false;
		}			
	}
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^([a-zA-Z0-9_]|\\-|\\.|\\')+@(([a-zA-Z0-9_]|\\-)+\\.)+[a-zA-Z]{2,4}$");

	if (!r1.test(str) && r2.test(str)) {
		return true;
	} else {
		alert("\nYou must specify a valid EMail Address (i.e. in the format: user@domain).\n\nThank You");
		return false;
	}	
}

function ltrim(str) { var s=str; while(s.charAt(0)==' ')s=s.slice(1); return(s)}
function rtrim(str) { var s=str; while(s.charAt(s.length-1)==' ')s=s.slice(0,-1); return(s)}
function trim(str) { return ltrim(rtrim(str)) }

function drc () { return 0; }
function src () { return 0; }
function drs () { return 0; }

function dlc () { return 0; }
function slc () { return 0; }
function dls () { return 0; }


function dcc () { return 0; }
function scc () { return 0; }
function dcs () { return 0; }

function nd  () { return 0; }
function loadBio () { return 0; }
function fadeInit() { return 0; }
function ckImg() { return 0; }
function camLoad() { return 0; }
//unSavedChanges = false;

void function gcmvoid () {}

//rmt 11/21/5 clipboard functionality
var ddPostingType;

function DistributeItem(itemId, itemType, postingType)
{
	ddPostingType = postingType;
	ClipboardApp.FillSingleItemClipboard(itemId, itemType, FillSingleDocumentClipboard_CallBack);
}

function DistributeDocument(docCode, postingType)
{
	ddPostingType = postingType;
	ClipboardApp.FillSingleDocumentClipboard(docCode, FillSingleDocumentClipboard_CallBack);
}

function FillSingleDocumentClipboard_CallBack(response)
{
	if (response != null && response.value != null)
	{
		if (response.error != null)
		{
			alert("ERROR in FillSingleDocumentClipboard_CallBack");
			alert(response.error);
			return;
		}
		else
		{
			if (response.value == true)
			{
				var now = new Date();
				var dummy = now.getMinutes().toString() + '-' + now.getMilliseconds().toString();
				window.open('/Tools/Messaging/Mail.aspx?singleDocMode=true&dummy=' + dummy + '&postingType=' + 
					ddPostingType, 'mailDistDoc', 'height=760, width=1000, resizable=yes, scrollbars=no');
			}
		}
	}
}

