 // Alexander Sidko 2009
 // INDEPENDENT functions for Internetmap.Info 2.0

function goURL(url){
	window.location=url;
}

function showBanner() {
 		$("#pay_window").remove();

	var body = "<table id='paywindow' height='100%' width='100%' style='font-size:14pt'>"+
	"<tr><td valign='top'><span class='blue_title' style='text-align:center;float:left;'><b>Внимание — акция!</b></td></tr>"+
	"<tr><td align='center'>Обьявляется акция для участников Интернет Карты 1.0<br/>Проследуйте по ссылке ниже для ознакомления.<br/><a href='/imap/?a=akcia' style='font-size: 16pt'>Узнать подробнее</a></td></tr>"+
	"<tr><td align='center' valign='bottom'><span onclick='$(\"#pay_window\").fadeOut();' style='text-align:center;color:white;cursor:pointer;font-size:12pt;height:16pt;width:100%;background-color:red;display:block'><b>закрыть</b></span><td></tr></table>"; 
	$("body").append($("<div id='pay_window' style='position:fixed;width:100%;height:100%;z-index:10000;display:none'><div  class='centred' style='display:block;background-color:white;z-index:20000;border:#0072bc 2px solid;'>"+body+"</div></div>"));
	//$("body").append($("<div id='pay_window' class='centred' style='display:block;background-color:white;z-index:20000;border:#0072bc 2px solid;'>"+body+"</div>"));
	$("#pay_window").fadeIn();
 }
 
function PriceStick(price){
 	document.write("<span style=\"color:white;font-weight:bold;font-size:11pt;position:relative;line-height:20pt;background: url('price_stick.png') no-repeat; width:32px;height:24px;display: inline-block;margin-right:5px\">"+price+"$</span>");
 	}
	
function editableField(name,value){
	var rand = Math.rand*1000;

	document.write("<input id=\""+name+rand+"\" class=\"input_ed\" maxlength=\"24\" size=\"25\" disabled=\"disabled\"  type=\"text\" value=\""+value+"\">&nbsp;"+
	"<img id=\"edit"+name+rand+"\" src=\"icon_edit.gif\" onclick=\"\$(this).hide();\$('#save"+name+rand+"').show();fieldEdit("+name+rand+")\" style=\"cursor:pointer\"/>"+
	"<img id=\"save"+name+rand+"\" src=\"icon_disk.gif\" onclick=\"fieldSave("+name+","+rand+");\" style=\"cursor:pointer;display:none\"/><br>");
}

function fieldEdit(name,rand){
	$("#"+name+"_"+rand).removeAttr("disabled");
}
function fieldSave(name,rand,seed){
	
		value = $("#"+name+"_"+rand+seed).val();
		$("#"+name+"_"+rand+seed).attr("disabled","disabled");
    	$("#"+name+"_save_"+rand+seed).hide();
    	$("#"+name+"_edit_"+rand+seed).show();
		$("#"+name+"_"+rand+seed).val('Сохраняю...')
		 $.get("http://internetmap.info/cgi-bin/i-map.pl", {
              		     a: "save_field",
               			 field: name,
               			 rand: rand,
						 value: value
            }, function(){
            	
            	$("#"+name+"_"+rand+seed).val(value)
            });
}

function WarFieldSave(name,rand,seed){
	
	value = $("#"+name+"_"+rand+seed).val();
	$("#"+name+"_"+rand+seed).attr("disabled","disabled");
	$("#"+name+"_save_"+rand+seed).hide();
	$("#"+name+"_edit_"+rand+seed).show();
	$("#"+name+"_"+rand+seed).val('Сохраняю...')
	 $.get("http://internetmap.info/cgi-bin/wars.pl", {
          		     a: "quick_field_save",
           			 field: name,
           			 rand: rand,
					 value: value
        }, function(){
        	
        	$("#"+name+"_"+rand+seed).val(value)
        });
}
function refreshAvatar(src){
	$("#user_avatar").attr('src', src+'?'+Math.ceil(Math.random()*1000000));	
}
	function showTimer(second,counter_name){
if (second <= 0) {
	  document.getElementById(counter_name).value=00+":"+00+":"+00+"."+0; 
	  return true;
}	
	seconds = second;
	var hours = (seconds-seconds % 3600)/3600;	
 	var minutes =(seconds-hours*3600-seconds % 60)/60;
 	s_out = (seconds-hours*3600-minutes*60);
 	
	if (milisec<=0){ 
	    milisec=9 
	    seconds-=1 
 	} 
 	else if (seconds<=0){ 
	    milisec=9 
		seconds=59
	    minutes-=1 
 	} 
 
  	else if (minutes<0){ 
	    milisec=9
		seconds=59
		minutes=59  
	    hours-=1 
 	} 

 
 else 
    milisec-=1;
	h_out = hours;
	m_out = minutes;
	if (hours < 10) {h_out = "0"+hours.toString();}
	if (minutes < 10) {m_out= "0"+minutes.toString();}
	if (s_out < 10) {s_out= "0"+s_out.toString();}
    document.getElementById(counter_name).value=h_out+":"+m_out+":"+s_out+"."+milisec; 
    setTimeout(function(){showTimer(seconds,counter_name)},100); 
	}
	
