// Cookie Functions

function setcookie(c_name, c_value, c_expire, c_path, c_domain, c_secure) {
	// c_expire unit is seconds
	
	//need to check to see if a cookie exists with the same name since it will not automatically overwrite it. 
	
	cookie_value = c_name+"="+escape(c_value) ;
	
	if (c_expire == null) {
		cookie_value += "" ;
	}
	else {
		c_expire = c_expire / (60 * 60 * 24) ;
		var exdate=new Date() ;
		exdate.setDate(exdate.getDate()+c_expire) ;
		cookie_value += "; expires="+exdate.toGMTString() ;
	}
	
	if (c_path != null) {
		cookie_value += "; path="+escape(c_path) ;
	}
	
	if (c_domain != null) {
		cookie_value += "; domain="+escape(c_domain) ;
	}
	
	if (c_secure != null && c_secure != false && c_secure != 'false') {
		cookie_value += "; secure" ;
	}
		
	document.cookie = cookie_value ;
}

function getcookie(c_name) {
	if (document.cookie.length>0) {
		c_start = document.cookie.indexOf(c_name + "=") ;
		
		if (c_start != -1) { 
			c_start = c_start + c_name.length+1 ; 
			c_end = document.cookie.indexOf(";",c_start) ;
			
			if (c_end == -1) {
				c_end = document.cookie.length ;
			}
			
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	
	return null;
}

// String Functions

function replaceAll(find, replace, string) {
	while (string.search(find) >= 0) {
		string = string.replace(find, replace) ;
	}
	
	return string ;
}

// Ajax Functions

var ajax_inc = 0 ;

function GetXmlHttpObject() {
	var xmlHttp = null ;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest() ;
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP") ;
		}
		catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP") ;
		}
	}
		
	if (xmlHttp == null) {
		alert("Your browser does not support AJAX!") ;
		return ;
	} 
	else {
		return xmlHttp ;
	}
}

/*
function changeSubmit(id) {
	++ajax_inc ;
	var xmlHttp = "xmlHttp_"+ajax_inc ;
	eval(xmlHttp+" = GetXmlHttpObject() ;") ;
	
	if (eval(xmlHttp) == null) {
		return ;
	}
	
	// Custom processing
	value = document.getElementById("mass_"+id+"_value").value ;
	
	var url = "settings_edit_exe.php" ;
	url = url+"?sid="+Math.random() ;
	url = url+"&ajax=true" ;
	
	// Custom variables to send 
	url = url+"&action=Edit" ;
	url = url+"&set_id="+id ;
	url = url+"&value="+value ;
	
	eval(xmlHttp).onreadystatechange = function () {changeSubmitStateChanged(xmlHttp)} ;
	eval(xmlHttp).open("GET",url,true) ;
	eval(xmlHttp).send(null) ;
}

// callback function, should be named the same as the calling function+StateChanged 
function changeSubmitStateChanged(xmlHttp) {
	// eval(xmlHttp).readyState Values
	// 0 = Uninitialized
	// 1 = Open has been called but not send
	// 2 = send has been called
	// 3 = receiving data from server
	// 4 = finished
	
	// eval(xmlHttp).status codes
	// 200 = OK
	// 400 = Bad Request
	// 403 = Forbidden
	// 404 = Not Found
	// 50x  = Server Errors
	// 500 = Internal Server Error
	
	if (eval(xmlHttp).readyState == 4) {
		if (eval(xmlHttp).status == 200) {
			var response = eval(xmlHttp).responseText ;
			alert(response);
		}
		else {
			alert("Your request has failed\r\nThere may be a problem with your internet connection or a server error") ;
		}
	}
}
*/

// DOM functions 

function insertAfter(referenceNode, newNode) {
	referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling) ;
}

// php javascript functions (see javascript_php.js)

function urlencode( str ) {
                             
    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);
    };
    
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}

