function showDialog(url, width, height, scroll) {
	if (scroll == null) scroll = 1
	var x = (screen.width - width) / 2, y = (screen.height - height) / 2;
	if( x < 0 ) x = 0
	if( y < 0 ) y = 0
	window.open(url, '_blank', 'xposition=' + x + ', left=' + x +', yposition=' + y + ', top=' + y + ', width=' + width + ', height=' + height + ', location=0, menubar=0, resizable=0, scrollbars=' +  scroll + ', status=0, titlebar=0, toolbar=0, hotkeys=0')
	return false
}

function showDialogEx(url, width, height, scroll, name) {
	if (scroll == null) scroll = 1
	var x = (screen.width - width) / 2, y = (screen.height - height) / 2;
	if( x < 0 ) x = 0
	if( y < 0 ) y = 0
	var w = window.open(url, name, 'xposition=' + x + ', left=' + x +', yposition=' + y + ', top=' + y + ', width=' + width + ', height=' + height + ', location=0, menubar=0, resizable=0, scrollbars=' +  scroll + ', status=0, titlebar=0, toolbar=0, hotkeys=0')
	return w
}

function addBanners(url) {
	var x = (screen.width - 670) / 2, y = (screen.height - 600) / 2;
	if( x < 0 ) x = 0
	if( y < 0 ) y = 0
	var win = window.open(url, 'popularaddbanners', 'xposition=' + x + ', left=' + x +', yposition=' + y + ', top=' + y + ', width=670, height=600, location=0, menubar=0, resizable=0, scrollbars=1, status=0, titlebar=0, toolbar=0, hotkeys=0')
	win.focus()
}

function bnPreviw(width, height) {
	var x = (screen.width - width) / 2, y = (screen.height - height) / 2;
	if( x < 0 ) x = 0
	if( y < 0 ) y = 0
	return window.open('', '_blank', 'xposition=' + x + ', left=' + x +', yposition=' + y + ', top=' + y + ', width=' + width + ', height=' + height + ', location=0, menubar=0, resizable=0, scrollbars=0, status=0, titlebar=0, toolbar=0, hotkeys=0')
}

function checkMultisubmit(form) {
	if (form.inprogress) return false; else { form.inprogress = true; return true; }
}

function accountDialog(id) {
	showDialog('/admin/accountdialog/' + id + '/account/change_state', 460, 400)
}

function actionDialog() {
	width = 460
	height = 400
	var x = (screen.width - width) / 2, y = (screen.height - height) / 2;
	if( x < 0 ) x = 0
	if( y < 0 ) y = 0
	return window.open('', 'popular_action', 'xposition=' + x + ', left=' + x +', yposition=' + y + ', top=' + y + ', width=' + width + ', height=' + height + ', location=0, menubar=0, resizable=0, scrollbars=0, status=0, titlebar=0, toolbar=0, hotkeys=0')
}


function checkSet(el, middle) {
	// 0 - uncheck
	// -1 - middle
	// 1 - check
	// alert(el.value);
	if (middle) {
		el.checked = true
		el.setAttribute('indeterminate', true)
		el.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)"
		el.style.MozOpacity = 0.5
		el.value = -1
	} else {
		el.value = (el.value == -1) ? 1 : (el.value == 1) ? 0 : -1
		el.checked = (el.value == 0) ? false : true
		el.setAttribute('indeterminate', (el.value == -1) ? true : false)
		el.style.filter = (el.value == -1) ? "progid:DXImageTransform.Microsoft.Alpha(opacity=50)" : ''
		el.style.MozOpacity = (el.value == -1) ? 0.5 : 1
	}
}

function checkABtn(pref) {
	n = document.getElementById(pref + '_sites');	
	if (n) {		
		if (n.value != -1) {
			document.curform.submitbtn.disabled = false;
			return
		}
	}
	n = document.getElementById(pref + '_profiles');
	if (n) {		
		if (n.value != -1) {
			document.curform.submitbtn.disabled = false;
			return
		}
	}
	n = document.getElementById(pref + '_banners');
	if (n) {		
		if (n.value != -1) {
			document.curform.submitbtn.disabled = false;
			return
		}
	}
	document.curform.submitbtn.disabled = true;
}

/* validate form */

function checkMax(el,maxval) {
	if (maxval == 0) return
	var form = el.form, t = false
	for (var i=0, k = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == 'checkbox' && form.elements[i].name && form.elements[i].name == el.name && form.elements[i].checked)
			if (++k > maxval) t = true
	}
	if (t) el.checked = false
}

function checkMin(form,name) {
	var group = form[name]
	if (!group.length) return group.checked
	else for (var i=0; i < group.length; i++) if (group[i].checked) return true
	return false
}

function isEmail(str) {
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)")
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,5}|[0-9]{1,5})(\\]?)$")
	return (!r1.test(str) && r2.test(str))
}

var charCodes = new Array(' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ','!','"','#','$','%','&',"'",'(',')','*','+',',','-','.','/','0','1','2','3','4','5','6','7','8','9',':',';','<','=','>','?','@','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','[','\\',']','^','_','`','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','{','|','}','~','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');

function decodeBase64(string) {	
	string = string.split('+').join(' ');
	var comps = string.split('%');
	var out_str = ''
	for (var i=0;i<comps.length;i++) {
		if (i == 0) {
			out_str += comps[0];
			continue;
		}
		var hex_num = comps[i].slice(0,2);		
		var code = '';
		eval('code = 0x' + hex_num);
		out_str += charCodes[code];
		out_str += comps[i].slice(2,comps[i].length);
	}	
      	return out_str;
}

function get_cookie(name) {
	var cookie_name = name + "=";
	var pos = document.cookie.indexOf(cookie_name);	
	if (pos != -1) {
		offset = pos + cookie_name.length;
		var endstr = document.cookie.indexOf(";", offset);
		if (endstr == -1) endstr = document.cookie.length;
		return decodeBase64(document.cookie.substring(offset, endstr));
	}
   	return '';
}

function set_auth_info(user_field, pass_field, save_field, prefix) {
	prefix = (!prefix) ? '' : prefix + '_'
	var login = decodeBase64(get_cookie(prefix+'login'));
	var password = decodeBase64(get_cookie(prefix+'password'));
	var save = (decodeBase64(get_cookie(prefix+'save')) == '1' ? 1 : 0);	
	if (login != '') {
		user_field.value = login;
		if (save == 1) {
			if (pass_field)
				pass_field.value = password;
			if (save_field)
				save_field.checked = true;
		}
	}
}
