var s_cleared = false;

var oldCSS, total_cost=0, total_rur,ps;
var fe, fp;

function MM_findObj(n, d) { //v4.01
//  alert("find("+n+")");
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
//    alert("done");
  if(!x && d.getElementById) x=d.getElementById(n); return x;

}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function validateEmail(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}


function doUpdateTotalCost(){
  total_rur = Math.round(total_cost*document.forms['orderFrm'].elements['rate'].value*100)/100;
  total_rur = total_rur.toString();
  total_rur = total_rur.replace(".", ",");
  ps = total_rur.indexOf(",");
  if(ps>0&&ps<total_rur.length-1) total_rur = total_rur+"0";
  else if (ps<0) total_rur = total_rur+",00";
  document.forms['orderFrm'].elements['cost_total'].value = total_cost.toString()+" руб.";
}

function decrease(what) {
  fe = document.forms['orderFrm'].elements['qty'+what];
  fp = document.forms['orderFrm'].elements['price'+what];
  if(fe.value!="0") {
    fe.value = parseInt(fe.value)-1;
    total_cost = total_cost-parseFloat(fp.value);
  }
  doUpdateTotalCost();
}

function increase(what) {
  
  fe = document.forms['orderFrm'].elements['qty'+what];
  fp = document.forms['orderFrm'].elements['price'+what];
  fe.value = parseInt(fe.value)+1;
  total_cost = total_cost+parseFloat(fp.value);
  doUpdateTotalCost();
}

function doHighlight(what, color) {
	what.style.bgColor = color;
}

function doBlur(what, color) {
	what.style.bgColor = "";
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function checkCart(frm) {	
	var qty = parseInt(frm.elements['qty'].value);
	var mn = parseInt(frm.elements['mo'].value);	
	if(qty.toString()=="NaN") frm.elements['qty'].value = mn;
	if(qty<mn) frm.elements['qty'].value = mn;
	return true;
}

function addCart() {
	//var url = dp+'cart/index.php';
	var frm = document.forms['cartFrm'];
	
	var qty = parseInt(frm.elements['qty'].value);
	var mn = parseInt(frm.elements['mo'].value);
	
	if(qty.toString()=="NaN") frm.elements['qty'].value = mn;
	if(qty<mn) frm.elements['qty'].value = mn;
	
	document.forms['cartFrm'].elements['a1'].value = "add";
	openCartWindow();
	//window.location=url;		
	document.forms['cartFrm'].submit();		
}

function clearSearch(){
 if(s_cleared) return;
 else {
 document.forms['searchFrm'].elements['s'].value = "";
 s_cleared = true;
 }
}

function runActiveContent(what) {
	document.write(what);
}

function openCartWindow(url){ 
  if(url=="") url = dp+'cart/';
  if(url=="!") url = "";
  window.open(url, 'cart', 'width=650, height=350, scrollbars=yes, resizable=yes');
}

function doAddToCart(id, params){
  if(total_cost==0) alert("Пожалуйста выберите хотя бы одну позицию для добавления в корзину!");
  else {
    document.forms['orderFrm'].elements['a1'].value = "add";
    openCartWindow("!");
    document.forms['orderFrm'].submit();
  }
}

var ColorTimer = null;
var CurrentColor = new Array();
var dr = 1;
var dg = -1;
var db = -1;
var r_done = false;
var g_done = false;
var b_done = false;

function dec2hex (dec)
{
	var hexChars = "0123456789ABCDEF";
	var a = dec % 16;
	var b = (dec - a) / 16;
	hex = '' + hexChars.charAt (b) + hexChars.charAt (a);
	return hex;
}

function setCatColor (r, g, b)
{
	if(g<1) g = 0;
	if(r<1) r = 0;
	if(b<1) b = 0;
	if(g>255) g = 255;
	if(r>255) r = 255;
	if(b>255) b = 255;
	var code = document.getElementById ('rvpxiks');
	var color = '#' + dec2hex(r)+dec2hex(g)+dec2hex(b);
	if (code) code.style.color = color;
}

function initColors()
{
	CurrentColor.r = 0; // 207
	CurrentColor.g = 150; // 61
	CurrentColor.b = 0; // 36
	
	ColorTimer = setInterval (shiftColor, 50);
}

function shiftColor()
{
	var x = Math.floor (10 * Math.random());
	if (x < 4) CurrentColor.r = moveColorComponent(CurrentColor.r, 0, 200, "r");
	else if (x <7) CurrentColor.g = moveColorComponent(CurrentColor.g, 0, 200, "g");
	else CurrentColor.b = moveColorComponent(CurrentColor.b, 0, 200, "b");
	
	setCatColor (CurrentColor.r, CurrentColor.g, CurrentColor.b);

	if(r_done&&g_done&&b_done) {
		dr = -dr;
		db = -db;
		dg = -dg;
		r_done = false;
		g_done = false;
		b_done = false;
	}

	//alert("R="+CurrentColor.r.toString()+" ("+dr.toString()+"; "+r_done.toString()+"); G="+CurrentColor.g.toString()+" ("+dg.toString()+"; "+g_done.toString()+"); B="+CurrentColor.b.toString()+" ("+db.toString()+"; "+b_done.toString()+")");
	
	//clearInterval (ColorTimer);
	//ColorTimer = setInterval (SlightlyAlterColor, 150);	
}

function moveColorComponent (value, min, max, code)
{
	var step = Math.floor (20 * Math.random())-5;
	
	var d;
	
	if(code=="r") d = dr;
	if(code=="g") d = dg;
	if(code=="b") d = db;
	
	if (d<0) {
		if(value-step>min) value-=step;
		else {
			value = min;
			//alert(code+": "+value);
			if(code=="r") r_done = true;
			if(code=="g") g_done = true;
			if(code=="b") b_done = true;
		}
	} else {
		if(value+step<max) value+=step;
		else {
			value = max;
			//alert(code+": "+value);
			if(code=="r") r_done = true;
			if(code=="g") g_done = true;
			if(code=="b") b_done = true;
		}
	}

	if(code=="r") dr = d;
	if(code=="g") dg = d;
	if(code=="b") db = d;

	return value;
}

initColors(); 
