/**************************************
*   General code                      *
*   Powered by Sancho				  *
*   Sancho: info@direct-auction.ru    *
*   Web: direct-auction.ru            *
**************************************/
function id(obj){
	return document.getElementById(obj);
}
function updateCompany(cid,bid,pid) {
    var req		 = new JsHttpRequest();
	var price	 = id('price_'+cid+'_'+bid+'_'+pid).value;
	if(bid==0){
		var day  = id('day_'+cid+'_'+bid+'_'+pid).value;
	}else{
		var day  = 0;
	}
	var mode	 = id('mode_'+cid+'_'+bid+'_'+pid).value;
	var dop_mode = id('dop_mode_'+cid+'_'+bid+'_'+pid).value;
	var stop	 = id('stop_'+cid+'_'+bid+'_'+pid).value;
	var rsya	 = id('rsya_'+cid+'_'+bid+'_'+pid).value;
	
	var day_rate=1;
	var time_start = [];
	var time_end   = [];
	var rate	   = [];
	if(day_rate==1&&bid==0&&pid==0){
		
		for(d=1;d<=7;d++){
			var time_start_d = [];
			var time_end_d   = [];
			var rate_d	     = [];
			for(i=0;i<10;i++){
				time_start_d[i]		 = cronTable[cid+'_'+bid+'_'+pid][d][i]["ts_hour"]+':'+cronTable[cid+'_'+bid+'_'+pid][d][i]["ts_min"];
				time_end_d[i]		 = cronTable[cid+'_'+bid+'_'+pid][d][i]["te_hour"]+':'+cronTable[cid+'_'+bid+'_'+pid][d][i]["te_min"];
				rate_d[i]			 = cronTable[cid+'_'+bid+'_'+pid][d][i]["rate"];
				
			}
			time_start[d]	= time_start_d;
			time_end[d]		= time_end_d;
			rate[d]			= rate_d;
		}
	}
	if(id('stop_'+cid+'_'+bid+'_'+pid).checked==true) stop = 1; else stop = 2;
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			id('ajax_'+cid+'_'+bid+'_'+pid).style.display = 'none';
			//id('ajax_'+cid+'_'+bid+'_'+pid).innerHTML = 'Сохранено';
        }else{
			id('ajax_'+cid+'_'+bid+'_'+pid).style.display = 'inherit';
			id('ajax_'+cid+'_'+bid+'_'+pid).innerHTML = '<div class="ajax_loader">Идет сохранение...</div>';	
		}
    }
    req.open(null, '/ajax/update_company/', true);
    req.send( { cid: cid, bid: bid, pid: pid, mode: mode, dop_mode: dop_mode, price: price, stop: stop, day: day, time_start: time_start, time_end: time_end, rate: rate, rsya: rsya } );
}
function reloadCompany() {
    var req		 = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			location.reload();
        }else{
			id('ajax_reload').innerHTML = '<div class="ajax_loader">Идет обновление...</div>';	
		}
    }
    req.open(null, '/ajax/reload_company/', true);
    req.send( {  } );
}
function updateUser(uid) {
    var req		 = new JsHttpRequest();
	var value	 = id('frm_login_'+uid);
	//var pass	 = id('pass_'+uid).value;
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			var login = req.responseJS.login;	
			id('user_'+uid).innerHTML = '';
			show('btn_edit_'+uid);
			hide('form_edit_'+uid);
			location.reload();
        }else{
			hide('form_edit_'+uid);
			id('user_'+uid).innerHTML = '<div class="ajax_loader">Идет сохранение...</div>';	
		}
    }
    req.open(null, '/ajax/update_user/', true);
    req.send( { uid: uid, q: value } );
}
function insertUser() {
    var req		 = new JsHttpRequest();
	var value	 = id('frm_login_0');
	
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			id('user_0').innerHTML = '';
			hide('form_edit_0');
			location.reload();
        }else{
			hide('form_edit_0');
			id('user_0').innerHTML = '<div class="ajax_loader">Идет сохранение...</div>';	
		}
    }
    req.open(null, '/ajax/insert_user/', true);
    req.send( { q: value } );
}
function deleteUser(uid) {
    var req		 = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			hide('inf_'+uid);
        }
    }
    req.open(null, '/ajax/delete_user/', true);
    req.send( { uid: uid } );
}
///
function updateMode(mid) {
    var req		 = new JsHttpRequest();
	var value	 = id('value_'+mid).value;
	var name	 = id('name_'+mid).value;
	var template = id('template_'+mid).value;
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			id('user_'+mid).innerHTML = '';
			show('btn_edit_'+mid);
			hide('form_edit_'+mid);
			show('name_txt_'+mid);
			id('name_txt_'+mid).innerHTML = name;
        }else{
			hide('form_edit_'+mid);
			id('user_'+mid).innerHTML = '<div class="ajax_loader">Идет сохранение...</div>';	
		}
    }
    req.open(null, '/ajax/update_mode/', true);
    req.send( { id: mid, name: name, value: value, template: template } );
}
function insertMode() {
    var req		 = new JsHttpRequest();
	var value	 = id('value_0').value;
	var name	 = id('name_0').value;
	var template = id('template_0').value;	
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			id('user_0').innerHTML = '';
			hide('form_edit_0');
			location.reload();
        }else{
			hide('form_edit_0');
			id('user_0').innerHTML = '<div class="ajax_loader">Идет сохранение...</div>';	
		}
    }
    req.open(null, '/ajax/insert_mode/', true);
    req.send( { name: name, value: value, template: template } );
}
function deleteMode(mid) {
    var req		 = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			hide('inf_'+mid);
        }
    }
    req.open(null, '/ajax/delete_mode/', true);
    req.send( { id: mid } );
}

