/**
 * JavaScript functions for domaqua.ru electronic shop.
 * this functions utilize Subsys_JsHttpRequest_Js
 * @package domaqua.ru
 */

  function dk_JsHttpRequest_cat(force) {
//  	alert(direction);
    // Получаем текст запроса из <select>-поля .
    
//    alert(direction);
    
    var selcat = document.getElementById('cat_select');
    var catCodeVar = selcat.options[selcat.selectedIndex].value;
    
//    alert(catCodeVar);
    

    // Создаем новый объект JSHttpRequest.
   // var req = new Subsys_JsHttpRequest_Js();
    
    
//	for(var i in req) alert('Имя свойства: ' + i + ';'+ '\n Значение свойства:\n ' + req[i] + '\n');
//alert('req.onreadystatechange = '+req.onreadystatechange);
//    	alert('req.readyState = '+req.readyState);	
	
    $.get(siteURL+'/cat_JsHttpRequest.php',{ catCode: catCodeVar },function(data){
       // alert(data.subcat);
        var selsubcat = document.getElementById('subcat_select');
    	var arrsubcat = data.split('|');

    	//var query = data.query;
		var count = 0;
  		for(var i in arrsubcat){
     		count += 1;
  		}
       
        if (count > 1) {
	    	selsubcat.options.length = 0;	// Чистим select box Модель
			selsubcat.options[selsubcat.length] = new Option('Выберите подраздел', 0);
            
			for (key in arrsubcat) {
                arrsubcatT=arrsubcat[key].split('^');
				selsubcat.options[selsubcat.length] = new Option(arrsubcatT[1], arrsubcatT[0]);
			}
			selsubcat.disabled = false;
    	} else {
 	    	selsubcat.options.length = 0;	// Чистим select box Модель
			selsubcat.options[selsubcat.length] = new Option('Выберите подраздел', 0);
			selsubcat.disabled = true;
    	}
    });

  }
  
function dk_JsHttpRequest_ref_deal_type_id(){
    
	var sel_deal_type = document.getElementById('ref_deal_type_id');
    var deal_type_Text = sel_deal_type.options[sel_deal_type.selectedIndex].text;
	var selCondition = document.getElementById('condition');
	var selWindows = document.getElementById('windows');
	
	if (typeof max_Condition == 'undefined') { 
		max_Condition = selCondition.options.length;
		max_Windows = selWindows.options.length;
	}
//	alert(max_Condition);
    
	if (
				deal_type_Text == 'Продается'
			|| 	deal_type_Text == 'Продам'
			|| 	deal_type_Text == 'Сдам'
			|| 	deal_type_Text == 'Сдается'
			 
	) 
	{
		if (selCondition.options.length == max_Condition)
			selCondition.options.length = max_Condition - 1;
		if (selWindows.options.length == max_Windows)
			selWindows.options.length = max_Windows - 1;
	}

}

