 var i = 0 ;
 var timeout = null;
 var Request ='';
 var Garant = 0;

 var arts = [];
  
function makeorder(art,type,btype){
   try{
    q = document.getElementById(art).value;
    document.getElementById(art).value = '';
   }catch(e){
    try{
     q = document.getElementById(art+'.qu').value;
    }catch(e){
     q = 0;
    }
    //document.getElementById(art+'.qu').value = '';
   }
   
   if( type == 'del' ) q=0;
   
   //alert(""+art+"/"+q);
   src = "./index.php?order&"+type+"&art="+URLEncode(art)+"&q="+q+"&basket="+btype;
   
   reply = AjaxRQ('GET',src,null);
   
   document.getElementById('basket').innerHTML = reply;

   try{
    sum = document.getElementById('shop_glsum').innerHTML;
    if(( type=='add' ) & ( q > 0 ))
     alert("Насос добавлен в корзину Вашего заказа!\n Сумма Вашего заказа составляет: "+sum+" б.р.");
   }catch(e){
   }; 
}
function all_makeorders(){
 for(var i in arts) {
   src = "./index.php?order&add&art="+URLEncode(i)+"&q="+arts[i]+"&basket=small";
   reply = AjaxRQ('GET',src,null);
   document.getElementById(i).value = '';
 }
 document.getElementById('basket').innerHTML = reply;
   try{
    sum = document.getElementById('shop_glsum').innerHTML;
    alert("Насосы добавлены в корзину Вашего заказа!\n Сумма Вашего заказа составляет: "+sum+" б.р.");
   }catch(e){
   }; 
 
}
 
 
function onChange(){
 try{
  var rq = document.getElementById('sinput').value;
 }catch(e){
  var rq = Request;
 }
 
 var src = "";
 if( Garant == 0 ){
  src = "./index.php?calc&script&query="+URLEncode(rq)
 }else{
  src = "./index.php?calc&script&garant&query="+URLEncode(rq)
 }
 
 //debug.innerHTML = debugStr(document.getElementById('orgtxt'));
 
 if( rq.length == 0 ){
  //try{ orgtxt.style.display=''; }catch(e){ document.getElementById('orgtxt').style.display=''; }
  try{ sDIV.style.display='none'; }catch(e){ document.getElementById('sDIV').style.display='none'; };
 }else{
  el = document.getElementById('sinput');
  try{ sDIV.style.display=''; }catch(e){ document.getElementById('sDIV').style.display=''; };
  try{ sDIV.innerHTML = AjaxRQ('GET',src,null); }catch(e){ document.getElementById('sDIV').innerHTML=AjaxRQ('GET',src,null); };
	
 }
 
}

function onChangeTimer(value) {
  Request = value; 
	if (timeout) clearTimeout(timeout);
	timeout = setTimeout(onChange, 700);
}

function onBodyLoad(){
 onChange();
 try{
 if( opener.document.prjf == null ){
   buttons.style.display = 'none';
 } else {
   
 }
 }catch(e){
  buttons.style.display = 'none';
 }
}

function calcSumm(){
 gs = 0;
 var str = '';
 var strSent = '';

 table = document.getElementById('CurPrj');
 for(k=1;k<table.rows.length;k++){
  uid = table.rows[k].id.substr(2);
  q = document.getElementById('quan'+uid).value;
  price = document.getElementById('price'+uid).innerHTML;
  document.getElementById('sum'+uid).innerHTML = q * price;
	gs += q * price;
	str += ''+uid+'/'+q+'/'+price+' ';
	name = document.getElementById('name'+uid).innerHTML;
	strSent += ''+name+'   '+q+'pc  '+price+'EUR   \n';
 };
	
 
 try{
  document.getElementById('summ').value = gs;
 }catch(e){
 }

 try{
  document.getElementById('gsumm').innerHTML = gs;
 }catch(e){
 }

 try{
	if(table.rows.length > 0){
		document.getElementById('devices').value = strSent+'\n   Total: '+gs+'EUR';	
	}else{
		document.getElementById('devices').value = '';
	}

 }catch(e){}

/*
 try{
  document.getElementById('devices').innerHTML = srtSent;
 }catch(e){
  document.getElementById('summ').value = gs;
	document.getElementById('devices').value = str;
 }
*/
}

