var nextCallFunction = null;
var sid;
var pcs2;
var checkEvent;
var add; 
var tc;
var sb;
var sid1;
var lbl ;
var pcsAdd = 0;
var strSID;
//var checkQuantity = new Array();
var costArray = new Array();
var idsArray = new Array();
var quantityArray ;
var serviceParts;
var bool;
var shopText;
var isShopsel;
var pers;


function showPleaseWait(waitText)
{
	var dv = get_obj("pleaseWait");
	dv.style.height = "100%";
	dv.style.width = "100%";
	dv.style.position = "absolute";
	dv.style.top = 0;
	dv.style.left = 0;
	if(typeof waitText != "undefined") dv.innerHtml = waitText; else dv.innerHtml = "Please, wait...";
	dv.style.display = "block";
}

function hidePleaseWait(){
	var p2 = document.getElementById("pleaseWait");
	if(typeof p2 == "object") p2.style.display = "none"; 
}


function multiplyCost(inpID)
{        
  
    var y = get_obj(inpID).getAttribute("idsCount");	
		pcsAdd=0;	
		quantityArray = new Array();  
	
	 if(idsArray.length==0){idsArray.length=y;}
	 for(var i=0;i<idsArray.length;i++){
	
		var pcs = document.getElementById(idsArray[i]).value;		
		var re = /^[0-9]+$/;
		if (!pcs.match(re) || pcs==0 || pcs=="") {
		    document.getElementById(idsArray[i]).value=1;
		    pcs = document.getElementById(idsArray[i]).value;		
		}
		quantityArray.push(pcs); 
		pcsAdd = pcsAdd + (costArray[i] * quantityArray[i]);
	  
	    get_obj("total_cost").value = pcsAdd;
		var newCost = get_obj(inpID).getAttribute("newCost"); 
	    pers = get_obj(inpID).getAttribute("pers"); 
		
    }
	if(pers != 0){ 
	document.getElementById('persCost_' + inpID).value = newCost*pcs + " " + "AMD" + " (-" + pers + "%)";
	}
}

function updateCost(obj){
	
	var sid = obj.getAttribute("value");	
	serviceParts = obj.getAttribute("serviceParts");
	shopText = obj.getAttribute("shopText");
	isShopsel =  obj.getAttribute("isShopsel");
   	pcs2 = document.getElementById(strSID).value; 
	tc = parseFloat(get_obj("total_cost").value);
	sb = get_obj("submitBtn");
   
	lbl = get_obj("serivceLi_" + sid);
	
	if(obj.checked){ 
	   tc = tc + (pcs2 * add);  
	   lbl.style.color = "red";
	}
	else{ 
     var pcsOut = document.getElementById(strSID).value;
     	 tc = tc - (pcs2 * add); 
		 lbl.style.color = "black";
		 
		for(var i=0; i<=idsArray.length; i++){		
			 if(idsArray[i] == strSID){
			 idsArray.splice(i,1);  
			 }
			 if(costArray[i] == add){
			 costArray.splice(i,1); 
		 	 }
		
		}
	}
	
	if(tc <= 0 && !bool){ 
	sb.disabled = true; 
	} 
	if(tc <= 0 && bool){ 
	sb.disabled = false; 
	}	
	
	get_obj("total_cost").value = tc;
		
}


function checkForm(){ 
	var tc = parseFloat(get_obj("total_cost").value);
    var aa = new Array(21,22,23,24,25,102,106,107,108,109,110,111,112,113,118,119,120,121,122);
	
	for(var i=0;i<aa.length;i++){
	   for(var j=0;j<idsArray.length;j++){ 
	  	  if(aa[i] == idsArray[j]){
			if(bool && (document.getElementById('selshop_' +idsArray[j]).value==shopText || document.getElementById('selshop_' +idsArray[j]).value==""))
			{	
				alert(isShopsel);	
				return false;
			}
		  }
	   }
	}
	
	if(tc==0 && !bool){
	//alert(isShopsel);	
	return false;
	}
	submitAjaxForm("id_service_form");
	nextCallFunction = "loadPage('login');";
	return false;
}


function submitLoginForm()
{
	if(!checkLoginForm()) return false;
	submitAjaxForm("login_form");
	nextCallFunction = "loadPage('login');";
	return true;
}

function checkLoginForm()
{
	var un = Trim(get_obj("id_username").value);
	var ps = Trim(get_obj("id_password").value);
	var cv = Trim(get_obj("code").value);
	if(un == "") {alert("Please, enter your username.");return false;}
	if(ps == "") {alert("Please, enter your password.");return false;}
	if(cv == "") {alert("Please, enter security code.");return false;}
	return true;
}

