opera = (navigator.userAgent.indexOf('Opera') >= 0)? true : false;
ie = (document.all && !opera)? true : false;
dom = (document.getElementById)? true : false;
ie4 = (document.all && !dom)? true : false;

var moscowId = 12679;
var khimkyId = 11396;

function imageOff(imgName) 
{
	imgsrc = document[imgName].src;
	//сохр. расширение
	ext = imgsrc.substring(imgsrc.length-3, imgsrc.length);
	imgsrc=imgsrc.substring(0, imgsrc.length-7);
	imgsrc = imgsrc + "_off."+ext;
	if (imgs_loaded) {if (document.images) {document[imgName].src = imgsrc;}}
}

function imageOn(imgName) 
{
	imgsrc = document[imgName].src;
	//сохр. расширение
	ext = imgsrc.substring(imgsrc.length-3, imgsrc.length);
	imgsrc=imgsrc.substring(0, imgsrc.length-8);
	imgsrc = imgsrc + "_on."+ext;
	if (imgs_loaded) {if (document.images) {document[imgName].src = imgsrc;}}
}



function show_hide_drajon(v) {
if(v == 1) {
	document.forms.f1.drajon.style.visibility="visible";
	document.forms.f1.dcity.style.visibility="visible";
	if (dom) {
		document.getElementById('sel_rajon').style.display="block";
		document.getElementById('sel_city').style.display="block";
	}
	else {
		document.all['sel_rajon'].style.display="block";
		document.all['sel_city'].style.display="block";
	}
}
else {
	document.forms.f1.drajon.style.visibility="hidden";
	document.forms.f1.dcity.style.visibility="hidden";
	if (dom) {
		document.getElementById('sel_rajon').style.display="none";
		document.getElementById('sel_city').style.display="none";
	}
	else {
		document.all['sel_rajon'].style.display="none";
		document.all['sel_city'].style.display="none";
	}
}
}


function display_distance_pole(type) {
	
	if(dom) {
		document.getElementById("distance_from_mkad").style.display = type;
		document.getElementById("distance_from_mkad_help").style.display = type;
	}
	else {
		document.all["distance_from_mkad"].style.display = type;
		document.all["distance_from_mkad_help"].style.display = type;
	}
}

