function createRequestObject() {
  var ro;
  var browser = navigator.appName;
  if (browser == "Microsoft Internet Explorer"){
      ro = new ActiveXObject("Microsoft.XMLHTTP");
  } else{
      ro = new XMLHttpRequest();
  }
  return ro;
}

var http = createRequestObject();

function search(type) {
   var keywords = document.getElementById('keywords').value;
   if (keywords.length == 0) {
      document.getElementById('results').innerHTML = "";
      return;
   }
   else if (keywords.length < 3) {
      document.getElementById('results').innerHTML = "<p>Your search query must contain 3 or more characters.</p>";
      return;
   }
   http.open('get', '/inc_NEW/search.php?k='+keywords+'&type='+type);
   http.onreadystatechange = handleSearchResponse;
   http.send(null);
}
function handleSearchResponse() {
   if (http.readyState == 4) { 
      var response = http.responseText;
      //document.getElementById('sky').innerHTML = "";
      document.getElementById('results').innerHTML = response;
    }
}

function portfolio(artist_id, type, sort, current_page, ignore_id) {
   if (type == 'wallpaper') { kind = 'wallpaper'; }
   else { kind = type; }
   http.open('get', '/'+kind+'/_inc/portfolio_browser.php?artist_id='+artist_id+'&type='+type+'&sort='+sort+'&current_page='+current_page+'&id='+ignore_id);
   http.onreadystatechange = handlePortfolioResponse;
   http.send(null);
}
function handlePortfolioResponse() {
   if (http.readyState == 4) { 
      var response = http.responseText;
      document.getElementById('portfolio').innerHTML = response;
    }
}

function submissions(artist_id, status, sort, current_page, ignore_id) {
   http.open('get', '/wallpaper/_inc/submission_browser.php?artist_id='+artist_id+'&status='+status+'&sort='+sort+'&current_page='+current_page+'&id='+ignore_id);
   http.onreadystatechange = handleSubmissionsResponse;
   http.send(null);
}
function handleSubmissionsResponse() {
   if (http.readyState == 4) { 
      var response = http.responseText;
      document.getElementById('submissions').innerHTML = response;
    }
}

function submissionVote(id,type,tstamp) {
  document.getElementById('button_'+id).setAttribute("src", '/img_NEW/button_cast_your_vote_disabled.png');
  document.getElementById('button_link_'+id).setAttribute("href", '#');
  document.getElementById('vote_'+id).innerHTML = 'Updating...';
  http.open('get', '/'+type+'/_inc/submission_vote.php?id='+id+'&tstamp='+tstamp);
  http.onreadystatechange = handleResponse_submissionVote;
  http.send(null);
}
function handleResponse_submissionVote() {
  if (http.readyState == 4) { 
    var response = http.responseText;
    var update = new Array();

    if (response.indexOf('|' != -1)) {
       update = response.split('|');
       document.getElementById('vote_'+update[0]).innerHTML = update[1];      
       if (update[2] == '/img_NEW/button_voted.png') {
         document.getElementById('button_link_'+update[0]).setAttribute("href", '#');
       }
    }
  }
}

function submissionVote2(id,type,tstamp) {
  //document.getElementById('button_'+id).setAttribute("src", '/img_NEW/button_cast_your_vote_disabled.png');
  //document.getElementById('button_link_'+id).setAttribute("href", '#');
  document.getElementById('detail').innerHTML = 'Updating...';
  http.open('get', '/'+type+'/_inc/submission_vote.php?id='+id+'&tstamp='+tstamp);
  http.onreadystatechange = handleResponse_submissionVote2;
  http.send(null);
}
function handleResponse_submissionVote2() {
  if (http.readyState == 4) { 
    var response = http.responseText;
    var update = new Array();

    if (response.indexOf('|' != -1)) {
       update = response.split('|');
       document.getElementById('detail').innerHTML = update[1];      
       //if (update[2] == '/img_NEW/button_voted.png') {
       //  document.getElementById('button_link_'+update[0]).setAttribute("href", '#');
       //}
    }
  }
}

