var ttop=0;
var count;

function scrollf() {

        if (document.getElementById("podar"))
	document.getElementById("podar").style.top = (document.documentElement.scrollTop - Math.round(getClientHeight())) + 'px';
        if (document.getElementById("h"))
	document.getElementById("h").value = document.documentElement.scrollTop;
}

window.onscroll = scrollf;

function ScrollWindowTo(h) {

        if (typeof(h) != "undefined")
            window.scroll(0,h);
}

function getWidth()
{
  return (document.documentElement.scrollWidth > document.documentElement.offsetWidth)?
    document.documentElement.scrollWidth:
    document.documentElement.offsetWidth;
}

function getHeight()
{
  return (document.documentElement.scrollHeight > document.documentElement.offsetHeight)?
    document.documentElement.scrollHeight:
    document.documentElement.offsetHeight;
}

function OnReady() {

	if (typeof(h) != "undefined")
	  ScrollWindowTo(h);
	
	//document.getElementById("podar").style.left=(getWidth()-820)/2 +820;
}


function GoDown() {
     if (ttop<-80) {ttop+=80;}
     else {ttop = 0;}
     document.getElementById("lenta").style.top = ttop+"px";
}

function GoUp(id) {
     var h;
     if (id>4)
     {
          h=-1*((id-1)*77-325);
          if (ttop<=h) {ttop = h;}
          ttop-=80;
          document.getElementById("lenta").style.top = ttop+"px";
     }
}

function showCommentsForm() {
    if (document.getElementById('comment_form')) {
        return;
    }
	
	var scroll_top = self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
	//var scroll_left = self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) || (document.body && document.body.scrollLeft);
	//alert(scroll_top);
	
    var div = document.createElement('div');
    div.id = 'comment_form';
    div.style.zIndex = 1000;
    div.style.position = 'absolute';
   // div.style.top = ((getClientHeight()/2)-250)+document.documentElement.scrollTop+"px";
    div.style.top = ((screen.availHeight - 470)/ 2)+scroll_top+"px";
    //div.style.left = ((getClientWidth()/2)-185)+"px";
    div.style.left = ((screen.availWidth - 320)/ 2)+"px";
    div.style.width = '340px';
    div.style.height = '470px';
    div.style.padding = '5px';
    div.style.backgroundColor = '#fff';
    div.style.textAlign = 'left';
    div.style.border='6px solid #00fe21';
    div.style.color='black';
    div.style.fontWeight='normal';
    div.style.padding='15px';
    div.innerHTML = 'Заполните следующие поля:<br><br>Наименование товара (например, платье):';
    var input = document.createElement('input');
    input.type = 'text';
    input.setAttribute('id', 'basket_name');
    input.style.width = '100%';
    div.appendChild(input);
    div.innerHTML += '<br><br>Артикул товара (если есть, например S007):';
    input = document.createElement('input');
    input.type = 'text';
    input.setAttribute('id', 'basket_article');
    input.style.width = '100%';
    div.appendChild(input);
    div.innerHTML += '<br><br>Цает и фасон товара (например красное с цветком):';
    input = document.createElement('input');
    input.type = 'text';
    input.setAttribute('id', 'basket_type');
    input.style.width = '100%';
    div.appendChild(input);
    div.innerHTML += '<br><br>Размер (см. таблицу в разделе "Размеры"):';
    input = document.createElement('input');
    input.type = 'text';
    input.setAttribute('id', 'basket_size');
    input.style.width = '100%';
    div.appendChild(input);
    div.innerHTML += '<br><br>Ваши пожелания и вопросы:';
    var textarea = document.createElement('textarea');
    textarea.setAttribute('id', 'basket_comment');
    textarea.style.width = '100%';
    textarea.style.height = '180px';
    div.appendChild(textarea);
    div.innerHTML += '<br><br>';
    var img = document.createElement('img');
    img.src = '/css/images/but_tobasket.png';
    img.style.float = 'left';
    img.style.margin = '0 20px';
    img.style.cursor = 'pointer';
    img.onclick = addToBasket;
    div.appendChild(img);
    img = document.createElement('img');
    img.src = '/css/images/but_cancel.png';
    img.style.float = 'right';
    img.style.margin = '0 20px';
    img.style.cursor = 'pointer';
    img.onclick = closeCommentForm;
    div.appendChild(img);
    document.body.appendChild(div);
}


