﻿
var keep = false;
function ToggleLoginPanel()
{
	var LoginPanel = document.getElementById ('LoginPanel');
	if (LoginPanel)
	{
		var display = LoginPanel.style.display;
		if (display == 'none' || !display)
		{
			LoginPanel.style.display = 'block';
			var LoginInput = document.getElementById ('Username');
			if (LoginInput) LoginInput.focus();
			
			keep = true;
			document.onclick = CloseLoginPanel;
			document.onkeydown = EscapeLoginPanel;
	
			var UsernameField = document.getElementById ('Username');
			var PasswordField = document.getElementById ('Password');
			var SessionSubmit = document.getElementById ('SessionSubmit');
			// if (UsernameField) UsernameField.onclick = Keep;
			//if (PasswordField) PasswordField.onclick = Keep;
			//if (SessionSubmit) SessionSubmit.onclick = Keep;
			LoginPanel.onclick = Keep;
		}
		else
		{
			LoginPanel.style.display = 'none';
		}
		return false;
	}
	else return true;	
}


function ToggleLoginPanel2()
{
	var LoginPanel = document.getElementById ('LoginPanel2');
	if (LoginPanel)
	{
		var display = LoginPanel.style.display;
		if (display == 'none' || !display)
		{
			LoginPanel.style.display = 'block';
			var LoginInput = document.getElementById ('Username');
			if (LoginInput) LoginInput.focus();
			
			keep = true;
			document.onclick = CloseLoginPanel;
			document.onkeydown = EscapeLoginPanel;
	
			var UsernameField = document.getElementById ('Username');
			var PasswordField = document.getElementById ('Password');
			var SessionSubmit = document.getElementById ('SessionSubmit');
			// if (UsernameField) UsernameField.onclick = Keep;
			//if (PasswordField) PasswordField.onclick = Keep;
			//if (SessionSubmit) SessionSubmit.onclick = Keep;
			LoginPanel.onclick = Keep;
		}
		else
		{
			LoginPanel.style.display = 'none';
		}
		return false;
	}
	else return true;	
}


function Keep()
{
	keep = true;
}

function CloseLoginPanel (event)
{
	if (keep)
	{
		keep = false;
		return;
	}
	var LoginPanel = document.getElementById ('LoginPanel');
	if (!LoginPanel) return;
	LoginPanel.style.display = 'none';

	document.onclick = null;
	document.onkeydown = null;
}

function EscapeLoginPanel (event)
{
	if (window.event) event = window.event;
	var code = event.keyCode ? event.keyCode : event.which ? event.which : null;
	if (code == 27)
	{
		var LoginPanel = document.getElementById ('LoginPanel');
		if (!LoginPanel) return;
		LoginPanel.style.display = 'none';

		document.onclick = null;
		document.onkeydown = null;
	}
}

function TestLogin()
{
	var UsernameField = document.getElementById ('Username');
	var PasswordField = document.getElementById ('Password');
	var SessionSubmit = document.getElementById ('SessionSubmit');
	if (!UsernameField || !PasswordField || !SessionSubmit) return;

	SessionSubmit.disabled = !UsernameField.value || !PasswordField.value; 
}

function SubproductChange ()
{
	var result = {ru: 0, us: 0, eu: 0};
	var f = 1;
	for (var c in SubproductsChange) {
		var o = document.getElementById ('Subproduct' + c);
		if (o && o.checked) {
			if(f) {
				f = 0;
				result.ru -= PriceRURSimple;
				result.eu -= PriceEURSimple;
				result.us -= PriceUSDSimple;
			};
			var a = SubproductsChange[c];
			result.ru += a.ru + PriceRURSimple;
			result.eu += a.eu + PriceEURSimple;
			result.us += a.us + PriceUSDSimple;
		};
	};
	
	return result;
}

function SubproductChecked (e)
{
	PricedSubproductSelected(0,GlobalLang,SubproductChange());
	SubproductSelected();
}

function SubproductSelected()
{
	var AddToChartButton = document.getElementById ('AddToChartButton');
	if (!AddToChartButton || !SubproductsChange) return;

	var disabled = true;
	
	var hasOrder = false;
	var hasPreorder = false;
	
	for (var c in SubproductsChange)
	{
		var checkbox = document.getElementById ('Subproduct' + c);
		if (checkbox && checkbox.checked)
		{
			disabled = false;
			if(SubproductsChange[c].preorder) {
				hasPreorder = true;
			} else {
				hasOrder = true;
			}
		}
	}
	AddToChartButton.disabled = disabled;
	if(hasPreorder && hasOrder){
		AddToChartButton.value = OrderAndPreorderText;
	} else if(hasPreorder) {
		AddToChartButton.value = PreorderText;
	} else {
		AddToChartButton.value = ProductPreorder ? PreorderText : OrderText;
	}
}

