//pseWin - Namespace for different types of windows.
//pseWin.SIMPLE - Window without toolbars, menubar etc.
//pseWin.SCROLL - Window with scroll, without toolbars, menubar etc.
//pseWin.SCROLLRESIZE - Window with scroll, resizable, without toolbars, menubar etc.
//pseWin.FULLSIZE - Window that will open and fill the screen completely (but not windows toolbar at the bottom).
//pseWin.COMPLETE - Window with all toolbars, menubar etc.
//pseWin.PRINT - Window with menubar with printbutton.
var pseWin = new Object();
pseWin.SIMPLE = 1;
pseWin.SCROLL = 2;
pseWin.SCROLLRESIZE = 3;
pseWin.FULLSIZE = 4;
pseWin.COMPLETE = 5;
pseWin.PRINT = 6;

/*===============
Name: openWin
Purpose: Open windows of different types.
         Can be called with optional nr of parameters (except for mandatory pseWinType and url).
Parameters:
    pseWinType (Object) - type of window that should be opened
    url (String) - url in new window
    width (Number)
    height (Number)
    title (String) unique name for window if it's important not to reuse eventual previously opened windows
    top (Number)
    left (Number)
Example of usage:
    onclick="openWin(pseWin.SCROLLRESIZE,'/elin_v2/main_do.jsp',500,400,'elinWinPopup',10,10);"
Return value: if window was opened or not, inverse, so it can stop an normal open
===============*/
// Complete is like _blank on anchor.. Scroll, resize, menubar,location and statusbar.

function openWindow(pseWinType, url, width, height, title, top, left) {
    var newWin = openWindowAndGetRef(pseWinType, url, width, height, title, top, left);
	// Check if pop-up was blocked
	if( newWin ){
		//Put focus on opened window.
		newWin.focus();
		// return false so the browser don't follows the link
		return false;
	} else {
		// return true so the browser will follow the link
		return true;
	}
}

/*===============
Name: openWin
Purpose: Hide return value from openWindow for href="javascript: can't handle it.
Unknown reason why this function is needed.
Return value: none
===============*/
function openWin(pseWinType, url, width, height, title, top, left) {
	openWindow(pseWinType, url, width, height, title, top, left);
}

/*===============
Name: openWin
Purpose: Open windows of different types.
         Can be called with optional nr of parameters (except for mandatory pseWinType and url).
Parameters:
    pseWinType (Object) - type of window that should be opened
    url (String) - url in new window
    width (Number)
    height (Number)
    title (String) unique name for window if it's important not to reuse eventual previously opened windows
    top (Number)
    left (Number)
Example of usage:
    onclick="openWindowAndGetRef(pseWin.SCROLLRESIZE,'/elin_v2/main_do.jsp',500,400,'elinWinPopup',10,10);"
Return value: A reference to the window that was opened.
===============*/
function openWindowAndGetRef(pseWinType, url, width, height, title, top, left) {
    var prefs = "";

    if (title == null) {
        //window title is null, set default
        title = "winTitle";
    }
    if (pseWinType == pseWin.FULLSIZE && window.screen) {
        //If window should be of type FULLSIZE, set width and height highest possible (according to screen)
        prefs = "width=" + (screen.availWidth - 10) + ",height=" + (screen.availHeight - 30);
    } else if (width == null || height == null) {
        //Width or height not specified, set default values.
        prefs = "width=500,height=300";
    } else {
        //Width and height specified.
        prefs = "width=" + width + ",height=" + height;
    }

    if (pseWinType == pseWin.FULLSIZE) {
        //If window should be of type FULLSIZE, set position to 0,0.
        top = 0;
        left = 0;
    }
	//Set Top pref if it's supplied
	if( top != null ){
		prefs += ",screenY=" + top + ",top=" + top;
	}
	//Set Left pref if it's supplied
	if( left != null ){
		prefs += ",screenX=" + left + ",left=" + left;
	}

    switch (pseWinType) {
        case 1: //SIMPLE
            prefs += ',toolbar=0,scrollbars=no,status=no,resizable=no';
            break;
        case 2: //SCROLL
            prefs += ',toolbar=0,scrollbars=yes,status=no,resizable=no';
            break;
        case 3: //SCROLLRESIZE
            prefs += ',toolbar=0,scrollbars=yes,status=no,resizable=yes';
            break;
        case 4: //FULLSIZE
            prefs += ',toolbar=0,scrollbars=yes,status=no,resizable=yes';
            break;
        case 5: //COMPLETE, ALL FEATURES
            prefs += ',location=1,toolbar=1,menubar=1,scrollbars=yes,status=yes,resizable=yes';
        case 6: //PRINT FEATURES
            prefs += ',location=0,toolbar=1,menubar=1,scrollbars=yes,status=yes,resizable=yes';
    }
    //Return reference to window.
    var newWindow = window.open(url, title, prefs);
    return newWindow;

}


/*===============
Name: popupVillkor
Purpose: Opens window from href on anchor
TODO: This function should be moved to the application that uses it.
Return value: if window was opened or not inverse
===============*/
function popupVillkor(e) {
	if (!e) e = window.event;
	return openWindow(3, getTargetElement(e).href, 700, 615);
}


/*===============
Name: printWin
Purpose: Trigger print of document (and allows future changes if necessary, not using window.print directly in html).
Return value: none
===============*/
function printWin(){
    window.print();
}

/*============================================================
Name:    goSelectedURL
Purpose:
         Use as onclick-event on a button together with a selectlist.
         The different options in the selectlist contains different URLs.
         First the user select an option then he/she clicks on a button.. and
         get directed to the selected url.
Parameters:
         selectFieldId (String) The id of the selectlist.
Example of usage:
         <form action="">   //surrounding form-tag
         <select id="mySelectList" ...
              //options containing URLs
         </select>
         <input type="button" onclick="goSelectedURL('mySelectList')" ...
         </form>   //surrounding form-tag

         ( Important! Remember never to nest form-tags. ie <form><form></form></form> )

Return value:
         none
============================================================*/
function goSelectedURL(selectFieldId){
    //get reference to selectlist
    var objSelectField = document.getElementById(selectFieldId);
    //set loction to selected option-url
    window.location = objSelectField.options[objSelectField.selectedIndex].value;
}
















