function getLoginBox() {
	new Ajax.Updater('loginBox', '/?act=login', { method: 'get' });
}

function doLogin(username, password, auto) {
	auto = auto ? 1 : 0;

	if (auto==1) {
		var pars = 'username=' + username + '&password=' + password + '&auto=' + auto + '&login=1';
	} else {
		var pars = 'username=' + $F('username') + '&password=' + $F('password') + '&auto=' + auto + '&login=1';
	}
	
	
	new Ajax.Updater('loginBox', '/?act=login', { method: 'post', postBody:pars, evalScripts: true });
}


function doLoginEnterKey(e, username, password, auto) { 
 	if(window.event){ // IE
		var whichCode = e.keyCode
	}
	else if(e.which){ // Netscape/Firefox/Opera
		var whichCode = e.which
	}
	else { return; }
 	 	
 	if (whichCode == 13) { // only if enter is pressed 
		doLogin(username, password, auto);
 	} 
}

function open_win(email) {
	window.open("?act=send_email&tpl=1&email="+email,"_blank","location=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=600, height=360");
	
}


function submitForm() {
	var email = document.getElementById('email').value;
	if(!email) alert('Моля въведете e-mail');
	else document.email_form.submit();
}


function submitFormEnterKey(e) { 
 	if(window.event){ // IE
		var whichCode = e.keyCode
	}
	else if(e.which){ // Netscape/Firefox/Opera
		var whichCode = e.which
	}
	else { return; }
 	 	
 	if (whichCode == 13) { // only if enter is pressed 
		submitForm();
 	} 
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ){
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}




function showHide(id) {
  	var field = document.getElementById(id);
	if(field.style.visibility == "hidden") {
  		field.style.visibility = "visible";
		field.style.overflow = "visible";
		field.style.height = "100%";
	} else {
		field.style.visibility = "hidden";
		field.style.overflow = "hidden";
		field.style.height = "0px";
	}
}

function showHideCommentForm(imgpath,show) {
	if (!Get_Cookie('username')){
		alert('За да публикувате коментар трябва да сте регистриран потребител! Ако имате създаден акаунт удостоверете самоличността си (горе в дясно) или се регистрирайте.');
		return;
	}
	
  	var field = document.getElementById('newCommentLayer');
	if(field.style.visibility == "hidden") {
  		field.style.visibility = "visible";
		field.style.overflow = "visible";
		field.style.height = "180px";
	} else {
		if(show) return;
		field.style.visibility = "hidden";
		field.style.overflow = "hidden";
		document.getElementById('smiles').style.visibility = "hidden";
		field.style.height = "0px";
	}
 }
 
function showSmiles() {
	if(document.getElementById('smiles').style.visibility == "hidden") {
		document.getElementById('smiles').style.visibility = "visible";
	}else{
		document.getElementById('smiles').style.visibility = "hidden";
	}
}

function hideSmiles(txt) {
	if(txt.length > 0) {
		insertIntoSelection(txt);
	} else {
		document.getElementById('comment').focus();
	}
	document.getElementById('smiles').style.visibility = 'hidden';
}

function insertIntoSelection(txt) {
    var field = document.getElementById('comment');

	//IE support
	if (document.selection) {
		field.focus();
		sel = document.selection.createRange();
		sel.text = txt;
	}
	//MOZILLA/NETSCAPE support
	else if (field.selectionStart || field.selectionStart == "0") {
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var chaineSql = field.value;

		field.value = chaineSql.substring(0, startPos) + txt + chaineSql.substring(endPos, chaineSql.length);
	} else {
		field.value += txt;
	}
}
