/** Page: javascript functions for studiesabroad.com
 *
 * @author      Katherine Carr <kcarr@studiesabroad.com>
 * @copyright   Copyright (c) 2003 by International Studies Abroad Inc.
 * @version     $Id: main.js,v 1.15 2011/03/31 19:42:28 pmarbach Exp $
 * @package     studiesabroad.com-lib
 * @subpackage  Javascript
 */

// store previous onload events.
var previousOnload = window.onload;

// set onload.
window.onload = globalOnload;

// print window global.
var printWindow;

// storage of global color values.
var navColors = Array();

navColors['countryCellDefault'] = '#224E73';
navColors['countryCellHover']   = '#132F45';

// Method: globalOnload() {{{
/** Function/Method: globalOnload()
 *
 * Do window onload without overloading any onloads
 * being set by other include libraries.
 *
 * @author      Ryan Gallagher <binerman@users.sourceforge.net>
 * @access      public
 * @global      previousOnload  storage for former onload function.
 * @return      boolean false
 */
function globalOnload() {
    // call previously existing events.
    if (previousOnload != null) {
        previousOnload();
    }

    // write onclick events for popups of print windows.
    replacePrintLinks();

    // print window if we are print-friendly document.
    detectPrintablePage();

    return;
}
// }}}

function clearMenuCell(id) {
    if (null == id) {
        return;
    }

    obj = document.getElementById(id);

    if (null != obj) {
        obj.style.backgroundColor = navColors['countryCellDefault'];
    }
}

function highlightMenuCell(id) {
    if (null == id) {
        return;
    }

    obj = document.getElementById(id);

    if (null != obj) {
        obj.style.backgroundColor = navColors['countryCellHover'];
    }
}

// Method: quickJump() {{{
/** Method: quickJump()
 *
 * Expects to find the form 'quick-jump' containing a select
 * box, 'nav-select', that has valid urls as the option values.
 * Changes the page location to the choosen value.
 *
 * @author      Katherine Carr <kcarr@studiesabroad.com>
 * @access      public
 * @return      boolean         FALSE
 */
function quickJump() {
    if (null == document.forms['quick-jump'] && null == document.forms['quick-jump'].elements['nav-select']) {
        return false;
    }

    form = document.forms['quick-jump'];
    index = form.elements['nav-select'].selectedIndex;

    if (0 < index) {
        option = form.elements['nav-select'].options[index].value;
        location.href=option;
        form.onsubmit = deadSubmit;
    }

    return false;
}
// }}}

// Method: deadSubmit() {{{
/** Method: deadSubmit()
 *
 * Removes the submit action from a form.
 *
 * @author      Katherine Carr <kcarr@studiesabroad.com>
 * @access      public
 * @return      boolean         FALSE
 */
function deadSubmit() {
    return false;
}
// }}}

// Method: replacePrintLinks() {{{
/** Function/Method: replacePrintLinks()
 *
 * Dicovers <a> tags that correspond to a print-view or print-page
 * based on class name "print-link" and writes popup event instead.
 *
 * see openPrintWindow() for the properties of the popup that is
 * the result.
 *
 * @author      Ryan Gallagher <binerman@users.sourceforge.net>
 * @access      public
 * @global      mixed   printWindow     window object.
 * @return      boolean void
 */
function replacePrintLinks() {
    nodes = document.getElementsByTagName('A');

    for (i = 0; i < nodes.length; i++) {
        n = nodes[i];
        thisClass = '';
        if (n.className != null) {
            thisClass = n.className;
        } else {
            thisClass = n.getAttribute('class');
        }
        if (thisClass == 'print-link') {
            url = n.getAttribute('href');
            n.setAttribute('href', 'javascript:openPrintWindow(\'' + url + '\');');
            n.setAttribute('target', '');
        }
    }
    return;
}
// }}}

// Method: openPrintWindow() {{{
/** Function/Method: openPrintWindow()
 *
 * Opens a printable size popup window to the specified url.
 *
 * @author      Ryan Gallagher <binerman@users.sourceforge.net>
 * @access      public
 * @param       string url      destination url.
 * @global      printWindow     target window object.
 * @return      boolean void
 */
