// JavaScript Document
//Prototype Extensions
Object.extend(Element, {
  getWidth: function(element) {
       element = $(element);
       return element.offsetWidth;
  },
  setWidth: function(element,w) {
       element = $(element);
      element.style.width = w +"px";
  },
  setHeight: function(element,h) {
       element = $(element);
      element.style.height = h +"px";
  },
  getTop: function(element) {
       element = $(element);
       return element.style.top;
  },

  setTop: function(element,t) {
       element = $(element);
      element.style.top = t +"px";
  },
  setSrc: function(element,src) {
      element = $(element);
      element.src = src;
  },
  setHref: function(element,href) {
      element = $(element);
      element.href = href;
  },
  setInnerHTML: function(element,content) {
    element = $(element);
    element.innerHTML = content;
  }
});



function showSelectBoxes(){
  selects = document.getElementsByTagName("select");
  for (i = 0; i != selects.length; i++) {
    selects[i].style.visibility = "visible";
  }
}

// ---------------------------------------------------

function hideSelectBoxes(){
  selects = document.getElementsByTagName("select");
  for (i = 0; i != selects.length; i++) {
    selects[i].style.visibility = "hidden";
  }
}

// ---------------------------------------------------

// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll(){

  var yScroll; //Vert
  var xScroll; //Horz

  if (self.pageYOffset) {
    yScroll = self.pageYOffset;
    xScroll = self.pageXOffset;
  } else if (document.documentElement && document.documentElement.scrollTop){   // Explorer 6 Strict
    yScroll = document.documentElement.scrollTop;
    xScroll = document.documentElement.scrollLeft;
  } else if (document.body) {// all other Explorers
    yScroll = document.body.scrollTop;
    xScroll = document.body.scrollLeft;
  }

  arrayPageScroll = new Array(xScroll,yScroll)
  return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){

  var xScroll, yScroll;

  if (window.innerHeight && window.scrollMaxY) {
    xScroll = document.body.scrollWidth;
    yScroll = window.innerHeight + window.scrollMaxY;
  } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
    xScroll = document.body.scrollWidth;
    yScroll = document.body.scrollHeight;
  } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
    xScroll = document.body.offsetWidth;
    yScroll = document.body.offsetHeight;
  }

  var windowWidth, windowHeight;
  if (self.innerHeight) {  // all except Explorer
    windowWidth = self.innerWidth;
    windowHeight = self.innerHeight;
  } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
    windowWidth = document.documentElement.clientWidth;
    windowHeight = document.documentElement.clientHeight;
  } else if (document.body) { // other Explorers
    windowWidth = document.body.clientWidth;
    windowHeight = document.body.clientHeight;
  }

  // for small pages with total height less then height of the viewport
  if(yScroll < windowHeight){
    pageHeight = windowHeight;
  } else {
    pageHeight = yScroll;
  }

  // for small pages with total width less then width of the viewport
  if(xScroll < windowWidth){
    pageWidth = windowWidth;
  } else {
    pageWidth = xScroll;
  }


  arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
  return arrayPageSize;
}

// -----------------------------------------------------------------------------------

/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function serialize(myArray)
{
    var counter = 0;
    var vardef = "";
    for (var key in myArray)
    {

        counter = counter +1;
        var length = myArray[key].length;
        if (length == "undefined")
        length = 1;
        vardef = vardef + "s:" + key.length + ":\"" + key + "\";" + "s:" + length + ":\"" + myArray[key] + "\";";

    }
    var serialized = "a:" + counter + ":{" + vardef + "}";
    return serialized;
}

