function ajaxFunction() {
  var xmlHttp;
  try {
    xmlHttp=new XMLHttpRequest();
  } catch (e){
		try{
		  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e){
			try{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}

var request = ajaxFunction();

////////////////////////////////////////////////////////////////////////

function checkDomain(dom){
	if (!dom){
		var domain = document.getElementById('domain').value;
	} else {
		var domain = dom;
	}

	domain = domain.toString();
	domain = str_replace(" ","",domain);
	
	
//	document.getElementById('domain').value = domain;
//alert("."+domain.length);
	
	if (domain.length>1){
		
		obj = ajaxFunction();
		obj.onreadystatechange=function(){
			if(obj.readyState==4){
				outRes = document.getElementById('checkdomain');
				outRes.innerHTML = obj.responseText;
				if (obj.responseText!='' && obj.responseText!="ERROR") {
					obj = new Array;
					for (i=0; i<ext.length; i++ )
					{
						obj[i] = ajaxFunction();
						whois_domain(obj[i],domain,ext[i]);
					}
				} else if(obj.responseText=="ERROR"){
					outRes.innerHTML = "Neteisingai įvedėte interneto svetainės vardą.";

				}
			}
		}
		obj.open("GET","/main/public/ajax/whois_domain_check.php?domain="+domain,true);
		obj.send(null);
	} else {
		outRes = document.getElementById('checkdomain');
		outRes.innerHTML = '';
	}
}

function checkDomain_new(dom){
	if (!dom){
		var domain = document.getElementById('domain').value;
	} else {
		var domain = dom;
	}

	if (domain.length>1){
		domain = domain.toString();
		domain = str_replace(" ","",domain);
		document.getElementById('domain').value = domain;
		obj = ajaxFunction();
		obj.onreadystatechange=function(){
			if(obj.readyState==4){
				document.location.href="/check_domain_new.php"; 
			}
		}
		obj.open("GET","/main/public/ajax/add_domain_to_session_for_check.php?domain="+domain,true);
		obj.send(null);
	}
}
function whois_domain(obj,dmn,ext){
	request = ajaxFunction();
	obj.onreadystatechange=function(){
		if(obj.readyState==3){
			outObjResp = document.getElementById('pos'+ext);
			outObjResp.innerHTML = "tikrinam...";
			outObjResp.style.backgroundColor = '#EFEFEF';
			
		}
		if(obj.readyState==4){
			outObjRes = document.getElementById('pos'+ext);
			outObjRes.innerHTML = obj.responseText;
			outObjRes.style.backgroundColor = 'silver';
			
		}
	}
	obj.open("GET","/main/public/ajax/check_domain.php?domname="+dmn+"&extens="+ext,true);
	obj.send(null);
}

function orderDomains(frm){
	str = getSelectedFields(frm);
	if (str=='') {
		errObj = document.getElementById('mess');
		errObj.innerHTML = '<span style="color:red;">Nepasirinkote jokio vardo</span>';
	} else {
		obj = ajaxFunction();
		obj.onreadystatechange=function(){
			if(obj.readyState==4){
				//outObjRes = document.getElementById('test');
				//outObjRes.innerHTML = obj.responseText+" | "+str;
				window.document.reload();
//				reload_order_list();
			}
		}
		obj.open("GET","/main/public/ajax/add_new_domain_to_cart.php?"+str,true);
		obj.send(null);
	}
}
function orderDomains_transfer(domain_name){
//alert(domain_name);
	if (domain_name=='') {
		errObj = document.getElementById('mess');
		errObj.innerHTML = '<span style="color:red;">Nepasirinkote jokio vardo</span>';
	} else {
		obj = ajaxFunction();
		obj.onreadystatechange=function(){
			if(obj.readyState==4){
//				errObj = document.getElementById('mess');
//				errObj.innerHTML = obj.responseText;
//				document.reload();
				reload_order_list();
			}
		}
		obj.open("GET","/main/public/ajax/add_transfer_domain_to_cart.php?domain_name="+domain_name,true);
		obj.send(null);
	}
}
function reload_order_list(){
//	document.location.reload();
	outObjRes = document.getElementById('domain_cart');
	outObjRes.innerHTML = "Atnaujinama...";
	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			if (ob.responseText=='') {
				document.location.href='./index.php';
			} else {
				outObjRes.style.display='block';
				outObjRes.innerHTML = ob.responseText;
			}
		} 
	}
	ob.open("GET","/main/public/orders/ordered_domains.php",true);
	ob.send(null);
}

function getSelected(fobj,fname) { 
//	alert("imam forma");
	var str = ""; 
	var ft = ""; 
	var fv = ""; 
	var fn = ""; 
	var els = ""; 
	for(var i = 0;i < fobj.elements.length;i++) { 
		els = fobj.elements[i]; 
		ft = els.title; 
		fv = els.value; 
		fn = els.name; 
		if (els.name == fname) {
			switch(els.type) { 
				case "checkbox":  
					if(els.checked) return encodeURI(fv);  
				break;      
				case "radio":  
					if(els.checked) return encodeURI(fv);  
				break;
				case "select-one": 
					return encodeURI(fv);  
				break;
			}
		}
	} 
	return false;  
}

function getSelectedFields(fobj) { 
//	alert("imam forma");
	var str = ""; 
	var ft = ""; 
	var fv = ""; 
	var fn = ""; 
	var els = ""; 
	for(var i = 0;i < fobj.elements.length;i++) { 

		els = fobj.elements[i]; 
		ft = els.title; 
		fv = els.value; 
		fn = els.name; 

		switch(els.type) { 
			case "checkbox":  
				if(els.checked) str += fn + "=" + encodeURI(fv) + "&";  
			break;  
			case "select-one": 
				str += fn + "=" +  els.options[els.selectedIndex].value + "&";  
			break;
			case "radio":  
				if(els.checked) str += fn + "=" + encodeURI(fv) + "&";  
			break;
			case "hidden":
			case "text":  
				str += fn + "=" + encodeURI(fv) + "&";  
			break;
			case "password":  
				str += fn + "=" + encodeURI(fv) + "&";  
			break;
		} // switch  
	} // for  
	str = str.substr(0,(str.length - 1));  
	return str;  
} 



function remove_from_order(type,id,pid){
	outObjRes = document.getElementById('domain_cart');
	outObjRes.innerHTML = "Atnaujinama...";
//	alert(type+","+id+","+pid);
	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			reload_order_list();
			//outObjRes.innerHTML = ob.responseText;
		}
	}
	ob.open("GET","/main/public/orders/remove_from_order.php?type="+type+"&id="+id+"&pid="+pid,true);
	ob.send(null);
}