function PricedSubproductSelected (state, lang, price)
{
	var PriceRURValue = PriceRURSimple;
	var SalePriceRUR = document.getElementById ('SalePriceRUR');
	var PriceEURValue = PriceEURSimple;
	var PriceUSDValue = PriceUSDSimple;
	
	if(SalePriceRUR)
	{
		var SalePriceRURValue = PriceRURSale;
	}
	
	if (state) 
	{
		PriceRURValue += PriceRURAdditional;
		PriceEURValue += PriceEURAdditional;
		PriceUSDValue += PriceUSDAdditional;
	}
	
	
	
	if (price)
	{
		PriceRURValue += price.ru;
		PriceEURValue += price.eu;
		PriceUSDValue += price.us;
	}
	
	if(price.sale_price)
	{
		SalePriceRURValue += price.sale_price;
		//PriceEURValue += SalePriceEURAdditional;
		//PriceUSDValue += SalePriceUSDAdditional;
		PriceEURValue += price.sale_additional_price_eur;
		PriceUSDValue += price.sale_additional_price_usd;
	}
	
	return showChange(PriceRURValue, SalePriceRURValue, PriceUSDValue, PriceEURValue, lang);
}

function showChange (PriceRURValue, SalePriceRURValue, PriceUSDValue, PriceEURValue, lang)
{
	var PriceRUR = document.getElementById ('PriceRUR');
	var PricePiece = document.getElementById ('PricePiece');
	var SalePriceLine = document.getElementById ('SalePriceLine');
	var SalePriceRUR = document.getElementById ('SalePriceRUR');
	var SalePricePiece = document.getElementById ('SalePricePiece');
	var PriceUSD = document.getElementById ('PriceUSD');
	var PriceEUR = document.getElementById ('PriceEUR');
	
	if (!PriceRUR || !PriceUSD || !PriceEUR) return;
	
	PriceRURValue = parseInt (PriceRURValue * Discount * 100) + '';
	SalePriceRURValue = parseInt (SalePriceRURValue * Discount * 100) + '';
	PriceUSDValue = parseInt (PriceUSDValue * Discount * 100) + '';
	PriceEURValue = parseInt (PriceEURValue * Discount * 100) + '';
	
	var separator = lang == 'ru' ? ',' : '.';
	var roundSeparator = lang == 'ru' ? '&nbsp;' : ',';
	
	var s = PriceRURValue.substring (0, PriceRURValue.length - 2) + separator + PriceRURValue.substring (PriceRURValue.length - 2);

	if(s.length > 7) s = s.substring(0, s.length - 6) + roundSeparator + s.substring(s.length - 6, s.length);
	
	
	PriceRUR.innerHTML = s;
	
	if ( SalePriceRUR )
	{
		var sp = SalePriceRURValue.substring (0, SalePriceRURValue.length - 2) + separator + SalePriceRURValue.substring (SalePriceRURValue.length - 2);
		if(sp.length > 7) sp = sp.substring(0, sp.length - 6) + roundSeparator + sp.substring(sp.length - 6, sp.length);
		
		SalePriceRUR.innerHTML = sp;
	}
	
	PriceEUR.innerHTML = PriceEURValue.substring (0, PriceEURValue.length - 2) + separator + PriceEURValue.substring (PriceEURValue.length - 2);
	PriceUSD.innerHTML = PriceUSDValue.substring (0, PriceUSDValue.length - 2) + separator + PriceUSDValue.substring (PriceUSDValue.length - 2);
	
	if ( SalePriceRUR )
	{
		if(SalePriceRUR.innerHTML != PriceRUR.innerHTML)
		{
			PriceRUR.className = 'nonactual';
			PricePiece.className = 'nonactual';
			SalePriceRUR.className = 'actual';
			SalePricePiece.className = 'actual';
		}else{
			PriceRUR.className = 'actual';
			PricePiece.className = 'actual';
			SalePriceRUR.className = 'nonactual';
			SalePricePiece.className = 'nonactual';
		}
	}
}

