var baseURL = window.location.href.substr(0, window.location.href.lastIndexOf("/")+1);
var dataChanged = false;

function addEvent(elm, evType, fn, useCapture) {
     // cross-browser event handling for IE5+, NS6+ and Mozilla/Gecko
     // By Scott Andrew
//     alert ("Adding event " + fn);
     if (elm.addEventListener) {
          elm.addEventListener(evType, fn, useCapture);
          return true;
     } else if (elm.attachEvent) {
          var r = elm.attachEvent('on' + evType, fn);
          return r;
     } else {
          elm['on' + evType] = fn;
     }
}

function getFormObject(id) {
	testObj = document.getElementById(id);
	if ((typeof(testObj) == "object") && (testObj !== null)) {
		return testObj;
	}
	return;
}

function get_help(item) {
    var targetWidth;
    var targetHeight;
    var availWidth = screen.availWidth;
    var availHeight = screen.availHeight;
	if (availWidth < 1000) {
         targetWidth = availWidth;
	} else {
         targetWidth = 1000;
	}
    if (availHeight < 700) {
         targetHeight = availHeight;
    } else {
         targetHeight = 700;
    }
    var attributes = "width=" + targetWidth + ",height=" + targetHeight + ",dependent=yes,resizable=yes,scrollbars=yes,title=yes";    
    window.open(baseURL + "getHelp.php?section=" + item, "help", attributes);
}

function checkDataLoss(e) {
	if (dataChanged) {
		e.returnValue = "You have unsaved changes that will be lost if you leave this page without saving them.  Do you want to continue?";
		return "You have unsaved changes that will be lost if you leave this page without saving them.  Do you want to continue?";
	}
}

function checkUnstoredData() {
    if (dataChanged) {
        if (confirm("You have unsaved changes.  Click \"OK\" to exit without saving the changes, or \"Cancel\" to return to your design.")) {
            dataChanged = false;
            location.href = baseURL + "index.php";
        }
    } else {
        location.href = baseURL + "index.php";
    }
}

function translateCardColorToHex(color) {
    var allowedColors = new Array();
    var colorCodes = new Array();
    var colorCode = null;
    allowedColors[0] = "White";
    allowedColors[1] = "Ivory";
    colorCodes[0] = "#FFFFFF";
    colorCodes[1] = "#FFFFEE";
    for (i=0; i<allowedColors.length; i++) {
		if (color == allowedColors[i]) {
        	colorCode = colorCodes[i];
            break;
        }
    }
	return colorCode;
}

function getBorderSource(selectedBorderInk, selectedBorderStyle) {
    var imgSource;
    if ((selectedBorderInk == "None") || (selectedBorderStyle == "None")) {
        imgSource = "images/spacer.gif";
    } else {
        var imgName;
        if (selectedBorderInk.length == 5) {
			imgName = selectedBorderInk.substr(1, 4);
        } else {
            imgName = selectedBorderInk;
        }
        imgSource = "images/" + selectedBorderStyle + imgName + ".gif";
    }
	return imgSource;	
}

function getColor($colorIn){
    if ($colorIn == "White"){
        return "#FFFFFF";
    } else if ($colorIn == "Ivory") {
        return "#FFFFEE";
    } else if ($colorIn == "Gray") {
        return "#DCDCDC";
    } else if ($colorIn == "BLK") {
      return "#000000";
    } else if (($colorIn == "MMAG") || ($colorIn == "MAG")) {
      return "#C50067";
    } else if (($colorIn == "MYEL") || ($colorIn == "YEL")) {
      return "#FFFF00";
    } else if (($colorIn == "MCYN") || ($colorIn == "CYN")) {
      return "#00A0DD";
    } else if (($colorIn == "MSIL") || ($colorIn == "SIL")) {
      return "#C0C0C0";
    }
}


addEvent(window, "beforeunload", checkDataLoss, false);


