/*
THIS LICENSE HEADER MUST REMAIN INTACT FOR LEGAL USE!
Pagefusion; Pagefusion Core Platform
Description: A powerful, easy-to-use, and highly customizable content management system.
Copyright (C), 2010 Pagefusion.com, All Rights Reserved.
Author: Chris Paul, chris.paul@pagefusion.com
Support portal: www.pagefusion.com

Filename: /libraries/core/javascript/main.js
Description: handles miscellaneous javascript stuff

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

var ie = document.all;
var nn = document.getElementById && !document.all;

function rand(min, max) {
    // example: rand(1, 1);
    // returns 1
    var argc = arguments.length;
    if (argc == 0) {
        min = 0;
        max = 2147483647;
    } else if (argc == 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters (only 1 given)!');
    } // if
    return Math.floor(Math.random() * (max - min + 1)) + min;
} // function

function setStyle(objId, styleName, styleVal) {
    if (document.getElementById(objId)) {
		document.getElementById(objId).style[styleName]=styleVal;
	} // if
} // function

function setClass(objId, classNameVal) {
	if (document.getElementById(objId)) {
    	document.getElementById(objId).className=classNameVal;
	} // if
} // function

function stripLeading(string, chr) {
   var finished = false;
   for (var i = 0; i < string.length && !finished; i++)
       if (string.substring(i,i+1) != chr) finished = true;
   if (finished) return string.substring(i-1); else return string;
} // function

function stripTrailing(string, chr) {
   var finished = false;
   for (var i = string.length - 1; i > -1 && !finished; i--)
       if (string.substring(i,i+1) != chr) finished = true;
   if (finished) return string.substring(0,i+2); else return string;
} // function

function trim(string, chr) {
    return stripTrailing(stripLeading(string, chr), chr);
} // function

// open browser window
function pfmlWindow(id, url, title, width, height, scrollbars, resizable) {
	if (!id || id == "undefined") { var id = rand(); } // if
	if (!width) { var width = 800; } // if
	if (!height) { var height = 600; } // if
	
	// calculate middle of screen (dual monitor compatible)
	var a = typeof window.screenX != 'undefined' ? window.screenX : window.screenLeft;
    var i = typeof window.screenY != 'undefined' ? window.screenY : window.screenTop;
    var g = typeof window.outerWidth !='undefined' ? window.outerWidth : document.documentElement.clientWidth;
    var f = typeof window.outerHeight != 'undefined' ? window.outerHeight: (document.documentElement.clientHeight-22);
    var h = (a < 0) ? window.screen.width + a : a;
    var leftVal = parseInt(h + ((g - width) / 2), 10);
    var topVal = parseInt(i + ((f-height) / 2.5), 10);
	
	// add window request variable
	url = url+"&wreq=1";
	
	top.open(url, id, 'width='+width+',height='+height+',left='+leftVal+',top='+topVal+',scrollbars='+scrollbars+',resizable='+resizable+'');
} // function

// open DHTML dialog window
function pfmlDialog(url, title, icon, closeIcon, width, height, scrolling) {
	
	if (typeof mreq != 'undefined' && title != "Login") {
		if (mreq) {
			window.location.href="index.php?"+url+"&no_skin=1";
		} // if
	} else if (typeof portal_nodialog != 'undefined' && portal_nodialog == "Y") {
		window.location.href="index.php?"+url+"&no_skin=1";
	} else {
		var skinDir = get_cookie("skinDir");
	
		var tmpUrl="/index.php?";
	
		if (url.indexOf("PID") > -1) {
			url = tmpUrl + url;
		} else if (url.indexOf("PGID") > -1) {
			if (typeof tPID != 'undefined') {
				url = tmpUrl+"PID="+tPID+"&"+url;
			} // if
		} else { 
			if (typeof tPID != 'undefined' && typeof tPGID != 'undefined') {
				url = tmpUrl+"PID="+tPID+"&PGID="+tPGID+"&"+url;
			} // if
		} // if
		
		if (skinDir == "undefined") { skinDir = "default"; } // if
		if (!title) { title = "New Dialog";	} // if
		if (!icon) { icon = "/components/dialog/images/dialog.png";	} // if
		if (!closeIcon) { closeIcon = "/skins/"+skinDir+"/images/close.png"; } // if
		//if (!width) { width = 0; } // if
		//if (!height) { height = 0; } // if
		if (scrolling == "undefined") { scrolling = 0; } // if
		
		modalDialogURL(1, dialog, 'dialog_iframe', skinDir, title, icon, closeIcon, url, width, height, scrolling);
	} // if
} // function

// load javascript/css file dynamically within *.js file
function loadFile(filename, filetype) {
	if (filetype == "js") {
		var fileref = document.createElement('script');
		fileref.setAttribute("type","text/javascript");
		fileref.type = "text/javascript";
		fileref.setAttribute("src", filename);
		fileref.src=filename;
	} else if (filetype == "css") {
		var fileref = document.createElement("link");
		fileref.setAttribute("rel", "stylesheet");
		fileref.setAttribute("type", "text/css");
		fileref.setAttribute("href", filename);
	} // if
	
	if (typeof fileref != "undefined") {
		document.getElementsByTagName("head")[0].appendChild(fileref);
	} // if
} // function

// remove javascript/css file dynamically within *.js file
function removeFile(filename, filetype) {
	var targetelement=(filetype == "js")? "script" : (filetype=="css")? "link" : "none" // determine element type to create nodelist from
	var targetattr=(filetype == "js")? "src" : (filetype=="css")? "href" : "none" // determine corresponding attribute to test for
	var allsuspects=document.getElementsByTagName(targetelement)
	for (var i=allsuspects.length; i>=0; i--) { // search backwards within nodelist for matching elements to remove
		if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
		allsuspects[i].parentNode.removeChild(allsuspects[i]) // remove element by calling parentNode.removeChild()
	} // for
} // function


// select all rows within grid
function select_all(form, fieldname, len) {
	var strlen = fieldname.length;

	with (document.forms[form]) {
		for (i=0; i<elements.length; i++) {
			thiselm = elements[i];
			if (thiselm.name != undefined) {
				if (thiselm.name.substring(0,strlen) == fieldname || thiselm.name.substring(0,len) == fieldname) {
					if (thiselm.disabled == false) {
						thiselm.checked = !thiselm.checked;
					} // if
				} // if
			} // if
		} // for
	} // with
} // function

// calculate height and width of specified panels
function pf_equalheight_set() {
 var i, objHeight, setHeight=0, numPixels, objCont=document.pf_equalheight, adjustment=document.adjustment;
	
	if (objCont && objCont.length) {
		for (i=1; i<objCont.length; i+=2) {
			objCont[i+1].style.paddingBottom = ""; // set padding for containers to NULL
		} // for
		
		// set height of both columns to second panel height
		if (objCont[3]) {
			setHeight = objCont[3].offsetHeight;
		
			// gets difference of height between containers and adds the necessary pixels
			for(i=1; i<objCont.length; i+=2) {
				objHeight = objCont[i].offsetHeight;
				if (objHeight < setHeight) {
					numPixels = setHeight-objHeight+adjustment;
					objCont[i+1].style.paddingBottom = numPixels + "px";
				} // if
				
				if (objHeight > setHeight) {
					numPixels = setHeight-objHeight+adjustment;
					objCont[i+1].style.paddingBottom = numPixels + "px";
				} // if
			} // for
		} // if
	} // if
} // function

// set height of lower container equal to that of higher container
function pf_equalheight_set2max() {
 var i, maxHeight, objHeight, setHeightTo=0, numPixels, objCont=document.pf_equalheight, adjustment=document.adjustment;
	
	if (objCont && objCont.length) {
		for (i=1; i<objCont.length; i+=2) {
			objCont[i+1].style.paddingBottom = ""; // set padding for containers to NULL
		} // for
		
		// set height of both columns to largest height
		for (i=1; i<objCont.length; i+=2) {
			objHeight = objCont[i].offsetHeight;
			if (objHeight > maxHeight) {
				maxHeight = objHeight;
			} // if
		} // for

		// gets difference of height between containers and adds the necessary pixels
		for(i=1; i<objCont.length; i+=2) {
			objHeight = objCont[i].offsetHeight;
			if (objHeight < setHeightTo) {
				numPixels = setHeightTo-objHeight;
				objCont[i+1].style.paddingBottom = numPixels + "px";
			} // if
		} // for
	} // if
} // function

// set ids for specified containers
function pf_equalheight() { // pf_equalheight("panels", "adjustment", id1, tag1, id2, tag2, id3, tag3);
	var container, specified_tag, current_element, adjustment;
	if (document.getElementById) {
		document.pf_equalheight = new Array();
		document.pf_equalheight[0] = arguments[0]; // passed arguments
		document.adjustment = arguments[1];

		for (i=2; i<arguments.length; i+=2) { // loop through arguments (element names specified): 
			current_element=null;
			container=document.getElementById(arguments[i]); // id of specified container
			if (container) {
				specified_tag = container.getElementsByTagName(arguments[i+1]); // specified tag (located within container)
				if (specified_tag) {
					current_element = specified_tag[specified_tag.length-1];
					if (!current_element.id) {
						current_element.id = "pf_eq" + i; // set id for each element
					} // if
				} // if
			} // if
			
			if (container && current_element) {
				document.pf_equalheight[document.pf_equalheight.length]=container; // last tag of specified container
				document.pf_equalheight[document.pf_equalheight.length]=current_element; // last tag within specified container
			} // if
		} // for
		
		if (arguments[0] == "panels") {
			setInterval("pf_equalheight_set2max()", 10);
		} else {
			setInterval("pf_equalheight_set()", 10);
		} // if
	} // if
} // function

// get elements by classname
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
} // function

// convert curly quotes and other characters
function convert(s) {
    s = s.replace( /\u2018/g, "'" );
    s = s.replace( /\u2019/g, "'" );
    s = s.replace( /\u201c/g, '"' );
    s = s.replace( /\u201d/g, '"' );
    s = s.replace( /\u2013/g, '-' );
    s = s.replace( /\u2014/g, '--' );
    return s;
} // function

function close_window(url) {
	if ((typeof portal_nodialog != 'undefined' && portal_nodialog == "Y") || (typeof mreq != 'undefined' && mreq)) {
		window.location="/index.php?PID="+tPID+"&PGID="+tPGID;
	} else {
		if (window.top.dialog) {
			window.stop();
			window.top.dialog.hide();
			if (url) {
				window.top.location=url;
			} // if
		} else {
			window.stop();
			window.close();
			if (url) {
				window.parent.location=url;
			} // if
		} // if
	} // if
} // function

function createCheckboxArrays(form) {
	for (var i=0;i<form.elements.length;i++) {
		var el=form.elements[i];
		if (el.type=="checkbox") {
			if (el.name.substring(el.name.length-2) == "[]") {
				var n=el.name.substring(0,el.name.length-2);
				if (!form.checkboxes) { form.checkboxes=new Object() ;}
				if (!form.checkboxes[n]) { form.checkboxes[n]=new Array(); }
				form.checkboxes[n].length++;
				form.checkboxes[n][form.checkboxes[n].length-1]=el;
			} // if
		} // if
	} // for
} // if

function pfmlScrollToBottom() {
	dh=document.body.scrollHeight;
	ch=document.body.clientHeight;
	if (dh > ch) {
		moveme=dh-ch;
		window.scrollTo(0,moveme)
	} // if
} // function


