var isIE4=(navigator.appVersion.indexOf("MSIE")!=-1 && parseFloat(navigator.appVersion)>=4)?true:false;
var isNN4=(navigator.appName=="Netscape" && parseFloat(navigator.appVersion)<5)?true:false;
var isNN6=(navigator.appName=="Netscape" && parseFloat(navigator.appVersion)>=5)?true:false;
var isOPERA=(navigator.appName=="Opera" && parseFloat(navigator.appVersion)>=7)?true:false;

function rusoft() {
  window.open('http://rusoft.ru');
}

function OpenPopup(src) {
	if (arguments[1]) popup_name = arguments[1];
	else popup_name = 'popup';
	if (arguments[2]) popup_width = arguments[2];
	else popup_width = 500;
	if (arguments[3]) popup_height = arguments[3];
	else popup_height = 400;
	window.open(src, popup_name, 'toolbar=no,location=no,status=no,menubar=no,resizable=yes,directories=no,scrollbars=yes,width='+popup_width+',height='+popup_height).focus();
	return false
}

function openImage(image) {
	window.open("/popup.php?img="+image, "popupimage", "scrollbars=0, resizable=1, width=520, height=450").focus();
}

function imgOpen(imgURL,imgWidth,imgHeight,Title) {
	var imgWndw=window.open('','_blank','width='+imgWidth+',height='+
	imgHeight+',toolbar=no,menubar=no,location=no,status=no,'+
	'resizable=yes,scrollbars=no');
	var imgTitle=(Title)?Title:imgURL+": "+imgWidth+'x'+imgHeight;
	with (imgWndw.document){
		open();
		write('<ht'+'ml><he'+'ad><ti'+'tle>'+imgTitle+'</ti'+'tle>'+
		'</he'+'ad><bo'+'dy onclick="window.close(); return false;" leftmargin="0" '+
		' topmargin="0" oncontextmenu="window.close(); return false;" '+
		'rightmargin="0" bottommargin="0" marginwidth="0" '+
		'marginheight="0"><img src="'+imgURL+'" width="'+imgWidth+
		'" height="'+imgHeight+'" border="0" alt="'+imgTitle+
		'"></bo'+'dy></ht'+'ml>');
		close();
	}
	return false;
}

function check(form_name,all_count) {
	if (form_name == 'goods') {
		count = 0;
		for(kk=0;kk<document.forms.goods.elements.length;kk++) {
			if (document.forms.goods.elements[kk].value == 'шт.' || document.forms.goods.elements[kk].value == '') count++;
		}
		if (count == all_count) { alert("Введите количество товаров!"); return false; }
	}
	return true;
}

var f = 0;

function about_order(id) {
	if (f != 1) {
		if (isNN6) location.href = "/cabinet/orders/your_orders?order_id="+id;
		else document.location.href = "/cabinet/orders/your_orders?order_id="+id;
	}
	f = 0;
	return false;
}

function about_comment(id) {
	if (f != 1) {
		if (isNN6) location.href = "/cabinet/orders/comments?id="+id;
		else document.location.href = "/cabinet/orders/comments?id="+id;
	}
	f = 0;
	return false;
}

function delete_comment(id){
	if (confirm('Удаление комментария № '+id+'. Вы уверены?') == true) {
		if (isNN6) location.href = "/cabinet/orders/comments?id="+id+"&act=delete";
		else document.location.href = "/cabinet/orders/comments?id="+id+"&act=delete";
	}
	f = 1;
	return false;
}

function On(id,name,count){
	document.getElementById('block'+id).style.backgroundColor='#FF0000';
	document.getElementById('pr_type_name').innerHTML = name;
	if (id > (count-1)) id = id - count;
	document.getElementById('pr_type_name').style.padding = '5px 0px 0px '+(id*35)+'px';
}
function Out(id,item_id,type_id){
	if (type_id != item_id || (!item_id && !type_id)) {
		document.getElementById('block'+id).style.backgroundColor='#7D7D7D';
	}
	document.getElementById('pr_type_name').innerHTML = '';
}

 function ShowMail(user, domain1, domain2) {
	var email = user+'&#64;'+domain1+'&#46;'+domain2;
	if (ShowMail.arguments[3]) name = ShowMail.arguments[3];
	else name = email;
	document.writeln('<a href="mailto:'+email+'">'+name+'</a>');
}

