var xmlHttp
var xmlBask
var xmlComment
var xml_stock

function MM_findObj(n, d) { //v4.01
  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);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

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 Toggle(item) {
   obj=document.getElementById(item);
   visible=(obj.style.display!="none")
   key=document.getElementById("x" + item);
   if (visible) {
     obj.style.display="none";
	 pic=document.getElementById("p" + item);
	 pic.src = "pics/plus.jpg";
     
   } else {
      obj.style.display="block";
      pic=document.getElementById("p" + item);
	 pic.src = "pics/minus.jpg";
   }
}

function SetFocus(elem) {
	document.getElementById(elem).focus();
}

function MoveFocus(move, pref, id) {
	if(move == 'up')
		next = id - 1;
	if(move == 'down')
		next = id + 1;
	
		
	if(move == 'up' || move == 'down'){
		/*var n_elem = document.getElementById('tr_'+next);
		var p_elem = document.getElementById('tr_'+id);
		p_elem.style.backgroundColor = color;
		n_elem.style.backgroundColor = '#c0d2ec';*/
		document.getElementById(pref+next).focus();
	}
	else{
		document.getElementById(pref+id).focus();
		/*p_elem = document.getElementById('tr_'+id);
		p_elem.style.backgroundColor = '#c0d2ec';*/
		
	}
	
	
	
}

function returnColor(id, color) {
	var p_elem = document.getElementById('tr_'+id);
	p_elem.style.backgroundColor = color;
}

function change_focus(id, color) {
	if(document.getElementById('tr_'+id))
	    document.getElementById('tr_'+id).style.backgroundColor = color;
    if(document.getElementById('tr1_'+id))
	    document.getElementById('tr1_'+id).style.backgroundColor = color;

}


function isNumber(elem){
    var numericExpression = /^[0-9]+$/
    if(elem.value.match(numericExpression))
        return true;
    else
        return false;
    
}


function show_items(group, addr){
	obj=null
	obj=GetXmlHttpObject();
	if (obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser till the latest version!");
		return;
	}
	/*
	if(group == ''){
		document.getElementById('sec_data').innerHTML= 'Preču katalogs';
		document.getElementById('search_text').value = '';
		get_sec_text(1);
		return;
	}*/
	
	
	document.getElementById('sec_data').innerHTML= 'Loading...';
	document.getElementById('search_text').value = '';
	
	url = 'get_data.php?group='+group+'&type=1&sid='+Math.random()
  	obj.onreadystatechange=function(){
  		if(checkReadyState(obj)){
  			document.getElementById('sec_data').innerHTML=obj.responseText;
  			//show_filter(1);
  			get_sec_text(1);
            // get_basket_small();
		}
  	};
  	obj.open("GET",url,true);
  	obj.send(null);
}

function show_orders(){
	obj=null
	obj=GetXmlHttpObject();
	if (obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser till the latest version!");
		return;
	}
	
	document.getElementById('sec_data').innerHTML= 'Loading...';
	document.getElementById('search_text').value = '';
	
	url = 'get_data.php?type=4&sid='+Math.random()
  	obj.onreadystatechange=function(){
  		if(checkReadyState(obj)){
  			document.getElementById('sec_data').innerHTML=obj.responseText;
  			//show_filter(4);	
  			get_sec_text(4);
           //get_basket_small();
		}			
  	};
  	obj.open("GET",url,true);
  	obj.send(null);
}

function show_clients(){
	obj=null
	obj=GetXmlHttpObject();
	if (obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser till the latest version!");
		return;
	}
	
	document.getElementById('sec_data').innerHTML= 'Loading...';
	document.getElementById('search_text').value = '';
	
	url = 'get_data.php?type=5&sid='+Math.random()
  	obj.onreadystatechange=function(){
  		if(checkReadyState(obj)){
  			document.getElementById('sec_data').innerHTML=obj.responseText;
  			//show_filter(4);	
  			get_sec_text(5);
		}			
  	};
  	obj.open("GET",url,true);
  	obj.send(null);
}

function set_client(code){
	set_obj=null
	set_obj=GetXmlHttpObject();
	if (set_obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser till the latest version!");
		return;
	}
	
	set_url = 'set_client.php?code='+code+'&sid='+Math.random()
  	set_obj.onreadystatechange=function(){
  		if(checkReadyState(set_obj)){
  			get_sec_text(1);
  			document.getElementById('client_name').innerHTML=set_obj.responseText;
  			document.getElementById('sec_data').innerHTML='';
		}			
  	};
  	set_obj.open("GET",set_url,true);
  	set_obj.send(null);
}


function show_basket(){
	obj=null
	obj=GetXmlHttpObject();
	if (obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser till the latest version!");
		return;
	}
	document.getElementById('sec_data').innerHTML= 'Loading...';
	document.getElementById('search_text').value = '';
	
	url = 'get_data.php?type=3&sid='+Math.random()
  	obj.onreadystatechange=function(){
  		if(checkReadyState(obj)){
  			document.getElementById('sec_data').innerHTML=obj.responseText;
  			//show_filter(3);
  			get_sec_text(3);
            document.getElementById('basket_small').innerHTML='';
		}			
  	};
  	obj.open("GET",url,true);
  	obj.send(null);
}