function updateTemplate(tid) {
    var req		 = new JsHttpRequest();
	var name	 = id('name_'+tid).value;
	var formula  = id('formula_'+tid).value;
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			id('user_'+tid).innerHTML = '';
			show('btn_edit_'+tid);
			hide('form_edit_'+tid);
			show('name_txt_'+tid);
			id('name_txt_'+tid).innerHTML = name;
        }else{
			hide('form_edit_'+tid);
			id('user_'+tid).innerHTML = '<div class="ajax_loader">Идет сохранение...</div>';	
		}
    }
    req.open(null, '/ajax/update_template/', true);
    req.send( { id: tid, name: name, formula: formula } );
}
function insertTemplate() {
    var req		 = new JsHttpRequest();
	var formula	 = id('formula_0').value;
	var name	 = id('name_0').value;
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			id('user_0').innerHTML = '';
			hide('form_edit_0');
			location.reload();
        }else{
			hide('form_edit_0');
			id('user_0').innerHTML = '<div class="ajax_loader">Идет сохранение...</div>';	
		}
    }
    req.open(null, '/ajax/insert_template/', true);
    req.send( { name: name, formula: formula } );
}
function deleteTemplate(tid) {
    var req		 = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			hide('inf_'+tid);
        }
    }
    req.open(null, '/ajax/delete_template/', true);
    req.send( { id: tid } );
}
function edit_settings(value) {
    var req		 = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			id('ajax_reload').innerHTML = 'Сохрание прошло успешно';
        }else{
			id('ajax_reload').innerHTML = '<div class="ajax_loader">Идет сохранение...</div>';	
		}
    }
    req.open(null, '/ajax/edit_settings/', true);
    req.send( { q: value } );
}
function login(value) {
    var req = new JsHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			var out = req.responseJS.out;
			if(out==1){
				document.location.href = '/';
			}else{
				alert('Неверно указан пароль');
			}
        }
    }
    req.open(null, '/ajax/login/', true);
    req.send( { q: value } );
}

function show(obj){
	id(obj).style.display = '';	
}
function hide(obj){
	id(obj).style.display = 'none';	
}
function showHide(objMain,obj){
	if(id(obj).style.display == 'none'){
		id('link_'+obj).innerHTML = 'отключить';
		id(obj).style.display = '';	
		id('rate_'+obj).value = 1;
	}else{
		id('link_'+obj).innerHTML = 'настроить';
		id(obj).style.display = 'none';
		id('rate_'+obj).value = 0;
	}
}
function checkedList(obj){
	if(id('check_'+obj).checked==true){
		id('inf_'+obj).className = 'tbobySelect';
		//id('inf_'+obj).style.backgroundColor = '#ECF9FF';
	}else{
		id('inf_'+obj).className = '';
		//id('inf_'+obj).style.backgroundColor = '#fff';
	}	
}

