function loadInto(src, destId, evt)
{

  link_href=(src.href || src.getAttribute('href'));

  advAJAX.get({
    url: link_href,
    parameters : {
      "AJAXOK" : "1"
    },
    onSuccess : function(obj) {
	document.getElementById(destId).innerHTML=obj.responseText;
	Hide('loading');
    },
    onError : function(obj) {
	document.getElementById(destId).innerHTML="Błąd "+obj.status;
    }
  });
  if (window.XMLHttpRequest || window.ActiveXObject) cancelEvent(evt);
};

function ajaxLoadInto(url, destId, dontShowLoadingImg)
{
	var target = document.getElementById(destId);
	if (!dontShowLoadingImg) target.innerHTML = '<img src="/images/loading.gif"/>';
	if (!dontShowLoadingImg) Show('loading2');
	if (obj) {
		obj.open("GET", url);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				if (!dontShowLoadingImg) Hide('loading2');
			}
		}
		obj.send(null);
	}
};

addEvent(document, 'click', function(evt)
{

 evt = evt || window.event;

 if (evt.which > 1 || evt.button > 1) return;
 var src = evt.target || evt.srcElement;
 if (src.nodeType && src.nodeType != 1) src = src.parentNode;
 while (src)
 {
  var srcName = (src.nodeName||src.tagName||'').toLowerCase();
  if (srcName == 'a' && src.className && src.className.match(/^LinkAJAX-(.+)$/))
  {
   	return loadInto(src, RegExp.$1, evt);
  }
  src = src.parentNode;
 }
}, 1);

//]]>

function setOpacity(elemId, opacity){
	var elem = document.getElementById(elemId);
	elem.style.opacity = opacity;
	elem.style.filter = 'alpha(opacity='+ opacity*100 +')';
}

function Show(obj) {
	document.getElementById(obj).style.display = '';
}
function Hide(obj) {
	document.getElementById(obj).style.display = 'none';
}
function go(site)
{
	window.location.href = site;
	return false;
}

function changeMain(url){
	var target = document.getElementById("mainDiv");
	target.innerHTML = "<p>Proszę czekać...</p>";
	Show('loading2');
	if (obj) {
		obj.open("GET", url);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
				Hide('loading2');
			}
		}
		obj.send(null);
	}
}

function sendData(objId, target){
	var vObj = document.getElementById(objId);
	//var vName = vObj.getAttribute("name");

	Show('loading2');
	if (obj) {
		obj.open("GET", target + "?" + vObj.name + "=" + vObj.value);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				//target.innerHTML = obj.responseText;
				Hide('loading2');
			}
		}
		obj.send(null);
	}
}

function ocDrawScores(eventId){

	var target = document.getElementById('ocScores'+eventId);
	Show('ocScores'+eventId);
	target.innerHTML = "Ładowanie wyników...";
	if (obj) {
		obj.open("GET", "/actions/ocDrawScores.php?eventId=" + eventId);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				target.innerHTML = obj.responseText;
			}
		}
		obj.send(null);
	}
}

function lowerBanner(){
	document.getElementById("middlead").style.marginTop = '19px';
}


function createNewAutocomplete(textBoxId, url){
	new autocomplete(textBoxId,url);
}