function go_to_cart(url){
	var referer = "";
	if (url != "") referer = "?referer=" + url;
	if (isNN6) location.href = "/cart"+referer;
	else document.location.href = "/cart"+referer;
	return false;
}

function ch(id,specinfo) {
	if (specinfo)
		window.open('/popup?title=Сообщение&msg_pid='+id,'popup','width=350, height=200, menubar=no, toolbar=no, scrollbars=no, resizable=yes').focus();
	document.forms['goods'+id].submit();
	return false;
}

function changeImage_over(id){
	document.getElementById('left_part'+id).src="images/blue_button_10.gif";
	document.getElementById('right_part'+id).src="images/blue_button_14.gif";
	document.getElementById('back_part'+id).style.backgroundImage="url(images/blue_button_12.gif)";
}
function changeImage_out(id){
	document.getElementById('left_part'+id).src="images/brown_button_24.gif";
	document.getElementById('right_part'+id).src="images/brown_button_28.gif";
	document.getElementById('back_part'+id).style.backgroundImage="url(images/brown_button_27.gif)";
}

function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
      endstr = document.cookie.length;
   return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
        var arg = name + "=";
        var alen = arg.length;
        var clen = document.cookie.length;
        var i = 0;
        while (i < clen) {
                var j = i + alen;
                if (document.cookie.substring(i, j) == arg)
                        return getCookieVal (j);
                i = document.cookie.indexOf(" ", i) + 1;
                        if (i == 0)
                                break;
                }
   return null;
}

function SetCookie (name, value) {
		var argv = SetCookie.arguments;
        var argc = SetCookie.arguments.length;
        var expires = (argc > 2) ? argv[2] : null;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;
        document.cookie = name + "=" + escape (value) +
                ((expires == null) ? "" : ("; expires=" +
				expires.toGMTString())) +
                ((path == null) ? "" : ("; path=" + path)) +
                ((domain == null) ? "" : ("; domain=" + domain)) +
                ((secure == true) ? "; secure" : "");
}

function Slice(show, hide){
	if (document.getElementById('doyouknow2').style.display == 'none') {
		document.getElementById('doyouknow2').style.display = 'block';
		document.getElementById('slice').style.backgroundImage = 'url(images/slice3.gif)';
		document.getElementById('slice').style.height = '43px';
		document.getElementById('slice').title = hide;
		// Сохраняем в куки
		date = new Date();
		date.setTime(date.getTime() + (365 * 24 * 3600 * 1000));
		SetCookie("showslice", "1", date, "/");
	}
	else {
		document.getElementById('doyouknow2').style.display = 'none';
		document.getElementById('slice').style.backgroundImage = 'url(images/slice1.gif)';
		document.getElementById('slice').style.height = '22px';
		document.getElementById('slice').title = show;
	}
}

function ShowDiv(id, flag){
	document.getElementById(id).style.display = flag == true ? 'block' : 'none';
}

var DivsCoords = new Array();

function MoveDiv(event, id, id2){
	var coord = this.DivsCoords[id];
	if( isIE4 || isOPERA ) {
		document.getElementById(id).style.left = event.offsetX + coord['x'] + 15;
		document.getElementById(id).style.top = event.offsetY + coord['y'] + 15;
	} else if( isNN6 || isNN4) {
		document.getElementById(id).style.left = event.pageX + 15;
		document.getElementById(id).style.top = event.pageY + 15;
	}
}

// get X coordinate of element's upper-left corner
function getDivX( szDivName )
{
	if( isIE4 )
	{
		var nPos = 0;
		objCurrent = document.all[ szDivName ];

		while( objCurrent != null )
		{
			nPos += objCurrent.offsetLeft;
			objCurrent = objCurrent.offsetParent;
		}
		return nPos;
	}
	if( isNN4 )
		return document.layers[ szDivName ].pageX; 
	if( isNN6 || isOPERA )
		return document.getElementById( szDivName ).offsetLeft;
	return 0;
}		