/*function dk_JsHttpRequest_city_id(){
	var selcity = document.getElementById('city_id');
    var cityCodeVar = selcity.options[selcity.selectedIndex].value;
    
    var req = new Subsys_JsHttpRequest_Js();
    
    req.onreadystatechange = function() {
    	if (req.readyState == 4) {
    		var sel = document.getElementById('street_id');
    		var query = req.responseJS.query; 
    		var cityCode = req.responseJS.cityCode; 
    		var arr_str = req.responseJS.arr_str; 
    		var count = 0;
  			for(var i in arr_str){
     			count += 1;
  			}
  			if (count > 0) {
  				sel.options.length = 0;
  				sel.options[sel.length] = new Option('', 0);
  				for (key in arr_str) {
					sel.options[sel.length] = new Option(arr_str[key], key);
				}
				sel.disabled = false;
  			}else{
  				sel.options.length = 0;
  				sel.disabled = true;
  			}
  		
			//document.getElementById('result').innerHTML = query;
		}
    }
    
    req.caching = false;
    file = siteURL+'/city_JsHttpRequest.php';
    req.open('GET', file, true);
    req.send({ cityCode: cityCodeVar });
}
  
function dk_JsHttpRequest_street_id(){
	var selstreet = document.getElementById('street_id');
	var selcity = document.getElementById('city_id');
	var streetCodeVar = selstreet.options[selstreet.selectedIndex].value;
	var cityCodeVar = selcity.options[selcity.selectedIndex].value;
	
	var req = new Subsys_JsHttpRequest_Js();
	
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			var sel 		= document.getElementById('house_id');
			var query 		= req.responseJS.query; 
    		var cityCode 	= req.responseJS.cityCode; 
    		var streetCode 	= req.responseJS.streetCode; 
    		var arr_str 	= req.responseJS.arr_str; 
    		var count 		= 0;
    		
  			for(var i in arr_str){
     			count += 1;
  			}
			if (sel != null)
			{
				if (count > 0) {
					sel.options.length = 0;
					sel.options[sel.length] = new Option('', 0);
					for (key in arr_str) {
						sel.options[sel.length] = new Option(arr_str[key], key);
					}
					sel.disabled = false;
				}else{
					sel.options.length = 0;
					sel.disabled = true;
				}
			}
			//document.getElementById('result').innerHTML = 'city='+cityCode+' street='+streetCode;
			//document.getElementById('result').innerHTML = query;
		}
	}
	
	req.caching = false;
    file = siteURL+'/street_JsHttpRequest.php';
    req.open('GET', file, true);
    req.send({ streetCode: streetCodeVar,  cityCode: cityCodeVar});
}

function dk_JsHttpRequest_house_id(){
	var selhouse = document.getElementById('house_id');
	var houseCodeVar = selhouse.options[selhouse.selectedIndex].value;
	
	var house_number = document.getElementById('field_house_number_add');
	house_number.value = selhouse.options[selhouse.selectedIndex].text;
	
	var req = new Subsys_JsHttpRequest_Js();
	
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			var query 			= req.responseJS.query; 
			var houseCode 		= req.responseJS.houseCode; 
			var arr_house 		= req.responseJS.arr_house; 
			var change_fields 	= req.responseJS.change_fields; 
			var change_fields_id 	= req.responseJS.change_fields_id; 
			
			var house_floor = document.getElementById('house_floor');
			var house_type = document.getElementById('house_type_id');
			
			//var house_number = document.getElementById('field_house_name_add');
			//house_number.value = selhouse.options[houseCode].value;
			//house_number.value = 'hjhjhjhjhj';
			
			str= '';
			for(var i in change_fields){
				//str = str + i;
				var types = arr_house[i];
				//str = str + 'end';
				if(change_fields[i]=='select'){
					//str = str + '--' + i;
					//document.getElementById('result').innerHTML == 'jdgfkhgkfdrghkjdf';
					var elem = document.getElementById(change_fields_id[i]).options;
					//str = str + '(' + elem.length + ')';
					for(j=0; j<elem.length; j++){
						//str = str + '[' + elem[j].text + ']';
						//str = str + elem[j].text + '=' + types + '__';
						if(types.toUpperCase()==elem[j].text.toUpperCase()){
							//str = elem[j].text + '-----' + types;
							elem[j].selected = true;
						}
					}
				}else if(change_fields[i]=='userhandler'){
					eval("var __userhandler = null; if (typeof(" + change_fields_id[i] + ") != typeof(undefined)) __userhandler = " + change_fields_id[i] + ";");
					if (__userhandler != null)
						__userhandler(arr_house);
				}else{
					var elem = document.getElementById(change_fields_id[i]);
					elem.value = arr_house[i];
					//str = str + change_fields_id[i];
				}
				//str = str + 'end';
			}
			
			
			//house_number.value = selhouse.options[houseCode].value;
			//house_type.options[3].selected = true;
			//house_type.selectedIndex
			//document.getElementById('result').innerHTML = 'dsdsdsds';
			//document.getElementById('result').innerHTML = house_type.options[3].text;
		}
	}
	
	req.caching = false;
    file = siteURL+'/house_JsHttpRequest.php';
    req.open('GET', file, true);
    req.send({ houseCode: houseCodeVar });
}*/
function dk_JsHttpRequest_voting(){
	var formElem = document.getElementById('voting');
	var arrInput = formElem.getElementsByTagName('input');
	//alert(arrInput.length);
	var radio = 0;
	for(var i=0; i<arrInput.length; i++){
		if(arrInput[i].checked){
			radio = arrInput[i].value;
		}
		if(arrInput[i].name == 'voting_id')
			var voting_id = arrInput[i].value;
		if(arrInput[i].name == 'archives')
			var archives = arrInput[i].value;
	}
	if(radio == 0){
		alert('Вы не выбрали ни одного ответа');
		return;
	}
    
	$.get(siteURL+'/voting_JsHttpRequest.php',{ setradio: radio, voting_id: voting_id, archives: archives },function(data){
            var getradio = data;
			var elem = document.getElementById('voting');

			elem.innerHTML = getradio;

             $.cookie('voting_'+voting_id,voting_id, {expires: 1000, path:'/'});


     });
}
function dk_JsHttpRequest_notepad(id, table){


	$.getJSON(siteURL+'/notepad_JsHttpRequest.php',{ id: id, table: table },function(data){
	        var count_arr = data.count_arr;

			var elem1 = $('#main-notepad');
			if (count_arr > 0) {
				elem1.html('<A href="'+notepad_href+'">Блокнот</A>&nbsp;&nbsp;'+ count_arr);
			} else {
				elem1.html('<span>Блокнот пуст</span>');
			}
    });
}
function dk_JsHttpRequest_login(){
	var login = $('#loginp');
	$.get(siteURL+'/login_JsHttpRequest.php',{ login: login.attr('value') },function(data){
            var error =data;

			var err = $('#authoriz_error');

			if(error==0){
				err.show();
				err.html('Вы ввели неверный Логин!');
			} else {
				err.hide();
            }
    });
}
function dk_JsHttpRequest_pswd(){
	var login = $('#loginp');
	var pswd = $('#pswd');
	
	$.get(siteURL+'/pswd_JsHttpRequest.php',{ login: login.attr('value'), pswd: pswd.attr('value') },function(data){

          var error = data.error;
			var text = data.text;

			var err = $('#authoriz_error');

			if(error==0){
				err.show();
				err.html(text);
			}else{
				$('#login_form_partner').submit();
			}
    });
}