function assign_hosting_plane(fdata,for_domain){
	outObjRes = document.getElementById('test');
	selected = getSelected(fdata,"hosting");
	period = getSelected(fdata,"hostingperiod");
	outObjRes.innerHTML = selected;
	showWaitScreen();
	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			outObjRes.innerHTML += ob.responseText;
			reload_order_list();
			hideWaitScreen();
		}
	}

	ob.open("GET","/main/public/orders/assign_hosting_plane.php?selected="+selected+"&period="+period+"&for="+for_domain,true);
	ob.send(null);
}
function assign_email(fdata,for_domain){
	outObjRes = document.getElementById('test');
	selected = fdata;
	showWaitScreen();
	outObjRes.innerHTML = selected;
	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			outObjRes.innerHTML += ob.responseText;
			reload_order_list();
			document.getElementById('email').value = '';
			hideWaitScreen();
		}
	}

	ob.open("GET","/main/public/orders/assign_email.php?selected="+selected+"&for="+for_domain,true);
	ob.send(null);
}

function assign_redirect(fdata,for_domain){

	ftype = getSelected(fdata,"redirect");

	fvalue = document.getElementById('redirect_to').value;

	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			if (ob.responseText!='') {
				document.getElementById('redirect_message').innerHTML=ob.responseText;
			} else {
				document.getElementById('redirect_message').innerHTML='';
				reload_order_list();
			}
		}
	}

	ob.open("GET","/main/public/orders/assign_redirect.php?type="+ftype+"&value="+fvalue+"&for="+for_domain,true);
	ob.send(null);
}

function showWaitScreen(){
	var eDiv=document.createElement("DIV");
	eDiv.style.padding='20px';
	eDiv.id='waitScreen';
	eDiv.style.position="absolute";
	eDiv.style.backgroundColor="red";
	eDiv.innerHTML="Palaukite...";
	document.body.appendChild(eDiv);
	center_position('waitScreen');
}
function hideWaitScreen(){
	var eDiv = document.getElementById('waitScreen');
	eDiv.style.display='none';
}

function center_position(div)
{
	if(obj=document.getElementById(div)){
		var scroll = 'no';
		var top='30px', left='20px';
		obWidth = obj.clientWidth;
		obHeight = obj.clientHeight;
		bodyWidth = document.body.clientWidth;
		bodyHeight = document.body.clientHeight;
		
		if(obHeight < bodyHeight-68){
			obj.style.top = Math.floor((bodyHeight - obHeight)/2-14)+"px";
		} else {
			if (objC=document.getElementById(div+"_content"))
			{
				objC.style.height = (bodyHeight-60)+"px";
				objC.style.overflow = 'auto';
			}
			obj.style.top = top;
			obj.style.height = (bodyHeight-35)+"px";
			obj.style.width = (obWidth+20)+"px";
		}
		if(obWidth < bodyWidth-30){
			obj.style.left = Math.floor((bodyWidth - obWidth)/2)+"px";
		}else {
			if (objC=document.getElementById(div+"_content"))
			{
				objC.style.width = (bodyWidth-58)+"px";
				objC.style.overflow = 'auto';
				objC.style.height = (obHeight)+"px";
			}
			
			obj.style.left = left;
			obj.style.width = (bodyWidth-40)+"px";
			
		}
		//width = Math.min(width, screen.width-10);	
		//height = Math.min(height, screen.height-28);	
	}
}

function show_auth_form(name){
	aDiv = document.getElementById('auth_c');
	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			aDiv.innerHTML=ob.responseText;
		}
	}
	ob.open("GET","/main/public/"+name+".php",true);
	ob.send(null);
}