function addslashes(str) {
  str=str.replace(/\'/g,'\\\'');
  str=str.replace(/\"/g,'\\"');
  str=str.replace(/\\/g,'\\\\');
  str=str.replace(/\0/g,'\\0');
  return str;
}

function stripslashes(str) {
  str=str.replace(/\\'/g,'\'');
  str=str.replace(/\\"/g,'"');
  str=str.replace(/\\\\/g,'\\');
  str=str.replace(/\\0/g,'\0');
  return str;
}

function js_array_to_php_array (a)
// This converts a javascript array to a string in PHP serialized format.
// This is useful for passing arrays to PHP. On the PHP side you can
// unserialize this string from a cookie or request variable. For example,
// assuming you used javascript to set a cookie called "php_array"
// to the value of a javascript array then you can restore the cookie
// from PHP like this:
//    <?php
//    session_start();
//    $my_array = unserialize(urldecode(stripslashes($_COOKIE['php_array'])));
//    print_r ($my_array);
//    ?>
// This automatically converts both keys and values to strings.
// The return string is not URL escaped, so you must call the
// Javascript "escape()" function before you pass this string to PHP.
{
    var a_php = "";
    var total = 0;
    for (var key in a)
    {
        ++ total;
        a_php = a_php + "s:" +
                String(key).length + ":\"" + String(key) + "\";s:" +
                String(a[key]).length + ":\"" + String(a[key]) + "\";";
    }
    a_php = "a:" + total + ":{" + a_php + "}";
    return a_php;
}

  //************************************************************************************************************************************
  //************************************************************************************************************************************
  //************************************************************************************************************************************


<!-- Original:  Ronnie T. Moore -->
<!-- Web Site:  The JavaScript Source -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function isValidDate(dateStr) {
// Date validation function courtesty of
// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert(dateStr + " Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false;
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;
}

function isValidTime(timeStr) {
// Time validation function courtesty of
// Sandeep V. Tamhankar (stamhankar@hotmail.com) -->

// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.

var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

var matchArray = timeStr.match(timePat);
if (matchArray == null) {
alert("Time is not in a valid format.");
return false;
}
hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];

if (second=="") { second = null; }
if (ampm=="") { ampm = null }

if (hour < 0  || hour > 23) {
alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
return false;
}
if (hour <= 12 && ampm == null) {
if (confirm("Please indicate which time format you are using.  OK = Standard Time, CANCEL = Military Time")) {
alert("You must specify AM or PM.");
return false;
   }
}
if  (hour > 12 && ampm != null) {
alert("You can't specify AM or PM for military time.");
return false;
}
if (minute < 0 || minute > 59) {
alert ("Minute must be between 0 and 59.");
return false;
}
if (second != null && (second < 0 || second > 59)) {
alert ("Second must be between 0 and 59.");
return false;
}
return true;
}

function dateDiff(dateform) {
date1 = new Date();
date2 = new Date();
diff  = new Date();

if (isValidDate(dateform.firstdate.value) && isValidTime(dateform.firsttime.value)) { // Validates first date
date1temp = new Date(dateform.firstdate.value + " " + dateform.firsttime.value);
date1.setTime(date1temp.getTime());
}
else return false; // otherwise exits

if (isValidDate(dateform.seconddate.value) && isValidTime(dateform.secondtime.value)) { // Validates second date
date2temp = new Date(dateform.seconddate.value + " " + dateform.secondtime.value);
date2.setTime(date2temp.getTime());
}
else return false; // otherwise exits

// sets difference date to difference of first date and second date

diff.setTime(Math.abs(date1.getTime() - date2.getTime()));

timediff = diff.getTime();

weeks = Math.floor(timediff / (1000 * 60 * 60 * 24 * 7));
timediff -= weeks * (1000 * 60 * 60 * 24 * 7);

days = Math.floor(timediff / (1000 * 60 * 60 * 24));
timediff -= days * (1000 * 60 * 60 * 24);

hours = Math.floor(timediff / (1000 * 60 * 60));
timediff -= hours * (1000 * 60 * 60);

mins = Math.floor(timediff / (1000 * 60));
timediff -= mins * (1000 * 60);

secs = Math.floor(timediff / 1000);
timediff -= secs * 1000;

dateform.difference.value = weeks + " weeks, " + days + " days, " + hours + " hours, " + mins + " minutes, and " + secs + " seconds";

return false; // form should never submit, returns false
}
//  End -->














//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//--- COOKIES -----------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------

/*
  This note must stay intact.

  Author:
    Mattias Rundqvist, Webparts (www.webparts.se)
  License:
    Creative Commons Attribution-Share Alike 3.0 License
    http://creativecommons.org/licenses/by-sa/3.0/
*/

function wp_Cookie( mcfg ) {

  var cookie = new Array();
  var cookie_cache = "";

  mcfg = mcfg?mcfg:{};

  var cfg = {
    expires : mcfg["expires"]?mcfg["expires"]:"",
    expires_unit : mcfg["expires_unit"]?mcfg["expires_unit"]:"minutes",
    path : mcfg["path"]?mcfg["path"]:"/",
    use_local_path : (mcfg["use_local_path"]==true)?true:false,
    domain : mcfg["domain"]?mcfg["domain"]:"",
    secure : (mcfg["secure"]==true)?true:false
  };

  if( cfg["use_local_path"] == true )
    cfg["path"] = "";

  function calcExpires( expires ) {

    if( expires ) {
      var expires_units = {
        years : (365*24*60*60*1000),
        months : (30*24*60*60*1000),
        weeks : (7*24*60*60*1000),
        days : (24*60*60*1000),
        hours : (60*60*1000),
        minutes : (60*1000),
        seconds : (1000)
      };

      var now = new Date();
      expires = expires * expires_units[cfg["expires_unit"]];
      expires = new Date( now.getTime() + expires );
    }
    return expires;
  }

  function cache() {

    // -- Har document.cookie ändrats?
    if( cookie_cache == document.cookie )
      return false;

    // -- Töm befintlig information...
    cookie = new Array();

    // -- ...och cache'a om...
    cookie_cache = document.cookie;
    var pairs = cookie_cache.split("; ");
    var name="",value="";

    for( count = 0; count < pairs.length; count++ ) {
      name = unescape(pairs[count].split("=")[0]);
      if( pairs[count].indexOf("=") != -1 )
        value = unescape(pairs[count].split("=")[1]);
      if( name ) {
        cookie[name] = value;
      }
    }
    return true;
  }

  this.length = function() {
    cache();
    var tmp = this.cookies();
    return tmp?tmp.length:0;
  }

  this.get = function( name ) {
    cache();
    var value = null;
    if( typeof name != "undefined" ) {
      // -- returnera kakans värde
      if( typeof cookie[name] != "undefined" )
        value = cookie[name];
    }
    return value;
  }

  this.cookies = function() {
    cache();
    var value = null;
    // -- returnera namn på alla kakor
    for( name in cookie ) {

      if( !value ) {
        value = new Array();
      }

      // -- Av någon anledning returneras även prototypes i for-in satser...?
      if( typeof value[name] != "function" )
        value[value.length]=name;
    }

    return value;
  }

  this.set = function( name, value ) {

    // expires, path, domain, secure
    var expires = calcExpires( (typeof arguments[2]!="undefined")?arguments[2]:cfg["expires"] );
    var path = (typeof arguments[3]!="undefined")?arguments[3]:cfg["path"];
    var domain = (typeof arguments[4]!="undefined")?arguments[4]:cfg["domain"];
    var secure = (typeof arguments[5]!="undefined")?arguments[5]:cfg["secure"];

    var str_cookie = name + "=" +escape( value ) +
    ( ( expires ) ? ";expires=" + expires.toGMTString() : "" ) +
    ( ( path ) ? ";path=" + path : "" ) +
    ( ( domain ) ? ";domain=" + domain : "" ) +
    ( ( secure ) ? ";secure" : "" );

    document.cookie = str_cookie;

    return true;
  }

  this.remove = function( name ) {

    if( typeof name == "undefined" ) {
      name = "";
    }

    this.set(name,"",-1);

    cache();
  }

  this.clear = function() {

    cache();
    names = this.cookies();

    for( count = 0; count < names.length; count++ ) {
      this.set(names[count],"",-1);
    }
    cache();

  }

  this.test = function() {

    if( typeof document.cookie == "undefined" )
      return false;

    this.set("test_cookie_name","test_cookie_value");

    if( this.get("test_cookie_name")!="test_cookie_value" )
      return false;

    this.remove("test_cookie_name");

    return true;
  }

}


//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//--- END COOKIES -------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