function autocomplete(textBoxId, url) {
    var ac = this;
    this.textbox     = document.getElementById(textBoxId);
    this.div         = document.getElementById('Target_'+textBoxId);
    var feedback	 = document.getElementById('Feedback_'+textBoxId);
    this.list        = this.div.getElementsByTagName('a');
    this.pointer     = null;
    var gpointer	 = null;

    ajaxObj = obj;

    if (url.indexOf('?') == -1) {
		url += '?';
	}

    document.onmouseup = function ( e ){
		var target = (e && e.target) || (event && event.srcElement);
		//alert(target.id);
		if (target.id != textBoxId) {
			Hide ('Target_'+textBoxId);
		}
	};

    this.selectDiv = function( inc ) {
    	this.pointer = gpointer;
    	Show('Target_'+textBoxId);
        if( this.pointer !== null && this.pointer+inc >= 0 && this.pointer+inc < this.list.length ) {
            this.list[this.pointer].className = 'ASLink';
            this.pointer += inc;
            this.list[this.pointer].className = 'ASLinkActive';
            this.textbox.value = this.list[this.pointer].innerHTML;
        }
        if( this.pointer === null ) {
            this.pointer = 0;
            this.list[this.pointer].className = 'ASLinkActive';
            this.textbox.value = this.list[this.pointer].innerHTML;
        }
        gpointer = this.pointer;
    }

    this.runFeedback = function () {
    	if (feedback) {
	    	var inputValue = document.getElementById(textBoxId).value;
	    	if (obj) {
				obj.abort();
				fburl = url + '&feedback=1&value='+encodeURIComponent(inputValue);
				Show('loading2');
				obj.open("GET", fburl);
				obj.onreadystatechange = function() {
					if (obj.readyState == 4 && obj.status == 200) {
						if (obj.responseText != '') {
							feedback.innerHTML = obj.responseText;
							Hide('loading2');
							if (obj.responseText.length > 30) {
								makeItBlue(textBoxId);
							} else {
								makeItNormal(textBoxId);
							}
						}
					}
				}
				obj.send(null);
			}
		}
    }

    this.textbox.onkeydown = function( e ) {

        e = e || window.event;

        var inputValue = document.getElementById(textBoxId).value;

        this.focus();

        switch( e.keyCode ) {

			case 9: //tab
                Hide('Target_'+textBoxId);
                break;
        	case 13: //enter
                Hide('Target_'+textBoxId);
                ac.runFeedback();
                return false;
                break;
            case 38: //up
                ac.selectDiv(-1);
                ac.runFeedback();
                return false;
                break;
            case 40: //down
                ac.selectDiv(1);
                inputValue = document.getElementById(textBoxId).value;
                ac.runFeedback();
                return false;
                break;
            default:
				ajaxObj.abort();
				gpointer = null;
				var target = document.getElementById("Target_"+textBoxId);
				var textbox = document.getElementById(textBoxId);
				if (inputValue.length >= 1) {
					if (ajaxObj) {

						ajaxObj.open("GET", url + '&value=' + encodeURIComponent(inputValue));
						ajaxObj.onreadystatechange = function() {
							if (ajaxObj.readyState == 4 && ajaxObj.status == 200) {
								if (ajaxObj.responseText != '') {
									Show('Target_'+textBoxId);
									target.innerHTML = ajaxObj.responseText;
									//-- feedback:
										ac.runFeedback();
									//--
								} else {
									Hide('Target_'+textBoxId);
								}
							}
						}
						ajaxObj.send(null);
					}
				} else {
					Hide('Target_'+textBoxId);
				}
			break;
        }
    }

}

function simpleAjaxGet(url) {

	var response = '';
	if (obj) {
		obj.abort();
		Show('loading2');
		obj.open("GET", url);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				if (obj.responseText != '') {
					Hide('loading2');
					response += obj.responseText;
					return 'abc';
				}
			}
		}
		obj.send(null);
	}

}




function setOpacityById(elid, opacity){
	document.getElementById(elid).style.opacity = opacity;
	opacity *= 100;
	document.getElementById(elid).style.filter = 'alpha(opacity='+opacity+')';
}

function setOpacity(el, opacity){
	el.style.opacity = opacity;
	opacity *= 100;
	el.style.filter = 'alpha(opacity='+opacity+')';
}

function runHardwareWizard(hw_type){
	var target = document.getElementById("hwWizard");
	setOpacity(target, 0.9);
	Show('hwWizardOuter');
		if (obj) {
			obj.open("GET", "/actions/hardwareWizard.php?hw_type=" + encodeURIComponent(hw_type));
			obj.onreadystatechange = function() {
				if (obj.readyState == 4 && obj.status == 200) {
					target.innerHTML = obj.responseText;
				}
			}
			obj.send(null);
		}
}

function runNotebookWizard(){
	var target = document.getElementById("notebookWizard");
	setOpacity(target, 0.95);
	//Show('notebookWizardOuter');

		if (obj) {
			obj.open("GET", "/actions/notebookWizard.php");
			obj.onreadystatechange = function() {
				if (obj.readyState == 4 && obj.status == 200) {
					target.innerHTML = obj.responseText;
					$('#notebookWizardOuter').fadeIn(1000);
				}
			}
			obj.send(null);
		}
}

var defbgcolor = '';
var defcolor = '';

function makeItGreen(id){
	if (!defbgcolor) {

		defbgcolor = document.getElementById(id).style.backgroundColor;
		defcolor = document.getElementById(id).style.color;
	}
	document.getElementById(id).style.backgroundColor = '#f7fbee';
	document.getElementById(id).style.color = '#678921';
}


