﻿//Function customPopup() version 1.2 
//Original source last update: 16:01 2007-12-13
//Coded and commented by:
    //Linus Persson
    //Webdesigner
    //Softgear AB
    //+46 (0) 735 88 74 14
    //linus.persson@softgear.se
    //http://softgear.se
    
//If you copy this code and change something in it, write when you did it here.
//-----------------------------------------------------------------------------
//Changed by:   Linus Persson
//Last change:  16:02 2007-12-13
//-----------------------------------------------------------------------------


//Call this function width: customPopup('URL to display');
//Example: <a href="#" onclick="return customPopup('myPopupWindow.htm');">Click here to open popup</a>
function customPopup(purl) {
    
    //This function opens a new window, positioned in the center of the screen.
    //Width and height on the window will depend on the users screen resolution.
    
    //===> USE THE FOLLOWING VARIABLES TO COSTOMIZE YOUR OWN WINDOW!
    //======================================================================================================================================================================
    //Setting variables that we use to custommize our new window
    //These variables we can change dependig on where we run this code.
    var support = "support@softgear.se";            //Mail used for contact if any problem is detected
    var english = false;                            //True = report errors in english; False = report errors in swedish
    var scroll  = false;                            //If our new window should have a scrollbar or not
    var pixelsX = true;                             //WINDOW WIDTH    True = define window size by using pixels; False = define window size by using percentage
    var pixelsY = true;                             //WINDOW HEIGHT   True = define window size by using pixels; False = define window size by using percentage
    var ourX    = 720;                              //IF "PixelsX" id TRUE: define window width with pixels   |  IF "PixelsX" id FALSE: define window width with percentage
    var ourY    = 735;                              //IF "PixelsX" id TRUE: define window height with pixels  |  IF "PixelsX" id FALSE: define window height with percentage
    //======================================================================================================================================================================
    //===> END OF WINDOW CUSTOMIZATION
    
    //Checking so that width and height is correctly set
    //If not, we get an error message and the code stops running.
    if( isNaN(ourX) && isNaN(ourY) ) {
        alert("Some of the variables that requires to be a number isNaN!");
        return false;
    }
    
    //checking if we're going to use scrollbars or not.
    if( scroll ) {
        scroll = ",scrollbars";
    }
    if( !scroll ) {
        scroll = "";
    }
    
    //Setting other variables, these should not be changed!
    var popUrl  = purl;                             //This is the URL that will be loaded into our new window
    var winX    = window.screen.width+"string";     //User screen width
    var winY    = window.screen.height+"string";    //User screen height
    var popWin  = null;                             //Variable used to control our new window
    
    //Making sure that screen width/height is a number
    var rexp    = /\D/gi;                   //Regular Expression looking for non-digits
    winX        = winX.replace(rexp,"");    //Replacing non-digits with nothing
    winY        = winY.replace(rexp,"");    //Replacing non-digits with nothing
    
    if(!isNaN(winX) && !isNaN(winY))
    {
        //Setting custom width and height for out new window
        //If width or height should be defined with percentage
        if( !pixelsX ) {
            //Converting the percentage we defined erlier so we can use it mathematical.
            ourX = ourX / 100;
            //Setting width and height with percentage (screen width * our percentage)
            var popX = Math.round(winX * ourX);
        }
        if( !pixelsY ) {
            //Converting the percentage we defined erlier so we can use it mathematical.
            ourY = ourY / 100;
            //Setting width and height with percentage (screen width * our percentage)
            var popY = Math.round(winY * ourY);
        }
        //If width and height should be defined with pixels
        if( pixelsX ) {
            //Setting width and height with pixels
            var popX = ourX;
        }
        if( pixelsY ) {
            //Setting width and height with pixels
            var popY = ourY;
        }
        
        
        //Setting custom position for our new window
        //Substracting window width/height devided by 2 from screen width/height devided by 2 to get correct positioning.
        var posX = (winX / 2) - (popX / 2);
        var posY = (winY / 2) - (popY / 2);
            
        //Opening our new window and defining properties for it
        popWin = window.open(popUrl,"name","width=" + popX + ",height=" + popY + "" + scroll + "");
        //Moving our window to where we want it
        popWin.moveTo( posX, posY );
        //Forcing the users browser to focus on our new window
        popWin.focus();
    }
    else {
        //If something went wrong we display a error message.
        //Language depends on what we set
        if( !english )
            alert("Någonting gick fel!\nVar snäll kontakta " + support + " om problemet fortsätter att uppstå!\nTack!");
        if( english )
            alert("Something went wrong\nPlease contact " + support + " if this problem continues to occur\nThank you!");
    }
    
    //Returning false so that if we use a link to trigger this function it wont jump to another page or reload it
    //It also prevents any further code to run, comment away return to run more code, we might want some code to help us debug?
      return false;
      
    
    //Optional code goes here
    var op = "";
    
    op = op + "winX:\t\t" + winX + "px\n";
    op = op + "winY:\t\t" + winY + "px\n";
    if(pixelsX){
        op = op + "ourX:\t\t" + ourX + "px\n"; }
    if(pixelsY){
        op = op + "ourY:\t\t" + ourY + "px\n"; }
    if(!pixelsX){
        op = op + "ourX:\t\t" + ourX*100 + "%\n"; }
    if(!pixelsY){
        op = op + "ourY:\t\t" + ourY*100 + "%\n"; }
    op = op + "Position:\n";
    op = op + "left:\t\t" + posX + "px\n";
    op = op + "top:\t\t" + posY + "px";
    
    alert(op);
    //Optional code ends here
      return false;
      
}
