
// custom prototype methods

Element.addMethods ({
	scrollToSide : function (element, side) {
		element = $(element) ;
		
		if (side == 'top') {
			element.scrollTop = 0 ;
			return true ;
		}
		else if (side == 'right') {
			element.scrollLeft = element.scrollWidth ;
		}
		else if (side == 'bottom') {
			element.scrollTop = element.scrollHeight ;
			return true ;
		}
		else if (side == 'left') {
			element.scrollLeft = 0 ;
		}
		
		return false ;
	}, 
	disabledClass : function (element, dir) {
		element = $(element) ;
		
		if (dir == null) {
			return element.hasClassName('disabled') ;
		}
		if (dir === true) {
			element.addClassName('disabled') ;
		}
		if (dir === false) {
			element.removeClassName('disabled') ;
		}
	}, 
	readonlyClass : function (element, dir) {
		element = $(element) ;
		
		if (dir == null) {
			return element.hasClassName('readonly') ;
		}
		if (dir === true) {
			element.addClassName('readonly') ;
		}
		if (dir === false) {
			element.removeClassName('readonly') ;
		}
	}, 
	checkedClass : function (element, dir) {
		element = $(element) ;
		
		if (dir == null) {
			return element.hasClassName('checked') ;
		}
		if (dir === true) {
			element.addClassName('checked') ;
			return true ;
		}
		if (dir === false) {
			element.removeClassName('checked') ;
			return true ;
		}
	}
}) ;

// 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) {
		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 validateFile(e, ext) {
	var ok = false ;
	var ext_str = '' ;
	
	$A(ext).each(function(i) {
		ext_str = ext_str+"."+i+"\r\n" ;
		
		if (e.value.toLowerCase().search("."+i) != -1) {
			ok = true ;
		}
	}) ;
	
	if (ok != true) {
		alert("This file must be in one of the following formats:\r\n"+ext_str) ;
		regenInput(e) ;
	}
}

function regenInput(e) {
	e.value = "" ;
	var n = e.cloneNode(true);
	e.parentNode.insertBefore(n, e) ;
	e.parentNode.removeChild(e) ;
}

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

// Window functions

var windows = {} ;


function popup(url, win_name) {
	url = url.replace('-thumb.', '-full.') ;
	
	if (!win_name) {
		win_name = "popupwin" ;
	}
	
	if (windows[win_name]) {
		windows[win_name].close() ;
	}
	
	win = window.open('', win_name, "resizable=1,scrollbars=0,location=0,status=0,toolbar=0,menubar=0,width=350,height=350") ;
	
	windows[win_name] = win ;
	
	var img = new Image() ;
	img.src = url ;
	
	if (img.complete == true) {
		resize() ;
	}
	else {
		img.onload = function () {resize() ;} ;
	}

	function resize() {
		win.location = url ;
		
		var w = img.width - win.document.body.clientWidth + 0 ;
		var h = img.height - win.document.body.clientHeight + 0 ;
		
		win.resizeBy(w, h) ;
		win.focus() ;
		win.document.close() ;
		img.onload = null ;
	} ;
	
	return false ;
}

function preview(file, nw, nh, win_name, win_title) {
	file = file.replace('https://', '') ;
	file = file.replace('http://', '') ;
	file = file.replace('www.centralfarm.com', '') ;
	file = file.replace('-thumb.', '-full.') ;
	
	if (!nw) {
		nw = '' ;
	}
	
	if (!nh) {
		nh = '' ;
	}
	
	if (!win_title) {
		win_title = '' ;
	}
	
	if (!win_name) {
		win_name = "preview" ;
	}
	
	if (windows[win_name]) {
		windows[win_name].close() ;
	}
	
	preview_window = window.open("/common/preview.php?file="+urlencode(file)+"&width="+nw+"&height="+nh+"&win_title="+win_title, win_name, "resizable=1,scrollbars=0,location=0,status=0,toolbar=0,menubar=0,width=350,height=350") ;
	
	windows[win_name] = preview_window ;
	
	preview_window.focus() ;

	return false ;	
}

/* Processing function */

var procWin = {
	procWin_img : new Image(), 
	
	procWin_img_src : '/common/images/loading.gif', 
	
	show : function () {
		$(document.body).insert(' <table id="procWin" cellspacing="0" cellpadding="0" style="position: fixed; z-index: 1000; width: 100%; height: 100%; top: 0px; left: 0px; "> <tr> <td style="vertical-align: middle; text-align: center; background-image: none; "> <table cellspacing="0" cellpadding="0" style="margin: auto; "> <tr> <td> <div style="padding: 45px 75px; background-color: #eee; border: solid 6px #d5d5d5; border-radius: 20px; box-shadow: 0px 0px 20px #666; "> <p style="margin-top: 0px; color: #777; font-size: 20px; font-weight: normal; ; ">Processing</p> <img src="/common/images/loading.gif" id="procWin_img" alt="Loading" /> </div> </td> </tr> </table> </td> </tr> </table> ') ;
	}, 
	
	hide : function () {
		$('procWin').remove() ;
	}, 
	
	init : function () {
		this.procWin_img.src = this.procWin_img_src ;
	}
}

procWin.init() ;

HTMLElement.prototype.click = function() {
	var evt = this.ownerDocument.createEvent('MouseEvents');
	evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
	this.dispatchEvent(evt);
}

/* Give checkbox an off value */
/* example <input type="checkbox" name="cb" id="cb" value="onvalue" off="offvalue" /> */

function checkboxInit() {
	var hcb ;
	
	$$("form").each (function(f) {
		Event.observe(f, 'submit', function(ev) {
			$$('input[type="checkbox"][off]').each(function(e) {
				hcb = new Element('input', {'type':'hidden'}) ;
				e.insert({'before':hcb}) ;
				hcb.writeAttribute({'name':e.readAttribute('name')}) ;
				e.writeAttribute({'name':''}) ;
				
				if (e.checked == true) {
					hcb.value = e.value ;
				}
				else {
					hcb.value = e.readAttribute('off') ;
				}
			}) ;
		}) ;
	}) ;
}

Event.observe(window, 'load', checkboxInit) ;

/* get radio button group value.  returns value or null */

function radio(fn) {
	var r = document.getElementsByName(fn) ;
	
	var v = null ;
	
	$A(r).each(function(e) {
		if (e.checked == true) {
			v = e.value ;
		}
	}) ;
	
	return v ;
}

// session vars 

function session(post, red) {
	if (typeof post === "string") {
		var q = post.indexOf('?') ;
		
		if (q !== false) {
			if (!red) {
				red = post.substring(0, q) ;
			}
			
			post = post.parseQuery() ;
		}
	}
	
	post.ajax = 1 ;
	
	var url = "/common/session.php" ;
	
	new Ajax.Request(url, {
		parameters : post, 
		asynchronous : false, 
		onSuccess: function(r) {
			if (red) {
				if (red === '') {
					red = location ;
				}
				
				location = red ;
			}
		}, 
		onFailure: function() {
			alert("Your request has failed") ;
		}
	}) ;
}