function uploadSelected(){
	var obj = document.getElementsByTagName('input');
	var len = obj.length;
	var cbpid = [];
	var k = 0;
	for(i=0;i<len;i++){
		var relAttribute = String(obj[i].getAttribute('rel'));
		if(relAttribute=='check'&&obj[i].checked == true){
			cbpid[k] = obj[i].value;
			k++;
		}
	}
//////////////////////

    var req		 = new JsHttpRequest();
	var price	 = id('price_0_0_0').value;
	
	var day  = 0;

	var mode	 = id('mode_0_0_0').value;
	var dop_mode = id('dop_mode_0_0_0').value;
	var stop	 = id('stop_0_0_0').value;

	if(id('stop_0_0_0').checked==true) stop = 1; else stop = 0;
    req.onreadystatechange = function() {
        if (req.readyState == 4) {
			id('ajax_0_0_0').innerHTML = 'Сохранено';
        }else{
			id('ajax_0_0_0').innerHTML = '<div class="ajax_loader">Идет сохранение...</div>';	
		}
    }
    req.open(null, '/ajax/update_phrases/', true);
    req.send( { cbpid: cbpid, mode: mode, dop_mode: dop_mode, price: price, stop: stop } );
}
var cron_tmp	 = [];
var cron_buffer	 = [];
function changeBuf(key,value){
	cron_tmp[key] = value;
}
function saveBuffer(day,cont){
	for(k=0;k<10;k++){
		cron_buffer['tmp_ts_hour_'+k+''] = id('tmp_ts_hour_'+cont+'['+day+']['+k+']').value;
		cron_buffer['tmp_ts_min_'+k+'']	 = id('tmp_ts_min_'+cont+'['+day+']['+k+']').value;
		cron_buffer['tmp_te_hour_'+k+''] = id('tmp_te_hour_'+cont+'['+day+']['+k+']').value;
		cron_buffer['tmp_te_min_'+k+'']	 = id('tmp_te_min_'+cont+'['+day+']['+k+']').value;
		cron_buffer['tmp_rate_'+k+'']	 = id('tmp_rate_'+cont+'['+day+']['+k+']').value;
	}
}
function loadBuffer(day,cont){
	for(k=0;k<10;k++){
		id('tmp_ts_hour_'+cont+'['+day+']['+k+']').value = cron_buffer['tmp_ts_hour_'+k+''];
		id('tmp_ts_min_'+cont+'['+day+']['+k+']').value	 = cron_buffer['tmp_ts_min_'+k+''];
		id('tmp_te_hour_'+cont+'['+day+']['+k+']').value = cron_buffer['tmp_te_hour_'+k+''];
		id('tmp_te_min_'+cont+'['+day+']['+k+']').value	 = cron_buffer['tmp_te_min_'+k+''];
		id('tmp_rate_'+cont+'['+day+']['+k+']').value	 = cron_buffer['tmp_rate_'+k+''];
		changeBuf('tmp_ts_hour_'+cont+'_'+day+'_'+k+'',id('tmp_ts_hour_'+cont+'['+day+']['+k+']').value);
		changeBuf('tmp_ts_min_'+cont+'_'+day+'_'+k+'',id('tmp_ts_min_'+cont+'['+day+']['+k+']').value);
		changeBuf('tmp_te_hour_'+cont+'_'+day+'_'+k+'',id('tmp_te_hour_'+cont+'['+day+']['+k+']').value);
		changeBuf('tmp_te_min_'+cont+'_'+day+'_'+k+'',id('tmp_te_min_'+cont+'['+day+']['+k+']').value);
		changeBuf('tmp_rate_'+cont+'_'+day+'_'+k+'',id('tmp_rate_'+cont+'['+day+']['+k+']').value);
	}
}
function stroyForm(day,cont){

	var text = '<table border="0" style="width:440px;"><tr><td>';
	text += '<table border="0" style="width:340px;"><tr><td colspan="2" style="width:120px">Время от<td colspan="2" style="width:120px">до<td style="width:100px">% от режима';
	for(k=0;k<10;k++){
		//text += '<div style="margin-bottom:1px;">'
		text += '<tr><td align="right">'

		+'<select style="width:50px;" id="tmp_ts_hour_'+cont+'['+day+']['+k+']" onchange="changeBuf(\'tmp_ts_hour_'+cont+'_'+day+'_'+k+'\',this.value)">';
		for(j=0;j<=24;j++){
			text += '<option value="'+j+'"';
			if(cron_tmp['tmp_ts_hour_'+cont+'_'+day+'_'+k]==j) text +='selected="selected"';
			else if(cronTable[cont][day][k]["ts_hour"]==j) text +='selected="selected"';
			//else if(id('ts_hour_'+cont+'['+day+']['+k+']').value==j) text +='selected="selected"';
			text += '>'+j+'</option>';
		}
		text += '</select>'
		
		+'<td align="left">:&nbsp;<select style="width:50px;" id="tmp_ts_min_'+cont+'['+day+']['+k+']" onchange="changeBuf(\'tmp_ts_min_'+cont+'_'+day+'_'+k+'\',this.value)">';
		for(j=0;j<=5;j++){
			text += '<option value="'+(j*10)+'"';
			if(cron_tmp['tmp_ts_min_'+cont+'_'+day+'_'+k]==(j*10)) text +='selected="selected"';
			else if(cronTable[cont][day][k]["ts_min"]==(j*10)) text +='selected="selected"';
			//else if(id('ts_min_'+cont+'['+day+']['+k+']').value==(j*10)) text +='selected="selected"';
			text += '>'+(j*10)+'</option>';
		}
		text += '</select>'
		+'<td align="right">' 

		+'<select style="width:50px;" id="tmp_te_hour_'+cont+'['+day+']['+k+']" onchange="changeBuf(\'tmp_te_hour_'+cont+'_'+day+'_'+k+'\',this.value)">';
		for(j=0;j<=24;j++){
			text += '<option value="'+j+'"';
			if(cron_tmp['tmp_te_hour_'+cont+'_'+day+'_'+k]==j) text +='selected="selected"';
			else if(cronTable[cont][day][k]["te_hour"]==j) text +='selected="selected"';
			//else if(id('te_hour_'+cont+'['+day+']['+k+']').value==j) text +='selected="selected"';
			text += '>'+j+'</option>';
		}
		text += '</select>'

		+'<td align="left">:&nbsp;'
		+'<select style="width:50px;" id="tmp_te_min_'+cont+'['+day+']['+k+']" onchange="changeBuf(\'tmp_te_min_'+cont+'_'+day+'_'+k+'\',this.value)">';
		for(j=0;j<=5;j++){
			text += '<option value="'+(j*10)+'"';
			if(cron_tmp['tmp_te_min_'+cont+'_'+day+'_'+k]==(j*10)) text +='selected="selected"';
			else if(cronTable[cont][day][k]["te_min"]==(j*10)) text +='selected="selected"';
			//else if(id('te_min_'+cont+'['+day+']['+k+']').value==(j*10)) text +='selected="selected"';
			text += '>'+(j*10)+'</option>';
		}
		text += '</select>'
		+'<td>'
		+'<select style="width:80px;" id="tmp_rate_'+cont+'['+day+']['+k+']" onchange="changeBuf(\'tmp_rate_'+cont+'_'+day+'_'+k+'\',this.value)">';
		for(j=0;j<=20;j++){
			text += '<option value="'+(j*10)+'"';
			if(cron_tmp['tmp_rate_'+cont+'_'+day+'_'+k]==(j*10)) text +='selected="selected"';
			else if(cronTable[cont][day][k]["rate"]==(j*10)) text +='selected="selected"';
			//else if(id('rate_'+cont+'['+day+']['+k+']').value==(j*10)) text +='selected="selected"';
			text += '>'+(j*10)+'%</option>';
		}
		text += '</select>'
		+'';
		//+'</div>';
	}
	var days =  [,'понедельник','вторник','среда','четверг','пятница','суббота','воскресенье'];

	text += '</table>';
	text += '<td valign="top">';
	for(i=1;i<=7;i++){
		if(day==i)
		text +='<div class="dayOn"><a href="javascript:stroyForm('+i+',\''+cont+'\')">'+days[i]+'</a></div>';
		else
		text +='<div class="dayOff"><a href="javascript:stroyForm('+i+',\''+cont+'\')">'+days[i]+'</a></div>';
	}

	text +='<div class="dayOff" style="margin-top:40px"><a href="javascript:saveBuffer('+day+',\''+cont+'\')">копировать</a></div>'
	+'<div class="dayOff"><a href="javascript:loadBuffer('+day+',\''+cont+'\')">вставить</a></div>'
	+'</table>';
	id('day_box_'+cont).innerHTML = text;
}

