var winLocationFull = String(window.location);
var tempLoc = winLocationFull.split('?');
var winLocation = tempLoc[0];
if(tempLoc[1]){
	var winLocationVars = tempLoc[1];
}else{
	var winLocationVars = '';
}
var mouseX = 0;
var mouseY = 0;
document.onclick=getMouseCoordinates;
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false
// If NS -- that is, !IE -- then set up for mouse capture
//if (!IE) document.captureEvents(Event.MOUSEMOVE)
// Main function to retrieve mouse x-y pos.s
function getMouseCoordinates(e) {
 var tempX = 0; 
 var tempY = 0; 
 if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft
    tempY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX
    tempY = e.pageY
  }
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  mouseX = tempX;
  mouseY = tempY;
}
var scrollSave;
function saveScrollPos(){
	if (window.pageYOffset){
		scrollSave = window.pageYOffset;
	} else {
		scrollSave = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
	}
}
function getElementPosition(elemID){
	var offsetTrail = document.getElementById(elemID);
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail){
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined'){
		offsetLeft += document.body.leftMargin;
		offsetTop += document.body.topMargin;
	}
	return {left:offsetLeft,top:offsetTop};
}
function selectAll(FormName, FieldName, CheckValue){
  if(!document.forms[FormName]){
	return;
  }
  var objCheckBoxes = document.forms[FormName].elements[FieldName];
  if(!objCheckBoxes){
	return;
  }
  var countCheckBoxes = objCheckBoxes.length;
  if(!countCheckBoxes){
	objCheckBoxes.checked = CheckValue;
  }else {
    for(var i = 0; i < countCheckBoxes; i++) {
	objCheckBoxes[i].checked = CheckValue;
    }
  }
}
function getFileSelection(fieldName,find){
	var curSelection = new Array();
	var amount = 0;
	var docForms = document.forms;
	for(var i = 0; i < docForms.length; i++){
		var inputFields = docForms[i].elements;
		for(var ii = 0; ii < inputFields.length; ii++){
			if(fieldName){
				if(inputFields[ii].type == 'checkbox' && inputFields[ii].name == fieldName){
					if(inputFields[ii].checked==find){
						curSelection[amount] = inputFields[ii].value;
						amount++;
					}
				}
			}else{
				if(inputFields[ii].type == 'checkbox'){
					if(inputFields[ii].checked==find){
						curSelection[amount] = inputFields[ii].value;
						amount++;
					}
				}
			}
		}
	}
	if(amount > 0){
		return curSelection;
	}else{
		return false;
	}
}
function confirmDelete(thisItem){
        var agree = confirm("Are you sure you want to perementaly delete " + thisItem + "?");
        if(agree){
                return true ;
        }else{
                return false ;
        }
}
function closeBox(id){
	$("#" + id).empty();
	$("#" + id).remove();
}
function popupHelper(id, title, html){
	if(document.getElementById(id)){
		document.getElementById(id).style.display='block';
	}else{
		$('body').append('<div id="' + id + '" class="popupHelper"></div>');
		var helpHtml = '<div class="title" id="helper_' + id + '">';
		helpHtml = helpHtml + '<span style="float: left;">' + title + '</span>';
		helpHtml = helpHtml + '<div class="closeX"  style="position: absolute; right: 0px; top: 0px;" onclick="closeBox(\'' + id + '\');" title="Close Helper">X</div>';
		helpHtml = helpHtml + '<div style="clear: both;"></div>';
		helpHtml = helpHtml + '</div>';
		helpHtml = helpHtml + '<div class="popBody">' + html + '</div>';
		helpHtml = helpHtml + '</div>';
		$("#" + id).append(helpHtml);
		$("#helper_" + id).mousedown(function(){
			$("#" + id).draggable({
				//helper: 'clone',
				opacity: 0.7,
				zIndex: 105
			});
		});
		$("#helper_" + id).mouseup(function(){
			$("#" + id).draggable( "destroy" );
		});
	}
	document.getElementById(id).style.top = mouseY + 'px';
	document.getElementById(id).style.left = mouseX + 'px';
}
function confirmClick(thisText, goto){
   if(thisText){
	var agree = confirm(thisText);
	if(agree){
		if(goto){
			window.location.href= goto ;
		}
		return true ;
	}else{
		return false ;
}
   }
}
function popup(url, name, widgets){
	if(typeof popupWin == 'object'){
		popupWin.close();
	}
	popupWin = window.open(url, name, widgets);
	popupWin.opener.top.name = "opener";
	popupWin.focus();
}
function newPopInWindow(title,width,height){
	if(height){
		height = height + 'px';
	}else{
		var height='auto';
	}
	$("body").append('<div id="dimmer"></div>');
	$("body").append('<div id="newPlacementHolder"></div>');
	$("#newPlacementHolder").append(
		'<div id="PopInWindow" style="margin: auto; position: relative; width: ' + width + 'px; border: 1px solid #555; background: #fff;">'
			+ '<div style="padding: 3px; position: relative; font-size:14px; font-weight: bold; background: #ccc; border-bottom: 1px solid #555; text-align:center;">'
				+ '<span id="PopInWindowTitle">' + title + '</span>' 
				+ '<div class="closeX" style="position: absolute; top: 0px; right: 0px;" onclick="closePopInWindows();">X</div>'
			+ '</div>'
			+ '<div id="PopInWindowBody" style="padding: 3px; text-align: left; overflow: auto; height:' + height + '"></div>'
		+ '</div>'
	);
	
}
function closePopInWindows(){
	$("#PopInWindow").empty();
	$("#PopInWindow").remove();
	$("#newPlacementHolder").empty();
	$("#newPlacementHolder").remove();
	$("#dimmer").remove();
}
function popup(url, name, widgets){
	if(typeof popupWin == 'object'){
		popupWin.close();
	}
	popupWin = window.open(url, name, widgets);
	popupWin.opener.top.name = "opener";
	popupWin.focus();
}
function startProgressBar(msg){
	if(document.getElementById('progressBar')){
		$("#progressBar").empty();
		$("#progressBar").append('<div style="text-align: center; padding: 5px;"><img src="/images/processing.gif" /></div>');
		$("#progressBar").append('<div style="text-align: center; color: #009; font-weight: bold;">' + msg + '</div>');
	}else{
		saveScrollPos(); //save scroll position
		window.scroll(0,0);
		if($("#dimmer").length<1){
			$('<div id="dimmer"></div>').appendTo("body");
		}
		$('<div id="containerBar" style="z-index: 101; position: absolute; top: 25%; width:100%; text-align: center;"></div>').appendTo("body");
		$('<div id="progressBar" style="font-size: 14px; font-weight: bold; padding: 5px; text-align:left; border: 2px solid #999; background:#fff; margin: 0px auto 0px auto; width:300px;"></div>').appendTo("#containerBar");
		$('<div style="text-align: center; padding: 5px;"><img src="/images/processing.gif" /></div>').appendTo("#progressBar");
		$('<div style="text-align: center; color: #009; font-weight: bold;">' + msg + '</div>').appendTo("#progressBar");
	}
}
function closeProgressBar(msg){
	if(msg != null){
		$("#progressBar").empty();
		$("#progressBar").append('<div style="text-align: center;">' + msg + '</div>');
		$("#progressBar").append('<div style="text-align: center;"><input type="button" value="Ok" onclick="closeProgressBar(null);" /></div>');
		setTimeout("closeProgressBar(null);",5000);
		
	}else{
		$("#containerBar").empty();
		$("#containerBar").remove();
		$("#dimmer").remove();
		window.scroll(0,scrollSave);
	}
	return;
}
function changeLayout(layout){
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if ((version < 7) && (document.body.filters)){
		if(layout=='Smart'){
			alert('The Smart layout is not compatible with your browser.');
			return;
		}
	}
	
	
	if(document.getElementById('layoutStyle')){
		var curLayout = document.getElementById('layoutStyle').href;
		document.getElementById('layoutStyle').href='/stylesheets/layout/' + layout + '.css';
	}
}
function changeColor(color){
	if(document.getElementById('colorStyle')){
		document.getElementById('colorStyle').href='/stylesheets/color/' + color + '.css';
	}
}
// Return new array with duplicate values removed
function uniqueArray(array){
	var a = [];
	var l = array.length;
	for(var i=0; i<l; i++) {
		for(var j=i+1; j<l; j++) {
			// If array[i] is found later in the array
			if (array[i] === array[j]){
				j = ++i;
			}
		}
		a.push(array[i]);
	}
	return a;
}
function stopRKey(evt) {
	var evt  = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type=="text")) { return false; }
}
function pageSetup(){
	//document.onkeypress = stopRKey;
	var normalInputs = $("textarea,select,:text,:password");
	for(var i=0; i<normalInputs.length; i++){
		if( normalInputs[i].className=='' ){
			$(normalInputs[i]).addClass("fieldInput");
			$(normalInputs[i]).focus(function () {
				this.className='fieldFocus';
			});
			$(normalInputs[i]).blur(function () {
				this.className='fieldInput';
			});	
		}
	}
	var buttons = $(":submit,:button,:file");
	for(var i=0; i<buttons.length; i++){
		if( buttons[i].className=='' ){
			$(buttons[i]).addClass("button");
			$(buttons[i]).mouseover(function () {
				this.className='buttonHover';
			});
			$(buttons[i]).mouseout(function () {
				this.className='button';
			});
			$(buttons[i]).focus(function () {
				this.className='buttonFocus';
			});
			$(buttons[i]).blur(function () {
				this.className='button';
			});
		}
	}
}
$(document).ready(function(){
	pageSetup();
});