function MoveLoginFields (where)
{
	var LoginAsNewUser = document.getElementById ('LoginAsNewUser');
	var LoginAsExistingUser = document.getElementById ('LoginAsExistingUser');
	var PasswordReset = document.getElementById ('PasswordReset');
	
	var Select1 = document.getElementById ('radio-actionSelect-1');
	var Select2 = document.getElementById ('radio-actionSelect-2');

	if (!LoginAsNewUser || !LoginAsExistingUser || !PasswordReset || !Select1 || !Select2) return;

	var LoginAsNewUserHtml = LoginAsNewUser.innerHTML;
	var LoginAsExistingUserHtml = LoginAsExistingUser.innerHTML;

	var Html = LoginAsNewUserHtml.length > LoginAsExistingUserHtml.length ? LoginAsNewUserHtml : LoginAsExistingUserHtml;

	if (Select1.checked)
	{
		LoginAsNewUser.innerHTML = Html;
		LoginAsExistingUser.innerHTML = '';
		PasswordReset.style.display = 'none';
	}
	else
	{
		LoginAsExistingUser.innerHTML = Html;
		LoginAsNewUser.innerHTML = '';
		PasswordReset.style.display = 'inline';
	}
}

function RecalcTotalCost (part, position)
{	
	var DeliveryCost = 0;
	
	for (var p = 1;; p++)
	{		
		var cell = document.getElementById ('ShipmentCosts' + p);
		if (!cell) break;

		for (var c = 1;; c++)
		{
			var span = document.getElementById ('ShipmentCost' + p + '-' + c);
			if (!span) break;
			
			var nobr = span;
			for(var i = 0; i < span.childNodes.length; i++) {
			    var node = span.childNodes[i];
			    if(node.nodeType == 1 && node.nodeName.toLowerCase() == 'nobr') {
			        nobr = node;
			        break;
			    }
			}
			if (p == part)
			{
				if (position == c)
				{
					span.className = 'visible';
					var value = nobr.innerHTML;
					value = value.replace (',', '.');
					DeliveryCost += parseFloat (value);
				}
				else
				{
					if (part == p) span.className = 'invisible';
				}
			}
			else
			{
				if (span.className == 'visible')
				{					
					var value = nobr.innerHTML;
					value = value.replace (',', '.');
					DeliveryCost += parseFloat (value);
				}
			}
		}		
	}
	
	var TotalToPay = document.getElementById ('TotalToPay');
	if (!TotalToPay) return;

	var total = TotalNudePrice + DeliveryCost + TotalDiscount;
	
	total = parseInt (total * 100.0);
	total /= 100.0;
	total = '' + total;
	if (total.indexOf ('.') == -1) total = total + '.00';
	if (total.indexOf ('.') == total.length - 2) total = total + '0';
	if (Lang == 'ru') total = total.replace ('.', ',');
	TotalToPay.innerHTML = total;
}

var OpenedSubFields = null;
function OpenSubFields (who)
{
	var subFields = document.getElementById (who);
	if (!subFields) return;

	if (OpenedSubFields) OpenedSubFields.className = 'invisible';
	subFields.className = 'visible';
	OpenedSubFields = subFields;
}


function writeImage(src, width, height) {
	document.writeln('<img src="' + src + '" alt="" border="0" height="' + height + '" width="' + width + '">');
}

function getW() {
	return document&&document.body.clientWidth ? document.body.clientWidth : 0;
}
/////////////////////////////////// Adding2Favorites
function getBrowserInfo() {
 var t,v = undefined;
 if (window.opera) t = 'Opera';
 else if (document.all) {
  t = 'IE';
  var nv = navigator.appVersion;
  var s = nv.indexOf('MSIE')+5;
  v = nv.substring(s,s+1);
 }
 else if (navigator.appName) t = 'Netscape';
 return {type:t,version:v};
}
 
function bookmark(a){
 var url = window.document.location;
 var title = window.document.title;
 var b = getBrowserInfo();
 if (b.type == 'IE' && 7 > b.version && b.version >= 4) window.external.AddFavorite(url,title);
 else if (b.type == 'Opera') {
  a.href = url;
  a.rel = "sidebar";
  a.title = url+','+title;
  return true;
 }
 else if (b.type == "Netscape") window.sidebar.addPanel(title,url,"");
 else alert("Нажмите CTRL-D, чтобы добавить страницу в закладки.");
 return false;
}




