// Pop Up Window

var showImage ;

function popUp(image) {	
	image = image.replace('-thumb.', '-full.') ;
	thisImage = new Image() ;
	thisImage.src = (image) ;
	popUpResize(image) ;
}

function popUpResize(image) {
	if((thisImage.width != 0) && (thisImage.height != 0)) {
		popUpDisplay(image) ;
	}
	else {
		doResize = "popUpResize('"+image+"')" ;
		resizeDelay = setTimeout(doResize, 20) ;
	}
}

function popUpDisplay(image) {
	newWidth = thisImage.width + 20 ;
	newHeight = thisImage.height + 20 ;
	popUpDim = "width="+newWidth+",height="+newHeight ;
	showImage = window.open(image, "popUp", popUpDim+", resizable=1, scrollbars=1") ;
}

// 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 ;
}

function getQV(qs, variable) {
	// to grab current qs in browser address bar send through qs as ""
	// to set all qs vars send through variable as ""
	if (qs == "") {
		qs = window.location.search.substring(1) ;
	}
	
	var vars = qs.split("&");
	
	if (variable) {
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			
			if (pair[0] == variable) {
				return pair[1];
			}
		}
	}
	else {
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			var eval_string = ""+pair[0]+" = '"+pair[1]+"' ;" ;
			eval(eval_string) ;
		}
		
		return true;
	}
}

// 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 if (eval(xmlHttp).status != 0) {
			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;
}

// Button Effect 

function upEffect(cur){
	if(document.getElementById) {
		cur.className='button_up' ;
	}
}

function normalEffect(cur){
	if(document.getElementById) {
		cur.className='button' ;
	}
}

// Window functions

function popup(url, title, options) {
	var newWin = window.open(url, title, options) ;
	
	if(!newWin) {
		newWin = window.open('',title, options);
		newWin.location.href = url;
	}
	
	return newWin ;
}

function resizeWin(e) {
	if(document.all) {
		winHeight=document.all[e].offsetHeight;
		winWidth=document.all[e].offsetWidth;
	}
	window.resizeTo(winWidth+20,winHeight+20);
}

function preview(file, width) {
	file = file.replace('-thumb.', '-full.') ;

	if (document.getElementById(width)) {
		width = document.getElementById(width).value ;
	}
	if (!(width)) {
		width = "" ;
	}
	
	previewAjax(file, width) ;
}

function previewAjax(file, width) {
	++ajax_inc ;
	var xmlHttp = "xmlHttp_"+ajax_inc ;
	eval(xmlHttp+" = GetXmlHttpObject() ;") ;
	
	if (eval(xmlHttp) == null) {
		return ;
	}
		
	var url = "/common/preview_ajax.php" ;
	url = url+"?sid="+Math.random() ;
	url = url+"&ajax=true" ;
	
	url = url+"&file="+urlencode(file) ;
	url = url+"&width="+width ;
	
	eval(xmlHttp).onreadystatechange = function () {previewAjaxStateChanged(xmlHttp, file)} ;
	eval(xmlHttp).open("GET",url,true) ;
	eval(xmlHttp).send(null) ;
}

function previewAjaxStateChanged(xmlHttp, file) {
	if (eval(xmlHttp).readyState == 4) {
		if (eval(xmlHttp).status == 200) {
			var response = eval(xmlHttp).responseText ;
						
			width = (getQV(response, "width") * 1) ;
			height = (getQV(response, "height") * 1) ;
			
			var b_width = width + 20 ;
			var b_height = height + 20 ;
			
			if (b_width < 200) {
				b_width = 200 ;
			}
			if (b_height < 150) {
				b_height = 150 ;
			}
			
			if (typeof preview_window != 'undefined') {
				try {
					preview_window.close() ;
				}
				catch (err) {}
			}
			
			var url = "/common/preview.php?file="+urlencode(file)+"&width="+width+"&height="+height ;
			var options = "width="+b_width+", height="+b_height+", resizeable=1, scrollbars=1" ;
			preview_window = window.open(url, "preview", options) ; 
			preview_window.focus() ;
		}
		else if (eval(xmlHttp).status != 0) {
			alert("Your request has failed\r\nThere may be a problem with your internet connection or a server error") ;
		}
	}
}


