//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//	common.js
//	Common/base scripts and functions
//	v3.20
//	
//	Copyright 2008 by Gary Pajor / New Media Now, LLC
//	All rights reserved.
//	
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


/*
	3.20	Modified hoverhelp creation, e.g. makeHH() to use <span> tag, not <a>,
			by calling new function createHoverElement().
			In doing this, renamed createAnchor() to createHoverElement() and added
			a new parameter: htmlTag, which defaults to 'span'. Redefined createAnchor()
			to call the new function createHoverElement() with htmlTag parameter set to 'a'.
			Several other functions call createAnchor, so wanted to leave it intact for
			bw compatibility.
	3.01	Modified openListViewWin(): Added line to give filter parameter
			default value of '' (if argument not supplied).  Otherwise,
			CGI paramater v_filter ends up with literal value of 'undefined'
			giving error or undesirable results in UI template that relies
			on the x_viewFldValList command created by this function.

*/

// Stub: Used as value of onLoad handler in body tag of main GUI template.
// Function redefined accordingly and when/where needed in 'view' templates.
function initPage() {}


var browName = 'IE';
if ( navigator.appName.indexOf('Netscape') != -1 ) {
	browName = 'NN';
}
function textareaTag(name,rows,ieCols,wrap) {
	wrap = wrap || "virtual";
	var cols = ieCols;
	if ( browName == 'NN' ) {
		cols = parseInt(ieCols*.7);
	}
	var tag = "<textarea name=" + name 
								+ " rows=" + rows
								+ " cols=" + cols
								+ " wrap=" + wrap
								+ ">"
								;
	document.write( tag );
}


