var isMac = (navigator.appVersion.indexOf("Mac") != -1)?true:false;

function resizeAdminDiv(elementid,minwidth,minheight) {
	var w = 0;
	var h = 0;
  
	//Get the browser width and height
	if(self.innerWidth) {
		w = self.innerWidth;
		h = self.innerHeight;
	}
	else if(document.documentElement && document.documentElement.clientWidth) {
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	}
	else if(document.body) {
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	//Reset the height and width if necessary
	var e = document.getElementById(elementid);
	e.style.width = (w<minwidth) ? minwidth+'px' : '100%'; 
	e.style.height = (h<minheight) ? minheight+'px' : '100%'; 
}

function openClientWindow(url,width,height) {
	var defaultSize = [800,600]; 
	var clientWindow = window.open(url,'client','width='+((width)?width:defaultSize[0])+',height='+((height)?height:defaultSize[1])+',resizable=yes,scrollbars=yes');
	clientWindow.focus();
}

function openAdminWindow(url) {
	var adminWindow = window.open(url,'admin','height=825,width=800,resizable=yes,scrollbars=yes');
	if(adminWindow) adminWindow.focus();
}

function openInpagePopup(url,w,title) {
	var popup = document.getElementById("popup");
	popup.innerHTML = '<div id="popuphdr">'+
				'<div id="popuphdrcontainer">'+
					'<div id="popuphdrcontent">'+
						'<h4>'+title+'</h4>'+
					'</div>'+
				'</div>'+
			'</div>'+
			'<div id="popupbody">'+
				'<div id="popupbodycontainer">'+
					'<div id="popupcontent">'+
						'<div id="popupcontentcontainer">Loading ...</div>'+
					'</div>'+
				'</div><div id="popupclose" onclick="closePopup();"></div>'+
			'</div>';
	loadUrlIntoPopup(url);
}

function initPopup(w,title) {
	document.popupInUse = true;
	
	var canopy = document.getElementById("popupcanopy");
	if(!canopy) {
		canopy = document.createElement("div");
		canopy.id = "popupcanopy";
		if(document.all) canopy.style.filter = "alpha(opacity=50)";
		document.getElementsByTagName("body")[0].appendChild(canopy);
		var popup = document.createElement("div");
		popup.id = "popup";
		popup.style.visibility = "hidden";
		document.getElementsByTagName("body")[0].appendChild(popup);
		$(popup).draggable();
		window.onscroll = resizeCanopyContainer;
		window.onresize = resizeCanopyContainer;
		popup.innerHTML = '<div id="popuphdr">'+
					'<div id="popuphdrcontainer">'+
						'<div id="popuphdrcontent">'+
							'<h4>'+title+'</h4>'+
						'</div>'+
					'</div>'+
				'</div>'+
				'<div id="popupbody">'+
					'<div id="popupbodycontainer">'+
						'<div id="popupcontent">'+
							'<div id="popupcontentcontainer"></div>'+
						'</div>'+
					'</div><div id="popupclose" onclick="closePopup();"></div>'+
				'</div>';
		
	}
	else {
		var popup = document.getElementById("popup");
		popup.style.display = "block";
		popup.style.visibility = "hidden";
		canopy.style.display = "block";
		popup.getElementsByTagName('h4')[0].innerHTML = title;
	}

	with(popup.style) {
		width = w + "px";
	}
}

function openPopup(url,w,title) {
	initPopup(w,title);
	loadUrlIntoPopup(url);
}


function showAlert(title,msg) {
	initPopup(300,title);
	document.getElementById("popupcontentcontainer").innerHTML = '<p>'+msg+'</p><div class="popupbtns"><p class="imagebutton"><a href="#" onclick="closePopup();return false;">Close</a></p></div>';
	resizeCanopyContainer();
	centerPopupOnCanopy();
	document.getElementById("popup").style.visibility = "visible";
}

function loadUrlIntoPopup(url) {
	var popup = document.getElementById("popup");
	popup.style.visibility = "hidden";
	sendPopupRequest('GET', url , 'popupcontentcontainer', null);
	resizeCanopyContainer();
}

function closePopup(){
	if(window.name == "upload") window.close();
	else {
		document.popupInUse = false;
		
		var popup = document.getElementById("popup");
		if(popup) {
			popup.style.display = "none";
		}
		var canopy = document.getElementById("popupcanopy");
		if(canopy) canopy.style.display = "none";
		showSelectBoxesInBackground();
	}
}

function resizeCanopyContainer() {
	var canopy = document.getElementById("popupcanopy");
	var viewSize = getViewableWindowSize();
	var scrollDistance = getScrollXY();
	
	var pageWidth = document.getElementById("contentcontainer").offsetWidth;
	var pageHeight = document.getElementById("contentcontainer").offsetHeight + document.getElementById("hdr").offsetHeight + document.getElementById("ftr").offsetHeight;

	with(canopy.style) {
		width = (viewSize[0] < pageWidth) ? pageWidth + "px" : "100%";
		height = (viewSize[1] < pageHeight) ? pageHeight + "px" : "100%";
	}
}

function hideSelectBoxesInBackground() { // hide select boxes because they show through popup layers in IE
	if(document.all) {
		// IE so check version
		var version = parseFloat(navigator.userAgent.substring(navigator.userAgent.toLowerCase().indexOf("msie")+5));
		if(version < 7) {
			var pageContainer = document.getElementById("tier4");
			var selects = pageContainer.getElementsByTagName("select");
			for(var i = selects.length-1; i >= 0; i--) {
				selects[i].style.visibility="hidden";
			}
		}
	}
}

function showSelectBoxesInBackground() { // show select boxes that have been hidden for popup layers
	if(document.all) {
		// IE so check version
		var version = parseFloat(navigator.userAgent.substring(navigator.userAgent.toLowerCase().indexOf("msie")+5));
		if(version < 7) {
			var pageContainer = document.getElementById("tier4");
			var selects = pageContainer.getElementsByTagName("select");
			for(var i = selects.length-1; i >= 0; i--) {
				selects[i].style.visibility="visible";
			}
		}
	}
}

function centerPopupOnCanopy(obj) {
	obj = document.getElementById("popup");
	var scrollDistance = getScrollXY();
	var viewSize = getViewableWindowSize();
	if(obj.className == "inpage") {
		obj.style.visibility = "visible";
		return;
	}
	var halfHeight = obj.offsetHeight/2;
	var halfWidth = obj.offsetWidth/2;
	with(obj.style) {
		left = Math.max(0,((viewSize[0]/2)-halfWidth+scrollDistance[0]))+"px";
		top = Math.max(0,((viewSize[1]/2)-halfHeight+scrollDistance[1]))+"px";
		hideSelectBoxesInBackground();
		display = "none";
		visibility = "visible";
	}
	//obj.style.display = "none";
	$(obj).fadeIn("fast");
}

function getViewableWindowSize() {
	if( typeof( window.innerWidth ) == 'number' ) { // Non-IE
		return [window.innerWidth, window.innerHeight];
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { // IE 6+ in 'standards compliant mode'
		return [document.documentElement.clientWidth, document.documentElement.clientHeight];
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { // IE 4 compatible
		return [document.body.clientWidth, document.body.clientHeight];
	}
	return [0, 0];
}

function getScrollXY() {
	if(typeof(window.pageYOffset) == 'number' ) { // Netscape compliant
		return [window.pageXOffset,window.pageYOffset];
	} else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) { // DOM compliant
		return [document.body.scrollLeft,document.body.scrollTop];
	} else if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) { // IE6 standards compliant mode
		return [document.documentElement.scrollLeft,document.documentElement.scrollTop];
	}
	return [0, 0];
}

// Generic popup action functions
function sendPopupRequest(method, url, target, payload) {
	var canopy = document.getElementById("popupcanopy");
	canopy.request = (window.XMLHttpRequest) ? new XMLHttpRequest() : (window.ActiveXObject) ? new ActiveXObject("MSXML2.XMLHTTP") : null;
	canopy.payload = payload;
	canopy.callback = onCompletePopupRequest;
	canopy.path = url;
	canopy.method = method.toUpperCase();
	canopy.checkReadyState = checkReadyState;
	if(canopy.request) {
		try {
			canopy.request.open(canopy.method, canopy.path, true);
			if(canopy.method == "POST") {
				canopy.request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
				canopy.request.setRequestHeader("Content-length", canopy.payload.length);
				canopy.request.setRequestHeader("Connection", "close");
			}
			canopy.request.onreadystatechange = canopy.checkReadyState;
			canopy.request.send(canopy.payload);
		}
		catch(e) {
			document.getElementById("popupcontentcontainer").innerHTML = "There was an error loading this content.";
			centerPopupOnCanopy();
		}
	}
}

function checkReadyState() {
	var canopy = document.getElementById("popupcanopy");
	if(canopy.request.readyState == 4) {
		if(canopy.request.status == 200) canopy.callback();
		else {
			document.getElementById("popupcontentcontainer").innerHTML = "There was an error loading this content.";
			centerPopupOnCanopy();
		}
	}
}

function onCompletePopupRequest() {
	var canopy = document.getElementById("popupcanopy");
	var html = canopy.request.responseText;
	if(html.indexOf("<body>") > 0) {
		html = html.substring(html.indexOf("<body>")+6);
		html = html.substring(0,html.indexOf("</body>"));
	}
	document.getElementById("popupcontentcontainer").innerHTML = html;
	centerPopupOnCanopy();
	var scripts = document.getElementById("popupcontentcontainer").getElementsByTagName("script");
	var script = "";
	for(var i = 0; i < scripts.length; i++) {
		script += scripts[i].text;
	}
	eval(script);
}

function submitPopupForm(form) {
	displayLoader(form);
	var params = "";
	for(var i = form.elements.length - 1; i >= 0; i--) {
		if(form.elements[i].type != 'checkbox' || (form.elements[i].type == 'checkbox' && form.elements[i].checked)) {
			params += "&" + form.elements[i].name + "=" + encodeURI(form.elements[i].value);
		}
	}
	if(params.length > 0) params = params.substring(1);
	sendPopupRequest(form.method, form.action , 'popupcontentcontainer', params);
	return false;
}

function displayLoader(form) {
	var loadMsg = document.createElement('DIV');
	loadMsg.id = "popuploadmsg";
	loadMsg.innerHTML += '<p><img src="/img/ajax-loader_5.gif" width="28" height="28" /></p>';
	form.style.visibility = "hidden";
	loadMsg.style.top = (parseInt(document.getElementById("popupcontentcontainer").offsetHeight)/2)-14 + "px";
	document.getElementById("popupcontentcontainer").appendChild(loadMsg);
}

function showPopupError(txt) {
	var errorMsg = document.getElementById("popupErrorMsg");
	if(!errorMsg) {
		errorMsg = document.createElement('p');
		errorMsg.className = "alertbad";
		errorMsg.id = "popupErrorMsg";
		var parentContainer = document.getElementById("popupcontentcontainer");
		parentContainer.insertBefore(errorMsg,parentContainer.firstChild);
	}
	errorMsg.innerHTML = txt;
	errorMsg.style.display = "block";
}

function validateSingleRequiredInput(field,msg) {
	if(field.value.length < 1) {
		showPopupError(msg);
		return false;
	}
	return true;
}

// scrolling listbox functions

function showScrollListItem(item) {
	var list = item.parentNode;
	if(typeof list.selectedItem != "undefined" && list.selectedItem != this) {
		list.selectedItem.className = "";
	}
	item.className = "selectrow";
	list.selectedItem = item;

	var scrollListContainer = item.parentNode.parentNode;
	var details = document.getElementById("scrollinglist-"+scrollListContainer.id);
	if(!details) {
		details = document.createElement("div");
		details.id = "scrollinglist-"+scrollListContainer.id;
		details.className = "scrollList-details";
		scrollListContainer.appendChild(details);
	}
	details.innerHTML = item.getElementsByTagName("div")[0].innerHTML;
}