function setAuthCookie(name, value, expires, path, domain, secure) {
	if(!path && $('default_cookie_path')){
		path = $('default_cookie_path').value;
	}
	if(!domain){
		if(document.getElementById('site_auth_cookie_path')){
			domain = document.getElementById('site_auth_cookie_path').value;
		}else if(document.getElementById('site_cookie_path')){
			domain = document.getElementById('site_cookie_path').value;
		}
	}

	document.cookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function getAuthCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if(cookie.length > 0){
		offset = cookie.indexOf(search);
		if(offset != -1){
			offset += search.length;
			end = cookie.indexOf(";", offset);
			if(end == -1){
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function getAuthUrl(host_name){
	var url = document.location.href;
	url = url.replace(/\?return=.*/,'');
	url = url.replace(/http:\/\/.*?\//,"");
	if(url.match(/logout=1/)) url = '';
	if(url.match(/login/)) url = '';

	setAuthCookie('chk_auth', '1', 0, '/');
	if(host_name != window.location.hostname){
		var temp = window.location.hostname.match(/([a-z]+[A-Z]+[0-9]*)/ig);
		url = url + '&domain=' + temp[0];
		var replace = temp[0] + '.';
		window.location.href = 'http://' + window.location.hostname.replace(replace,'') + '/login/?return=/' + url;

	}else{
		window.location.href = 'http://' + window.location.hostname + '/login/?return=/' + url;
	}	
}


function getRegisterUrl(host_name) {
	var url = document.location.href;
	url = url.replace(/\?return=.*/,'');
	url = url.replace(/http:\/\/.*?\//,"");
	if(url.match(/logout=1/)) url = '';
	if(url.match(/login/)) url = '';
	
	if (host_name != window.location.hostname){
		var temp = window.location.hostname.match(/([a-z]+[A-Z]+[0-9]*)/ig);
		url = url + '&domain=' + temp[0];
		var replace = temp[0] + '.';
		window.location.href = 'http://' + window.location.hostname.replace(replace,'') + '/register/?return=/' + url;
	}
	else {
		window.location.href = 'http://' + window.location.hostname+'/register/?return=/' + url;
	}
}