function submitOrderDetailsForm(){
	var id_fullname = Trim(get_obj("id_fullname").value);
	var id_email = Trim(get_obj("id_email").value);
	var id_phone = Trim(get_obj("id_phone").value);
	var id_cell = Trim(get_obj("id_cell").value);
	var id_street = Trim(get_obj("id_street").value);
	var id_home = Trim(get_obj("id_home").value);
	
	if(id_fullname == "") {alert_focus("Please, fill in your full name.", "id_fullname");return false;}
	if(id_email == "" || !validEmail(id_email)) {alert_focus("Please, fill in correct email address.", "id_email");return false;}
	if(id_phone == "") {alert_focus("Please, fill in your phone number.", "id_phone");return false;}
	if(id_cell == "") {alert_focus("Please, fill in your cell phone number.", "id_cell");return false;}
	if(id_street == "") {alert_focus("Please, fill in your street.", "id_street");return false;}
	if(id_home == "") {alert_focus("Please, fill in your house/home number.", "id_home");return false;}
	
	submitAjaxForm("order_details_form");
	nextCallFunction = "loadPage('order_accept');";
	
	return false;
}

function startNewUserRegistration(){
	loadPage("registration", "content");
}

function submitRegistrationForm(isAjax){
	if(typeof isAjax == "undefined") isAjax = true;
	var id_fullname = Trim(get_obj("id_fullname").value);
	var id_username = Trim(get_obj("id_username").value);
	var id_password = Trim(get_obj("id_password").value);
	var id_password_retype = Trim(get_obj("id_password_retype").value);
	var id_email = Trim(get_obj("id_email").value);
	var id_phone = Trim(get_obj("id_phone").value);
	var id_cell = Trim(get_obj("id_cell").value);
	var id_street = Trim(get_obj("id_street").value);
	var id_home = Trim(get_obj("id_home").value);
	
	if(id_fullname == "") {alert_focus("Please, fill in your full name.", "id_fullname");return false;}
	if(id_username == "") {alert_focus("Please, fill in your username.", "id_username");return false;}
	if(id_password == "") {alert_focus("Please, fill in your password.", "id_password");return false;}
	if(id_password_retype == "") {alert_focus("Please, retpye your password.", "id_password_retype");return false;}
	if(id_password != id_password_retype) {alert_focus("Please, retype correct password.", "id_email");return false;}
	if(id_email == "" || !validEmail(id_email)) {alert_focus("Please, fill in correct email address.", "id_email");return false;}
	if(id_phone == "") {alert_focus("Please, fill in your phone number.", "id_phone");return false;}
	if(id_cell == "") {alert_focus("Please, fill in your cell phone number.", "id_cell");return false;}
	if(id_street == "") {alert_focus("Please, fill in your street.", "id_street");return false;}
	if(id_home == "") {alert_focus("Please, fill in your house/home number.", "id_home");return false;}

	if(!isAjax) {
		submitAjaxForm("registration_form", true);
		nextCallFunction = "loadPage('registration_ok')";
	}
	
	return isAjax;
}

function loadOrderAccept(){
	loadPage("order_accept");
}

function checkExtendedUrl(){
	var p = detectPageHistory();
	if(p == "services") return;
	loadPage(p);
	return false;
}

function submitOrderAccept()
{
	var agr = document.getElementById("agreement");
	if(!agr.checked) {
		alert(error_agree);
		return false;
	}
	
	//alert("tc===========" + tc);
	submitAjaxForm("order_accept"); 
	if(tc==0){
	nextCallFunction = "loadPage('non_payment');";
	}
	else{
	nextCallFunction = "loadPage('payment');";
	}
	return false;
}

function orderAccept(){
	
}

function detectPageHistory(){
	var hr = document.location.href;
	var parts = hr.split("#");
	if(parts.length <= 1) return "services";
	return parts[parts.length-1];
	
}

function goPrev(){
	//alert(historyData);
	loadPage(getHistory());
}

function checkOnlineQuestionForm(){
	var a2 = Trim(get_obj("id_question").value);
	
	if(a2.length < 10) {
		alert_focus(error[0], "id_question");
		return false;
	}
	
	return true;
}

