/**
 * Resize a popup window to 90% of the screen width and to two thirds of the screen height.
 * The function also moves the window to the screen top an 5% from the left of the screen.
 */
function setWindowWidthToScreenWidth() 
{
    if (parent.parent == null)
     parent.resizeTo(screen.width * 0.9, screen.height - screen.height/3);
}

/**
 * Set/unset focus on an image.
 * The method change the src of the image with ID given by parameter "sImgID"
 * to an javascript Image with the same name. or with the same name and a digit2.
 */
function focusImage(sImgID, bFocus) {

	theImg = getObj(sImgID);

	if(bFocus)
		theImg.src = eval(sImgID).src;
	else
		theImg.src = eval(sImgID + "2").src;
}

/**
 * Set/unset clicked on an image.
 * The method change the src of the image with ID given by parameter "sImgID"
 * to an javascript Image with the same name. or with the same name and a digit3.
 */
function pushImage(sImgID, bPushed) {
	theImg = getObj(sImgID);
	if(bPushed)
		theImg.src = eval(sImgID + "3").src;
	else
		theImg.src = eval(sImgID + "2").src;
}

/**
 * Resize a popup window to the width and height of an object that can be I.e. a table
 * or a div element.
 */ 
function setWindowSize(objectId) 
{
    // Get the input object
    object = document.getElementById(objectId);

    // Get object meassures
    var height = object.offsetHeight;
    var width = object.offsetWidth;

    // Maximum height
    if(height > screen.height * 0.7)
        height = screen.height * 0.7;

    // Maximum width
    if(width > screen.width * 0.8)
        width = screen.width * 0.8;

    // Outside right screen border?
    if(parent.screenLeft + width > screen.width)
        parent.moveBy((parent.screenLeft + width - screen.width) * -2);

    // Outside bottom screen border?
    if(parent.screenTop + height > screen.height)
        parent.moveBy(0, (parent.screenTop-20) * -1);

    // Resize window
    parent.resizeTo(width + 30, height + 50);
}

function getObj(objectId) {
    return document.getElementById(objectId);
}

function popup(URI, width, height) { popup(URI, width, height, '', 'no'); }
function popup(URI, width, height, name) { popup(URI, width, height, name, 'no'); }
function popup(URI, width, height, name, scrollbars) 
{
 try {
  var win = window.open(URI, name,"width="+width+",height="+height+",status=no,toolbar=no,menubar=no,location=no,scrollbars=" + scrollbars + ",resizable=no");
  if (win == null) win = window.open(URI, "", "width="+width+",height="+height+",status=no,toolbar=no,menubar=no,location=no,scrollbars=" + scrollbars + ",resizable=yes");
  if (win != null) {
	  win.focus();
	  win.moveTo(30,30);
  }
 } catch(e) {
 }
}
function resizable_popup(URI, width, height) { resizable_popup(URI, width, height, ''); }
function resizable_popup(URI, width, height, name) 
{
 var win=window.open(URI, name,"width="+width+",height="+height+",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");
 win.focus();
}