function loadCrontable(obj,cont){
	initCrontable(cont);
	var text = '<div id="day_box_'+cont+'"></div>';
	text += '<div style="margin-top:20px; text-align:center"><a href="javascript:saveCrontable(\''+cont+'\')" class="btn">Сохранить</a> <a href="javascript:deleteCrontable(\''+cont+'\')" class="btn">Удалить</a></div>';	
	hs.htmlExpand(obj, { headingText: 'Дневное распределение бюджета', maincontentText: text, width:500, height:380 });
	stroyForm(1,cont);
}
function saveCrontable(cont){
	var text = '';

	for(d=1;d<=7;d++){
		for(k=0;k<10;k++){
			cronTable[cont][d][k]["ts_hour"] = (cron_tmp['tmp_ts_hour_'+cont+'_'+d+'_'+k+''])?cron_tmp['tmp_ts_hour_'+cont+'_'+d+'_'+k+'']:0;
			cronTable[cont][d][k]["ts_min"] = (cron_tmp['tmp_ts_min_'+cont+'_'+d+'_'+k+''])?cron_tmp['tmp_ts_min_'+cont+'_'+d+'_'+k+'']:0;
			cronTable[cont][d][k]["te_hour"] = (cron_tmp['tmp_te_hour_'+cont+'_'+d+'_'+k+''])?cron_tmp['tmp_te_hour_'+cont+'_'+d+'_'+k+'']:0;
			cronTable[cont][d][k]["te_min"] = (cron_tmp['tmp_te_min_'+cont+'_'+d+'_'+k+''])?cron_tmp['tmp_te_min_'+cont+'_'+d+'_'+k+'']:0;
			cronTable[cont][d][k]["rate"] = (cron_tmp['tmp_rate_'+cont+'_'+d+'_'+k+''])?cron_tmp['tmp_rate_'+cont+'_'+d+'_'+k+'']:0;
			if(cronTable[cont][d][k]["rate"]>0){
				text += '1';
			}	
		}
	}

	if(text.length>0){
		id('box_ctb_'+cont).innerHTML = '<a href="javascript://" >изменить дневной бюджет</a>';
	}else{
		id('box_ctb_'+cont).innerHTML = '<a href="javascript://" >настроить дневной бюджет</a>';
	}
	hs.close();hs.updateAnchors();
}
function initCrontable(cont){
	var text = '';
	for(d=1;d<=7;d++){
		for(k=0;k<10;k++){
			cron_tmp['tmp_ts_hour_'+cont+'_'+d+'_'+k+''] = cronTable[cont][d][k]["ts_hour"];
			cron_tmp['tmp_ts_min_'+cont+'_'+d+'_'+k+''] = cronTable[cont][d][k]["ts_min"];
			cron_tmp['tmp_te_hour_'+cont+'_'+d+'_'+k+''] = cronTable[cont][d][k]["te_hour"];
			cron_tmp['tmp_te_min_'+cont+'_'+d+'_'+k+''] = cronTable[cont][d][k]["te_min"];
			cron_tmp['tmp_rate_'+cont+'_'+d+'_'+k+''] = cronTable[cont][d][k]["rate"];
		}
	}
}
function deleteCrontable(cont){
	for(d=1;d<=7;d++){
		for(k=0;k<10;k++){
			cronTable[cont][d][k]["ts_hour"] = 0;
			cronTable[cont][d][k]["ts_min"] = 0;
			cronTable[cont][d][k]["te_hour"] = 0;
			cronTable[cont][d][k]["te_min"] = 0;
			cronTable[cont][d][k]["rate"] = 0;
		}
	}
	id('box_ctb_'+cont).innerHTML = '<a href="javascript://" >настроить дневной бюджет</a>';
	hs.close();hs.updateAnchors();
}	
function Dump(d,l) {
    if (l == null) l = 1;
    var s = '';
    if (typeof(d) == "object") {
  s += typeof(d) + " {\n";
  for (var k in d) {
for (var i=0; i<l; i++) s += "  ";
s += k+": " + Dump(d[k],l+1);
  }
  for (var i=0; i<l-1; i++) s += "  ";
  s += "}\n"
    } else {
  s += "" + d + "\n";
    }
    return s;
}