function checkService(serviceID) 
{ 
	  sid = get_obj(serviceID); 
	  strSID = sid.id.substr(8);
	  add = parseFloat(sid.getAttribute("serviceCost")); 
	  var oldCost = parseFloat(sid.getAttribute("oldCost"));
      var pers1=parseFloat(sid.getAttribute("pers"));
           if(sid.checked){ bool=sid.checked;
		      document.getElementById('text_'+ strSID).style.visibility ='visible';
		      document.getElementById('text_'+ strSID).style.height='auto';
			  document.getElementById(strSID).style.visibility ='visible';
			  document.getElementById('persCost_' + strSID).style.color = 'red';
			  document.getElementById(strSID).value=1; 
			  idsArray.push(strSID);
			  costArray.push(add);
			} 
		    else{ bool=sid.checked;  
			  document.getElementById('text_'+ strSID).style.visibility ='hidden';
			  document.getElementById('text_'+ strSID).style.height='0px';
		      document.getElementById(strSID).style.visibility ='hidden';	
			  document.getElementById('persCost_' + strSID).style.color = 'black';
			  if(pers1 != 0){ 
			  document.getElementById('persCost_' + strSID).value = oldCost + " " + "AMD" + " (-" + pers1 + "%)";
			  }
			} 
	updateCost(sid);
	return true;
}

// for control, if preview window open for preview or not
var global_preview = false;
var move_window = false;
var add_pixels = true;
var obj_float_div = get_obj('img_preview');
var getCoordsFromObject = false;
document.onmousemove = document_onmousemove;
var pos_X = 0;
var pos_Y = 0;
   
function document_onmousemove(e) {

	if(!obj_float_div) return;
	if(add_pixels == true) {add_x = 10; add_y = 10;} else {add_x = -10; add_y = -10;}
	if(move_window == true) {add_x = -10; add_y = -10;}
	
   pos_X = 0;
   pos_Y = 0;
   
   if(getCoordsFromObject != false) {
	   	e = getCoordsFromObject;
   } else if ( !e ) {
	   e = window.event;
   }

   if ( e ) {
      if ( typeof(e.pageX) == 'number' ) {
         pos_X = e.pageX; pos_Y = e.pageY;
      } else if ( typeof(e.clientX) == 'number' ) {
         pos_X = e.clientX; pos_Y = e.clientY;
         if ( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
            pos_X += document.body.scrollLeft; pos_Y += document.body.scrollTop;
         } else if ( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
            pos_X += document.documentElement.scrollLeft; pos_Y += document.documentElement.scrollTop;
         }
      }
   }
 
   var scroll_X = 0, scroll_Y = 0;
   if ( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.debug || navigator.vendor == 'KDE' ) ) {
      scroll_X = document.body.scrollLeft; scroll_Y = document.body.scrollTop;
   } else if ( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.debug || navigator.vendor == 'KDE' ) ) {
      scroll_X = document.documentElement.scrollLeft; scroll_Y = document.documentElement.scrollTop;
   }
 
   var win_size_X = 0, win_size_Y = 0;
   if (window.innerWidth && window.innerHeight) {
      win_size_X = window.innerWidth; win_size_Y = window.innerHeight;
   } else if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientHeight) {
      win_size_X = document.documentElement.clientWidth; win_size_Y = document.documentElement.clientHeight;
   } else if (document.body && document.body.clientWidth && document.body.clientHeight) {
      win_size_X = document.body.clientWidth; win_size_Y = document.body.clientHeight;
   }
 
   pos_X += add_x; pos_Y += add_y;
 
/*   if (obj_float_div.offsetWidth && obj_float_div.offsetHeight) {
      if (pos_X - scroll_X + obj_float_div.offsetWidth + 5 > win_size_X) pos_X -= (obj_float_div.offsetWidth + 25);
      if (pos_Y - scroll_Y + obj_float_div.offsetHeight + 5 > win_size_Y) pos_Y -= (obj_float_div.offsetHeight + 20);
   }
*/
// window.status = pos_X+":"+pos_Y;
   obj_float_div.style.left = pos_X + "px"; 
   obj_float_div.style.top = pos_Y + "px";
 
}




function showHelp(txt)
{
	var el = document.createElement("div");
	el.className = "help_preview";
	el.id = "help_preview";
	el.style.display = "block";
	el.innerHTML = txt;

	document.body.appendChild(el);
	
	obj_float_div = el;
	document_onmousemove();
}


function hideHelp()
{
	if(typeof obj_float_div != "object") {return;}
	document.body.removeChild(obj_float_div);
	obj_float_div = null;
}

function openTicketWindow(id){
	openBrWindow("order_ticket.php?id=" + id,"ticket","width=375,height=675,resizable=0,statusbar=0");
}