/*
c - id города доставки
u - 0-retail, >0-админ, дилер или офисный пользователь
d - статус пользователя 0-retail,1-silver,2-gold..
t - тип доставки (см options)
only_courier - признак, что доставка осуществляется только курьером
*/
function selectDelivery(c,u,d,t,only_courier) {


	clearcombo(document.f1.dtype);
	var option0 = new Option("Самовывоз из магазина", 0);
	var option1 = new Option("Доставка по России и миру (компания СПСР)", 1);
	var option2 = new Option("Доставка курьером по Москве и области", 2);
	var option3 = new Option("Доставка по России (компания Грузовозофф)", 3);
	
	//add
	var option4 = new Option("Доставка курьером по Москве в этот же день", 5);
	var option5 = new Option("Доставка курьером по Москве в течение 3 часов", 6);
	//add

	if (u != 0){//заказ из офиса или админ или дилер
		if(c == moscowId || c == khimkyId){ // доставка в Москву или Химки
			if (d != 0){//для дилера
				document.f1.dtype.options[0]=option0; //самовывоз
				document.f1.dtype.options[1]=option2; //курьер
				document.f1.dtype.options[2]=option3; //Грузовозофф
				document.f1.dtype.options[3]=option4; //dsd
				document.f1.dtype.options[4]=option5; //срочная
				
				if(t==0) { //самовывоз
					document.f1.dtype.options[0].selected="true";
					display_distance_pole("none");
				}
				if(t==2) {//курьер
					document.f1.dtype.options[1].selected="true";
					(c==moscowId) ? display_distance_pole("block") : display_distance_pole("none");
				}
				if(t==3) {//грузовозофф
					document.f1.dtype.options[2].selected="true";
					display_distance_pole("none");
				}
				if(t==4) {//курьер в этот же день 
					document.f1.dtype.options[3].selected="true";
					display_distance_pole("none");
				}
				if(t==5) {//срочная доставка
					document.f1.dtype.options[4].selected="true";
					display_distance_pole("none");
				}

			}
			else { //для админов и off-r
				document.f1.dtype.options[0]=option0; //самовывоз
				document.f1.dtype.options[1]=option1; //СПСР
				document.f1.dtype.options[2]=option2; //курьер
				document.f1.dtype.options[3]=option3; //Грузовозофф

				document.f1.dtype.options[4]=option4; //dsd
				document.f1.dtype.options[5]=option5; //срочная

				if(t==0) {
					document.f1.dtype.options[0].selected="true";
					display_distance_pole("none");
				}
				if(t==1) {
					document.f1.dtype.options[1].selected="true";
					display_distance_pole("none");
				}
				if(t==2) {
					document.f1.dtype.options[2].selected="true";
					(c==moscowId) ? display_distance_pole("block") : display_distance_pole("none");
				}
				if(t==4) {
					document.f1.dtype.options[3].selected="true";
					display_distance_pole("none");
				}
				if(t==5) {
					document.f1.dtype.options[4].selected="true";
					display_distance_pole("none");
				}
				if(t==6) {
					document.f1.dtype.options[5].selected="true";
					display_distance_pole("none");
				}
			}
		}
		else { //другие города
			display_distance_pole("none");
			if (d != 0){//для дилера
				document.f1.dtype.options[0]=option0; //самовывоз
				document.f1.dtype.options[1]=option1; //СПСР
				document.f1.dtype.options[2]=option3; //Грузовозофф
				if(t==0) //самовывоз
					document.f1.dtype.options[0].selected="true";
				if(t==1)
					document.f1.dtype.options[1].selected="true";
				if(t==3)
					document.f1.dtype.options[2].selected="true";
			}
			else { //для админов и off-r
				document.f1.dtype.options[0]=option0; //самовывоз
				document.f1.dtype.options[1]=option1; //СПСР
				document.f1.dtype.options[2]=option3; //Грузовозофф

				if(t==0)
					document.f1.dtype.options[0].selected="true";
				if(t==1)
					document.f1.dtype.options[1].selected="true";
				if(t==3)
					document.f1.dtype.options[2].selected="true";
			}
		}
		
		//document.f1.dtype.options[t].selected="true";

	}
	else {//retail из интернета
		if (c == moscowId || c == khimkyId){// доставка в Москву или Химки
			document.f1.dtype.options[0]=option2; //курьер
			document.f1.dtype.options[1]=option4; //dsd
			document.f1.dtype.options[2]=option5; //срочная

			if(t==2)
				document.f1.dtype.options[0].selected="true";
			if(t==5)
				document.f1.dtype.options[1].selected="true";
			if(t==6)
				document.f1.dtype.options[2].selected="true";
		}
		else {//другие города
			document.f1.dtype.options[0]=option1; //спср
			document.f1.dtype.options[0].selected="true";
		}
		
	}

	//document.f1.dtype.options[t].selected="true";

}

function clearcombo(elem){	
	var i;	
	for (i = elem.options.length - 1; i >= 0; i--) 
	elem.options[i] = null;	
	elem.selectedIndex = -1;
}

function subscribe(id)
{
	open_window("pages/notify_me.php?pid="+id,400,230);
}

function printImage(img,width,height) {
	width+=10;
	height+=10;
	win2=window.open("","win2","menubar=yes, scrollbars=no, resizable=yes, status=no, width="+width+" height="+height+"");
	win2.document.writeln('<HTML><HEAD></HEAD>');
	win2.document.writeln("<BODY>");
	win2.document.writeln("<img src='"+img+"'>");
	win2.document.writeln('</BODY></HTML>');
	win2.document.writeln("<script language=\"JavaScript\">");
	win2.document.writeln("<!--");
	win2.document.writeln("var browser_name = navigator.appName;");
	win2.document.writeln("if (browser_name == \"Netscape\") {");
	win2.document.writeln("window.print();");
	win2.document.writeln("} else {");
	win2.document.writeln("var WebBrowser = '<object id=\"WebBrowser1\" width=0 height=0 classid=\"clsid:8856F961-340A-11D0-A96B-00C04FD705A2\"></object>';");

	win2.document.writeln("document.body.insertAdjacentHTML('beforeEnd', WebBrowser);");
	win2.document.writeln("WebBrowser1.ExecWB(6, 2);");
	win2.document.writeln("}");
	win2.document.writeln("//-->");
	win2.document.writeln("</script>");
}

