﻿//GenFuncs... hehehe
var windowOptions = 'location=no,menubar=no,resizable=no,scrollbars=no,toolbar=no,directories=no,dependent=yes';


function winPopup(){
	var args = winPopup.arguments;
	var arg_no = args.length;
	var urlPath;
	var iHeight = 500;
	var iWidth = 500;
	var sOptions;
	var sCompleteOptions;

	switch (arg_no){
		case 1:
			urlPath = args[0];
			sOptions = windowOptions;
			break;
		case 2:
		    urlPath = args[0];
		    sOptions = args[1];
		    break;
		case 3:
			urlPath = args[0];
			iWidth = args[1];
			iHeight = args[2];
			sOptions = windowOptions;
			break;
		case 4:
			urlPath = args[0];
			iWidth = args[1];		
			iHeight = args[2];
		    sOptions = args[3];
		default:
			throw('Invalid number of arguments.');      
	}
    sCompleteOptions = 'height=' + iHeight + ',width=' + iWidth + ',' + sOptions;

	window.open(urlPath, '_blank', sCompleteOptions);
}

function ToggleEnabled(tableName, controlTypeName, boolDisabled){
	var tbl = document.getElementById(tableName);
	if(tbl == null) return;
	var ctls = tbl.getElementsByTagName(controlTypeName);
	i = ctls.length;
	while(i--){
		ctls[i].disabled = boolDisabled;
	}
}

function PrintThis(ctlName){ 
	var sOption="toolbar=no,location=no,directories=no,menubar=no,"; 
		sOption+="scrollbars=yes,width=800,height=600"; 

	var sWinHTML = document.getElementById(ctlName).innerHTML; 

	var winprint=window.open("","",sOption); 

	winprint.document.open(); 
	winprint.document.write('<html><body>'); 
	winprint.document.write(sWinHTML);          
	winprint.document.write('</body></html>'); 
	winprint.document.close(); 
	winprint.focus(); 
	winprint.print();
}

function showDemo(siteName) {
    var width = 600;
    var height = 400;
    // temporary until all new demos are out
	var bigDemos = 'edriving,kysts,indy';
    if (bigDemos.indexOf(siteName) != -1) {
        width = 640;
        height = 480;
    }
    winPopup('/app_themes/' + siteName + '/templates/demo.htm', width, height);
}

function readCookie(name){
	var ca = document.cookie.split(';');
	var nameEQ = name + "=";
	for(var i=0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length, c.length);
		}
    }
	return null;
}



/*This function reqires the /wz_tooltip/wz_tooltip.js to be included. It is used to 
	change the tooltip on a dropdown. Example:
	
<select id="ddlFoo" onmouseover="setDDLTip(this);" onchange="setDDLTip(this);">
	<option ttip="test o test o test o">Lithium (Li)</option>
	<option ttip="seriously this is some <br />text with html in it<br />cool beans.">Sodium (Na)</option>
	<option>Potassium (K)</option>
	<option ttip="Fluorine title">Fluorine (F)</option>
	<option ttip="Chlorine title">Chlorine (Cl)</option>
	<option ttipCtl="payLater">Bromine (Br)</option>
</select>
The last option uses the ttipCtl to point to a div that has the the display style attribute set to none
*/
function setDDLTip(ddl){
	if(ddl){
		var tipTitle = ddl.options[ddl.selectedIndex].text;
		var ttip = ddl.options[ddl.selectedIndex].getAttribute('ttip');
		var ttipCtl = ddl.options[ddl.selectedIndex].getAttribute('ttipCtl');

		if(ttipCtl) {
			TagToTip(ttipCtl, DURATION, 8000, STICKY, true, CLOSEBTN, true, FOLLOWMOUSE, false, TITLE, tipTitle, TITLEALIGN, 'center', TITLEFONTSIZE, '12pt', WIDTH, 500, FIX, CalcXY(ddl), FOLLOWMOUSE, false);
		}else if(ttip) {
			Tip(ttip, DURATION, 8000, STICKY, true, CLOSEBTN, true, FOLLOWMOUSE, false, TITLE, tipTitle, TITLEALIGN, 'center', TITLEFONTSIZE, '12pt', WIDTH, 300, FIX, CalcXY(ddl), FOLLOWMOUSE, false);
		}
	}
}

function setCtlTip(ctl){
	if(ctl){
		var ttip = ctl.getAttribute('ttip');
		var ttipCtl = ctl.getAttribute('ttipCtl');
		var tipTitle = ctl.getAttribute('ttipTitle');
		if(tipTitle == null) tipTitle = '';

		if(ttipCtl) {
			TagToTip(ttipCtl, TITLE, tipTitle, TITLEALIGN, 'center', TITLEFONTSIZE, '12pt', WIDTH, 500, FIX, CalcXY(ctl), FOLLOWMOUSE, false);
		} else if(ttip) {
			Tip(ttip, TITLE, tipTitle, TITLEALIGN, 'center', TITLEFONTSIZE, '12pt', WIDTH, 300);
		} 
	}
}

function CalcXY(ctl){
	var x = 0;
	var y = 0;
	
//	if(ctl.getBoundingClientRect){
//		var rect = ctl.getBoundingClientRect();
//		x = rect.left;
//		y = rect.top; 
//	} else {
		var rect = getCtlRect(ctl);
		x = rect.x;
		y = rect.y;
//	}
	return new Array(x, y + ctl.offsetHeight);
}

function getCtlRect (ctl) {
	var rect = { x: 0, y: 0, width: ctl.offsetWidth, height: ctl.offsetHeight };
	while (ctl) {
		rect.x += ctl.offsetLeft;
		rect.y += ctl.offsetTop;
		ctl = ctl.offsetParent;
	}
	return rect;
}