function DropRow( ind ){
 table = document.getElementById('CurPrj');
 for(k=1;k<table.rows.length;k++){
  uid = table.rows[k].id.substr(2);
	if( (''+uid) == (''+ind) ){
	 table.deleteRow(k);
	 break;
	}
 };
 calcSumm();
	return false;
}

function deleteRow(r){
	var i=r.parentNode.parentNode.rowIndex;
	document.getElementById('CurPrj').deleteRow(i);
	calcSumm();
	return false;
}

function addGarant(art,name){
 try{ garant_name.value=name; }catch(e){ document.getElementById('garant_name').value=name; };
 try{ garant_art.value=art; }catch(e){ document.getElementById('garant_art').value=art; };
}
 
function addOrder(uid,name,price){
  i = uid;
	table = document.getElementById('CurPrj');
	
	if( document.getElementById('tr'+i) == null ) {
	
 	  tr = table.insertRow(table.rows.length);
		
  	tr.id = 'tr'+i; 
  	
  	td = tr.insertCell(0);
  	td.innerHTML = name;
  	td.id = 'name'+i;
  	td.className = 'line';
  	tr.appendChild(td);
  	
  	td = tr.insertCell(1);
  	td.innerHTML = price;
  	td.style.textAlign='right';
  	td.id = 'price'+i;
  	td.className = 'line';
  	tr.appendChild(td);
  	
  	td = tr.insertCell(2); //количество
  	td.innerHTML = "<input type='text' id='quan"+i+"' value='1' class='input' style='width: 30px;text-align: right;' onkeyup='javascript:calcSumm();' />";
  	tr.appendChild(td);
  	
  	td = tr.insertCell(3); //сумма
  	td.innerHTML = price * 2;
  	td.style.textAlign='right';
  	td.className = 'line';
  	td.id = 'sum'+i;
  	tr.appendChild(td);
  	
  	td = tr.insertCell(4); //удаление
  	td.innerHTML = "<a href='#' style='color:red' onclick='return deleteRow(this)' >X</a>";
  	td.style.textAlign='center';
  	td.className = 'line';
  	td.id = 'drop'+i;

  	tr.appendChild(td);
	} else {
	  document.getElementById('quan'+i).value ++ ; 
	}
	
  calcSumm();
	return false;
}

function SaveRes( what ){
 if( (what == 1) || ( what==3 ) ){
  opener.document.prjf.summ.value=gsumm.innerHTML;
 }
 
 if( (what == 2) || ( what==3 ) ){
  opener.document.prjf.devices.value += "\n";
	
	table = document.getElementById('CurPrj');
	
  for(k=1;k<table.rows.length;k++){
   uid = table.rows(k).id.substr(2);
   q = document.getElementById('quan'+uid).value;
	 if( q==0 )continue;
   name = document.getElementById('name'+uid).innerHTML;
	 opener.document.prjf.devices.value += ""+k+". "+name+" - "+q+"st.\n";
  }
	
 }
} 

function URLEncode( val )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" + "";
					//"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = val;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "%20";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
				encoded += "%20";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	//document.URLForm.F2.value = encoded;
	return encoded;
};
 
function onBlur(){
 try{
  sDIV.style.display='none';
 }catch(e){
	document.getElementById('sDIV').style.display='none';
 }
}
function onBlurTimer(){
	timeout = setTimeout(onBlur, 200);
}

function openCalc(){
 regForm.style.display='none';
 regCalc.style.display='';
 
 regCalc.innerHTML = AjaxRQ('GET','index.php?calc&obj',null);
 
 return false;
}