function openPrintWindow(url) {

    /* if no print feature, give them menu options */
    if (window.print) {
        menu = 'no';
    } else {
        menu = 'yes';
    }

    /* open the window */
    printWindow = window.open(url, "printWindow",
      'height=700,width=650,innerHeight=650,innerWidth=650,'
      + 'scrollbars=yes,resizable=yes,position=3,'
      + 'location=no,menubar=' + menu + ',status=no'
      + 'screenX=10,screenY=10'
      );
    return;
}
// }}}

// Method: detectPrintablePage() {{{
/** Function/Method: detectPrintablePage()
 *
 * Always run this, if we see a special node indicating this
 * page was generated from the print buttons, confirm and print.
 *
 * looks for a node by the id of "printer-friendly"
 * htmlprint.xsl inserts this node.
 *
 * @author      Ryan Gallagher <binerman@users.sourceforge.net>
 * @access      public
 * @return      boolean void
 */
function detectPrintablePage() {
    if (document.getElementById('printer-friendly')) {
        if (window.print) {
            /* we have print functionality, confirm and print */
            if (confirm("Print this page now?")) {
                window.print();
            }
        } else {
            /* can't do anything for these folks */
        }
    }
    return;
}
// }}}

// Method: showVCard() {{{
/** Function/Method: showVCard()
 *
 * Pops up window to show vcard for specified staff member
 *
 *
 * @author      Angela Roles <aroles@studiesabroad.com>
 * @access      public
 * @param       string cardId      staffId of desired staff member
 * @return      boolean void
 */
function showVCard(cardId) {
   var openUrl = '/viewStaffVCard/'+cardId;
   window.open(openUrl, 'VCard', "toolbar=no,location=no,status=no,scrollbars=no,resizable=yes,width=350,height=200");
}
//}}}

// Method: showGalleryImage() {{{
/** Function/Method: showGalleryImage()
 *
 * Pops up window to show Gallery Image
 *
 *
 * @author      Chris Nienhuis <cnienhuis@studiesabroad.com>
 * @access      public
 * @param       string oid     Gallery Object Id (IE infoObjectId)
 * @param       string gclass  Gallery Class
 * @param       string gtype   Gallery Type
 * @param       string gtable  Gallery Table
 * @param       string gkey    Gallery Table Key
 * @param       string h,w     Height and Width of popup
 * @return      boolean void
 */
function showGalleryImage(oid, gclass, gtype, gtable, gkey,h,w) {
   var openUrl = '/popups/viewGalleryPopUp.php?oid='+oid+'&gclass='+gclass+'&gtype='+gtype+'&gtable='+gtable+'&gkey='+gkey;
   window.open(openUrl, 'VGallery', "toolbar=no,location=no,status=no,scrollbars=no,resizable=yes,width="+w+",height="+h);
   return;
}
//}}}


// Method: urgentNotice(url) {{{
/** Function/Method: urgentNotice()
 *
 * Pops up window to show an arbitrary urgent notice
 *
 *
 * @author      Eric Minto <eric@studiesabroad.com>
 * @access      public
 * @param       string url      URL of page that contains the notice, assuming '.php' extension
 * @return      boolean void
 */
function urgentNotice(url) {
   window.open(url+'.php', 'Notice', "toolbar=no,location=no,status=no,scrollbars=auto,menubars=no,resizable=yes,width=338,height=443");
}

function showDuration(count, total) {
	for(var i = 1; i <= total; i++) {
		document.getElementById('wrapper-'+i).className = document.getElementById('wrapper-'+i).className.replace(" selected","");
		if(i == count) {
			document.getElementById('wrapper-'+i).className += " selected";
			document.getElementById('duration-box-'+i).style.display = 'block';
			document.getElementById('duration-link-'+i).className = "selected";
			document.getElementById('duration-link-'+i).childNodes[0].setAttribute("src",document.getElementById('duration-link-'+i).childNodes[0].getAttribute("src").replace(".on.",".hover."));
		} else {
			document.getElementById('duration-box-'+i).style.display = 'none';
			document.getElementById('duration-link-'+i).className="default";
			document.getElementById('duration-link-'+i).childNodes[0].setAttribute("src",document.getElementById('duration-link-'+i).childNodes[0].getAttribute("src").replace(".hover.",".on."));
		}
	}
}