function sndReq(id,type) {
  document.getElementById('fav_'+id).innerHTML = '<span style="color: gray;">One moment please...</span>';
  http.open('get', '/inc_NEW/atf.php?id='+id+'&type='+type);
  http.onreadystatechange = handleResponse;
  http.send(null);
}
function handleResponse() {
  if (http.readyState == 4) { 
    var response = http.responseText;
    var update = new Array();
    if (response.indexOf('|' != -1)) {
      update = response.split('|');
      document.getElementById('fav_'+update[0].replace(/^\s+|\s+$/g, '')).innerHTML = update[1];
    }
    if (update[1] == '<span style="color: red;">not added</span>') {
      document.getElementById(update[0]+'_instructions').innerHTML = '<p class="box" style="padding: 10px;">Please <a href="/account/login/">login</a> to add an item to your favorites list.  If you aren\'t a member yet, <a href="/account/signup/">create an account</a>. It\'s free.</p>';
    }
  }
}


function sndReq_stream(action,type,id) {
  //alert('action: '+action+', type: '+type+', id: '+id);
  document.getElementById('stream').innerHTML = '<span style="color: #666666;">One moment please...</span>';
  http.open('get', '/inc_NEW/ats.php?action='+action+'&type='+type+'&id='+id);
  http.onreadystatechange = handleResponse_stream;
  http.send(null);
}
function handleResponse_stream() {
  if (http.readyState == 4) { 
    var response = http.responseText;
    document.getElementById('stream').innerHTML = response;
  }
}


function voteReplace(id, page, sort, artist_id) {
  http.open('get', '/wallpaper/_inc/vote_guts.php?id='+id+'&page='+page+'&sort='+sort+'&artist_id='+artist_id);
  http.onreadystatechange = vrHandleResponse;
  http.send(null);
}
function vrHandleResponse() {
   if (http.readyState == 4) { 
      var response = http.responseText;
      document.getElementById('vote_guts').innerHTML = response;
   }
}

function imgload(base, foo, id) {
  var val = foo.options[foo.selectedIndex].value;
  if (val != '') {
    padded = id+'';  
    while (padded.length < 5) { padded = '0' + padded; }
    document.getElementById('download_'+id).innerHTML = "<a href=\"/wallpaper/7yz4ma1/"+padded+'_'+base+'_'+val+'.jpg'+"\"><img src=\"/img_NEW/button_download.png\" alt=\"Click here to download\" title=\"Select your desired resolution from the menu to the left, then click here to download.\" align=\"center\" /></a>";
    //for (var j=1; j <= 10; j++) {
      //document.getElementById('res_'+val+'_'+j).selected = true;
      //alert(document.getElementById('info_'+j).innerHtml.value);
    //}
  }
  else {
    document.getElementById('download_'+id).href = '';
    document.getElementById('download_'+id).innerHTML = '<img src="/img_NEW/button_download_disabled.png" alt="Select your desired resolution from the menu to the left, then click here to download." title="Select your desired resolution from the menu to the left, then click here to download." align="center" onclick="javascript:alert(\'Please select your screen resolution from the popup menu prior to clicking the download button.\')" />';
  }
}

function sndIconDload(foo, id, base, extension) {
  var val = foo.options[foo.selectedIndex].value;
  if (val != '') {
    padded = id+'';  
    while (padded.length < 5) { padded = '0' + padded; }
    document.getElementById('dl_'+id).innerHTML = "<a href=\"/icons/dl/"+padded+'_'+base+'_'+val+extension+"\"><img src=\"/img_NEW/button_download.png\" alt=\"Click here to download\" title=\"Select your computer operating system from the menu to the left, then click here to download.\" align=\"center\" /></a>";
    //for (var j=1; j <= 10; j++) {
      //document.getElementById('res_'+val+'_'+j).selected = true;
      //alert(document.getElementById('info_'+j).innerHtml.value);
    //}
  }
  else {
    document.getElementById('dl_'+id).href = '';
    document.getElementById('dl_'+id).innerHTML = '<img src="/img_NEW/button_download_disabled.png" alt="Select your desired resolution from the menu to the left, then click here to download." title="Select your desired resolution from the menu to the left, then click here to download." align="center" onclick="javascript:alert(\'Please select your computer operating system from the popup menu prior to clicking the download button.\')" />';
  }
}

