/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

/*
 * Login helper
 * http://etraction.co.nz/
 *
 * Copyright (c) 2009 Mark Haussmann
 * Dual licensed under the MIT and GPL licenses.
 *
 * Date: 2009-09-19 17:34:21 -0500 (Thu, 19 Sep 2009)
 */
 
// alters the logins so that they have labels as text in the boxes.
// password field alternates between a password box and a text box.
$(document).ready(function () {
	(function loginLabels() {
		var name = document.getElementById('SZUsername');
		var pword = document.getElementById('SZPassword');
		if (name == undefined || pword == undefined) {return;}
		var defaultNameText = "Username";
		var defaultPwordText = "Password";
		
		name.onblur = function () {  if (this.value == '') { this.value = defaultNameText; } }
		name.onfocus = function () { if (this.value == defaultNameText) { this.value = ''; } }
		pword.onblur = function () {  if (this.value == '') { this.value = defaultPwordText; ChangeInputType(this, 'text'); } }
		pword.onfocus = function () { if (this.value == defaultPwordText) { this.value = ''; ChangeInputType(this, 'password'); } }
		if (name.value == "") { name.value = defaultNameText; }
		if (pword.value == "") { pword.value = defaultPwordText; ChangeInputType(pword, 'text'); }
	}) ();
	
	function ChangeInputType(el, newType){
		if ($.browser.msie) {
			var input2 = document.createElement('input');
			input2.id = el.id;
			input2.value = el.value;
			input2.className = el.className;
			input2.name = el.name;
			input2.onblur = el.onblur;
			input2.onfocus = el.onfocus;
			input2.type = newType;
			
			el.parentNode.replaceChild(input2,el);
			
			if (newType == 'password') input2.focus();
		} else {
			el.type = newType;
		}
	}

	// secure zones are stored in a cookie for users that are logged in
	// they are retrieved once per session from the e360 server on login
	(function setSecureZones() {
		// get cookie
		var myZoneID = $.cookie('optimalsports_securezoneid');
		
		if (myZoneID == null) {
			return false;
		}
		
		$('.hasSecureZoneId a').each(function () {
			$(this).attr('href', $(this).attr('href').replace('[secure_zone_id]', myZoneID));
		});
	}) ();
});
	
window.setSecureZoneCookie = function (id) {
	if (id != null) {
		$.cookie('optimalsports_securezoneid', id);
	}
}