function makeItBlue(id){
	if (!defbgcolor) {
		defbgcolor = document.getElementById(id).style.backgroundColor;
		defcolor = document.getElementById(id).style.color;
	}
	document.getElementById(id).style.backgroundColor = '#eef5fb';
	document.getElementById(id).style.color = '#235687';
}

function makeItNormal(id){
	document.getElementById(id).style.backgroundColor = 'white';
	document.getElementById(id).style.color = '#525252';
}

/**
 *
 * @access public
 * @return void
 **/
function setUploaderBehavior(title, value, settings_id, id){
	if (title != value) {
		Show('ssuploader_'+settings_id+'_'+id);
		if (document.getElementById('scoreimage_'+settings_id+'_'+id).value) {
			document.getElementById('save'+settings_id+'_'+id).src = '/images/save.gif';
		} else {
			document.getElementById('save'+settings_id+'_'+id).src = '/images/savedisabled.gif';
		}
	} else {
		Hide('ssuploader_'+settings_id+'_'+id);
		document.getElementById('save'+settings_id+'_'+id).src = '/images/savedisabled.gif';
	}
}


function platformValidate(profile){


	switch(profile){
		case 'platform':
			if (!document.newPlatform.cpu_id) {
				alert('Musisz wybrać procesor');
				return false;
			}
			if (!document.newPlatform.motherboard_id) {
				alert('Musisz wybrać płytę główną');
				return false;
			}
			if (!document.newPlatform.gpu_id) {
				alert('Musisz wybrać chipset karty graficznej');
				return false;
			}
			if (!document.newPlatform.ram_id) {
				alert('Musisz wybrać pamięć RAM');
				return false;
			}
			if (!document.newPlatform.hdd_id) {
				alert('Musisz wybrać dysk twardy');
				return false;
			}
			if (!document.newPlatform.os_id) {
				alert('Musisz wybrać system operacyjny');
				return false;
			}
			if (!document.newPlatform.cpu_cooler_id) {
				alert('Musisz wybrać chłodzenie procesora. Użyj Hardware Wizard, jeżeli Twój produkt nie znajduje się na liście. Dla chłodzeń stockowych wpisz "BOX" i wybierz odpowiedni cooler.');
				return false;
			}
			break;
		case 'gpu':
			if (!document.newPlatform.motherboard_id) {
				alert('Musisz wybrać płytę główną');
				return false;
			}
			if (!document.newPlatform.cpu_id) {
				alert('Musisz wybrać procesor');
				return false;
			}
			if (!document.newPlatform.gpu_id) {
				alert('Musisz wybrać chipset karty graficznej');
				return false;
			}
			if (!document.newPlatform.gc_id) {
				alert('Musisz wybrać kartę graficzną');
				return false;
			}
			if (!document.newPlatform.os_id) {
				alert('Musisz wybrać system operacyjny');
				return false;
			}
			break;
		case 'cpu':
			if (!document.newPlatform.motherboard_id) {
				alert('Musisz wybrać płytę główną');
				return false;
			}
			if (!document.newPlatform.cpu_id) {
				alert('Musisz wybrać procesor');
				return false;
			}
			if (!document.newPlatform.ram_id) {
				alert('Musisz wybrać pamięć RAM');
				return false;
			}
			if (!document.newPlatform.cpu_cooler_id && !document.newPlatform.cpu_cooler_custom) {
				alert('Musisz wybrać chłodzenie procesora. Użyj Hardware Wizard, jeżeli Twój produkt nie znajduje się na liście. Dla chłodzeń stockowych wpisz "BOX" i wybierz odpowiedni cooler.');
				return false;
			}
			break;
		case 'hdd':
			if (!document.newPlatform.motherboard_id) {
				alert('Musisz wybrać płytę główną');
				return false;
			}
			if (!document.newPlatform.hdd_id) {
				alert('Musisz wybrać dysk twardy');
				return false;
			}
			if (!document.newPlatform.os_id) {
				alert('Musisz wybrać system operacyjny');
				return false;
			}
			break;
		default:
			;
	} // switch

	document.newPlatform.submit();
}

function removeScore(s, b)
{
	Show('loading2');
	var ans = confirm('Czy na pewno chcesz usunąć ten wynik?');
	if (obj && ans) {
		document.getElementById('mainview').style.opacity = '0.5';
		obj.open("GET", '/actions/ranks_removeScore.php?settingsId='+s+'&benchmarkId='+b);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				Hide('loading2');
				alert('Wynik został usunięty');
				if (document.getElementById('bmk_'+s+'_'+b)) {
					document.getElementById('bmk_'+s+'_'+b).value='';
				}
				document.getElementById('mainview').style.opacity = '1';
			}
		}
		obj.send(null);
	}
}