function hoverOverDuration(i) {
	if(document.getElementById('duration-link-'+i).className!=="selected") {
		document.getElementById('duration-link-'+i).childNodes[0].setAttribute("src",document.getElementById('duration-link-'+i).childNodes[0].getAttribute("src").replace(".on.",".hover."));
	}
}

function hoverOutDuration(i) {
	if(document.getElementById('duration-link-'+i).className!=="selected") {
	document.getElementById('duration-link-'+i).childNodes[0].setAttribute("src",document.getElementById('duration-link-'+i).childNodes[0].getAttribute("src").replace(".hover.",".on."));
        }
} 

// Method: filterChars(s,charList) {{{
/** Function/Method: makeNumeric(s)
 *
 * Filter a string leaving only the characters in the list passed as parameter
 *
 *
 * @author      Carlos Quintero  <cquintero@studiesabroad.com>
 * @access      public
 * @param       string s            String to be filter
 * @param       string charList     String to be filter
 * @return      string              String after being filtered
 */


function filterChars(s, charList)
{
	var s1 = "" + s; // force s1 to be a string data type
	var i;
	for (i = 0; i < s1.length; )
	{
		if (charList.indexOf(s1.charAt(i)) < 0)
			s1 = s1.substring(0,i) + s1.substring(i+1, s1.length);
		else
			i++;
	}
	return s1;
}


// Method: makeNumeric(s) {{{
/** Function/Method: makeNumeric(s)
 *
 * Filter a string leaving only numbers
 *
 *
 * @author      Carlos Quintero  <cquintero@studiesabroad.com>
 * @access      public
 * @param       string s      String to be filter
 * @return      string        String after being filtered
 */
function makeNumeric(s)
{
	return filterChars(s, "1234567890.-");
}


// Method: formatNumber(val,digits,minval,maxval) {{{
/** Function/Method: makeNumeric(s)
 *
 * Format a String as Currency
 *
 *
 * @author      Carlos Quintero  <cquintero@studiesabroad.com>
 * @access      public
 * @param       string val      String to be formate
 * @param       int    digits   Number of decimal places
 * @param       real   minval   Minimun value allow
   @param       real   maxval   Max value allow
 * @return      string          String formated
 */
function formatNumber(val,digits,minval,maxval)
{
	var sval = "" + numval(val,digits,minval,maxval);
	var i;
	var iDecpt = sval.indexOf(".");
	if (iDecpt < 0) iDecpt = sval.length;
	if (digits != null && digits > 0)
	{
		if (iDecpt == sval.length)
			sval = sval + ".";
		var places = sval.length - sval.indexOf(".") - 1;
		for (i = 0; i < digits - places; i++)
			sval = sval + "0";
	}
	var firstNumchar = 0;
	if (sval.charAt(0) == "-") firstNumchar = 1;
	for (i = iDecpt - 3; i > firstNumchar; i-= 3)
		sval = sval.substring(0, i) + "," + sval.substring(i);

	return sval;
}



// Method: numval(val,digits,minval,maxval) {{{
/** Function/Method: makeNumeric(s)
 *
 * Format a String as Real
 *
 *
 * @author      Carlos Quintero  <cquintero@studiesabroad.com>
 * @access      public
 * @param       string val      String to be formate
 * @param       int    digits   Number of decimal places
 * @param       real   minval   Minimun value allow
   @param       real   maxval   Max value allow
 * @return      real            Value of the string formated
 */


function numval(val,digits,minval,maxval)
{
	val = makeNumeric(val);
	if (val == "" || isNaN(val)) val = 0;
	val = parseFloat(val);
	if (digits != null)
	{
		var dec = Math.pow(10,digits);
		val = (Math.round(val * dec))/dec;
	}
	if (minval != null && val < minval) val = minval;
	if (maxval != null && val > maxval) val = maxval;
	return parseFloat(val);
}

// }}}

// Method: getCheckedValue(radioObj) {{{
/** Function/Method: getCheckedValue(radioObj)
 *
 * return the value of the radio button that is checked
 * return an empty string if none are checked, or
 * there are no radio buttons
 *
 *
 * @author      Carlos Quintero  <cquintero@studiesabroad.com>
 * @access      public
 * @param       radio  radioObj     Object radio to from the form used
 * @return      string              Value of the radio button that is checked
 */

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}
// }}}

