function SelectWrapper(select) {
    this.select = select;

    this.getSelectedValue = function () {
        return select.options[this.select.selectedIndex].value;
    }

    this.getSelectedValues = function() {
        var result = [];
        for (var i = 0; i < this.select.options.length; i++) {
            if (this.select.options[i].selected) {
                result[result.length] = this.select.options[i].value;
            }
        }
        return result;
    }

    this.getSelectedOptions = function() {
        var result = [];
        for (var i = 0; i < this.select.options.length; i++) {
            if (this.select.options[i].selected) {
                result[result.length] = this.select.options[i];
            }
        }
        return result;
    }

    this.getSelectedOption = function() {
        for (var i = 0; i < this.select.options.length; i++) {
            if (this.select.options[i].selected) {
                return this.select.options[i];
            }
        }
    }

    this.selectValue = function(value) {
        for (var i = 0; i < this.select.options.length; i++) {
            if (this.select.options[i].value == value) {
                this.select.options[i].selected = true;
            }
        }

    }

    this.selectValues = function (values) {
        for (var i = 0; i < values.length; i++) {
            this.selectValue(values[i]);
        }
    }

    this.getSelectedTexts = function() {
        var result = [];
        for (var i = 0; i < this.select.options.length; i++) {
            if (this.select.options[i].selected) {
                result[result.length] = this.select.options[i].text;
            }
        }
        return result;
    }

    this.getSelectedText = function () {
        return this.select.options[this.select.selectedIndex].text;
    }

    this.selectAll = function() {
        for (var i = 0; i < this.select.options.length; i++) {
            this.select.options[i].selected = true;
        }
    }

    this.clearSelection = function () {
        for (var i = 0; i < this.select.options.length; i++) {
            this.select.options[i].selected = false;
        }
    }

    this.isSelected = function (index) {
        return this.select.options[index].selected;
    }

    this.clear = function(){
        this.select.options.length = 0;
    }

    this.setValues = function(values) {
        this.clear();
        this.addValues(values);
    }

    this.addValues = function(values) {
        for (var i = 0; i < values.length; i++){
            this.select.options[i] = new Option(values[i][1], values[i][0]);
            if (values[i].length == 3 ){
                this.select.options[i].title = values[i][2];
            }
        }
    }

    this.addOption = function(value, text) {
		var i = this.select.options.length;
		this.select.options[i] = new Option(text, value);
		this.select.options[i].title = value;
    }


	this.showSelected = function() {
        var newOptions = [];
        for (var i = 0; i < this.size(); i++){
            if(this.isSelected(i)){
                newOptions[newOptions.length] = this.select.options[i];
            }
        }
        this.clear();
        this.setOptions(newOptions);
    }

    this.size = function () {
        return this.select.options.length;
    }

    this.setOptions = function (newOptions){
        for (var i = 0; i < newOptions.length; i++){
            this.select.options[i] = newOptions[i];
        }
    }

    this.mergeValues = function(values){
        for (var i = 0; i < values.length; i++){
            if (! this.hasValue(values[i]) ){
                this.select.options[this.select.options.length] = new Option(values[i][1], values[i][0]);
                if (values[i].length == 3 ){
                    this.select.options[i].title = values[i][2];
                }
            }
        }
    }

    this.hasValue = function (value){
        for (var i = 0; i < this.select.options.length; i++) {
            if (value[0] == this.select.options[i].value) {
                return true;
            }
        }
        return false;
    }

    this.getText = function(value) {
        for (var i = 0; i < this.select.options.length; i++) {
            if (value == this.select.options[i].value) {
                return this.select.options[i].text;
            }
        }
        return null;
    }

	this.getOption = function(value){
		for (var i = 0; i < this.select.options.length; i++) {
			if (value == this.select.options[i].value) {
				return this.select.options[i];
			}
		}
		return null;
	}

}

