// JavaScript Document
function initXMLHttpClient() {
var xmlhttp;
try {
    // Mozilla / Safari / IE7
    xmlhttp = new XMLHttpRequest();
    } catch (e) {
    // IE
      var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0',
                                  'MSXML2.XMLHTTP.4.0',
                                  'MSXML2.XMLHTTP.3.0',
                                  'MSXML2.XMLHTTP',
                                  'Microsoft.XMLHTTP' );
      var success = false;
      for (var i=0;i < XMLHTTP_IDS.length && !success; i++) {
          try {
                xmlhttp = new ActiveXObject(XMLHTTP_IDS[i]);
                  success = true;
              } catch (e) {}
      }
        if (!success) {
            throw new Error('Unable to create XMLHttpRequest.');
        }
    }
  return xmlhttp;
}

  function HttpClient() { }
  HttpClient.prototype = {
      // type GET,POST passed to open
      requestType:'GET',
      // when set to true, async calls are made
      isAsync:false,

      // where an XMLHttpRequest instance is stored
      xmlhttp:false,

      // what is called when a successful async call is made
      callback:false,

      // what is called when send is called on XMLHttpRequest
      // set your own function to onSend to have a custom loading
     // effect
       onSend:function() {
         document.getElementById('HttpClientStatus').style.display =
                               'block';
     },

     // what is called when readyState 4 is reached, this is
     // called before your callback
      onload:function() {
          document.getElementById('HttpClientStatus').style.display =
                              'none';
      },

     // what is called when an http error happens
     onError:function(error) {
         alert(error);
     },

     // method to initialize an xmlhttpclient
     init:function() {
       try {
           // Mozilla / Safari
            this.xmlhttp = new XMLHttpRequest();
       } catch (e) {
           // IE
           var XMLHTTP_IDS = new Array('MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','Microsoft.XMLHTTP');
           var success = false;
           for (var i=0;i < XMLHTTP_IDS.length &&
             !success; i++) {
               try {
                   this.xmlhttp = new ActiveXObject
                     (XMLHTTP_IDS[i]);
                   success = true;
               } catch (e) {}
           }
           if (!success) {
               this.onError('Unable to create XMLHttpRequest.');
           }
        }
     },

     // method to make a page request
     // @param string url  The page to make the request to
     // @param string payload  What you're sending if this is a POST
    //                        request
    makeRequest: function(url,payload) {
         if (!this.xmlhttp) {
             this.init();
         }
         this.xmlhttp.open(this.requestType,url,this.isAsync);

         // set onreadystatechange here since it will be reset after a
        //completed call in Mozilla
         var self = this;
         this.xmlhttp.onreadystatechange = function() {
        self._readyStateChangeCallback(); }

         this.xmlhttp.send(payload);

         if (!this.isAsync) {
             return this.xmlhttp.responseText;
         }
    },

     // internal method used to handle ready state changes
    _readyStateChangeCallback:function() {
         switch(this.xmlhttp.readyState) {
              case 2:
               this.onSend();
               break;
            case 4:
               this.onload();
               if (this.xmlhttp.status == 200) {
                   this.callback(this.xmlhttp.responseText);
               } else {
                   this.onError('HTTP Error Making Request: '+
                                       '['+this.xmlhttp.
                                         status+']'+this.xmlhttp.
                                         statusText);
               }
               break;
         }
     }
 }

function GetValue(obj)
{
  var Object = document.getElementById(obj).value;
  return Object;
}

function getresult(page, divto, divloading, videourl, imageurl) {
document.getElementById(divto).style.display = 'none';
document.getElementById(divloading).style.display = 'block';
var client = new HttpClient();
client.isAsync = true;
client.callback = function(result) {
  document.getElementById(divto).style.display = 'block';
  document.getElementById(divloading).style.display = 'none';
  document.getElementById(divto).innerHTML = result;
}
  var pages = page + '?video=' + videourl + '&image=' + imageurl;
  client.makeRequest(pages,null);
}