// get Y coordinate of element's upper-left corner
function getDivY( szDivName )
{
	if( isIE4 )
	{
		var nPos = 0;
		objCurrent = document.all[ szDivName ];
	
		// prevent relative Y positioning problems in IE55 and IE6:
		if( objCurrent.style.position.toLowerCase() != "absolute" )
			objCurrent.style.position = "static";
	
		while( objCurrent != null )
		{
			nPos += objCurrent.offsetTop;
			objCurrent = objCurrent.offsetParent;
		}

		return nPos;
	}
	if( isNN4 )
		return document.layers[ szDivName ].pageY; 
	if( isNN6 || isOPERA)
		return document.getElementById( szDivName ).offsetTop;
	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Сотрудники
function PeopleAddRow() {
    var table = document.getElementById('people_table');
    var tr = document.createElement('TR');
    
    var fields = new Array('fio', 'post', 'phone2', 'phones', 'email');
    
    var td, input, count_rows = table.rows.length;
     
    for (var i=0; i<fields.length; i++) {
        td = document.createElement('TD');
        var input = document.createElement('INPUT');
        input.type = 'text';
        input.name = input.NAME = "fld["+(count_rows - 1)+"]["+fields[i]+"]";
        input.style.width = "100%";
        input.style.textAlign = "left";
        td.appendChild(input);
        tr.appendChild(td);
    }
    
    table.appendChild(tr);
    
    if (document.all) {
        table.outerHTML = '<table class="cart" cellspacing="0" cellpadding="0" width="100%" id="people_table">' + table.innerHTML + '<table>';
    }
}

function PeopleEditRow(num, id) {
    var table = document.getElementById('people_table');
    var fields = new Array('fio', 'post', 'phone2', 'phones', 'email');
    var tr = document.getElementById("row_" + num);
    if (!tr) return false;
    
    for (var i=0; i < tr.cells.length-1; i++) {
        var input = document.createElement('INPUT');
        input.type = 'text';
        input.name = "fld["+num+"]["+fields[i]+"]";
        input.style.width = "100%";
        input.style.textAlign = "left";
        input.value = tr.cells[i].innerHTML != "&nbsp;" ? tr.cells[i].innerHTML : "";
        tr.cells[i].innerHTML = "";
        tr.cells[i].appendChild(input);
    }
    
    var input = document.createElement('INPUT');
    input.type = 'hidden';
    input.name = "fld["+num+"][id]";
    input.value = id;
    document.getElementById('people_form').appendChild(input);
}

function PeopleDelRow(num, id) {
    var tr = document.getElementById("row_" + num);
    if (!tr) return false;
    
    var text = "Вы уверены, что хотите удалить запись \r\n";
    
    for (var i=0; i < tr.cells.length-1; i++) {
        if (tr.cells[i].innerHTML != "" && tr.cells[i].innerHTML != "&nbsp;") {
            text += tr.cells[i].innerHTML + "\r\n";
        }
    }
    text += "?";
    
    if (confirm(text) == false) return false;
    
    try {
        var table = document.getElementById("people_table");
        var index;
        for (var i=0; i < table.rows.length; i++) {
            if (table.rows[i].id == "row_" + num) {
                index = i;
                break;
            }
        }
        if (index) table.deleteRow(index);
        
        var input = document.createElement('INPUT');
        input.type = 'hidden';
        input.name = "del["+num+"]";
        input.value = id;
        document.getElementById('people_form').appendChild(input);
        
        
    } catch (e) {
    }
}

/////////////////////////////////////
function OrderDel(id) {
	if (confirm('Удаление заказа № '+id+'. Вы уверены?') == true) {
		if (isNN6) location.href = "/cabinet/orders/your_orders/?order_id="+id+"&act=delete";
		else document.location.href = "/cabinet/orders/your_orders/?order_id="+id+"&act=delete";
	}
	return false;
}

function OrderEdit(id) {
    if (isNN6) location.href = "/cabinet/orders/your_orders/?order_id="+id+"&act=edit";
    else document.location.href = "/cabinet/orders/your_orders/?order_id="+id+"&act=edit";
}

function nextStep(id) {
    var form = document.forms['next_step_form'];
    var elements = form.elements['next'];
    var checked = 0;
    for (var i=0; i < elements.length; i++) {
        if (elements[i].checked) {
            checked = elements[i].value;
            break;
        }
    }
    switch (checked) {
        case 0 : {
            alert('Выберите действие!');
            break;
        }
        case 'none' : {
            document.getElementById('edit_order_popup').style.display = 'none';
            break;
        }
        case 'clear' :
        case 'add' : {
            location.href = "/cabinet/orders/your_orders/?order_id="+id+"&act=edit&next="+checked;
            break;
        }
    }
}