﻿// Web Common JavaScript


// javascript constants
var jsTypeUndefined = "undefined";
var jsTypeFunction = "function";
var jsTypeObject = "object";
var jsTypeNumber = "number";
var jsTypeString = "string";

// finds and returns control by ID
function c(id) {
	return document.getElementById(id);
}

// returns value of a form control
function cv(id) {
	var obj = c(id);
	if (obj != null) {
		if (obj.type == 'select-one') { 
			if (obj.selectedIndex != -1) { return obj.options[obj.selectedIndex].value; } else { return ""; } }
		else { return obj.value; }
	} 
	else { alert("Error: control with ID '" + id + "' does not exist on a page"); return ""; }
}

// returns 'style' object of the control
function cs(id) { 
	var obj = c(id); 
	if (obj == null) { alert("Error: control with ID '" + id + "' does not exist on a page"); return null; } 
		else { return obj.style; } }

// returns control's text property or DropDownList selected item's text
function ct(id) {
	var obj = c(id);
	if (typeof(obj) != "undefined") {
		if (obj.type == 'select-one') { 
			if (obj.selectedIndex != -1) { return obj.options[obj.selectedIndex].innerText; } else { return ""; } }
		else { return obj.value; }
	} 
	else { alert("Error: control with ID '" + id + "' does not exist on a page"); return ""; }
}

function DropDownListSelectByValue(obj, value) {
	for (i=0; i<obj.options.length; i++) {
		if (obj.options[i].value == value) { obj.options[obj.selectedIndex].selected = false; obj.options[i].selected = true; }
	}
}

// cancels the default behaviour of the 'ENTER' button on a textbox
// and (if provided) clicks the 'custom' submit button, or executes code
function CancelTextboxSubmit(evt, submitButtonId, codeToExecute) 
{
	var key = getEventKey(evt);
	if (key == 13) 
	{
		freezeEvent(evt);
		if (submitButtonId != null && submitButtonId != "") { window.setTimeout("c('" + submitButtonId + "').click();", 100); }
		if (codeToExecute != null && codeToExecute != "") { window.setTimeout(codeToExecute, 100); }
		return false;
	}
	return true;
}


function isInteger(value) {
	var i;
	for(i = 0; i < value.length; i++) {
		if(isNaN(parseInt(value.substring(i, i + 1)))) { return false; }
	}
	return true;
}

function myAddEventListener(obj, eventName, functionRef)
{
	if (obj)
	{
		if (obj.addEventListener != null)
		{
			obj.addEventListener(eventName, functionRef, false);
		}
		else if (obj.attachEvent != null)
		{
			obj.attachEvent("on" + eventName, functionRef);
		}
	}
}

// Returns a number from 0 to maximum
function GetRandomInteger( maximum )
{
	return ( Math.floor ( Math.random ( ) * maximum ) );
}

function printPreview(title, style, html, onlyHtml) { 
	var p = window.open(); 
	if (!onlyHtml) { p.document.write("<html><head><title>" + title + "</title><style>body {text-align:center;}" + style + "</style></head><body>"); }
	p.document.write(html);
	if (!onlyHtml) { p.document.write("</body></html>"); }
	p.focus(); } // open a new browser window and show print contents
	
function replace(str,searchFor,replaceWith) {
	var i = str.indexOf(searchFor);
	while ( i!=-1 ) {
		var j = i+searchFor.length;
		str = str.substring(0,i)+replaceWith+str.substring(j,str.length);
		i = str.indexOf(searchFor,i+replaceWith.length);
	} return str; }
	

//	RETURNS: the string containing the IDs of all of the matching elements found
//	for example it would be used if you wanted to get the IDs of all of the textboxes in a given table...
//	EXAMPLES: 
//	var strIDs = getElementIdsFromNodes(document.getElementById('myTable'), 'INPUT', 'TEXT');
//	var strIDs = getElementIdsFromNodes(document.getElementById('myTable'), 'SELECT');
function getElementIdsFromNodes(nodes, nodeName, inputType) {
	var sIds = ""; // store IDs of matching elements here...
	var nodeMatches = false; // when the matching node is found, this var is set to true...
	for (var n=0; n<nodes.length; n++) { // loop through all nodes...
		nodeMatches = false; // initially the node does not match...
		if (nodes[n].nodeName == nodeName) { // test if the nodeName matches...
			if (inputType) { // if inputType is provided then test if it matches...
				if (nodes[n].type == inputType) { nodeMatches = true; }
			} else { nodeMatches = true; }
		}
		if (nodeMatches) { sIds += ',' + nodes[n].id; }
			else { sIds += getElementIdsFromNodes(nodes[n].childNodes, nodeName, inputType); }
	}
	return sIds;
}

function openDialogWindow(link, width, height) {
	if (!width) { width = "500"; }
	if (!height) { height = "200"; }
	window.open(link, '', "fullscreen=no, width=" + width + ", height=" + height + ", location=no, menubar=no, resizable=yes, scrollbars=no, status=no, titlebar=no, toolbar=no");
}

// trim function will get rid of whitespace character at the beginning and end of the given string
function trim(expr) {
	return expr.replace(/^\s+|\s+$/g,'');
}


function getEvent(event) {
	return (event ? event : window.event);
}

function getEventKey(evt)
{
	evt = (evt) ? evt : (window.event) ? event : null;
	if (evt)
	{
		var cCode = (evt.charCode) ? evt.charCode :
				((evt.keyCode) ? evt.keyCode :
				((evt.which) ? evt.which : 0));
		return cCode; 
	}
}

function freezeEvent(e) {
	if (e.preventDefault) e.preventDefault();
	e.returnValue = false;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	return false;
}//freezeEvent

function getEventElement(e) {
	if (e == null) { e = getEvent(event); }
	return (e.srcElement ? e.srcElement: (e.target ? e.target : e.currentTarget));
}//getEventElement()

function getOffsetTop(elementId)
{
	var el = c(elementId);
	if (el == null) return 0;
	return el.offsetTop;
}
function getOffsetLeft(elementId)
{
	var el = c(elementId);
	if (el == null) return 0;
	return el.offsetLeft;
}








// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function SetCookie(name, value, expires, path, domain, secure) 
{
	var curCookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function GetCookie(name) 
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) 
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} 
	else
		begin += 2;
		
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function DeleteCookie(name, path, domain) 
{
	if (GetCookie(name)) 
	{
		document.cookie = name + "=" + 
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