function addToBasket() {

	if (!document.getElementById('system_user_id') || (document.getElementById('system_user_id').value == 0 )) {
		document.getElementById('comment_form').innerHTML = "<table width='100%'><tr><td align='center'>Необходимо зарегистрироваться!</td></tr><tr><td align='center'><a href='/registration' class='truelink'>Регистрация</a></td></tr></table><br/>" ;
		document.getElementById('comment_form').style.height = '67px';
		document.getElementById('comment_form').innerHTML += "<img src='/css/images/but_cancel.png' alt='Закрыть' style='margin: 0 20px; float: right; cursor: pointer;' onClick='closeCommentForm();'/>";
	}
	else{
		var input = document.createElement('input');
		input.type = 'hidden';
		input.name = 'i_select';
		input.value =
			'Название товара: ' + document.getElementById('basket_name').value + '<br>' +
			'Артикул: ' + document.getElementById('basket_article').value + '<br>' +
			'Цвет и фасон: ' + document.getElementById('basket_type').value + '<br>' +
			'Размер: ' + document.getElementById('basket_size').value + '<br>' +
			'Комментарии:<br>' + document.getElementById('basket_comment').value + '<br>';
		document.getElementById('order_form').appendChild(input);
		document.order.submit();
	}
}

function closeCommentForm() {
    if (document.getElementById('comment_form')) {
        document.getElementById('comment_form').parentNode.removeChild(document.getElementById('comment_form'));
    }
}


function makeOrder0() {
    var i_id = document.order.i_id.value;
    var i_name = document.order.i_name.value;
    var i_price = document.order.i_price.value;
    var i_weight = document.order.i_weight.value;
    var i_dos = document.order.i_dos.value;
    var i_h = document.getElementById("h").value;
    var i_select = document.getElementById('order_params').value.replace("\n", ' ');
    var new_loc = 'http://localhost:9090/?sess_id='+session_id+'&i_id='+i_id+'&i_name='+i_name+'&i_price='+i_price+'&i_weight='+i_weight+'&i_dos='+i_dos+'&i_h='+i_h+'&i_select='+i_select+'&rand=' + Math.random();
    document.getElementById('order_params').parentNode.parentNode.removeChild(document.getElementById('order_params').parentNode);
    window.setTimeout("window.location='"+new_loc+"';", 1000);
}

function selectAll() {
      var i;
      for (i=1; i<=count; i++)
	  {
document.getElementById('cb'+i).checked = true;
	  }
}

function var_dump(vr,recursive,indent) {

    var result = "";

    if (!indent) indent = "";

    if (typeof(vr) == 'object') {

        result = result + "\n";

        for (key in vr) {
            if (recursive) {
                result = result + indent + "[" + key + "]=>" + var_dump(vr[key], indent+"\t")+"\n";
            } else {
                result = result + "[" + key + "]=>" + vr[key] + "\n";
            }
        }

    } else {

        result = vr;
    }

    return result;
}

function getClientHeight() {
    var wiH = window.innerHeight ? window.innerHeight : 0;
    var dbH = document.body.clientHeight ? document.body.clientHeight : 0;
    var deH = document.documentElement ? document.documentElement.clientHeight : 0;

    var wHeight = 0;

    if( wiH > 0 ) {
      wHeight = ( (wiH - dbH) > 1 && (wiH - dbH) < 30 ) ? dbH : wiH;
      wHeight = ( (wHeight - deH) > 1 && (wHeight - deH) < 30 ) ? deH : wHeight;
    } else wHeight = ( deH > 0 ) ? deH : dbH;

    return wHeight;
}

function getClientWidth() {

    var deW = document.documentElement ? document.documentElement.clientWidth : 0;
    var dbW = window.innerWidth ? window.innerWidth : document.body.clientWidth;

    var wWidth = 0;

    wWidth = ( deW > 1 ) ? deW : dbW;

    return wWidth;
}

