/**
	MusicSend:Utilities
	
	2006 - 2007 UMS/MusicSend
**/

// Returns URL parameter
function GetURLParameter(key){
	var url = document.URL;
	var q_pos = url.indexOf('?');
	if(q_pos == -1)
		return null;
	url = url.substr(q_pos + 1);
	q_pos = url.indexOf(key);
	if(q_pos == -1)
		return null;
	url = url.substr(q_pos);
	q_pos = url.indexOf('=');
	if(q_pos == -1)
		return null;
	url = url.substr(q_pos + 1);
	if((q_pos = url.indexOf('&')) >= 0){
		url = url.substr(0, q_pos);
	}
	return unescape(url);
}

// Creates proprer HTTP/XML request object
function CreateHTTPRequest(type){
	var http_request = false;
	if(window.XMLHttpRequest){
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType){
           http_request.overrideMimeType(type);
		}
	}else{
		try{
           http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e){
           try{
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
           }catch(e){
			   http_request = false;   
		   }
        }
	}
	return http_request;
}

// Finds an encoded parameter in a source
function GetEncodedParameter(key, src){
	var k_pos = src.indexOf(key);
	if(k_pos == -1)
		return null;
	src = src.substr(k_pos);
	k_pos = src.indexOf('=');
	if(k_pos == -1)
		return true;
	src = src.substr(k_pos + 1);
	if((k_pos = src.indexOf('&')) >= 0){
		src = src.substr(0, k_pos);
	}
	return unescape(src);
}

function SetSessionId(id2){
	document.cookie = "sid=" + id2 + "; path=/";
}

function GetSessionId(){
	
	var src = document.cookie;
	var k_pos = src.indexOf("sid");
	if(k_pos == -1)
		return null;
	src = src.substr(k_pos);
	k_pos = src.indexOf('=');
	if(k_pos == -1)
		return null;
	src = src.substr(k_pos + 1);
	if((k_pos = src.indexOf(';')) >= 0){
		src = src.substr(0, k_pos);
	}
	return unescape(src);
}

function GetCookieValue(key){
	var src = document.cookie;
	var k_pos = src.indexOf(key);
	if(k_pos == -1)
		return null;
	src = src.substr(k_pos);
	k_pos = src.indexOf('=');
	if(k_pos == -1)
		return null;
	src = src.substr(k_pos + 1);
	if((k_pos = src.indexOf(';')) >= 0){
		src = src.substr(0, k_pos);
	}
	return unescape(src);
}

// Returns current domain name of the URL
function GetDomainName(){
	var domain = document.URL;
	var index = -1;
	domain = domain.substr(7);
	if((index = domain.indexOf('/')) != -1){
		domain = domain.substr(0, index);
	}
	return domain;
}

// Captures Enter key press and executes code
function CaptureEnter(e, code){
	//if(e.key == 13 || e.which == 13){
		//eval(code);
	//}
}

// Disables a form
function DisableForm(form){
	for(var i = 0; i < form.elements.length; i++){
		form.elements[i].disabled = true;
	}
}

// Enable form
function EnableForm(form){
	for(var i = 0; i < form.elements.length; i++){
		form.elements[i].disabled = false;
	}
}

// Global lookup arrays for base64 conversions
var enc64List, dec64List;
   
// Load the lookup arrays once
function initBase64( ) {
    enc64List = new Array( );
    dec64List = new Array( );
    var i;
    for (i = 0; i < 26; i++) {
        enc64List[enc64List.length] = String.fromCharCode(65 + i);
    }
    for (i = 0; i < 26; i++) {
        enc64List[enc64List.length] = String.fromCharCode(97 + i);
    }
    for (i = 0; i < 10; i++) {
        enc64List[enc64List.length] = String.fromCharCode(48 + i);
    }
    enc64List[enc64List.length] = "+";
    enc64List[enc64List.length] = "/";
    for (i = 0; i < 128; i++) {
        dec64List[dec64List.length] = -1;
    }
    for (i = 0; i < 64; i++) {
        dec64List[enc64List[i].charCodeAt(0)] = i;
    }
}
   
// Encode a string
function base64Encode(str) {
    var c, d, e, end = 0;
    var u, v, w, x;
    var ptr = -1;
    var input = str.split("");
    var output = "";
    while(end == 0) {
        c = (typeof input[++ptr] != "undefined") ? input[ptr].charCodeAt(0) : 
            ((end = 1) ? 0 : 0);
        d = (typeof input[++ptr] != "undefined") ? input[ptr].charCodeAt(0) : 
            ((end += 1) ? 0 : 0);
        e = (typeof input[++ptr] != "undefined") ? input[ptr].charCodeAt(0) : 
            ((end += 1) ? 0 : 0);
        u = enc64List[c >> 2];
        v = enc64List[(0x00000003 & c) << 4 | d >> 4];
        w = enc64List[(0x0000000F & d) << 2 | e >> 6];
        x = enc64List[e & 0x0000003F];
        
        // handle padding to even out unevenly divisible string lengths
        if (end >= 1) {x = "=";}
        if (end == 2) {w = "=";}
        if (end < 3) {output += u + v + w + x;}
    }
    // format for 76-character line lengths per RFC
    var formattedOutput = "";
    var lineLength = 76;
    while (output.length > lineLength) {
        formattedOutput += output.substring(0, lineLength) + "\n";
        output = output.substring(lineLength);
    }
    formattedOutput += output;
    return formattedOutput;
}
   
// Decode a string
function base64Decode(str) {
	if(str == ""){
		return "";
	}
    var c=0, d=0, e=0, f=0, i=0, n=0;
    var input = str.split("");
    var output = "";
    var ptr = 0;
    do {
        f = input[ptr++].charCodeAt(0);
        i = dec64List[f];
        if ( f >= 0 && f < 128 && i != -1 ) {
            if ( n % 4 == 0 ) {
                c = i << 2;
            } else if ( n % 4 == 1 ) {
                c = c | ( i >> 4 );
                d = ( i & 0x0000000F ) << 4;
            } else if ( n % 4 == 2 ) {
                d = d | ( i >> 2 );
                e = ( i & 0x00000003 ) << 6;
            } else {
                e = e | i;
            }
            n++;
            if ( n % 4 == 0 ) {
                output += String.fromCharCode(c) + 
                          String.fromCharCode(d) + 
                          String.fromCharCode(e);
            }
        }
    } while (typeof input[ptr] != "undefined");
    output += (n % 4 == 3) ? String.fromCharCode(c) + String.fromCharCode(d) : 
              ((n % 4 == 2) ? String.fromCharCode(c) : "");
    return output;
}
   
// Self-initialize the global variables
initBase64( );