function removeScoreById(id)
{
	Show('loading2');
	var ans = confirm('Czy na pewno chcesz usunąć ten wynik?');
	if (obj && ans) {
		document.getElementById('mainview').style.opacity = '0.5';
		obj.open("GET", '/actions/ranks_removeScore.php?scoreId='+id);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				Hide('loading2');
				alert('Wynik został usunięty');
				var thumb = document.getElementById('scoreThumb'+id)
				if (thumb) {
					thumb.style.display = 'none';
				}
				document.getElementById('mainview').style.opacity = '1';
			}
		}
		obj.send(null);
	}
}

function removePlatform(platformId){
	var ans = confirm('Czy na pewno chcesz usunąć wybraną platformę? Usunięte zostaną również WSZYSTKIE podpięte eventy i benchmarki!');
	if (ans) {
		location.href = '/rankingi/zarzadzaj/platforma/'+platformId+'/usun';
	}
}

function removeSettings(eventId){
	var ans = confirm('Czy na pewno chcesz usunąć event? Usunięte zostaną również WSZYSTKIE podpięte benchmarki!');
	if (ans)
		location.href = '/rankingi/zarzadzaj/ustawienia/usun/'+eventId;
}


function loadHWChanger(platformId, hw_type){
	Show('loading2');
	if (obj) {
		obj.open("GET", '/actions/ranks_hardwareChanger.php?platformId='+platformId+'&hw_type='+hw_type);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				Hide('loading2');
				document.getElementById('p'+platformId+hw_type).innerHTML=obj.responseText;
			}
		}
		obj.send(null);
	}
}

function changeHardware(platformId, hw_type, field){
	var hw_id = document.forms['p'+platformId+'change'].elements[field].value;
	Show('loading2');
	if (obj) {
		obj.open("GET", '/actions/ranks_changeHardware.php?platformId='+platformId+'&hw_type='+hw_type+'&hw_id='+hw_id);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				Hide('loading2');
				alert(obj.responseText);
			}
		}
		obj.send(null);
	}
}


function mail(to, defsubject){

	var subject = encodeURIComponent(prompt('Temat wiadomości', defsubject));
	if (subject != null && subject != 'undefined' && subject != 'null') {
		var message = encodeURIComponent(prompt('Treść wiadomości'));
		if (message != null && message != 'undefined' && message != 'null') {
			if (to && (subject != 'undefined') && (message != 'undefined')) {
				var url = "/actions/sendMessage.php";
				var params = "message=" + message + "&subject=" + subject + "&userId=" + to;
				onComplete = function() {
					alert("Wiadomość została wysłana.");
				}
				onEnd = function() {}
				startPOSTRequest(url, params, onComplete, onEnd);
			}
		}
	}

}

function moveScore(scoreId, targetSettingsId){

	document.getElementById('mainview').style.opacity = '0.5';
	if (obj) {
		obj.open("GET", '/actions/ranks_moveScore.php?scoreId='+scoreId+'&targetSettingsId='+targetSettingsId);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				document.getElementById('mainview').style.opacity = '1';
				alert(obj.responseText);
			}
		}
		obj.send(null);
	}
}

function calculateFrequency(){
	document.getElementById('cpuFrequency').innerHTML =
	Math.round(document.newEvent.cpu_multiplier.value * document.newEvent.cpu_bus_speed.value);
}


function sendReport(scoreId){

	var info = prompt("Szczegóły raportu");
	if (obj && scoreId) {
		obj.abort();
		obj.open("GET", '/actions/ranks_sendReport.php?scoreId='+scoreId+'&details='+encodeURIComponent(info));
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				alert('Raport został wysłany');
			}
		}
		obj.send(null);
	}

}

function getCeneoTip(pId){
	if (obj) {
		obj.abort();
		obj.open("GET", '/actions/getCeneoTip.php?pId='+pId);
		obj.onreadystatechange = function() {
			if (obj.readyState == 4 && obj.status == 200) {
				Tip(obj.responseText);
				setTimeout('UnTip()', 2000);
			}
		}
		obj.send(null);
	}
}


function addNotebook(){

	if (!document.notebook.cpu_id) {
		alert('Musisz wybrać procesor. Wybór potwierdź enterem.');
		return false;
	}

	if (!document.notebook.gpu_id) {
		alert('Musisz wybrać układ graficzny. Wybór potwierdź enterem.');
		return false;
	}


	notebook.submit();
}