function search_items(){
	var search_text = document.getElementById('search_text').value;
	if(search_text == '' || search_text.length < 2){
		alert('Kļūda: Ievadiet vismaz 2 simbolus!');
		return;
	}
	
	document.getElementById('sec_data').innerHTML= 'Loading...';	
		
	var parameters = "search_text=" + encodeURI(search_text);
    url = 'get_data.php?type=2&sid='+Math.random()
	xmlHttp = null;
    xmlHttp = PostXmlHttpObject();
    if (xmlHttp == null) {
    	alert('Cannot create XMLHTTP instance');
        return;
    }
    xmlHttp.onreadystatechange = function(){
		if (checkReadyState(xmlHttp)) {
            document.getElementById('sec_data').innerHTML=xmlHttp.responseText;
            //show_filter(2);
  			get_sec_text(2);
            //get_basket_small();
		}	
      };
      xmlHttp.open('POST', url, true);
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", parameters.length);
      xmlHttp.setRequestHeader("Connection", "close");
      xmlHttp.send(parameters);
}

function show_filter(sec){
	filter_obj=null
	filter_obj=GetXmlHttpObject();
	if (filter_obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser till the latest version!");
		return;
	}
	document.getElementById('sec_filter').innerHTML= 'Loading...';
	
	filter_url = 'get_filter.php?type='+sec+'&sid='+Math.random()
  	filter_obj.onreadystatechange=function(){
  		if(checkReadyState(filter_obj)){
  			document.getElementById('sec_filter').innerHTML=filter_obj.responseText;	
		}			
  	};
  	filter_obj.open("GET",filter_url,true);
  	filter_obj.send(null);
}



function get_sec_text(type){
	sec_obj=null
	sec_obj=GetXmlHttpObject();
	if (sec_obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser till the latest version!");
		return;
	}
	sec_url = 'get_sec_text.php?type='+type+'&sid='+Math.random()
  	sec_obj.onreadystatechange=function(){
  		if(checkReadyState(sec_obj)){
  			document.getElementById('sec_text').innerHTML= sec_obj.responseText;	
		}			
  	};
  	sec_obj.open("GET",sec_url,true);
  	sec_obj.send(null);
}


function get_item_stock(code){
	var stock;
	stock_obj=null
	stock_obj=GetXmlHttpObject();
	if (stock_obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser till the latest version!");
		return;
	}
	
	url = 'get_item_stock.php?code='+code+'&sid='+Math.random()
  	stock_obj.onreadystatechange=function(){
  		if(checkReadyState(stock_obj)){
  			stock = stock_obj.responseText;	
		}			
  	};
  	stock_obj.open("GET",url,true);
  	stock_obj.send(null);
}


//---add to basket
function basket_add(code, type){
// type: 0 - order array(email only),
//       1 - basket array(document+email)

	var stock;
	var numericExpression = /^[0-9]+(\.[0-9]+)?$/;
	var elem = eval("document.getElementById('qty_"+code+"')");
	if(elem.value.length == 0 || !(elem.value.match(numericExpression))){
		alert('Kļūda: Daudzums nav cipars!');
		elem.focus();
		return;
	}
	
	var parameters = "code=" + encodeURI(code) +
				  "&value=" + encodeURI(elem.value)+
                  "&type=" + type;
			  
    add_url = "basket_add.php?sid="+Math.random();
	add_obj = null;
    add_obj = PostXmlHttpObject();
    if (add_obj == null) {
    	alert('Cannot create XMLHTTP instance');
        return;
    }
    add_obj.onreadystatechange = function(){
		if (checkReadyState(add_obj)) {
			add_result = add_obj.responseText;	
			if(add_result == 1){
			   get_basket_sum(type);
			   /*
               if(type == 1){
                  get_basket_small();
               }
               */
               var tr_elem = eval("document.getElementById('tr_"+code+"')");
     		   tr_elem.style.backgroundColor="#F7EFCE";
               if(type == 0) {
                   var tr_elem = eval("document.getElementById('tr1_"+code+"')");
                   if(tr_elem)
     			   tr_elem.style.backgroundColor="#F7EFCE";
               }
			}
			else{
				qty_elem = eval("document.getElementById('qty_"+code+"')");
				qty_elem.value = '';
				qty_elem.focus();
				alert('Kļūda: Daudzums ir nepareizs!');
				return;
			}
		}
    };
    add_obj.open('POST', add_url, true);
    add_obj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    add_obj.setRequestHeader("Content-length", parameters.length);
    add_obj.setRequestHeader("Connection", "close");
    add_obj.send(parameters);
}