function sndReq_remove(id,type) {
  document.getElementById('fav_'+id).innerHTML = '<span style="color: #bb2f0e;">Removing...</span>';
  http.open('get', '/inc_NEW/rff.php?id='+id+'&type='+type);
  http.onreadystatechange = handleResponse2;
  http.send(null);
}
function handleResponse2() {
  if (http.readyState == 4) {
    document.getElementById('list_'+http.responseText.replace(/^\s+|\s+$/g, '')).innerHTML = '';
  }
}
function cmt(id,mode,type) {
  document.getElementById('controls_'+id).innerHTML = 'One moment please...';
  http.open('get', '/inc_NEW/cmt.php?id='+id+'&mode='+mode+'&type='+type);
  http.onreadystatechange = handleResponse3;
  http.send(null);
}
function handleResponse3() {
  if(http.readyState == 4){
    var response = http.responseText;
    var update = new Array();

    if(response.indexOf('|' != -1)) {
      update = response.split('|');
      // if (update[1] != 'ignore') {
      //    document.getElementById('rating_'+update[0]).innerHTML = update[1];
      // }
      document.getElementById('controls_'+update[0].replace(/^\s+|\s+$/g, '')).innerHTML = update[2];
    }
  }
}

function pad(number,length) {
    var str = '' + number;
    while (str.length < length)
        str = '0' + str;
    return str;
}

function openpopup(popurl){
   winpops=window.open(popurl,"vote","width=490,height=380")
   winpops.moveTo(350,200);
}

function jumpToPage(URL_List) {
   var URL = URL_List.options[URL_List.selectedIndex].value;
   window.location.href = URL;
}

function select_my_resolution() {
   var res_menu = document.getElementsByName("resolution");
   var mywidth  = screen.width;
   var myheight = screen.height;
   if ( mywidth == '768' && myheight == '1024') {
      mywidth = '1024';
   }
   else if ( mywidth == '320' && (myheight == '396' || myheight == '480') ) {
      mywidth  = '640';
      myheight = '960';
   }
   else {
      label = mywidth+'x'+myheight;
   }
   for (var i=0; i<res_menu.length; i++) {
      for (var j=0; j<res_menu[i].options.length; j++) { 
         if (res_menu[i].options[j].value == mywidth+'x'+myheight) {
            res_menu[i].options[j].selected = true;
            imgload(res_menu[i].getAttribute('onChange').toString().split("'")[1], res_menu[i], res_menu[i].getAttribute('onChange').split("'")[3]);
         } 
      } 
   }
}


function select_my_os() {
   var os_menu = document.getElementsByName("platform");
   var os;         
   if      ( navigator.platform.indexOf('Mac')   >= 0 ) { os = 'macosx';  }
   else if ( navigator.platform.indexOf('Win')   >= 0 ) { os = 'windows'; } 
   else if ( navigator.platform.indexOf('Linux') >= 0 ) { os = 'linux';   } 
   for (var i=0; i<os_menu.length; i++) {
      for (var j=0; j<os_menu[i].options.length; j++) { 
         if (os_menu[i].options[j].value == os) {
            os_menu[i].options[j].selected = true;
            sndIconDload(os_menu[i], os_menu[i].getAttribute('onChange').split("'")[1], os_menu[i].getAttribute('onChange').split("'")[3], os_menu[i].getAttribute('onChange').split("'")[5]);
         } 
      } 
   }
}

