//==============================================================================

function test(item,x,y){
  if(item){
    var ih=item.height;
    var iw=item.width;
    if(iw>x)
      item.width=x;
    if(ih>y)
      item.height=y;  
  }
} 

//==============================================================================
  
function confirmDelete(action,information) {
  var check = window.confirm(information);
  if (check == true) {
    document.location.href = action; 
    return true;
  }
}

//==============================================================================

function changeFocus(){
  document.getElementById("passid").focus();
}

//==============================================================================

function loginTypeChange(){
	document.getElementById("passwordbox").innerHTML = "<input id=\"passid\" class=\"loginInputText\" type=\"password\" name=\"password\" />";
	setTimeout("changeFocus();",100)
}

//==============================================================================

function bannerHandler(source){
  var xmlHttp=getAjaxObject();
  xmlHttp.onreadystatechange=function(){
    //do nothing
  }  
  var address = "bannerHandler.php?source="+source;
  xmlHttp.open("GET",address,true);
  xmlHttp.send(null);
}

//==============================================================================

function selectMainMenu(index){
  document.getElementById('mainMenu1'+index).style.backgroundImage='url(graphics/mainMenu1_h.jpg)';
  document.getElementById('mainMenu2'+index).style.backgroundImage='url(graphics/mainMenu2_h.jpg)';
  document.getElementById('mainMenu3'+index).style.backgroundImage='url(graphics/mainMenu3_h.jpg)';
}

//==============================================================================

function unselectMainMenu(){
  for(i=1;i<8;i++){
    var e1=document.getElementById('mainMenu1'+i);
    if(e1){
      //alert(i)
      e1.style.backgroundImage='url(graphics/mainMenu1.jpg)';
      document.getElementById('mainMenu2'+i).style.backgroundImage='url(graphics/mainMenu2.jpg)';
      document.getElementById('mainMenu3'+i).style.backgroundImage='url(graphics/mainMenu3.jpg)';
    }
    else
      break;
  }
}

//==============================================================================

function submitGoogleSearch(){
  if (document.getElementById('q').value == "") {
    document.getElementById('q').focus();
    return false ;
  }
  return true ;
}

//==============================================================================

function getAjaxObject(){
  var xmlHttp;
  try{    // Firefox, Opera 8.0+, Safari    
    xmlHttp=new XMLHttpRequest();    
  }
  catch (e){    // Internet Explorer    
    try{      
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){      
      try{        
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        
      }
      catch (e){        
        alert("Your browser does not support AJAX!");        
        return false;        
      }     
    }
  }
  return xmlHttp;
}

//==============================================================================

function sendLinkToFriend(lang){

  if(lang=="pl"){
    var validationEmptyFields = "Uzupełnij wszystkie pola";
    var validationEmail = "Nieprawidłowy email";
    var langWait = "Czekaj";
    var langSentOK = "Email został wysłany";
    var langSentError = "Problem podczas wysyłania maila, spróbuj później";
  }
  else if(lang=="de"){
    var validationEmptyFields = "Füllen Sie alle Felder";
    var validationEmail = "Ungültige E-Mail";
    var langWait = "Wait";
    var langSentOK = "E-Mail wurde gesendet";
    var langSentError = "Problem beim Senden von E-Mail, bitte versuchen Sie es später";
  }

  var xmlHttp=getAjaxObject();  
  var name = document.form.name.value;
  var friend = document.form.friend.value;
  var email = document.form.email.value;
  var idprogramgame = document.form.idprogramgame.value;
  
  if(name=="" || friend=="" || email==""){
    alert( validationEmptyFields );
    return false;
  }
  
  if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form.email.value))){
    alert( validationEmail );
    form.email.focus();
    return false;
  }
  
  xmlHttp.onreadystatechange=function(){
      document.getElementById('sendLinkInfo').innerHTML='<div class="formLabelError">'+langWait+'...</div>';
      document.getElementById('sendLinkForm').style.display='none';
      //document.getElementById('sendLink').style.display='none';
      
      if(xmlHttp.readyState==4){
        var resp = xmlHttp.responseText;
        //alert(resp);
        if(resp==1)
          document.getElementById('sendLinkInfo').innerHTML='<div class="formLabelError">'+langSentOK+'</div>';
        else
          document.getElementById('sendLinkInfo').innerHTML='<div class="formLabelError">'+langSentError+'...</div>';
        document.getElementById('sendLinkForm').style.display='inline';
      }
  }
  
  var address = "index.php?action=sendLinkToFriend&name="+name+"&friend="+friend+"&email="+email+"&idprogramgame="+idprogramgame;
  xmlHttp.open("GET",address,true);
  xmlHttp.send(null);
}