function basket_del(code, type){
	del_obj=null
	del_obj=GetXmlHttpObject();
	if (del_obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser till the latest version!");
		return;
	}

	del_url = 'basket_del.php?type='+type+'&code='+code+'&sid='+Math.random()
  	del_obj.onreadystatechange=function(){
  		if(checkReadyState(del_obj)){
  			show_basket();
            get_basket_sum(type);
		}
  	};
  	del_obj.open("GET",del_url,true);
  	del_obj.send(null);
}

//---basket small
function get_basket_small(){
	small_obj=null;
	small_obj=GetXmlHttpObject();
	if (small_obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser!");
		return;
	}
	url = "get_small_basket.php?sid="+Math.random()
  	small_obj.onreadystatechange=function(){
  	if(checkReadyState(small_obj)) {
  		document.getElementById('basket_small').innerHTML=small_obj.responseText;
	}
  	};
  	small_obj.open("GET",url,true);
  	small_obj.send(null);
}




//---update basket sum
function get_basket_sum(type){
	sum_obj=null;
	sum_obj=GetXmlHttpObject();
	if (sum_obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser!");
		return;
	}
	url = 'get_basket_sum.php?type='+type+'&sid='+Math.random()
  	sum_obj.onreadystatechange=function(){
  	if(checkReadyState(sum_obj)) {
  		var response = sum_obj.responseXML.documentElement;
        res_obj_1=response.getElementsByTagName("sum_1");
        // res_obj_2=response.getElementsByTagName("sum_2");
        document.getElementById('basket_sum').innerHTML='&nbsp;'+res_obj_1[0].childNodes[0].nodeValue;
        
        /* if(type == 1){
      		document.getElementById('basket_sum').innerHTML='&nbsp;'+res_obj_1[0].childNodes[0].nodeValue;      		
      		document.getElementById('spec_basket_sum').innerHTML='&nbsp;'+res_obj_2[0].childNodes[0].nodeValue;
        }
        else
            document.getElementById('spec_basket_sum').innerHTML='&nbsp;'+res_obj_2[0].childNodes[0].nodeValue;
        */

	}
  	};
  	sum_obj.open("GET",url,true);
  	sum_obj.send(null);
}

//---confirm the order
function order_confirm(){
	xmlHttp=null;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser!");
		return;
	}
	url = "order_confirm.php?sid="+Math.random()
  	xmlHttp.onreadystatechange=function(){
  	if(checkReadyState(xmlHttp)){
		document.getElementById('sec_data').innerHTML = xmlHttp.responseText;
		get_basket_sum();
	}
  	};
  	xmlHttp.open("GET",url,true);
  	xmlHttp.send(null);
}

//---confirm the order
function get_order_spec(doc_code){
	obj=null;
	obj=GetXmlHttpObject();
	if (obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser!");
		return;
	}
	url = "get_order_spec.php?doc_code="+doc_code+"&sid="+Math.random()
  	obj.onreadystatechange=function(){
  	if(checkReadyState(obj)){
		document.getElementById('sec_data').innerHTML = obj.responseText;
	}
  	};
  	obj.open("GET",url,true);
  	obj.send(null);
}


function log_in(){
	var login = document.getElementById('login').value;
	var pass = document.getElementById('pass').value;
		
	var parameters = "login="+ encodeURI(login)+"&pass="+encodeURI(pass);
    url = "login.php?sid="+Math.random()
	xmlHttp = null;
    xmlHttp = PostXmlHttpObject();
    if (xmlHttp == null) {
    	alert('Cannot create XMLHTTP instance');
        return;
    }
    xmlHttp.onreadystatechange = function(){
		if (checkReadyState(xmlHttp)) {
			var result = xmlHttp.responseText;
			if(result == 1)
            	self.location='storage.php';
            else
            	document.getElementById('err_msg').innerHTML = result;
		}	
      };
      xmlHttp.open('POST', url, true);
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      xmlHttp.setRequestHeader("Content-length", parameters.length);
      xmlHttp.setRequestHeader("Connection", "close");
      xmlHttp.send(parameters);
}







//---logout
function logout(){
	obj=null;
	obj=GetXmlHttpObject();
	if (obj==null){
		alert ("Error: Cannot create XMLHTTP instance! Update the browser!");
		return;
	}
	url = "logout.php?sid="+Math.random()
  	obj.onreadystatechange=function(){
  	if(checkReadyState(obj)) {
  		self.location='index.php';
	}
  	};
  	obj.open("GET",url,true);
  	obj.send(null);
}


//---make get request
function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}
	catch (e){
  	// Internet Explorer
  	try{
    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  	catch (e){
    	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
	return xmlHttp;
	
}

//---make post request
function PostXmlHttpObject(){
	var xmlHttp=null;
	try{
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  		if (xmlHttp.overrideMimeType) {
         	// set type accordingly to anticipated content type
             xmlHttp.overrideMimeType('text/html');
         }
  	}
	catch (e){
  	// Internet Explorer
  	try{
    	xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  	catch (e){
    	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
	
}

//---check ready state
function checkReadyState(obj){
  if(obj.readyState == 4){
    if(obj.status == 200){
      return true;
    }
    else{
      alert("Error: Problem retrieving XML data");
    }
  }
}