function openwin(url,name,wd,ht,menubar,toolbar,loc) { 
	name = name || 'WW';
	wd = wd || 400;
	ht = ht || 400;
	menubar = menubar || "no";
	toolbar = toolbar || "no";
	loc = loc || "yes";
	window.open( url,name,winstring(wd,ht,menubar,toolbar,loc) ).focus();
}
function winstring(w,h,mbar,tbar,loc) {
	return "width="+w+",height="+h+",status=yes,location="+loc+",menubar="+mbar+",toolbar="+tbar+",scrollbars=yes,resizable=yes";
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Used to check that a selection has been made from a checkbox list.
// Prevents submission of form if no items selected.
// Usage: <form> tag; Handler: onSubmit
function items_selected(chbx_name,f,msg) {
	var selectionmade;
	for ( var i=0; i<f.length; i++ ) {
		var e = f.elements[i];
		if ( e.type == "checkbox" ) {
			if ( e.name == chbx_name && e.checked ) { selectionmade = 'true'; }
		}
	}
	if ( selectionmade ) return true;
	alert(msg);
	return false;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function checkall(chbx_name) {
	var e;
	for(i=0;i<document.chkbox_selection.elements.length;i++) {
		e = document.chkbox_selection.elements[i];
		if ( e.name == chbx_name )	e.checked = 'true';
	}
}

function uncheckall(chbx_name) {
	var e;
	for(i=0;i<document.chkbox_selection.elements.length;i++) {
		e = document.chkbox_selection.elements[i];
		if ( e.name == chbx_name )	e.checked = '';
		//document.chkbox_selection.elements[i].checked = '';
	}
}

function reverse(chbx_name) {
	var e;
	for(i=0;i<document.chkbox_selection.elements.length;i++) {
		e = document.chkbox_selection.elements[i];
		if ( e.name == chbx_name ) {
			if( e.checked ) {
				e.checked = '';
			} else {
				e.checked = 'true';
			}
		}
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function _isblank(s) {
	for ( var i=0; i<s.length; i++ ) {
		var c = s.charAt(i);
		if ( ( c != ' ' ) && ( c != '\n' ) && ( c != '\t' ) ) return false;
	}
	return true;
}

function highlight(field) {
  field.select();
  field.focus();
}

function csoon(msg) { alert(msg + '\nComing Soon!') }

function dubClickSubmit(e) {
	if( navigator.appVersion.indexOf('Mac') != -1 && navigator.appVersion.indexOf('MSIE') > 0 )
		return;
	e.form.submit();
}

//~~~~~~~~~~ HoverHelp widget creation functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Create default 'hover help' widget
// NOTE: 'global' variable gHelpImgPath defined at top of main gui page template
function makeHH(varBaseName,popUpLocParam,popUpSticky,popUpWidth) {
	if ( !varBaseName ) return;
	var openElementText = createHoverElement(varBaseName,'',popUpLocParam,popUpSticky,popUpWidth);
	var imgTagText = "<img src=\"" + gHelpImgPath + "\" align=\"left\" border=0 hspace=0>";
	var closeElementText = "</span>";
	document.write(openElementText + imgTagText + closeElementText);
}

// Create a text link with a hover-help feature
function makeHHTextLink(linkText,anchorClass,varBaseName,popUpLocParam,popUpSticky,popUpWidth) {
	if ( !linkText ) {
		alert("Error in creating link text hover help ( makeHHAnc() ): Link text not defined.");
		return;
	}
	var openAnchorText = createAnchor(varBaseName,anchorClass,popUpLocParam,popUpSticky,popUpWidth);
	document.write(openAnchorText + linkText + "</a>");
}

// Create an image with a hover-help feature.  Must supply the full URL of the image.
function makeHHOnImage(imgUrl,varBaseName,popUpLocParam,popUpSticky,popUpWidth) {
	if ( !imgUrl ) {
		alert("Error in creating hover help on image: Link text not defined.");
		return;
	}
	var openAnchorText = createAnchor(varBaseName,'',popUpLocParam,popUpSticky,popUpWidth);
	var imgTagText = "<img src=\"" + imgUrl + "\" align=\"middle\" border=0 hspace=0>";
	document.write(openAnchorText + imgTagText + "</a>");
}

function createAnchor(varBaseName,tagClass,popUpLocParam,popUpSticky,popUpWidth) {
	return createHoverElement(varBaseName,tagClass,popUpLocParam,popUpSticky,popUpWidth,'a');
}

function createHoverElement(varBaseName,tagClass,popUpLocParam,popUpSticky,popUpWidth,htmlTag) {
	htmlTag = htmlTag || 'span';
	popUpLocParam = popUpLocParam || "CENTER";
	var captionTxtVar = "hhc_" + varBaseName;
	var bodyTxtVar = "hh_" + varBaseName;
	
	// Code string for checking caption text variable existence
	var checkCapCodeStr = "self."+captionTxtVar + " == null";
	
	// Code string for checking body text variable existence
	var checkBodyCodeStr = "self."+bodyTxtVar + " == null";
	
	var undefCapTxtVar = eval(checkCapCodeStr);
	var undefBodyTxtVar = eval(checkBodyCodeStr);
	
	// 'Variable definition code: Code string to 'eval' to declare/define missing variable
	var varDefCode;
	if ( undefCapTxtVar ) {
		varDefCode = "hhc_" + varBaseName + " = \"-No Caption Text-<br>Var: hhc_" + varBaseName + "\";";
		eval(varDefCode);
	}
	
	if ( undefBodyTxtVar ) {
		varDefCode = "hh_" + varBaseName + " = \"-No Body Text-<br>Var: hh_" + varBaseName + "\";";
		eval(varDefCode);
	}
	
	var overLibArgsTxt = bodyTxtVar + ", CAPTION, " + captionTxtVar + ", " + popUpLocParam;
	if ( popUpWidth ) overLibArgsTxt += ", WIDTH, " + popUpWidth;
	if ( popUpSticky ) overLibArgsTxt += ", STICKY";
	var tagTxt = "<" + htmlTag + " ";
	if ( tagClass ) {
		tagTxt += "class=\"" + tagClass + "\" ";
	}
	tagTxt += "href=\"javascript:void(0);\" onMouseOver=\"return overlib(";
	tagTxt += overLibArgsTxt + ");\" ";
	tagTxt += "onMouseOut=\"return nd();\">";
	return tagTxt;
}

//~~~~~~~~~~ End HoverHelp widget creation functions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// Open list viewer window: Open separate window to view list of 
// values of a given field in a given database table.
// NOTE: 'global' variable gCmdUrl defined at top of main gui page template
function openListViewWin( table,field,fieldLabel,sortDirection,sortType,filter ) {
	sortDirection = sortDirection || "asc";
	sortType = sortType || "alpha";
	filter = filter || "";
	if ( sortDirection != "asc" ) sortDirection = "desc";
	if ( sortType != "alpha" ) sortType = "num";
	var cmdUrl = gCmdUrl + "c=x_viewFldValList&v_table=" + table + "&v_field=" + field + "&v_fieldLabel=" + fieldLabel + "&v_sortDirection=" + sortDirection + "&v_sortType=" + sortType + "&v_filter=" + filter;
	window.open(cmdUrl,'lvw','resizable,scrollbars,status,width=400,height=300').focus();
}

// Open view data list window: Open separate window to view system list
// given by listName, e.g. lists hardcoded in SC::DataLists class;
// NOTE: 'global' variable gCmdUrl defined at top of main gui page template
function openViewDataListWin( listName,listLabel,invertHashFlag ) {
	invertHashFlag = invertHashFlag || 0;
	var cmdUrl = gCmdUrl + "c=x_viewDataList&v_ln=" + listName + "&v_label=" + listLabel + "&v_inv=" + invertHashFlag;
	window.open(cmdUrl,'vdlw','resizable,scrollbars,status,width=400,height=300').focus();
}

// Open 'help' window, for displaying context-sensitive help
function openCsHelpWin( helpFileName ) {
	var cmdUrl = gCmdUrl + "c=x_viewCsHelp&hfn=" + helpFileName;
	window.open(cmdUrl,'help','resizable,scrollbars,status,width=400,height=400').focus();
}