// add vip link functions

$(initCats);

function initCats(){

	function disVal()
	{
		multiSel(this);
		printHtml();
  }

  function multiSel(o)
  {

		if(typeof colectArr == 'undefined') colectArr = [];
		if(typeof colectArr[o.id] == 'undefined') colectArr[o.id] = [];

		$('#' + o.id + ' option:selected').each(function()
		{
			var exist = false;

			for(var x in colectArr[o.id])
			{
				if(colectArr[o.id][x] == this.text)
					exist = true;
					continue;
			}

			if(!exist)
			{
				if(!checkArr(o.id, colectArr[o.id])) return false;
				colectArr[o.id].push($(this).text());
			}

		});

		$('#' + o.id + ' option').each(function()
		{
			this.selected = false;

			for(var x in colectArr[o.id])
				if(this.text == colectArr[o.id][x]) this.selected = true;

		});

	}

  function delVal()
	{
		var txt = $(this).text();

		if(typeof colectArr != 'undefined')
			for(var x in colectArr)
				if(typeof colectArr[x] != 'undefined')
					for(var i in colectArr[x])
						if(colectArr[x][i] == txt) colectArr[x].splice(i, 1);

		$('#catView select option:selected').each(function(){ if($(this).text() == txt) this.selected = false; });

		printHtml();
		errMsg();
  }

  function printHtml()
  {
  	var html = '', a = [];

		$('#catView select option').each(function(){ if(this.selected) a.push($(this).text()); });

		for(var x in a)
			html += '<a href="javascript://">' + a[x] + '<img src="/resources/im/vip/del.gif" alt="" /><\/a> ';

		$('#selCat').html(html);
		$('#selCat a').click(delVal);
	}

	function checkArr(id, a)
  {
		var limit = id == 'singleCat'? 1 : 2;

		errMsg();

		if(a.length == limit)
		{
			errMsg(limit);
			return;
		}
		else
			return true;
	}

	function errMsg(l)
	{
		if(l)
			$('#catMsg').html('<b class="red">От това поле може да изберете ' + l + ' категори'
			+ (l > 1? 'и' : 'я') + '<\/b>');
		else
			$('#catMsg').html('');
	}

	$('#catView select').change(disVal);
}


/// rosy's

	function limitText(limitField, limitCount, limitNum){
		if (limitField.value.length > limitNum){
			limitField.value = limitField.value.substring(0, limitNum);
		}
		else{
			limitCount.value = limitNum - limitField.value.length;
		}
	}

    function hideRow(rowId){
        document.getElementById(rowId).style.display = 'none';
    }
    function showRow(rowId){
        document.getElementById(rowId).style.display = '';
    }
    function freeSubscr(){
        hideRow('vipImg');
        hideRow('mainCat');
        hideRow('ownerAddress');
        hideRow('ownerPhone');
        hideRow('invoiceData');
        hideRow('company');
        hideRow('address');
        hideRow('eik');
        hideRow('zdds');
        hideRow('mol');
        hideRow('paymentTitle');
        hideRow('paymentInfo');
        hideRow('paymentTip');
    }
    function paidSubscr(){
        showRow('vipImg');
        showRow('mainCat');
        showRow('ownerAddress');
        showRow('ownerPhone');
        showRow('invoiceData');
        showRow('company');
        showRow('address');
        showRow('eik');
        showRow('zdds');
        showRow('mol');
        showRow('paymentTitle');
        showRow('paymentInfo');
        showRow('paymentTip');
    }

