// -----------------------
// getViewportSize
// -----------------------	

function getViewportSize () {
	var viewportwidth;
	var viewportheight;
	var sizeArr = new Array();
	
	// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	
	if (typeof window.innerWidth != 'undefined')
	{
			viewportwidth = window.innerWidth,
			viewportheight = window.innerHeight
	}
	
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	
	else if (typeof document.documentElement != 'undefined'
		 && typeof document.documentElement.clientWidth !=
		 'undefined' && document.documentElement.clientWidth != 0)
	{
			 viewportwidth = document.documentElement.clientWidth,
			 viewportheight = document.documentElement.clientHeight
	}
	
	// older versions of IE
	
	else
	{
			 viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
			 viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}

	sizeArr["width"] = viewportwidth;
	sizeArr["height"] = viewportheight;
	
	return sizeArr;
}

// -----------------------
// popUp
// -----------------------	
function popUp(URL,width,height) {
	day = new Date();
	id = day.getTime();
	userWidth = ((window.screen.width-980)/2);
	userHeight = ((window.screen.height-748)/2);
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width="+ width +",height="+ height +",left = "+ userWidth +",top ="+ userHeight +"');");
}

// -----------------------
// getStyleValue
// -----------------------	
function getStyleValue (aHtmlObjId, aStyleProp) {
	var htmlObj = document.getElementById(aHtmlObjId);
	if (htmlObj.currentStyle) {
		var styleValue = htmlObj.currentStyle[aStyleProp];
	} else if (window.getComputedStyle) {
		var styleValue = document.defaultView.getComputedStyle(htmlObj, null).getPropertyValue(aStyleProp);
	}
	return styleValue;
}

// -----------------------
// showDialog
// -----------------------
function showDialog(x) {
	
	var transCoverHtmlObj = document.getElementById('transCover');
	var dialogHtmlObj = document.getElementById(x);
	var vpWidth;
	var vpHeight;
		
	//Show div obscuring a site view
	transCoverHtmlObj.style.display = 'block';
	
	//get viewport's width and height
	vpSize = getViewportSize();
	vpWidth = vpSize["width"]; //parseInt(self.innerWidth);
	vpHeight = vpSize["height"]; //parseInt(self.innerHeight);
	
	//get dialog's width and height
	dialogWidth = parseInt(getStyleValue(x, 'width'));
	dialogHeight = parseInt(getStyleValue(x, 'height'));
	
	//calculate position
	dialogTop = parseInt((vpHeight/2) - (dialogHeight/2));
	dialogLeft = parseInt((vpWidth/2) - (dialogWidth/2));
	
	//Position the Dialog
	//alert(dialogHtmlObj.style.top);
	dialogHtmlObj.style.top = dialogTop + "px";
	dialogHtmlObj.style.left = dialogLeft + "px";
	
	//Show dialog div
	a = dialogHtmlObj.style.display="block";
}

function closeDialog(aDialogId) {
	// hide dialog
	var dialogHtmlObj = document.getElementById(aDialogId);
	dialogHtmlObj.style.display = "none";
	
	// hide obscuring div
	document.getElementById('transCover').style.display = 'none';
}


// -----------------------
// generateItemPreview
// -----------------------
function attachItemPreview(aParentNodeId) {
	itemTitle = document.getElementById('itemTitleInput').value;
	itemDescr = document.getElementById('itemDescrInput').value; //.innerHTML

	// show preview
	dialogContentHtmlObj = document.getElementById(aParentNodeId);
	if ((itemTitle == '') && (itemDescr == '')) { 
		previewContentStr = '<center><strong>There is nothing to preview.</strong><br />Please fill in \"Job Title\" and/or \"Job Description\" first.</center>'; 
	} else {
		previewContentStr = '<h2>' +itemTitle +'</h2>' +itemDescr //unescapeHTML(jobTitle + jobDescr);
	}
	dialogContentHtmlObj.innerHTML = previewContentStr;
}

// -----------------------
// escapeHTML
// -----------------------
function escapeHTML (str)
{
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
};

// -----------------------
// unescapeHTML
// -----------------------
function unescapeHTML(html) {
   var htmlNode = document.createElement("DIV");
   htmlNode.innerHTML = html;
   if(htmlNode.innerText)
   return htmlNode.innerText; // IE
   return htmlNode.textContent; // FF
}

// ----------------------------------------
// checkIfNameExistSaveClose
// ----------------------------------------
function checkIfNameExistSaveClose(aInputName) {
	var existingItemNamesArr = itemNamesStr.split(',');
	var itemName = document.getElementById('itemNameInput').value;
	var itemNameAlreadyExist = 0;
	for (actNo in existingItemNamesArr) {
		actName = existingItemNamesArr[actNo];
		if (actName == itemName) {
			itemNameAlreadyExist = 1;
			break;
		}
	}
	if (itemNameAlreadyExist) {
		closeDialog('saveItemAs');
		showDialog('overwriteItemDialog');
	} else {
		closeDialog('overwriteItemDialog');
		closeDialog('saveItemAs');
		document.getElementById('actionInput').value = 'save';
		document.itemsForm.submit();
	}
}

// ----------------------------------------
// saveItem
// ----------------------------------------
function saveItem(aInputName) {
	document.getElementById('actionInput').value = 'save';
	var itemListMessageBar = document.getElementById('systemMessage');
	itemListMessageBar.innerHTML = '<p><strong>Saving...</strong></p>';
	document.itemsForm.submit();
}
	
// ----------------------------------------
// saveList
// ----------------------------------------
function saveList() {
	document.getElementById('actionInput').value = 'saveList';
	var itemListMessageBar = document.getElementById('systemMessage');
	itemListMessageBar.innerHTML = '<p><strong>Saving...</strong></p>';
	document.itemsForm.submit();
}

// ----------------------------------------
// numbersOnly
// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
// ----------------------------------------
function numbersOnly(myfield, e, dec)	{
	var key;
	var keychar;
	
	if (window.event) {
		key = window.event.keyCode;
	}	else if (e) {
		key = e.which;
	}	else {
		return true;
	}
	keychar = String.fromCharCode(key);
	
	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) {
		return true;
		
	// numbers
	}	else if ((("0123456789").indexOf(keychar) > -1)) {
		return true;

	// decimal point jump
	}	else if (dec && (keychar == ".")) {
			myfield.form.elements[dec].focus();
			return false;
	
	// otherwise
	} else {
		return false;
	}
}