function change_form_action(frm,pid,addName,addString){
if ((addString=="") || (addString=="0")){
	addString=1;
}
var loc="pages/add_buy.php?"+addName+"="+pid+"&kolvo_add="+addString;
frm.action="javascript:open_window('"+loc+"',300,50);void(0);";
return frm.action;
}

function _body_onload()
{
	return false;
}
function _body_onunload()
{
	lon();
}

function lon(target)
{
	try {
		if (parent.visibilityToolbar)
			parent.visibilityToolbar.set_display("standbyDisplayNoControls");
	} catch (e) {}

	try {
		if (!target)
			target = this;

		if (!target._lon_disabled_arr)
			target._lon_disabled_arr = new Array();
		else if (target._lon_disabled_arr.length > 0)
			return true;

		target.document.getElementById("loaderContainer").style.display = "";
		var select_arr = target.document.getElementsByTagName("select");

		for (var i = 0; i < select_arr.length; i++) {
			if (select_arr[i].disabled)
				continue;

			select_arr[i].disabled = true;
			_lon_disabled_arr.pop(select_arr[i]);
			var clone = target.document.createElement("input");
			clone.type = "hidden";
			clone.name = select_arr[i].name;
			var values = new Array();
			for (var n = 0; n < select_arr[i].length; n++) {
				if (select_arr[i][n].selected) {
					values[values.length] = select_arr[i][n].value;
				}
			}
			clone.value = values.join(",");
			select_arr[i].parentNode.insertBefore(clone, select_arr[i]);
		}
	} catch (e) {
		return false;
	}
	return true;
}

function loff(target)
{
	try {
		if (parent.visibilityToolbar) {
			parent.visibilityToolbar.set_display(visibilityCount
												 ? "standbyDisplay"
												 : "standbyDisplayNoControls");
		}
	} catch (e) {}

	try {
		if (!target)
			target = this;

		target.document.getElementById("loaderContainer").style.display = "none";

		if (target._lon_disabled_arr) {
			while(_lon_disabled_arr.length > 0) {
				var select = _lon_disabled_arr.push();
				select.disabled = false;

				var clones_arr = target.document.getElementsByName(select.name);
				for (var n = 0; n < clones_arr.length; n++) {
					if ("hidden" == clones_arr[n].type)
						clones_arr[n].parent.removeChild(clones_arr[n]);
				}
			}
		}
	} catch (e) {
		return false;
	}
	return true;
}
oldStatus='';

function setStatus(text) {
  oldStatus=top.status;
  top.status=text;
  return true;
}
function restoreStatus() {
  top.status=oldStatus;
}

/**
* управление окном сообщений
*/
function hideMessage()
{
   with (document.getElementById('hint_message'))
   {
		innerHTML = "";
		style.visibility = "hidden";
		style.color = "black";
   }
}

function createMessage(text, color, time_to_show)
{
	with(document.getElementById('hint_message'))
	{
		style.visibility = "visible";
		innerHTML        = text;
		style.color      = color;
		setTop();
	}
	if ( time_to_show > 0 ) {
		setTimeout("hideMessage()", time_to_show);
	}
	return true;
}

window.onscroll = setTop;

function setTop() {
	if(document.getElementById('hint_message')){
		if(MM_IE) {
			
			scrollHeight = document.body.scrollTop;
			if( typeof( window.pageYOffset ) == 'number' ) { scrollHeight = pageYOffset; scrollWidth = pageXOffset; } else {
				if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) { scrollHeight = document.body.scrollTop;} else {
					if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { scrollHeight = document.documentElement.scrollTop;} }
			}
			document.getElementById('hint_message').style.pixelTop = scrollHeight + 200;
		}
		else {
			top = window.pageYOffset + 200;
			document.getElementById('hint_message').style.top = top+"px";
		}
	}
	return true;
}

function changeCurrency(cur_id) {
	var url = window.location.href;
	if(url.indexOf('curr=') != -1) {
		url = url.substring(0,url.indexOf('curr=')-1) + url.substring(url.indexOf('curr=')+7,url.length);
	}
	if(url.indexOf('?') != -1 || url.indexOf('.html') != -1) {
		url += "&curr="+cur_id;
	} 
	else {
		url += "?curr="+cur_id;
	}
	window.location.replace(url);
}