function toggle(id,id2,id3,id4) {
var e = document.getElementById(id);
  e.style.position = 'relative';
		e.style.left = '0px';

var e2 = document.getElementById(id2);		      
	e2.style.position = 'absolute';
		e2.style.left = '-4000px';
		
var e3 = document.getElementById(id3);
	e3.style.position = 'absolute';
		e3.style.left = '-4000px';
		
var e4 = document.getElementById(id4);
	e4.style.position = 'absolute';
		e4.style.left = '-4000px';

}


function genrenext(genre,currentpage,divloading,divto,page) {
document.getElementById(divloading).style.display = 'block';
var client = new HttpClient();
client.isAsync = true;
client.callback = function(result) {
  document.getElementById(divto).style.display = 'block';
  //document.getElementById(divloading).style.display = 'none';
  document.getElementById(divto).innerHTML = result;
}
  var pages = page + '?genre=' + genre + '&page=' + currentpage;
  client.makeRequest(pages,null);	
document.getElementById(divloading).style.display = 'none';
}

function namenext(key,currentpage,divloading,divto,page) {
document.getElementById(divloading).style.display = 'block';
var client = new HttpClient();
client.isAsync = true;
client.callback = function(result) {
  document.getElementById(divto).style.display = 'block';
  //document.getElementById(divloading).style.display = 'none';
  document.getElementById(divto).innerHTML = result;
}
  var pages = page + '?key=' + key + '&page=' + currentpage;
  client.makeRequest(pages,null);	
document.getElementById(divloading).style.display = 'none';
}

function top100next(currentpage,divloading,divto,page) {
document.getElementById(divloading).style.display = 'block';
var client = new HttpClient();
client.isAsync = true;
client.callback = function(result) {
  document.getElementById(divto).style.display = 'block';
  //document.getElementById(divloading).style.display = 'none';
  document.getElementById(divto).innerHTML = result;
}
  var pages = page + '&page=' + currentpage;
  client.makeRequest(pages,null);	
document.getElementById(divloading).style.display = 'none';	
}

function togglegenre(page,divto,divloading,type) {
document.getElementById(divloading).style.display = 'block';
var client = new HttpClient();
client.isAsync = true;
client.callback = function(result) {
  document.getElementById(divto).style.display = 'block';
  //document.getElementById(divloading).style.display = 'none';
  document.getElementById(divto).innerHTML = result;
}
  var pages = page + '?type=' + type;
  client.makeRequest(pages,null);	
document.getElementById(divloading).style.display = 'none';
}

/*function additem(page,divto,divloading,chrn) {
document.getElementById(divloading).style.display = 'block';
var client = new HttpClient();
client.isAsync = true;
client.callback = function(result) {
  document.getElementById(divto).style.display = 'block';
  //document.getElementById(divloading).style.display = 'none';
  document.getElementById(divto).innerHTML = result;
}
  var pages = page + '?chrn=' + chrn;
  client.makeRequest(pages,null);	
document.getElementById(divloading).style.display = 'none';
}*/

function addtocart(chrn) {
	var iframecart = document.getElementById("buatcart");
	iframecart.src = "http://importmusik.com/artist/addtocart_ajax.php?chrn=" + chrn;
	access = 1;
}

function openURL(sURL) {
opener.document.location = sURL;
} 

function addtoplaylist(chrn) {
	var iframe = document.getElementById("fplaylist");	
	iframe.src = "http://importmusik.com/member/addtoplaylist_ajax.php?sid=" + chrn;
	access = 1;
}

function toggleMe(a){
var e=document.getElementById(a);
if(!e)return true;
if(e.style.display=="none"){
e.style.display="block"
} else {
e.style.display="none"
}
return true;
}

function refreshContent() {
	
document.getElementById('fartist').style.display = 'none';
document.getElementById('floading').style.display = 'block';
var client = new HttpClient();
client.isAsync = true;
client.callback = function(result) {
  document.getElementById('fartist').style.display = 'block';
  document.getElementById('floading').style.display = 'none';
  document.getElementById('fartist').innerHTML = result;
}
  var pages = 'refresh-random.php';
  client.makeRequest(pages,null);
}

