/*********************************************************************

*
 * File         : cnCommon.js
 * Author       : Dave Robertson
 * Date         : July 2001
 * Revision     : $Revision: 1.38 $
 * Modified     : $Id: cnCommon.js,v 1.38 2002/11/05 21:47:35 bmiller Exp $
 * Description  :
 * Notes        :
 */


function menuJump(box) {
    jumpto = box.options[box.options.selectedIndex].value;
    if (jumpto) {
        location.href = jumpto;
    }
}

function noHelp() {
    alert ('No help is available for this item');
}

//var changed = false;
function cancel(cancelUrl) {
    //param = "";
    //if (document.cnLayoutPageSelectForm) {
    //    param = "?page="+document.cnLayoutPageSelectForm.page.value;
    //}

    if (!cancelUrl || cancelUrl == '') {
        cancelUrl = '/index.php';
    }
    if (document.changedForm) {
        if (document.changedForm.changedFlag.value == 'true') {
            if (confirm('Exit without saving changes?')) {
                window.top.document.location.href=cancelUrl;
            }
        } else {
            window.top.document.location.href=cancelUrl;
        }
    } else {
        window.top.document.location.href=cancelUrl;
    }
}

function refreshScreen() {
    if (document.changedForm) {
        if (document.changedForm.changedFlag.value == 'true') {
            if (confirm('Refresh screen and lose changes?')) {
                location.reload();
            }
        } else {
            location.reload();
        }
    } else {
        location.reload();
    }
}

function resetForm(formObj) {
    if (document.changedForm) {
        if (document.changedForm.changedFlag.value == 'true') {
            if (confirm('Refresh form and lose changes?')) {
                formObj.reset();
                unchanged();
            }
        } else {
            formObj.reset();
            unchanged();
        }
    } else {
        formObj.reset();
        unchanged();
    }
}

function showLabel(message) {
    window.status = (arguments.length>0) ? message : '';
    return true;
}

function submitemail() {
    var line = document.sendemail.toaddress.value;
    if (line.indexOf("@") != -1 && line.indexOf(".") != -1) {
	    document.sendemail.submit();
	} else {
	    alert("Cannot send message with invalid email address.");
	}
    return;
}

function changed() {
    if (document.changedForm) {
        document.changedForm.changedFlag.value = 'true';
    }
}

function unchanged() {
    if (document.changedForm) {
        document.changedForm.changedFlag.value = 'false';
    }
}

function hasChanged() {
    if (document.changedForm.changedFlag.value == 'true') {
        return true;
    } else {
        return false;
    }
}

/*******************************************************************
* Function     : serializeSelect
* Author       : Ben Del Vacchio (delvach@commnav.com)
* Date         : July 2001
* Parameters   : me - <select> element reference
*                (optional) - int (see below)
* Return Value : Serialized string
*
* Notes: Converts select box data into a serialized string which
*        PHP's unserialize() function can convert into an array.
*        - The PHP array will be use .value for keys.
*        - An optional second parameter will set the starting index
*        - Currently only integers and strings are properly formatted.
*
*/
function serializeSelect(me) {
    var val;
    var start = 0;

    // Check for optional starting index
    if(arguments.length>1 && !isNaN(arguments[1]) && arguments[1]>0) {
        start = arguments[1];
        len   = me.length - start;
    } else {
        len   = me.length;
    }

    var out = 'a:' + len + ':{';

    // Format .value and .text and push to string
    // for use by PHP unserialize() to create an array
    for(i=start;i<me.length;i++) {

        val = me.options[i].value;
        txt = me.options[i].text;

        // If .value/key is string
        if(isNaN(val)) {
            out += 's:' + val.length + ':"' + val + '";';

        // if .value/key is numeric
        } else {
            out += 'i:' + val + ';';
        }

    // If .text/value is string
    if(isNaN(txt)) {
      out += 's:' + txt.length + ':"' + txt + '";';
    // If .text/value is numeric
    } else {
      out += 'i:' + txt + ';';
    }
  }

  out += '}';
  return out;
}

function serializeArray(me) {
    var val;
    var start = 0;

    // Check for optional starting index
    if(arguments.length>1 && !isNaN(arguments[1]) && arguments[1]>0) {
        start = arguments[1];
        len   = me.length - start;
    } else {
        len   = me.length;
    }

    var out = 'a:' + len + ':{';

    // Format .value and .text and push to string
    // for use by PHP unserialize() to create an array
    for(i=start;i<me.length;i++) {

        val = i;
        txt = me[i];

        // If .value/key is string
        if(isNaN(val)) {
            out += 's:' + val.length + ':"' + val + '";';

        // if .value/key is numeric
        } else {
            out += 'i:' + val + ';';
        }

    // If .text/value is string
    if(isNaN(txt)) {
      out += 's:' + txt.length + ':"' + txt + '";';
    // If .text/value is numeric
    } else {
      out += 'i:' + txt + ';';
    }
  }

  out += '}';
  return out;
}

