/*
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), 2009 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
*/

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 openWin(url, width, height, winID) {
	if (!winID || winID == "undefined") {
		var winID = rand();
	} // if
	
	if (!width) {
		width = 900;
	} // if
	
	if (!height) {
		height = 600;
	} // if
	
	top.open(url, winID, 'width='+width+',height='+height+',scrollbars=1,resizable=1');
} // 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

function pfml(url) {
	var tmpUrl="/index.php?";
	if (url.indexOf("PID")>-1) {
		url=tmpUrl+url;
	} else if (url.indexOf("PGID")>-1) {
		url=tmpUrl+"PID="+tPID+"&"+url;
	} else { 
		url=tmpUrl+"PID="+tPID+"&PGID="+tPGID+"&"+url;
	}
	top.location.href=url;
} // function

function pfmlDialog(url, title, icon, closeIcon, width, height, scrolling) {
	
	var skinDir = get_cookie("skinDir");

	var tmpUrl="/index.php?";

	if (url.indexOf("PID") > -1) {
		url = tmpUrl + url;
	} else if (url.indexOf("PGID") > -1) {
		url = tmpUrl+"PID="+tPID+"&"+url;
	} else { 
		url = tmpUrl+"PID="+tPID+"&PGID="+tPGID+"&"+url;
	} // 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 = 900; } // if
	if (!height) { height = 400; } // if
	if (!scrolling) { scrolling = 0; } // if
	
	modalDialogURL(1, dialog, 'dialog_iframe', skinDir, title, icon, closeIcon, url, width, height, scrolling);
} // 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.setAttribute("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

// 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.substring(0,strlen) == fieldname || thiselm.name.substring(0,len) == fieldname) {
				thiselm.checked = !thiselm.checked
			} // 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;
	
	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;
					objCont[i+1].style.paddingBottom = numPixels + "px";
				} // if
				
				if (objHeight > setHeight) {
					numPixels = setHeight-objHeight;
					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;
	
	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() {
	var container, specified_tag, current_element;
	if (document.getElementById) {
		document.pf_equalheight = new Array();
		document.pf_equalheight[0] = arguments[0]; // passed arguments
		
		for (i=1; 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

