function isEmail(src) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
}
/* utils functions */
function ballon(type, eid, data, displaytime) {
	url = false;
	created = false;
	if (data && data.charAt(0) == '/') url = data;
	if (!$(eid + '_' + type)) {
		new Insertion.After(eid + '_btn_' + type,'<div id="' + eid + '_' + type +'" class="btn_popup ballon_' + type + '"' + (url ? ' style="display:none;"' : '') + '><a href="javascript:Effect.Fade(\'' + eid + '_' + type + '\');void(0);" class="btn btn_close">close</a><div id="' + eid + '_' + type + '_space" class="content">' + (!url ? data : '') + '</div></div>');
		created = true;
	}
	if (!$(eid + '_' + type).visible() && url) {
                new Ajax.Updater(eid + '_' + type + '_space',url + '?eid=' + eid, {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', eid + '_' + type + '_space']});
        }
	if (displaytime == undefined || !created) Effect.toggle(eid + '_' + type);
	else setTimeout("Effect.Fade('" + eid + "_" + type + "');",displaytime);

}
/* end utils functions */
/* login functions */
var unamereq = null;
function validusername(silent) {
	if ($('user_username').value && $('user_username').value.length >= 3) return(true);
	else {
		if (!silent) $('checkusername').innerHTML = '<div class="error_message">User name must be at least 3 characters long.</div>';
		return(false);
	}
}
function checkemail(email, err) {
	if (!isEmail(email)) $(err).innerHTML = '<div class="error_message">E-mail address is invalid.</div>';
	else $(err).innerHTML = '';
}
function checkpassword(passwd, minlen, err) {
	if (passwd.length < minlen) $(err).innerHTML = '<div class="error_message">Password must be at least 6 characters long.</div>';
        else $(err).innerHTML = '';
}
function checkusername(request) {
	if (unamereq) unamereq.abort();
	unamereq = request;
}
function handleLocation(city_id) {
	if ($('user_country').value == 'US') {
		Element.show('user_postalcode_area');
		Element.hide('user_city_block');
	} else {
		Element.hide('user_postalcode_area');
		Element.show('user_city_block');
		country = $('user_country').value;
		new Ajax.Updater('ajaxupdater','/cities/cities/' + country + '?select=user_city&selected=' + (city_id ? city_id : 0) + '', {asynchronous:true, evalScripts:true, onLoading:function(request){Element.show('pg_user_country');}, onComplete:function(request){Element.hide('pg_user_country');Element.show('user_city_area');$('user_citynew').value='';}, requestHeaders:['X-Update', 'ajaxupdater']});
	}
}
/* end login functions */
/* search functions */
function srchFocus() {
	q = srchField;
	if (q && q.value == srchText) q.value = '';
	q.style.color = '#000000';
}
function srchBlur() {
    q = srchField;
    if (q && q.value == '') {
        q.value = srchText;
        q.style.color = '#c0c0c0';
    }
}
function srchPrepare() {
    q = srchField;
    if (q && q.value == srchText) q.value = '';
}
/* end search functinos */
/* polls functions */
function checkpoll(eid) {
	var options = Form.getInputs(eid + '_poll_form','radio','oid');
	var i = 0;
	while (i < options.length && !options[i].checked) i++;
	if (i < options.length && options[i].checked) {
		new Ajax.Updater(eid + '_poll_vote','/votes/vote/', {asynchronous:true, evalScripts:true, parameters:$(eid + '_poll_form').serialize(), requestHeaders:['X-Update', '' + eid + '_poll_vote']});
	} else {
		ballon('vote',eid,'ooops, in order to vote, you need to select your answer!',5000);
	}
}

/* end of polls functions */
function _comment(eid, comment_id, action, obj_type, obj_id) {
	if ($(eid + '_' + action)) {
		Element.remove($(eid + '_' + action));
	} else {
		new Insertion.After($(eid),'<div id="' + eid + '_' + action + '" class="addcomment"></div>');
		new Ajax.Updater('' + eid + '_' + action + '','/comments/' + action + '/' + comment_id + '?obj_type=' + obj_type + '&obj_id=' + obj_id + '&parent_id=' + comment_id + '&eid=' + eid + '&action=' + action + '', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', '' + eid + '_' + action + '']});
		window.location = '#' + eid;
	}
}

function embed(movie,width,height,params) {
	var embed = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="%%width%%" height="%%height%%" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="allowFullScreen" value="false" /><param name="movie" value="%%movie%%" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="%%movie%%" quality="high" bgcolor="#ffffff" width="%%width%%" height="%%height%%" align="middle" allowScriptAccess="sameDomain" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
	embed = embed.replace(/\%\%movie\%\%/g,movie);
	embed = embed.replace(/\%\%width\%\%/g,width);
	embed = embed.replace(/\%\%height\%\%/g,height);
	return(embed);
}

function showembed(movie, width, height, params) {
	html = embed(movie,width,height,params);
	return(html);
}

function popUp(URL,width,height) {
	day = new Date();
	id = day.getTime();
	eval("window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=" + width + ",height=" + height + ",left = 0,top = 0');");
}

function widgetcolor(key,background) {
	$(key + '_background').value = background;
	$(key + '_embed').value = widgeturl(key);
	$(key + '_embed_demo').innerHTML = widgeturl(key,200,250);
}

function widgetsize(key,width,height) {
	$(key + '_width').value = width;
	$(key + '_height').value = height;
	$(key + '_embed').value = widgeturl(key);
}

function widgeturl(key,width,height) {
	pid = $(key + '_pid').value;
	width = width ? width : $(key + '_width').value;
	height = height ? height : $(key + '_height').value;
	color = $(key + '_color').value;
	background = $(key + '_background').value;
	movie = "http://playground.gazonkerz.com/widget/poll.swf?pid=%%pid%%&color=0x%%color%%&background=0x%%background%%&ws=http://playground.gazonkerz.com/polls_services/index?wsdl";
	movie = movie.replace(/\%\%pid\%\%/g,pid);
	movie = movie.replace(/\%\%color\%\%/g,color);
	movie = movie.replace(/\%\%background\%\%/g,background);
	return(showembed(movie, width, height));
}

