// cookie.js - version 0.0
//
// Copyright (c) 2008. Andrei Ivanov.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   * Redistributions of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//   * Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//   * Neither the name of Adobe Systems Incorporated nor the names of its
//     contributors may be used to endorse or promote products derived from this
//     software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

// setCookie() 
// name - имя cookie
// value - значение cookie
// [expires] - дата окончания действия cookie (по умолчанию - до конца сессии)
// [path] - путь, для которого cookie действительно (по умолчанию - документ, в котором значение было установлено)
// [domain] - домен, для которого cookie действительно (по умолчанию - домен, в котором значение было установлено)
// [secure] - логическое значение, показывающее требуется ли защищенная передача значения cookie

function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

// getCookie() 
// name - имя считываемого cookie
function getCookie(name) {
        var prefix = name + "="
        var cookieStartIndex = document.cookie.indexOf(prefix)
        if (cookieStartIndex == -1)
                return null
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
        if (cookieEndIndex == -1)
                cookieEndIndex = document.cookie.length
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
}

// getCookieIndex
// Parameters:
//	idx	0- zone, 1- user, 2-password, 3- style sheet name, 4- language
// Return: user name stored int the cookie
function getCookieIndex (idx) {
	var cookie = getCookie('zup');
	if (cookie) {
		var ca = cookie.split(':');
		if (ca.length>idx) {
			return ca[idx];
		}
	}
	return "";
}

// getCookieZone
// Return: user name stored int the cookie
function getCookieZone () {
	return getCookieIndex(0);
}

// getCookieUser
// Return: user name stored int the cookie
function getCookieUser () {
	return getCookieIndex(1);
}

// getCookiePassword
// Return: password stored int the cookie
function getCookiePassword () {
	return getCookieIndex(2);
}

// getUserStyleSheetName
// Return: style sheet name
function getCookieUserStyleSheetName (defaultStyleName) {
	var ss = getCookieIndex(3);
	if (ss)
		return ss;
	else
		return defaultStyleName;
}

// getLanguage
// Return: language
function getLanguage (defaultLang) {
	var s = getCookieIndex(4);
	if (s)
		return s;
	else
		return defaultLang;
}

// setSelectValue(sel, v, exact)
// Return: true or false
function setSelectValue(sel, v, isnum, exact) {
	var vl;
	for (var i = 0; i < sel.options.length; i++) {
//		alert(sel.id + ' len: '+ sel.options.length + ' option: ' + sel.options[i].value + ' v:' + v);
		if (exact) {
			if (sel.options[i].value == v) {
				sel.selectedIndex = i;
				return true;
			}
		} else {
			if (isnum) {
				vl = parseInt(v);
			} else {
				vl = v;
			}
			if (sel.options[i].value >= vl) {
				sel.selectedIndex = i;
				return true;
			}
		}

	}
	if (isnum && sel.options.length)
		sel.selectedIndex = sel.options.length - 1;
	return false;
}

// extractTimeHour
// Return: 0..23 from the date given in seconds since epoch 1970
function extractTimeHour(secs) {
	var d = new Date();
	d.setTime(1000 * eval(secs));
	return d.getHours();
}


function urlParametersArray(url) {
	url = url.replace(/^\?/, "");
	var r = url.split('&');
	return r;
};