function Set(){
    this.array = [];

    this.initByArray = function (newArray){
        this.array = newArray;
    }

    this.initByString = function(str, sep) {
        this.array = str.split(sep);
        if (str.length == 0){
            this.array = [];
        }
    }

    this.remove = function (el){
        var newArray = [];
        for (var i = 0; i < this.size(); i++){
            if (this.get(i) != el){
                newArray[newArray.length] = this.get(i);
            }
        }
        this.array = newArray;
    }

    this.size = function (){
        return this.array.length;
    }

    this.get = function(index){
        return this.array[index];
    }

    this.contains = function(el){
        for (var i = 0; i < this.size(); i++){
            if (this.get(i) == el){
                return true;
            }
        }
        return false;
    }

    this.add = function (el){
        if (!this.contains(el)) {
            this.array[this.array.length] = el;
        }
    }

    this.toArray = function (){
        return this.array;
    }

    this.toString = function() {
        var result = "";
        var comma = "";
        for (var i = 0; i < this.size(); i++){
            result += comma + this.get(i);
            comma = ",";
        }
        return result;
    }
}
function addAllAtBookPage(form){
	if (form.oldAction == null){
		form.oldAction = form.action;
	}
	var buyQuantities = document.getElementsByName("buyQuantities");
	var buyShippingMethodIds = document.getElementsByName("buyShippingMethodIds");
	var buyInventoryIds = document.getElementsByName("buyInventoryIds");
	var includeToBuy = document.getElementsByName("includeToBuy");

	var query = "";
	var sep = "?"
	for (var i = 0; i < buyQuantities.length; i++){
		if (includeToBuy[i].checked){
			query += sep + "buyQuantities=" + buyQuantities[i].value +
					"&buyShippingMethodIds=" + buyShippingMethodIds[i].value +
					"&buyInventoryIds=" + buyInventoryIds[i].value;
			sep = "&";
		}
	}
	form.action = form.oldAction + query + sep + "buy=Add All";
	form.submit();
}
function buyBookAtBookPage(form, itemId){
	if (form.oldAction == null){
		form.oldAction = form.action;
	}
	var buyQuantity = document.getElementById("buyQuantities" + itemId);
	var buyShippingMethodId = document.getElementById("buyShippingMethodIds" + itemId);
	form.action = form.oldAction + "?buyQuantities=" + buyQuantity.value + "&buyShippingMethodIds=" + buyShippingMethodId.value + "&buy=";
	form.submit();
}
/**
 * This function is designed for IE because option onclick is not called within IE.
 * @param htmlSelect
 */
function selectChanged(htmlSelect){
	//option.onclick works in IE
	var select = new SelectWrapper(htmlSelect);
	var selectedOption = select.getSelectedOptions()[0];
	if (selectedOption.onclick != null){
		selectedOption.onclick();
	}
}

function contactUser( username ) {
	if (window.result != null) { window.result.transport.abort(); }
	document.location.href = '/messanger/action/SendMessage.action?username=' + username;
}

function getContentWidth() {
	return document.compatMode == 'CSS1Compat' && !window.opera ? document.documentElement.clientWidth : document.body.clientWidth;
}

function getContentHeight() {
	return document.compatMode == ('CSS1Compat' && !window.opera)
			? document.documentElement.clientHeight
			: document.body.clientHeight;
}

function getWindowWidth(){
	if( typeof( window.innerWidth ) == 'number' ) {
	  //Non-IE
	  return window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	  //IE 6+ in 'standards compliant mode'
	  return document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	  //IE 4 compatible
	  return document.body.clientWidth;
	}
}
function getWindowHeight(){
	if( typeof( window.innerWidth ) == 'number' ) {
	  //Non-IE
	  return window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	  //IE 6+ in 'standards compliant mode'
	  return document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	  //IE 4 compatible
	  return document.body.clientHeight;
	}
}

function setUpSize(){
	$('cp-overlay').style.height = getContentHeight() + 'px';
	$('cp-overlay').style.width = getContentWidth() + 'px';

	$('cp-content').style.top = (getWindowHeight() / 2.5) + 'px';
	$('cp-content').style.left = (getContentWidth() / 2 - 200)  + 'px';
	$('cp-content').style.position = "fixed";
}

window.onresize = function() {
	setUpSize();
}

function jConfirm(msg, onYes) {
	setUpSize();
	$('cp-msg').innerHTML = msg;
	$('cp-yes').onclick = onYes;
	$('cp-yes-no').show();
	$('cp-ok-tr').hide();
	$('cp').show();
}

function jAlert(msg, onclick) {
	setUpSize();
	$('cp-msg').innerHTML = msg;
	$('cp-yes-no').hide();
	$('cp-ok-tr').show();
	$('cp').show();
	if (onclick){
		var oldOnclick = $("cp-ok").onclick;
		$("cp-ok").onclick = function(){
			oldOnclick();
			onclick();
		};
	}
}

function errorNotify(msg) {
	$("titleerror").innerHTML = msg;
	jAlert(msg);
}