function showTimerJQ(obj){
	var data = obj.data("timer");
if (data.seconds <= 0) {
	  obj.val("00:00:00"); 
	  return true;
}	
	seconds = data.seconds;
	var hours = (seconds-seconds % 3600)/3600;	
 	var minutes =(seconds-hours*3600-seconds % 60)/60;
 	s_out = (seconds-hours*3600-minutes*60);
 	
	//if (milisec<=0){ 
	//    milisec=9 
	//    seconds-=1 
 	//} 
 	//else 
	if (seconds<=0){ 
	    milisec=9 
		seconds=59
	    minutes-=1 
 	} 
 
  	else if (minutes<0){ 
	    milisec=9
		seconds=59
		minutes=59  
	    hours-=1 
 	} 

 
 else 
    data.seconds-=1;
	h_out = hours;
	m_out = minutes;
	if (hours < 10) {h_out = "0"+hours.toString();}
	if (minutes < 10) {m_out= "0"+minutes.toString();}
	if (s_out < 10) {s_out= "0"+s_out.toString();}
    obj.val(h_out+":"+m_out+":"+s_out); 
    setTimeout(function(){showTimerJQ(obj)},1000); 
	}	
	
	function callPay(purse,amount,type,service_name){
		
		$("#call_pay_purse").html(purse);
		$("#call_pay_amount").html(amount);
		$("#call_pay_type").html(type);
		$("#call_pay_service").html(service_name);
		
				
		if (typeof(user_id) != 'undefined') {
			if (user_id<=0) {
					
					$("#pay_buttons").hide();
					$("#register_form").show();
					$("#pay_window").fadeIn();
								
			} else {
				type = type.replace(/_LOGIN_/,user_id);
				$("#flypay").load("/cgi-bin/i-map.pl?a=flypay&type="+type+"&amount="+amount+"&method=LINK",function(){
					
					$("#call_pay_type").html(type);
					$("#call_pay_wmc").attr('href',"wmk:paylink?url=http%3A//internetmap.info/cgi-bin/i-map.pl&purse="+purse+"&amount="+amount+"&method=POST&desc="+type);
					$("#call_pay_wml").attr('href',"https://light.webmoney.ru/pci.aspx?url=http%3A//internetmap.info/cgi-bin/i-map.pl&purse="+purse+"&amount="+amount+"&method=POST&desc="+type);
					$("#pay_buttons").show();
					$("#register_form").hide();
					$("#pay_window").fadeIn();
				});
				
			}
		}
		
	
	}
	
	function userMail(receiver, rand_num){
		$("#pay_window").remove();
		var body = "<table id='paywindow' width='100%' height='100%' style='font-size:14pt'><tr><td valign='top'><span class='blue_title' style='text-align:center;float:left;'><b>Написать продавцу</b></span><b></td></tr><tr><td>" +
	"<form method=POST action='/cgi-bin/engine.cgi'><input type=hidden name=a value='user_mail'><table width='100%'>"+
"<tr><td>Message</td><td colspan='3'><textarea name='text' cols=40 rows=5></textarea></td></tr>" +
"<tr><td>Enter code</td><td colspan='3'><img valign='middle' src='/cgi-bin/check_num.pl?num="+rand_num+"'>" +
"<input type=text name=code2><input type=hidden name=receiver_id value="+receiver+"> <input type=hidden name=code value='"+rand_num+"'></td></tr>" +
"<tr><td colspan='2' align='center'><input type=submit value='Отправить сообщение'></td></tr>" +
"</table></form></td></tr><tr><td align='center'><span onclick='$(\"#pay_window\").fadeOut();' style='text-align:center;color:white;cursor:pointer;font-size:12pt;height:16pt;width:100%;background-color:red;display:block'><b>закрыть</b></span><td></tr></table>"; 
		$("body").append($("<div id='pay_window' style='position:fixed;width:100%;height:100%;z-index:10000;display:none'><div  class='centred' style='display:block;background-color:white;z-index:20000;border:#0072bc 2px solid;'>"+body+"</div></div>"));
		
		$("#pay_window").fadeIn();
		}