function login(frm){
	aDiv = document.getElementById('auth_mess');
	str = getSelectedFields(frm);
	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			if (ob.responseText!='') {
				aDiv.innerHTML=ob.responseText;
			} else {
				document.location.reload();
			}
		}
	}
	ob.open("GET","/main/public/auth/login_action.php?"+str,true);
	ob.send(null);
}
function logout(){
	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			document.location.href=document.location.href;
		}
	}
	ob.open("GET","/main/public/auth/logout_action.php",true);
	ob.send(null);
}
function searchClient(){
	txt = document.getElementById('find_client').value;
	if (txt.length>2){
		
		ob = ajaxFunction();
		ob.onreadystatechange=function(){
			if(ob.readyState==4){
				aDiv = document.getElementById('showClist');
				aDiv.innerHTML=ob.responseText;
			}
		}
		ob.open("GET","/main/public/ajax/select_client_by_name.php?text="+txt,true);
		ob.send(null);
	} else {
		alert('Įveskite daugiau nei 2 simbolius');
	}
}

function login_as_client(cid){
	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			document.location.reload();
		}
	}
	ob.open("GET","/main/public/auth/login_action.php?cid="+cid,true);
	ob.send(null);
}

function forget_pass_form(frm){
	if (frm.usermail.value!='') {
		ob = ajaxFunction();
		str = getSelectedFields(frm);
		fDiv = document.getElementById('ForgetForm');
		aDiv = document.getElementById('ForgetError');
		ob.onreadystatechange=function(){
			if(ob.readyState==4){
				if(ob.responseText!=''){
					retText = explode("|",ob.responseText);
					if (retText[0]=="MESSAGE") {
						fDiv.style.border = '1px solid green';
						fDiv.style.padding = '15px';
						fDiv.style.color = 'green';
						fDiv.style.margin = '5px 0px';
						fDiv.innerHTML = retText[1];
					} else if(retText[0]=="ERROR") {
						document.getElementById('captcha').src = '/public/secure/securimage_show.php?' + Math.random();
						document.getElementById('captchaCode').value = '';
						aDiv.style.border = '1px solid red';
						aDiv.style.padding = '15px';
						aDiv.style.color = 'red';
						aDiv.style.margin = '5px 0px';
						aDiv.innerHTML = retText[1];
					} else {
						aDiv.innerHTML=ob.responseText;
					}
				}

			}
		}
		ob.open("GET","/main/public/priminti_slaptazodi.php?"+str,true);
		ob.send(null);
	} else {
		aDiv = document.getElementById('ForgetError');
		aDiv.style.border = '1px solid red';
		aDiv.style.padding = '15px';
		aDiv.style.color = 'red';
		aDiv.style.margin = '5px 0px';
		aDiv.innerHTML = "Įveskite savo el. pašto adresą.";
	}
}

function change_pass_form(frm){
	ob = ajaxFunction();
	str = getSelectedFields(frm);
	fDiv = document.getElementById('changeForm');
	aDiv = document.getElementById('changeError');
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			if(ob.responseText!=''){
				retText = explode("|",ob.responseText);
				if (retText[0]=="MESSAGE") {
					fDiv.style.border = '1px solid green';
					fDiv.style.padding = '15px';
					fDiv.style.color = 'green';
					fDiv.style.margin = '5px 0px';
					fDiv.innerHTML = retText[1];
				} else if(retText[0]=="ERROR") {
					document.getElementById('captcha').src = '/secure/securimage_show.php?' + Math.random();
					document.getElementById('captchaCode').value = '';
					aDiv.style.border = '1px solid red';
					aDiv.style.padding = '15px';
					aDiv.style.color = 'red';
					aDiv.style.margin = '5px 0px';
					aDiv.innerHTML = retText[1];
				} else {
					aDiv.innerHTML=ob.responseText;
				}
			}

		}
	}
	ob.open("GET","/main/public/keisti_slaptazodi.php?"+str,true);
	ob.send(null);
}

function publicRegistrationAction(frm){
	frm = document.getElementById('regForm');
	str = getSelectedFields(frm);
	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			obOut = document.getElementById('output_mess');
			if (ob.responseText=="OK"){
				window.location.href=window.location.href;
			} else{
				obOut.style.display = 'block';
				obOut.innerHTML = ob.responseText;
			}

		}
	}
	ob.open("GET","/main/public/ajax/registracija_action.php?"+str,true);
	ob.send(null);
}

function auto_login(){
	u = document.getElementById('user_name').value;
	p = document.getElementById('user_pass').value;
	ob = ajaxFunction();
	ob.onreadystatechange=function(){
		if(ob.readyState==4){
			if (ob.responseText!='') {
				aDiv = document.getElementById('auth_c');
				if (ob.responseText=="OK") {
					return true;
				} else {
					aDiv.innerHTML=ob.responseText;
				}
			}
		}
	}
	ob.open("GET","/main/public/auth/login_action.php?fname=public_login&u="+u+"&p="+p,true);
	ob.send(null);
}