//==============================================================================

function addToFavorites(iduser,idprogramgame){
  var xmlHttp=getAjaxObject();

  xmlHttp.onreadystatechange=function(){
      document.getElementById('favoritesText').style.display='inline';
      document.getElementById('favoritesText').innerHTML='<div class="formLabelError"><br>Czekaj...</div>';
      document.getElementById('favoritesLink').style.display='none';
            
      if(xmlHttp.readyState==4){
        var resp = xmlHttp.responseText;
        //alert(resp);
        if(resp==1){
          //document.getElementById('favoritesText').innerHTML='<div class="formLabelError"><br><a class="formLabelError" style="cursor:pointer;text-decoration:underline;" href="?action=favorites">{/literal}{#programsgames_addToFavoritesOk#}{literal}</a></div><br>';          
          document.getElementById('replaceImage').innerHTML='<a href="?action=favorites" title="Ulubione"><img src="images/ok.gif" border="0" alt="Ulubione"></a>';
         }        
        else
          document.getElementById('favoritesText').innerHTML='<div class="formLabelError"><br>Błąd zapisu, skontaktuj się z <a class=formLabelError href=mailto:kontakt@pobierzgryprogramy.pl>administratorem</a>.</div><br>';
      }
  }
  
  var address = "index.php?action=addFavorite&iduser="+iduser+"&idprogramgame="+idprogramgame;
  //alert(address);
  xmlHttp.open("GET",address,true);
  xmlHttp.send(null);
}

//==============================================================================

function showImagePopup(img){
  document.getElementById('imageImg').innerHTML='<img class="imgClass" src="'+img+'" onClick="Popup.hide(\'imageDiv\')">';
  Popup.showModal('imageDiv'); 
}

//==============================================================================

function selectBorder(elem){
  elem.className = "borderDivSelected";
}

//==============================================================================

function unselectBorder(elem){
  elem.className = "borderDiv";
}

//==============================================================================

function categoryRedirection(url){
  location.href=url;
}

//==============================================================================

function addTrailerClick(id){
  var xmlHttp=getAjaxObject();  
  var address = "index.php?action=addTrailerClick&id="+id;
  //alert(address);
  xmlHttp.open("GET",address,true);
  xmlHttp.send(null);
}

//==============================================================================

function allowScyscraper(val){
  var xmlHttp=getAjaxObject();
  var address = "index.php?action=allowScyscraper&val="+val;
  xmlHttp.open("GET",address,true);
  xmlHttp.send(null);
}

//==============================================================================

function showScyscraper(updateServer){
  var w=screen.width;
  if(w>1200) {
    document.getElementById("scyscraper").style.display='inline';
    if(updateServer!=1){
      allowScyscraper(1);
    }
  }
  
  if( window.innerHeight && window.scrollMaxY ) {
    // Firefox 
    //pageWidth = window.innerWidth + window.scrollMaxX;
    pageHeight = window.innerHeight + window.scrollMaxY;
  }
  else{ 
    if( document.body.scrollHeight > document.body.offsetHeight ){
      // all but Explorer Mac
      //pageWidth = document.body.scrollWidth;
      pageHeight = document.body.scrollHeight;
    }
    else{
      // works in Explorer 6 Strict, Mozilla (not FF) and Safari 
      //pageWidth = document.body.offsetWidth + document.body.offsetLeft; 
      pageHeight = document.body.offsetHeight + document.body.offsetTop; 
    }
  }
  
  if(pageHeight>1060) document.getElementById("scyscraper2nd").style.display='inline';
  if(pageHeight>1740) document.getElementById("scyscraper3rd").style.display='inline';
  //document.write('h='+pageHeight);
}
//==============================================================================

function clearStatus(){
  window.status='';
}

//==============================================================================

function redirection(action,id){
  location.href = "?action="+action+"&id="+id;
}

//==============================================================================

function errorMessageDiv(text){
  return '<div class="formLabelError">'+text+'</div>';
}

//==============================================================================

function p2pSystem(name){
  bannerHandler(10);
  window.open ("http://www.kookos.net/pr/1086/1/"+name,"p2p");
}

//==============================================================================