/**
 * Check if a text contains only alphabetic and/or numeric characters.
 *
 * param: sText       - Text to validate.
 * param: bAlphabetic - All characters "A-Ö, a-ö" are seen as valid when this parameter is true.
 * param: bNumeric    - All characters "0-9" are seen as valid when this parameter is true.
 * param: bBlanks     - Blank characters are seen as valid when this parameter is true.
 *
 * return: true if all characters in sText parameter is valid.
 */
 function isValid(sText, bAlphabetic, bNumeric, bBlanks) {
    var bValid = true;
    for (var i=0; bValid && i < sText.length; i++) {		
        var c = sText.charCodeAt(i);
        bValid = false;

        // Number?
        if(bNumeric && c >= 48 && c <=57)
            bValid = true

        // Big capital alphabetic?
        if(!bValid && bAlphabetic && ((c >= 65 && c <= 90) || c == 197 || c == 196 || c == 214 || c == 45))
            bValid = true;

        // Small letter alphabetic?
        if(!bValid && bAlphabetic && ((c >= 97 && c <= 122) || c == 229 || c == 228 || c == 246))
            bValid = true;

		// Blank space
        if(!bValid && bBlanks && c == 32)
            bValid = true;
    }
    return bValid;
 }

 cellIndex = 0;
 rowIndex = 0;

 function setEditMode(cell, rowIndex, cellIndex, baseURL, size) {
	 this.rowIndex = rowIndex;
	 this.cellIndex = cellIndex;
	 if(cell.name == "view") {
		 var oldValue = cell.innerHTML;
		 cell.name = "edit";

		var sKeyDown =  "if(event.keyCode == 9) {";
			sKeyDown += "setViewMode(parentNode, '"+ baseURL + "', true , '" + oldValue + "');";
			sKeyDown += "} else if(event.keyCode == 13) {";
			sKeyDown += "setViewMode(parentNode, '"+ baseURL + "', false , '" + oldValue + "');";
			sKeyDown += "}";
		var maxlength = "";
		if(size == undefined)
			size = (cell.innerText.length+2);
		else
			maxlength = "maxlength = \"" + size + "\"";
		cell.innerHTML = "<input onblur=\"setViewMode(parentNode, null, false, '" + oldValue + "');\" onchange=\"setViewMode(parentNode, '"+ baseURL + "', false, '" + oldValue + "');\" type=\"text\" value=\"" + cell.innerText + "\" size=\"" + size + "\" " + maxlength + "onkeydown=\"" + sKeyDown + "\">";
		//alert(cell.innerHTML);
 		//cell.firstChild.focus();
  		cell.firstChild.select();
	 }
 }

 function setViewMode(cell, baseURL, focusNext, oldValue) {

	 if(cell.name == "edit") {
		 cell.name = "view";
		 var value = cell.firstChild.value;
		 cell.innerHTML = value;
	 	 //alert("baseURL: " + baseURL + "\nvalue: " + value + "\nfocusNext: " + focusNext + "\noldValue: " + oldValue);
		 if(baseURL != null && oldValue != value)
			setHiddenURL(baseURL + "/modifyTableCell.do?rowIndex=" + rowIndex + "&cellIndex=" + cellIndex + "&value=" + value);
	 }
	 if(focusNext) {
		 var cell = cell.nextSibling;
		 if(cell != null && cell.name != "view")
			 cell.click();
		 while(cell != null && cell.name != "view")
			 cell = cell.nextSibling;
		 if(cell != null) {
			cell.click();
			cell.firstChild.select();
		 }
	 }
 }

 function setHiddenURL(sURL) {
	 parent.parent.frames["hidden"].location.href = sURL;
 }

 function getHiddenFrame() {
	 return parent.parent.frames["hidden"];
 }

 function getContentFrame() {
	 return parent.frames["content"].frames["frmContent"];
 }

 function getMainPage() {
	 return parent.parent.frames["content"];
 }

// Preloading images
imgCheckbox = new Image();
imgCheckbox.src = "images/checkbox.gif";
imgCheckbox2 = new Image();
imgCheckbox2.src = "images/checkbox_checked.gif";

function checkAll(theImg) {
	var elmts = document.forms[0].elements;
	if(theImg.src == imgCheckbox.src) {
		var bCheck = true;
		theImg.src = imgCheckbox2.src;
	} else {
		var bCheck = false;
		theImg.src = imgCheckbox.src;
	}
	for(i=0; i < elmts.length; i++) {
		if(elmts[i].type == "checkbox") {
			if(bCheck) {
				elmts[i].checked = true;
			} else {
				elmts[i].checked = false;
			}
		}
	}
}

/**
 * This function can be used in textfields that allows only numeric values.
 * add following code in the input tag: onkeydown="return isNumeric()"
 */
function isNumeric() {
	var isValid = false;
	if(event.keyCode == 27
		|| (event.keyCode >= 48 && event.keyCode <= 57)
		|| (event.keyCode >= 96 && event.keyCode <= 105)
		|| event.keyCode == 8
		|| event.keyCode == 46
		|| event.keyCode == 39
		|| event.keyCode == 37
		|| event.keyCode == 36
		|| event.keyCode == 35)
			isValid = true;
	if(!isValid)
		return false;
}