//if (!opener) {
//    if (!top.frames.cnHeader) {
//        top.location.replace('/index.php?url='+ escape(document.location));
//    }
//}

var subWindow;
function popup(page, w, h, extra) {
	var opt_scroll;
    if (!extra || extra==null) extra="";
    opt_scroll = (!w && !h) ? "SCROLLBARS," : "";
//    if (!w && !h) opt_scroll="SCROLLBARS,"; else opt_scroll="";
    if (!w || w==null) w=450;
    if (!h || h==null) h=480;
    popupClose();

    // Center window on page
    var hLoc      = Math.floor((screen.availHeight - h)/2);
    var wLoc      = Math.floor((screen.availWidth - w)/2);
    var setCenter = ",LEFT="+wLoc+",SCREENX="+wLoc+",TOP="+hLoc+",SCREENY="+hLoc;

    var win_features = opt_scroll+"RESIZABLE,WIDTH="+w+",HEIGHT="+h+setCenter+","+extra;
	subWindow = window.open(page,"winName",win_features);
	subWindow.focus();
}

function popupScroll(page, w, h) {
    if (!w || w==null) w=450;
    if (!h || h==null) h=480;
    popupClose();

    // Center window on page
    var hLoc      = Math.floor((screen.availHeight - h)/2);
    var wLoc      = Math.floor((screen.availWidth - w)/2);
    var setCenter = ",LEFT="+wLoc+",SCREENX="+wLoc+",TOP="+hLoc+",SCREENY="+hLoc;

    win_features = "RESIZABLE,SCROLLBARS,WIDTH="+w+",HEIGHT="+h+setCenter;

    subWindow = window.open(page,"winName",win_features);
    subWindow.focus();
}

// Placed in <BODY> tag's onunload event, kills stray window if needed.
function popupClose() {
    if(subWindow) {
        if(!subWindow.closed) {
            subWindow.close();
        }
    }
}

//============================================================================//


labels = new Array;
labels[0] = new Image(); labels[0].src = "images/spacer.gif";
labels[1] = new Image(); labels[1].src = "images/labelrefresh.gif";
labels[2] = new Image(); labels[2].src = "images/labeljunior.gif";
labels[3] = new Image(); labels[3].src = "images/labeltraining.gif";
labels[4] = new Image(); labels[4].src = "images/label24.gif";
labels[5] = new Image(); labels[5].src = "images/label128.gif";
labels[6] = new Image(); labels[6].src = "images/labeladmin.gif";
labels[7] = new Image(); labels[7].src = "images/label128web.gif";
labels[8] = new Image(); labels[8].src = "images/labelextra.gif";

function labelswap(num) {
    document.iconlabel.src = labels[num].src;
}

function refreshbody() {
    if (document.changedForm) {
        if (document.changedForm.changedFlag.value == 'true') {
            if (confirm('Refresh screen and lose changes?')) {
                location.reload();
            }
        } else {
            location.reload();
        }
    } else {
        location.reload();
    }
}


function settingsJump(selectbox) {
    var opt = selectbox.options[selectbox.options.selectedIndex];
    if (opt.value == 'aboutInfo') {
        popup('about.asp',420,250);
    	selectbox.options[0].selected = true;
        return;
    }
    if (opt.value == 'cnLicenseInfo') {
        popup('LicenseInfo.asp',490,230);
        selectbox.options[0].selected = true;
        return;
    }
    if (opt.value != '') {
        window.top.document.location.href=opt.value;
    	//selectbox.options[0].selected = true;
	    //window.parent.cnBody.focus();
	}
}

/**
 * @author  murko@ht-radio.org  Micke
 * @date    Sun Aug 17 13:00:56 CET 2003 
 * @param   inString - a string  
 * @return  string trimmed of whitespace
 */
function trim(inString) {
    while (inString.substr(0, 1) == " " || inString.substr(0, 1) == "\t") {
        inString = inString.substr(1);
    }

    while (inString.substr(-1) == " " || inString.substr(-1) == "\t") {
        inString = inString.substr(0, inString.length - 1);
    }

    return inString;
}

function appsJump(selectbox) {
    var opt = selectbox.options[selectbox.options.selectedIndex];
	if (opt.value != '') {
		window.top.location.href=opt.value;
		//selectbox.options[0].selected = true;
		//window.parent.cnBody.focus();
	}
}

function nav(link) {
    if (hasChanged()) {
        if (!confirm('WARNING!\n\nInte ändra nåt?')) {
            return;
        }
    }
    location.href = link;
}