function urlencode( str ) {
    // URL-encodes string  
    
    // %          note 1: info on what encoding functions to use from: http://xkr.us/articles/javascript/encode-compare/
    // *     example 1: urlencode('Kevin van Zonneveld!');
    // *     returns 1: 'Kevin+van+Zonneveld%21'
    // *     example 2: urlencode('http://kevin.vanzonneveld.net/');
    // *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                             
    var histogram = {}, tmp_arr = [];
    var ret = (str+'').toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    histogram['\u20AC'] = '%80';
    histogram['\u0081'] = '%81';
    histogram['\u201A'] = '%82';
    histogram['\u0192'] = '%83';
    histogram['\u201E'] = '%84';
    histogram['\u2026'] = '%85';
    histogram['\u2020'] = '%86';
    histogram['\u2021'] = '%87';
    histogram['\u02C6'] = '%88';
    histogram['\u2030'] = '%89';
    histogram['\u0160'] = '%8A';
    histogram['\u2039'] = '%8B';
    histogram['\u0152'] = '%8C';
    histogram['\u008D'] = '%8D';
    histogram['\u017D'] = '%8E';
    histogram['\u008F'] = '%8F';
    histogram['\u0090'] = '%90';
    histogram['\u2018'] = '%91';
    histogram['\u2019'] = '%92';
    histogram['\u201C'] = '%93';
    histogram['\u201D'] = '%94';
    histogram['\u2022'] = '%95';
    histogram['\u2013'] = '%96';
    histogram['\u2014'] = '%97';
    histogram['\u02DC'] = '%98';
    histogram['\u2122'] = '%99';
    histogram['\u0161'] = '%9A';
    histogram['\u203A'] = '%9B';
    histogram['\u0153'] = '%9C';
    histogram['\u009D'] = '%9D';
    histogram['\u017E'] = '%9E';
    histogram['\u0178'] = '%9F';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

function saveShout() {
	
document.getElementById('shout-loading').style.display = 'block';
document.getElementById('shout-box').style.display = 'none';
var s=urlencode(document.getElementById('shout-input').value);

var client = new HttpClient();
client.isAsync = true;
client.callback = function(result) {
  document.getElementById('shout-box').style.display = 'block';
  document.getElementById('shout-loading').style.display = 'none';
  document.getElementById('shout-input').value = result;
}
  var pages = 'status.php' + '?q=' + s;
  client.makeRequest(pages,null);
}

function getDrm(trxno) {

	var divto='drm-'+trxno;
	document.getElementById(divto).innerHTML = '<img src="http://importmusik.com/importstore/images/loading.gif">';
	
	var client = new HttpClient();
	client.isAsync = true;
	client.callback = function(result) {
	  document.getElementById(divto).innerHTML = result;
	}
  var pages = 'getdrm.php' + '?q=' + trxno;
  client.makeRequest(pages,null);
}


function reqSpecial(id) {
	
document.getElementById('show').style.display = 'none';
document.getElementById('loading').style.display = 'block';
var client = new HttpClient();
client.isAsync = true;
client.callback = function(result) {
  document.getElementById('show').style.display = 'block';
  document.getElementById('loading').style.display = 'none';
  document.getElementById('show').innerHTML = result;
}
  var pages = 'http://importmusik.com/artist/special.php?req=' +  id;
  client.makeRequest(pages,null);
}

function beAFan(cif) {
	
var client = new HttpClient();
client.isAsync = true;
client.callback = function(result) {

  document.getElementById('btnfan').innerHTML = result + '<b>You are now a fan of this Band</b>';
   /*jQuery(document).ready(function($) {
      $('a[rel*=facebox]').facebox() 
    })*/

}
  var pages = '/ajax_be_a_fan.php?cif=' +  cif ;
  client.makeRequest(pages,null);
}

