// JavaScript Document
var articleId="";
var voteArticleId="";
var userExits="";
var voteType="";
var xmlhttp=false;
var responsetxt = "";
var localId="";
var LinkId="";
var className="";
var myPicture=parseInt(Math.random()*99999999);
var oldSectionValue=0;
var nfiles = 1;
var offsetHeightUpload=0;
var authInfo = '1';
var server = getHost(window.location.href);
var mouseOnTooltip = false;
var duplicateArgs = '';
var rCurrent = 0;
var rCount = 5;
var rMax = 0;
var rClicked = false;
var times = 0;
var browserName=navigator.appName; 
var loadedAdverts = new Array();
var manualClick = false;
var manualClick_2 = false;
var e_shops = new Array();

var anim = new Image(313,90); 
anim.src = server+"/img/anim3.gif"; 
 
function getHost(url)
{
	var temp = new Array();
	temp = url.split('/');
	return temp[0]+'//'+temp[1]+temp[2];
}


String.prototype.trim = function() 
{
  return this.replace(/(^\s *)|(\s*$)/g,"")
}




function getSessionForNoCookies(withChar)
{
	return "";
}

function Expand()
{
  
  nfiles++ ;
  document.frmForm.txtFileNumber.value = nfiles ;
  //alert(document.frm.txtFileName.value);
  
  if (nfiles>40){
	  return false;
  }
   var c = '&nbsp;&nbsp;<font style="color: #666; font: normal 10px arial;"> * lodziņā ieraksti bildes adresi internetā un spied "Pievienot" vai</font><br>';
   c = c + '&nbsp;&nbsp;<font style="color: #666; font: normal 10px arial;"> * spied "Browse" un sameklē bildi savā datorā</font>';
   var adh = '<div class="profile_row"><label style="padding-top: 10px;">Attēls:</label> <label class="col2" id="label'+nfiles+'" style="padding-top: 10px;"><input type="file" name="File'+nfiles+'" id="File'+nfiles+'" size="25" class="inputText2" onChange="Expand();"></label>'+c+'</div>';
   adh = adh + '<div class="profile_row"><label>Attēla apraksts:</label> <label id="col2" style=" // width: 500px;"><Input Name="Desc-File'+nfiles+'" Size=20 value="" class="inputText2">&nbsp;</label></div>';
   //adh = adh + '<a id="addmore'+nfiles+'" href="#" OnClick="return(Expand());">Pievienot vēl attēlus</a></LABEL></div>';
   
  //document.getElementById('addmore'+(nfiles-1)).style.display="none";
  document.getElementById('files' + nfiles).innerHTML=adh;
  document.getElementById('files' + nfiles).style.display="block";
  window.scrollBy(0,65);
	if(browserName!="Microsoft Internet Explorer"){
			offsetHeightUpload = offsetHeightUpload + 22 ;
	}
	else
	{
		offsetHeightUpload = offsetHeightUpload - 48 ;
	}

  offsetHeightUpload = offsetHeightUpload + parseInt(document.getElementById('files' + nfiles).offsetHeight);
  //alert(offsetHeightUpload);
  //callSize(0, 0);
  return false;
}
//function for enable login with enter key
function enableEnterKeyLogin(pageScript, event)
{
	if(!event){var keyCode = window.event.keyCode;}
	else{var keyCode = event.keyCode;}
	if (keyCode == 13){loginCheck_username(pageScript);keyCode = 0;}
}

function functionOnEnter(functionName, event)
{
	if(!event)
	{
		var keyCode = window.event.keyCode;
	}
	else
	{
		var keyCode = event.keyCode;
	}
	if(keyCode == 13)
	{
		eval(functionName+"()");
		keyCode = 0;
	}
}

function deleteProfileImage(imageId)
{
	window.location.href = '/deleteProfileImage.php?imageid='+imageId;
}

function goBack(url)
{
	if(url != '') {
		window.location.href=url;
	} else {
		document.getElementById("FormIndex").action="/";
		document.getElementById("FormIndex").submit();
	}
}

function editProfile()
{
	document.getElementById("frmUser").action="/editProfile.php";
	document.getElementById("frmUser").submit();
}

function activeCategory()
{
	document.getElementById("frmUser").action="activeCategory.php";
	document.getElementById("frmUser").submit();
}

function updateProfile(userId)
{
	document.frm.userId.value=userId;
	document.getElementById("frm").action="updateProfile.php";
	document.getElementById("frm").submit();
}


function checkArticleType(type)
{
	document.Form1.approvalType.value=type;
	document.Form1.pages.value=1;
	document.Form1.action="articleApproval.php";
	document.Form1.submit();
}


function authCheck()
{
	var username = document.getElementById("userName").value;
	var password = document.getElementById("password").value;
	
	if (document.getElementById("userName").value != "" || document.getElementById("password").value != "")
	{
		var params = "";
		params = "username="+username+"&password="+password;
		var myAjax = new Ajax.Request('checkAuth.php', {method: 'post', parameters: params, onComplete: function(request) {authInfo = request.responseText; }});
    }
	else authInfo = '1';
}


function checkArticleSubmission(mode) 
{ 
	var num=0;
	var chkbool=0;
	var checkCount=0;
	var strID='';

	document.getElementById('save_mode').value = mode;

	if (authInfo != '1')
	{
		alert("lietotājvārds un/vai parole ievadīts nepareizi");
		return false;
	}

	//selectDay1=document.getElementById("selectDay");
	if (mode == 'add_active' || mode == 'add_draft' || mode == 'add_inactive') 
	if(!document.getElementById("rules").checked) 
	{
		alert("Jāpiekrīt lietošanas noteikumiem");
		return false;			
	}

	if(tinyMCE.getContent().trim()=="")
	{
		alert('Jāievada apraksts!');
		document.getElementById('description2').focus;
		return false;
	}
	
	if (document.getElementById('heading').value.trim()=="")
	{
		alert('Jāievada virsraksts!');
		document.getElementById('heading').focus;
		return false;
	}
	
	if (document.getElementById('heading').value.length > 60)
	{
		alert('Saīsiniet virsraksta simbolu skaitu līdz 60!');
		return false;
	}

	// check tag
	var i=0;
	var count = 0;
	for (i=0; i<110; i++)
	{
		if (document.getElementById('tag_'+i))
			if (document.getElementById('tag_'+i).checked == true)
				count++;
	}
	if (count > 2)
	{
		alert('Ne vairāk kā 2 tagi!');
		return false;
	}

	if (mode == 'edit_copy' || mode == 'edit_pub' || mode == 'edit_draft' || mode == 'edit_deactivate') 
		document.frmForm.action = "articleUpdate.php";
	if (mode == 'add_active' || mode == 'add_draft' || mode == 'add_inactive') 
		document.frmForm.action = "articleConfirmation.php";

	if (mode == 'edit_draft' || mode == 'add_draft')
		document.frmForm.target='_blank';
	else
		document.frmForm.target='_parent';

    changeType();
	document.frmForm.submit();
	setFlash();
	//if (duplicateArgs > "") processVote();
} 

function processVote()
{
 	var args = duplicateArgs.split('|||');
	var id = args[0];
	var subid = args[1];
	var user = args[2];
	add_vote(user, 0, id, 1, subid);
}

function changeType()
{
	var n = 0;
	var el;
	var label;
	var str = "";
	for (n=1; n<=nfiles; n++)
	{
		if(document.getElementById("File"+n))
		{
			el = document.getElementById("File"+n);
			str = el.value;
			if (str.length>4 && str.substring(0,4) == "http")
			{	
				//el.type = "text";
				//el.value = str;
				label = document.getElementById("label"+n);
				label.innerHTML = '<input type="text" value="'+str+'" name="File'+n+'" id="File'+n+'" size="25" class="inputText2" onChange="Expand();">';
			}
		}
	}
}

function setFlash()
{
	
	if (nfiles>1 || isVideo())
	{
		if (navigator.appName == "Netscape")
			document.getElementById('upload_embed').height = findPos(document.getElementById('bottom'))+37;
		else
			document.getElementById('upload').height = findPos(document.getElementById('bottom'))+35;
		document.getElementById('upload_div').style.display = "";
		document.getElementById('upload').style.display = "";
		document.getElementById('upload_div').innerHTML = '<img id="anim" width="313" height="90" src='+anim.src+'>';
	}
}


function isVideo()
{
	var is = false;
	var val = document.getElementById('video_file').value;
	if (val > "") is = true;
	return is;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	//return [curleft,curtop];
	return curtop;
}


function verifyTag(id)
{
	var i=0;
	var count = 0;
	for (i=0; i<150; i++)
	{
		if (document.getElementById('tag_'+i))
			if (document.getElementById('tag_'+i).checked == true)
				count++;
	}
	if (count > 2)
	{
		alert('Ne vairāk kā 2 tagi!');
		document.getElementById('tag_'+id).checked = false;
	}
	else
		if (id==122)
			if (document.getElementById('tag_'+id).checked)
				document.getElementById('bar_params').style.display='';
			else
				document.getElementById('bar_params').style.display='none';
}

function verifyNominations(id)
{
	var i=0;
	var count = 0;
	for (i=0; i<10; i++)
	{
		if (document.getElementById('nomination'+i))
			if (document.getElementById('nomination'+i).checked == true)
				count++;
	}
	if (count > 1)
	{
		alert('Jāizvēlas tikai viena nominācija!');
		document.getElementById('nomination'+id).checked = false;
	}
}

function login_chk()
{
	//alert("chk");
	if (document.getElementById("txtUserName").value=="")
	{
	alert(username);
	document.getElementById("txtUserName").focus();
	return false;
	}
	
	if (""==document.getElementById("txtPwd").value)
	{
	alert(password);
	document.getElementById("txtPwd").focus();
	return false;
	}

}

function validate_reg_form() 
{

	document.getElementById("tmp").value = document.getElementById("username").value;
	
	if(!document.getElementById("rules").checked) {
		alert(rules);
		return false;			
	}
	
	if( "" == document.getElementById("username").value.trim() ) {
		alert(username);
		document.getElementById("username").focus();
		return false;
	}
	
	if( 4 > (document.getElementById("username").value.trim()).length ) {
		alert(min_username);
		document.getElementById("username").focus();
		return false;
	}
	
	if( document.getElementById("password").value.trim() != document.getElementById("confirm_password").value.trim() ) {
		alert(match_password);
		document.getElementById("confirm_password").focus();
		return false;
	}

	if( (document.getElementById("emailAddress").value.trim()).length<5 ) 
	{
		alert("E-pasta adrese jāievada obligāti!");
		document.getElementById("emailAddress").focus();
		return false;
	}
	
}

function chaekspace() 
{
	document.getElementById("password").value = document.getElementById("password").value.trim();
}

function chkEmail()
{
	Email=document.getElementById("emailAddress").value;
	if(!Email.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) 
	{ 
		return false; 
	}
	else
	{ 
	   return true; 
	} 
		
}

function chkdate(objValue) 
{
	var strDatestyle = "US"; //United States date style
	//var strDatestyle = "EU";  //European date style
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear = "";
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objValue;
	var strSeparatorArray = new Array("-"," ","/",".");
	var separatorFound = '';
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
	strDate = datefield;
	
	if (strDate.length < 1) {
		return true;
	}

	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				err = 1;
				return false;
				break;
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
			separatorFound = strSeparatorArray[intElementNr]
			booFound = true;
   		}
	}
	
	if (booFound == false) 
	{
		return false 

	}

	if (strYear.length == 2) {
		strYear = '20' + strYear;
	}
// US style
	if (strDatestyle == "US") {
		strTemp = strDay;
		strDay = strMonth;
		strMonth = strTemp;
	}
	intday = parseInt(strDay, 10);
	if (isNaN(intday)) {
		err = 2;
		return false;
	}
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)) {
		for (i = 0;i<12;i++) {
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()) {
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		if (isNaN(intMonth)) {
			err = 3;
			return false;
		}
	}
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)) {
		err = 4;
		return false;
	}
	if (intYear<1900){
		err=5;
		return false;
	}
	if (intYear>2500){ // Added just to make sure that Year enetered is not ahead than 2500 : Sumeet Issue # 2276 : 04.01.2005
		err=5;
		return false;
	}
	if (intMonth>12 || intMonth<1) {
		err = 5;
		return false;
	}
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) {
		err = 6;
		return false;
	}
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) {
		err = 7;
		return false;
	}
	if (intMonth == 2) {
		if (intday < 1) {
			err = 8;
			return false;
		}
		if (LeapYear(intYear) == true) {
			if (intday > 29) {
				err = 9;
				return false;
			}
		}
		else {
			if (intday > 28) {
				err = 10;
				return false;
			}
		}
	}

	return true;
}

function LeapYear(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

	
function check_username()
{
	var usernameRegxp = /^([0-9A-Za-z_]+)$/;
	
	if (usernameRegxp.test(document.getElementById("username").value.trim()) != true)
	{
		alert(valid_username);
		document.getElementById("username").focus();
		return false;
	}
	var username = document.getElementById("username").value.trim();
	
	if (username==""){
			return false
	}
	
	var myRand=parseInt(Math.random()*99999999);
	var poststr="myRand="+ myRand + "&username="+encodeURI(username);
	var posturl="checkuser.php";
	var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr,asynchronous:'false', onComplete: function(request) {var retCheck = request.responseText; if (retCheck!="")	alert(retCheck);}});
}


function validateCommentForm()
{
	//document.getElementById("tmp").value = document.getElementById("comments_description").value;
	if( "" == document.getElementById("comments_description").value.trim() )
	{
		alert(comments_description);
		return false;
	}
	
	document.getElementById("antispam").value = 'abrakadabra';
}

function forget_form()
{
	//alert(document.getElementById("emailAddress").value.trim());
	if(document.getElementById("emailAddress").value.trim() == "") {
		alert(emailaddress);
		document.getElementById("emailAddress").value = document.getElementById("emailAddress").value.trim();
		document.getElementById("emailAddress").focus();
		return false;
	}
	if( false == chkEmail2() ) {
		alert(valid_email);
		return false;
	}

	document.getElementById("emailAddress").value = document.getElementById("emailAddress").value.trim();

}

function validate_forget_form()  {
	if( "" == document.getElementById("secretAnswer").value.trim()) {
		alert(secret);
		return false;
	}
	document.getElementById("secretAnswer").value = document.getElementById("secretAnswer").value.trim();
}

function chkEmail2()
{
	Email=document.getElementById("emailAddress").value.trim();
	if(!Email.match(/^[\w]{1}[\w\.\-_]*@[\w]{1}[\w\-_\.]*\.[\w]{2,6}$/i)) 
	{ 

		return false; 
	}
	else
	{ 
	   return true; 
	} 
}

function validate_change_password_form()
{
	if( "" == document.getElementById("old_Password").value.trim() ) {
		alert(current_password);
		return false;
	}
	
	if( "" == document.getElementById("new_password").value.trim() ) {
		alert(new_password);
		document.getElementById("new_password").focus();
		return false;
	}
	if( 5 > (document.getElementById("new_password").value.trim()).length ) {
		alert(password_spec);
		document.getElementById("new_password").focus();
		return false;
	}

	if( "" == document.getElementById("confirm_password").value.trim() ) {
		alert(confirm_password);
		document.getElementById("confirm_password").focus();
		return false;
	}
	
	if( document.getElementById("new_password").value.trim() != document.getElementById("confirm_password").value.trim() ) {
		alert(match_password);
		document.getElementById("confirm_password").focus();
		return false;
	}

}

function AddEdit(recordNo, fieldName, Mode, actionPage)
{
	FldName=eval("document.frm." + fieldName) 
	FldName.value=recordNo;
	document.frm.Mode.value=Mode;
	document.frm.action=actionPage;
	document.frm.submit();
}


function createCookie(name,value,days) 
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) 
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}

function genId()
{
	var r1=parseInt(Math.random()*9999999999);
	var r2=parseInt(Math.random()*9999999999);
	return r1.toString()+r2.toString();
}

function add_vote(userExists, pointId, articleId, voteType1, submitterId)
{
		//var str = "";
		//alert('userExists='+userExists+', pointId='+ pointId+', articleId='+ articleId+', voteType='+voteType1+', submitterId='+ submitterId);
		var voteArticleId = articleId;
		var voteType = voteType1 ;
        var str = '';
		var voterId = 0;

		voterId = readCookie('voteId');
		if (voterId == null) 
		{
			voterId = genId();
			createCookie('voteId', voterId, 365);
		}

		if(userExists==submitterId)
		{
			alert('Nevar balsot par savu rakstu!');
			return false;
		}
		else
		{

			var myRand=parseInt(Math.random()*99999999);
			var poststr="myRand="+ myRand + "&voterId="+voterId+ "&articleId="+encodeURI(voteArticleId)+"&voteType=" +voteType+"&submitterId=" +submitterId;
			var posturl="/votes.php";

			var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete:function(response) {
				 xmlhttpChangeVote(response,pointId, voteType);
				}});

		}
}


function xmlhttpChangeVote(originalRequest, pointId, voteType1)
{
	
	//alert(originalRequest);
	var voteType = voteType1 ;
	var retCheck = originalRequest.responseText;
	//alert(retCheck);
	var temp1=retCheck.split("|||");
	if (temp1[1]>0) {
		if (document.getElementById('userKarmaPointsHeader'))
			document.getElementById('userKarmaPointsHeader').innerHTML = temp1[1]+" ";
	}

	str = temp1[0];
	c1 = str.substr(str.length-1);
	c2 = str.substr(str.length-2);
	
	if((temp1[0] == 1 || temp1[0] == -1 || c1 == '1') && c2 != '11') {
		p = ' punkts';
	} else {
		p = ' punkti';
	}
	document.getElementById('point' + pointId).innerHTML = temp1[0]+p;
	
	
	 var up = document.getElementById("imgUp" + pointId);
	 var down = document.getElementById("imgDn" + pointId);
	
	if(voteType==1){
		if(up.className=="par_img"){
			up.className="par_img_voted"
			up.src ="/images/par_1.png";
			down.className="pret_img"
			down.src ="/images/pret_0.png";
		}
		else if(up.className=="par_img_voted"){
			up.className="par_img";
			up.src ="/images/par_0.png";
		}
	}
	else if(voteType==0){
		if(down.className=="pret_img_voted"){
			down.className="pret_img"
			down.src ="/images/pret_0.png";
			}
		else if(down.className=="pret_img"){
			down.className="pret_img_voted";
			down.src ="/images/pret_1.png";
			up.className="par_img";
			up.src ="/images/par_0.png";
			}
	}	
}



function add_vote_comments(userExists, commentId, articleId, voteType1, submitterId)
{
	//alert(userExists+' '+submitterId);
	var voteArticleId = articleId;
	var voteType = voteType1 ;
			
	var voterId = 0;
	voterId = readCookie('voteId');
	if (voterId == null) 
	{
		voterId = genId();
		createCookie('voteId', voterId, 365);
	}

		
	if(userExists==submitterId && submitterId>0)
	{
		alert('Nevar balsot par savu komentāru');
		return false;
	}
	else
	{
		var myRand=parseInt(Math.random()*99999999);
		var poststr="myRand="+ myRand + "&voterId="+voterId+ "&articleId="+encodeURI(voteArticleId)+"&voteType=" +voteType+"&commentId=" + commentId +"&submitterId=" + submitterId;
			//poststr = poststr.join("")
		var posturl="/votesComments.php";
			//var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete: xmlhttpChangeVote});
			//alert(pointId)
		var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete:function(response) {
			xmlhttpChangeVoteComment(response,commentId, voteType);
				}});
	}

}

function xmlhttpChangeVoteComment(originalRequest, commentId, voteType1)
{
	var voteType = voteType1;
	var retCheck = originalRequest.responseText;
	//alert(retCheck);
	var temp1=retCheck.split("|");
	if (temp1[1]>0) {
		if (document.getElementById('userKarmaPointsHeader'))
			document.getElementById('userKarmaPointsHeader').innerHTML = temp1[1]+" ";
	}
	if(temp1[0] == 1 || temp1[0] == -1) {
		p = ' punkts';
	} else {
		p = ' punkti';
	}
	document.getElementById('vote' + commentId).innerHTML = temp1[0]+p;	
	
	var up = document.getElementById("imgUp" + commentId);
	var down = document.getElementById("imgDn" + commentId);
	
	if(voteType==1){
		if(up.className=="img_par"){
			up.className="img_par_voted"
			up.src ="/images/par_1.png";
			down.className="img_pret"
			down.src ="/images/pret_0.png";
		}
		else if(up.className=="img_par_voted"){
			up.className="img_par";
			up.src ="/images/par_0.png";
		}
	}
	else if(voteType==0){
		if(down.className=="img_pret_voted"){
			down.className="img_pret"
			down.src ="/images/pret_0.png";
			}
		else if(down.className=="img_pret"){
			down.className="img_pret_voted";
			down.src ="/images/pret_1.png";
			up.className="img_par";
			up.src ="/images/par_0.png";
			}
		}	
}


function check_Link1_URL(linkname)
{
	if(document.getElementById(linkname).value.trim()!=""){
		var poststr=""; 
		var posturl=document.getElementById(linkname).value.trim();
		
		if(posturl.match('http://') == null && posturl.match('https://') == null) {
			posturl = 'http://'+posturl;
		} 
		
		//check link
		var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onFailure: function() {
			alert('Nekorekts links!');
			document.getElementById(linkname).value="";
			document.getElementById(linkname).focus();
			return false;
		}});
			

		
		if(linkname == 'utube') 
		{
			if(
				posturl.match('video.google.com') == null && 
				posturl.match('youtube.com') == null && 
				posturl.match('apollo.lv') == null &&
				posturl.match('videojug.com') == null &&
				posturl.match('collegehumor.com') == null &&
				posturl.match('video.yahoo.com') == null &&
				posturl.match('revision3.com') == null &&
				posturl.match('trubaz.com') == null &&
				posturl.match('metacafe.com') == null
			   ) 
			{
				alert('Video tiek atbalstīti tikai no: video.google.com, youtube.com, apollo.lv, videojug.com, metacafe.com, collegehumor.com, video.yahoo.com, revision3.com, trubaz.com');
				document.getElementById(linkname).value="";
				document.getElementById(linkname).focus();
				return false;
			}
		} 

	}
	return true;
}

function deleteComment(id, articleId)
{
	var flag = confirm("Vai Jūs tiešām vēlaties dzēst šo komentāru?");
	if( true == flag )
	{
		window.location.href="/comments.php?action=delete&id=" + id + "&articleId=" + articleId +"";
	
	}
}

function showCommentDiv(userExists)
{
	
	if(document.getElementById("message").style.display == "none" ) 
	{
		document.getElementById("message").style.display="block";
	}
	callSize()
}

function hideCommentDiv()
{
	if(document.getElementById("message").style.display == "block" ) 
	{
		document.getElementById("message").style.display="none";
	}
	callSize()
}

function hideDiv(id)
{
	document.getElementById("div" + id).innerHTML="";
}
function checkVal(id)
{
	if( document.getElementById("text" + id).value.trim() == "" )
	{
		document.getElementById("text" + id).value == "";
		document.getElementById("text" + id ).focus();
		alert("Please enter Comment");
		return false;
	}
}

function showTextArea(userExists, divId, articleId)
{	

	if(document.getElementById("div" + divId).innerHTML == "" )
	{
		document.getElementById("div" + divId).innerHTML +="<br><form method='post' action='comments.php'><input type='hidden' name='parentId' value='" + divId + "'><input type='hidden' name='action' value='add'><input type='hidden' name='articleId' value=" + articleId +"><textarea name='usercomment' id='text" + divId + "' rows='5' cols='60' class='comments_description'></textarea><br><input type='submit' name='addComment' value='Submit' class='buttonClass' onClick='javascript: return checkVal(" + divId + ");'>&nbsp;&nbsp;<input type='button' name='cancel' value='Cancel' class='buttonClass' onClick='javascript: hideDiv(" + divId + ");' ></form>";
		document.getElementById("text" + divId ).focus();

		callSize()
	}
}


function doEdit(divId, articleId) 
{
	
	if(document.getElementById("div" + divId).innerHTML == "" )
	{
		var commentDesc = document.getElementById(divId).innerHTML;
		//alert(document.getElementById(divId));
		document.getElementById("div" + divId).innerHTML +="<br><form method='post' action='comments.php'><input type='hidden' name='id' value='" + divId + "'><input type='hidden' name='action' value='edit'><input type='hidden' name='articleId' value="+ articleId + "><textarea name='usercomment' id='text" + divId + "' rows='5' cols='60' class='comments_description'>" + commentDesc +  "</textarea><br><input type='submit' name='addComment' value='Submit' onClick='javascript: return checkVal(" + divId + ");' class='buttonClass'>&nbsp;&nbsp;<input type='button' name='cancel' value='Cancel' class='buttonClass' onClick='javascript: hideDiv(" + divId + ");' ></form>";
		document.getElementById("text" + divId ).focus();
	}

}
	
//secreat quistion
function validate_change_password_form()
{
	if( "" == document.getElementById("old_Password").value.trim() ) {
		alert(current_password);
		return false;
	}

	if( "" == document.getElementById("new_password").value.trim() ) {
		alert(new_password);
		return false;
	}
	if( 5 > (document.getElementById("new_password").value.trim() ).length ) {
		alert(min_password);
		return false;
	}

	if( "" == document.getElementById("confirm_password").value.trim() ) {
		alert(confirm_password);
		return false;
	}
	if( 5 > (document.getElementById("confirm_password").value.trim() ).length ) {
		alert(min_confirm_password);
		return false;
	}

	if( document.getElementById("new_password").value.trim() != document.getElementById("confirm_password").value.trim() ) {
		alert(match_password);
		return false;
	}
}


function search_header()  {
	if( "" == document.getElementById("searchFor").value.trim() ) {
		alert(searchmsg);
		return false;
	}
}


function IsNumeric(sText, allowDecimal)
{
   var ValidChars = "0123456789";
   if (allowDecimal == 1)
	   ValidChars = ValidChars + ".";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


function ShowType(type, id)
{
	document.frmComments.type.value=type;
	document.frmComments.articleId.value=id;
	document.frmComments.submit();
}

function showModeType(type, tagId) 
{
	if(tagId>0){
		var addTagInUrl = "&tagId="+tagId;
	}else{
		var addTagInUrl = "";
	}
	window.location.href="/index.php?modeType="+type+addTagInUrl;
}	


function callPage(pageno, categoryId, modeType)
{
	document.Form1.pages.value=pageno;
	document.Form1.submit();
}


function changePwd()
{	
	document.getElementById("frmUser").action="/changePassword.php";
	document.getElementById("frmUser").submit();
}

function changePassword(id)
{
	document.frm12.userId.value=id;
	document.frm12.action="changePassword.php";
	document.frm12.submit();
}


function validateRegForm() 
{
	var usernameRegxp = '/^([0-9A-Za-z_]+)$/^';
	
	
	if( "" == document.getElementById("username").value.trim() ) {
		alert(blankUsername);
		document.getElementById("username").focus();
		return false;
	}
	
	if (usernameRegxp.test(document.getElementById("username").value.trim()) != true)
	{
		alert(valid_username);
		document.getElementById("username").focus();
		return false;
	}
	
	if( 5 > (document.getElementById("username").value.trim()).length ) {
		alert(min_username);
		document.getElementById("username").focus();
		return false;
	}
	
	if( "" == document.getElementById("emailAddress").value.trim() ) {
		alert(emailaddress);
	document.getElementById("emailAddress").focus();
		return false;
	}
	if( false == chkEmail()) {
		alert(valid_email);
		document.getElementById("emailAddress").focus();
		return false;
	}
	if( "" == document.getElementById("password").value.trim() ) {
		alert(password);
		document.getElementById("password").focus();
		return false;
	}
	if( 5 > (document.getElementById("password").value.trim()).length ) {
		alert(password_spec);
		document.getElementById("password").focus();
		return false;
	}

	if( "" == document.getElementById("confirm_password").value.trim() ) {
		alert(confirm_password);
		document.getElementById("confirm_password").focus();
		return false;
	}
	
	if( document.getElementById("password").value.trim() != document.getElementById("confirm_password").value.trim() ) {
		alert(match_password);
		document.getElementById("confirm_password").focus();
		return false;
	}
	if (( "" != document.getElementById("dobDay").value) || ( "" != document.getElementById("dobMonth").value) || ( "" != document.getElementById("dobYear").value)){
			if ( "" == document.getElementById("dobDay").value) {
				alert(date_day);
				return false;
			}
			if( "" == document.getElementById("dobMonth").value) {
				alert(date_month);
				return false;
			}
			if( "" == document.getElementById("dobYear").value) {
				alert(date_year);
				return false;
			}
		
			var ageStr = document.getElementById("dobMonth").value + "/" + document.getElementById("dobDay").value+"/"+document.getElementById("dobYear").value;
			
			//if (dobRegxp.test(ageStr) != true){
				
				if(false == chkdate(ageStr)){
				alert(dob);
				return false;
			}
		}

	
	txtNumber=document.getElementById("number").value.trim();
	
	if( "" == txtNumber) {
		alert(numbererr);
		document.getElementById("number").focus();
		return false;
	}
	
}

function deleteImage(id)
{
	var myRand=parseInt(Math.random()*99999999);
	var poststr="myRand="+ myRand + "&imageId="+id;
	var posturl="deleteImage.php";
	var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete:function(response) {
	xmlhttp_deleteImage(response,id); }});
	return false;
}

function xmlhttp_deleteImage(originalRequest, id){
	var retCheck = originalRequest.responseText;
	//alert(retCheck);
	if(retCheck == 1){
		document.getElementById('image'+id).innerHTML = '';
	}
}

function getKarmaHistory(month){
	var monthHistoryBlock = document.getElementById(month);
	var monthImage = document.getElementById('img_'+month);
	if(monthHistoryBlock.style.display=='none'){
		if(document.getElementById('historyEnabledFor'+month).value==1){
			monthHistoryBlock.style.display = '';
			monthImage.src = '/img/karmaHistory_1.png';
		}else{
			monthHistoryBlock.innerHTML="<img src='/img/loading.gif' />";
			monthHistoryBlock.style.display = '';
			var myRand=parseInt(Math.random()*99999999);
			var poststr="myRand="+myRand+"&month="+month;
			var posturl="getKarmaHistory.php";
			var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete:function(response){ xmlhttp_getKarmaHistory(response,month); }});
		}
	}else{
		monthImage.src = '/img/karmaHistory_0.png';
		monthHistoryBlock.style.display = 'none';
	}
	return false;
}

function xmlhttp_getKarmaHistory(originalRequest, month){
	var retCheck = originalRequest.responseText;
	//alert(retCheck);
	if(retCheck){
		var temp=retCheck.split("|||");
		if(temp[0]==1){
			document.getElementById('historyEnabledFor'+month).value = 1;
		}
		var monthBlock = document.getElementById(month);
		monthBlock.innerHTML = temp[1];
		document.getElementById('img_'+month).src = '/img/karmaHistory_1.png';
	}
}

function getMessage(id){
	var message = document.getElementById('message_'+id);
	if(message.style.display == 'none'){
		if(document.getElementById('getContent_'+id).value==0){
			document.getElementById('message_content_'+id).innerHTML="<img src='/img/loading.gif' />";
			var myRand=parseInt(Math.random()*99999999);
			var poststr="myRand="+ myRand + "&msgId="+id;
			var posturl="getMessage.php";
			var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete:function(response) {
			xmlhttp_getMessage(response,id); }});
		}else{
			message.style.display = '';
		}
	}else{
		message.style.display = 'none';
	}
	return false;
}

function xmlhttp_getMessage(originalRequest, id){
	var retCheck = originalRequest.responseText;
	//alert(retCheck);
	if(retCheck){
		var temp=retCheck.split("|||");
		if(temp[0]==1){
			document.getElementById('getContent_'+id).value = 1;
			if(temp[1]==1){
				document.getElementById('msgLink_'+id).style.fontWeight = 'normal';
			}
		}
		document.getElementById('message_content_'+id).innerHTML = temp[2];
		document.getElementById('message_'+id).style.display = '';
	}
}

function selectAllMessages(){
	var select = document.getElementById('selectAll').checked;
	var form = document.getElementById('deleteForm');
	for (var i = 0; i < form.elements.length; i++){    
		eval("form.elements[" + i + "].checked = select");
	}
}

function addPoll(pollId, userid, viewerId)
{

	var form = document.getElementById('pollForm');
	var isChecked = false;
	var checkedId = '';
	for(var i = 0; i< form.elements.length; i++){
		if(form.elements[i].checked == true){
			isChecked = true;
			checkedId = checkedId+form.elements[i].value+'_';
		}
	}
	if(!isChecked){
		alert('Lūdzu atzīmējiet savu atbildi!');
	}else{
		document.getElementById('quiz_in').innerHTML="<img src='/img/loading.gif' />";
		var myRand=parseInt(Math.random()*99999999);
		var poststr="myRand="+ myRand + "&pollId="+pollId+"&checkedId="+checkedId;
		var posturl="/submitPoll.php";
		//alert(poststr);
		var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete:function(response) {
		xmlhttp_submitPoll(response); }});
	}	
	return false;
}

function xmlhttp_submitPoll(originalRequest){
	var retCheck = originalRequest.responseText;
	//alert(retCheck);
	if(retCheck){
		var temp=retCheck.split("|||");
		if(temp[0]==1){
			document.getElementById('quiz_in').innerHTML = temp[1];
		}else{
			document.getElementById('quiz_in').innerHTML = "fail";
		}
	}
}

function changeSelfAdvert(id)
{
	var myRand=parseInt(Math.random()*99999999);
	var poststr="myRand="+myRand+"&articleId="+id;
	var posturl="/changeSelfAdvertStatus.php";
	var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete:function(response) {xmlhttp_changeSelfAdvert(response,id);}});
}

function xmlhttp_changeSelfAdvert(originalRequest, id){
	var retCheck = originalRequest.responseText;
	//alert(retCheck);
	if(retCheck){
		var temp=retCheck.split("|||");
		if(temp[0]==1){
			if(temp[1]==1){
				document.getElementById("sa"+id).innerHTML = "+";
			}else{
				document.getElementById("sa"+id).innerHTML = "-";
			}
		}
	}
}

function changeAdvertArticles(curr, all){
	var next = curr+1;
	if(next>all){
		next = 0;
	}
	document.getElementById('ad'+curr).style.display = 'none';
	document.getElementById('ad'+next).style.display = '';
	setTimeout("changeAdvertArticles("+next+","+all+")", 15000);
}

function checkTag(id){
	if(document.getElementById('tag_'+id).checked==true){
		document.getElementById('tag_'+id).checked = false;
	}else{
		document.getElementById('tag_'+id).checked = true;
	}
}


function prepareResponse(commentId)
{
	document.getElementById('responseOf').value = commentId;
	document.getElementById('comments_description').focus();
	var element = document.getElementById('comments_description');
	ScrollToElement(element);
}

function ScrollToElement(theElement)
{
  var selectedPosX = 0;
  var selectedPosY = 0;
            
  while(theElement != null)
 { 
	selectedPosX += theElement.offsetLeft;
    selectedPosY += theElement.offsetTop;
    theElement = theElement.offsetParent;
  }						  

 window.scrollTo(selectedPosX-100,selectedPosY-100);
}

function showTooltip(text, element, link)
{
	document.getElementById('tooltip_a').innerHTML = text;
	y = findPos(document.getElementById(element));
	h = document.getElementById(element).offsetHeight;
	document.getElementById('tooltip').style.left = 70;
	document.getElementById('tooltip').style.top = y+h+25;
	document.getElementById('tooltip').style.display = "";
	document.getElementById('tooltip_a').href=link;

}

function hideTooltip()
{
	document.getElementById('tooltip').style.display = "none";
}

function showTooltip2(element)
{
	y = findPos(document.getElementById(element));
	h = document.getElementById(element).offsetHeight;
	document.getElementById('tooltip2').style.left = 70;
	document.getElementById('tooltip2').style.top = y+h+15;
	document.getElementById('tooltip2').style.display = "";
}

function checkLink()
{
	var link = document.getElementById("link1").value;
	var res = 0;

	if (link.length > 8)
	{
		var params = "";
		params = "link="+link;
		var myAjax = new Ajax.Request('checkArticleLink.php', {method: 'post', parameters: params, onComplete: function(request) {res = request.responseText; duplicateLinkWarning(res); }});
    }
}

function duplicateLinkWarning(res)
{
	if (res != '0')
	{
		var args = new Array();
		args = res.split('|||');
		duplicateArgs = res; //global var for checkArticleSubmission;
		var id = args[0];
		var subid = args[1];
		var user = args[2];

		if (IsNumeric(id) && IsNumeric(subid) && IsNumeric(user))
		{

			var el = document.getElementById("warning");
			var e2 = document.getElementById("warning2");

		
			var viewArticleTag = '<a target="_blank" style="text-decoration: none" href="'+server+'/articles/'+id+'/1"><font style="color: green; font: normal 10px arial;">apskatīt rakstu</font></a>';
			var commentArticleTag = '<a target="_blank" style="text-decoration: none" href="'+server+'/articles/'+id+'/1#message"><font style="color: green; font: normal 10px arial;">komentēt rakstu</font></a>';

			var tags = "";
			tags = '<font style="color: red; font: normal 10px arial;">Brīdinājums: šāds raksts jau ir pievienots tribine.lv (raksts netiks pievienots!)</font><br>';
			tags = tags+viewArticleTag+' | '+commentArticleTag;
		
			e2.innerHTML = tags;	
			el.style.display = "";

			document.getElementById("isDuplicate").value=id;
		}
	}
	else
	{
		var el = document.getElementById("warning");
		el.style.display = "none";
		document.getElementById("isDuplicate").value='';
	}
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function showApolloHeader()
{
	var element = document.getElementById('apolloHeader');
	ScrollToElement(element); 
}

function loadReadMoreArticles(act, openArticleId)
{
	var callAjax = false;

	if (act == 0) { start = rCurrent; callAjax = true; }
	if (act == 1 && rCurrent+rCount<rMax && rMax>0) { start = rCurrent+rCount; callAjax = true; }
	if (act == -1 && rCurrent>0 ) { start = rCurrent-rCount; callAjax = true; }

	//alert('act='+act+' \nrCurrent='+rCurrent+' \nrCount='+rCount+' \nstart='+start+' \nopenArticleId='+openArticleId);
	//alert('spoki='+document.getElementById('readMoreSpoki').value);
	var spoki = document.getElementById('readMoreSpoki').value;
	if (callAjax)
	{
		document.getElementById('prev').innerHTML = '&nbsp;';
		document.getElementById('next').innerHTML = '&nbsp;';

		var poststr="start="+start+'&count='+rCount+'&openArticleId='+openArticleId;
		var posturl = "";
		if (spoki != 1)
			posturl = "/loadReadMoreArticles.php";
		else
			posturl = "/loadReadMoreArticles_spoki.php";
		//alert(posturl);
		var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete:function(response) {changeReadMoreArticles(response, act, spoki, openArticleId); }, onException: function(req,exception) {alert("The request had a fatal exception thrown.\n\n" +exception)} });
	}
}


function changeReadMoreArticles(originalRequest, act, spoki, openArticleId)
{
	//alert('act='+act+' \nspoki='+spoki+' \nopenArticleId='+openArticleId);
	var response = originalRequest.responseText;
	//alert(response);

	var strArr = response.split('|||||');
	if ((openArticleId != null)&&(strArr[1] >= 1))
	{
		rMax = strArr[1]-1;
	}
	else
	{
		rMax = strArr[1];
	}

	//alert(rMax);
	var content = strArr[0];
	var articles = content.split('||||');
	var len = articles.length;
	var el;
	var i=0;
	var str = '';
	var article = new Array();
	var domain = '';
	var link = '';

	if (spoki == 1) domain = 'http://www.spoki.lv';

	for (i=0; i<5; i++)
	{
		document.getElementById('readMoreItem'+i).style.display='none';
	}

	for (i=0; i<len; i++)
	{
		str = articles[i];
		article = str.split('|||');

		document.getElementById('readMoreImg'+i).src = domain+article[2];
		link = domain+'/articles/'+article[0]+'/1/';
		document.getElementById('readMoreImgLink'+i).href = link;
		el = document.getElementById('readMoreHeading'+i); 
		el.innerHTML = '<a href="'+link+'">'+article[1]+'</a>';
		document.getElementById('readMoreItem'+i).style.display='';
	}

/*
//debugs
alert('rMax='+rMax+'\nact='+act+' \nrCurrent='+rCurrent+' \nrCount='+rCount+' \nopenArticleId='+openArticleId+' \nspoki='+spoki);
*/
	if (act == 1) rCurrent = rCurrent + rCount;
	if (act == -1) rCurrent = rCurrent - rCount;

/*
//debugs
alert('rMax='+rMax+'\nact='+act+' \nrCurrent='+rCurrent+' \nrCount='+rCount+' \nopenArticleId='+openArticleId+' \nspoki='+spoki);
*/

	if (rCurrent == 0)
		document.getElementById('prev').innerHTML = '&nbsp;';
	else
		document.getElementById('prev').innerHTML = '<img id=readMorePrev src="/img/arrowPrev.png" onClick="loadReadMoreArticles(-1, '+openArticleId+')">';
		//document.getElementById('prev').innerHTML = '<img id=readMorePrev src="/img/arrowPrev.png" onClick="loadReadMoreArticles(-1)">';

	if (rCurrent+rCount >= rMax) 
		document.getElementById('next').innerHTML = '&nbsp;';
	else
		document.getElementById('next').innerHTML = '<img id=readMoreNext src="/img/arrowNext.png" onClick="loadReadMoreArticles(1, '+openArticleId+')">';
		//document.getElementById('next').innerHTML = '<img id=readMoreNext src="/img/arrowNext.png" onClick="loadReadMoreArticles(1)">';

	rClicked = false;
}

function addVideoStats(articleId, userId, userIp)
{
	//alert(articleId+' '+userId+' '+userIp);
	var poststr="userId="+ userId + "&userIp="+userIp+ "&articleId="+articleId;
	var posturl="/addVideoStats.php";
	var myAjax = new Ajax.Request(posturl, {method: 'post', parameters: poststr, onComplete:function(response) {  }});
}

function addVideoStats2(articleId, userId, userIp)
{
	//alert(articleId+' '+userId+' '+userIp);
	var poststr="userId="+ userId + "&userIp="+userIp+ "&articleId="+articleId+'&times='+times;
	var posturl="/addVideoStats2.php";
	var myAjax = new Ajax.Request(posturl, {method: 'post', parameters: poststr, onComplete:function(response) {  }});
}

function checkLength()
{
	if (document.getElementById('heading').value.length == 60)
	{
		alert('Maksimālais virsraksta garums sasniegts!');
	}
}

function switchTabs()
{
	if (!manualClick)
	{
		var tab1 = document.getElementById('latestArticles');
		var tab2 = document.getElementById('latestComments');

		if (tab1.className=="news_box_tab_passive") click_tab(tab1.title, false);
		else click_tab(tab2.title,false);

		setTimeout( "switchTabs()", 5000 );
	}
}

function click_tab(mode, manual)
{
	manualClick = manual;
	loadNews(mode);
}

function loadNews(mode)
{
	var posturl='';
	posturl="/"+mode+".txt"; 
	//alert(posturl);
	var myAjax = new Ajax.Request(posturl, {method: 'get', onComplete:function(response) { changeNews(response); }});	
}

function changeNews(response)
{
	var resp = response.responseText;
	//alert(resp);
	var i=0;
	if (resp>'')
	{
		
		var arr = resp.split('|||||');
		if (arr[0] == 'articles')
		{
			document.getElementById('latestArticles').className='news_box_tab_active';
			document.getElementById('latestComments').className='news_box_tab_passive';

			var parent = document.getElementById('news_box_body');
			
			parent.innerHTML = '<div style="width: 260px; height: 10px">&nbsp;</div>';

			var articles = arr[1].split('||||');
			var bg_color = '#fff';
			var html = ''; 
			var len = articles.length;
			for (i=0; i<len; i++)
			{
				
				var str = '';
				var article = articles[i].split('|||');
				if ( (i+2)%2==1) bg_color = '#eee';
				else bg_color = '#fff';

				str = str + '<div class="news_box_item" style="background-color: '+bg_color+'">';
				str = str + '<div style="font-size: 11px; font-weight: bold">';
				str = str + '<a class="simplelink" href="'+article[3]+'">'+article[1]+'<a>';
				str = str + '</div>';
				str = str + '<div style="font-size: 10px; ">';
				str = str + '<a class="simplelink" href="'+article[3]+'">'+article[2]+'<a>';
				str = str + '</div></div>';
				
				html = html+str;
			}
			parent.innerHTML = html;
		}

		if (arr[0] == 'comments')
		{
			document.getElementById('latestArticles').className='news_box_tab_passive';
			document.getElementById('latestComments').className='news_box_tab_active';

			var parent = document.getElementById('news_box_body');
			
			parent.innerHTML = '<div style="width: 260px; height: 10px">&nbsp;</div>';

			var comments = arr[1].split('||||');
			var bg_color = '#fff';
			var html = '';
			for (i=0; i<10; i++)
			{
				var str = '';
				var c = comments[i].split('|||');
				if ( (i+2)%2==1) bg_color = '#eee';
				else bg_color = '#fff';

				str = str + '<div class="news_box_item" style="background-color: '+bg_color+'">';
				
				//str = str + '<div style="font-size: 11px; ">';

				//if (c[1] != 'x') str = str + '<a class="simplelink" style="color: #FFA500" href="/'+c[2]+'">'+c[1]+'</a>';
				//else str = str + '<a class="simplelink">Anonīms</a>';
				
				//str = str + ' par ';
				//str = str + '<a class="simplelink" style="font-weight: bold" href="'+c[6]+'">'+c[4]+'<a>';
				//str = str + '</div>';
				// pirmais vards boldaa
				var temp = c[5];
				var idx = temp.indexOf(' ');
				if (idx>0)
					var res_str = '<b>'+temp.substr(0, idx)+'</b>'+temp.substr(idx, temp.length-idx);
				else
					var res_str = temp;

				str = str + '<div style="font-size: 10px; ">';
				str = str + '<a class="simplelink" style="color: #000;" href="'+c[6]+'#message">'+res_str +'<a>';
				str = str + '</div></div>';
				
				html = html + str;
			}
			parent.innerHTML = html;
		}
	}
}

function switchTabs_2(server)
{
	if (!manualClick_2)
	{
		var tab1 = document.getElementById('authorTab');
		var tab2 = document.getElementById('twitterTab');

		if (tab1.className=="news_box_tab_passive") loadAuthors(server, false);
		else loadTwitter(false);

		setTimeout( "switchTabs_2(server)", 5000 );
	}
}

function loadAuthors(server, manual)
{
	manualClick_2 = manual;
	var posturl="/popularAuthors.txt";
	var myAjax = new Ajax.Request(posturl, {method: 'get', onComplete:function(response) {changeAuthors(response,server); }});
}

function changeAuthors(response,server)
{
	var resp = response.responseText;
	//alert(resp);
	if (server=='http://www.tribine.lv')
		var host = server.split('//www.');
	else
		var host = server.split('//');
	if (resp>'')
	{
		document.getElementById('authorTab').className='news_box_tab_active';
		document.getElementById('twitterTab').className='news_box_tab_passive';
		
		var arr = resp.split('||||');

		var parent = document.getElementById('author_box_body');
		//parent.innerHTML = '<div style="width: 260px; height: 15px">&nbsp;</div>';
			
		var html = ''; 
		var len = arr.length;
		var bg_color = '#fff';

		for (i=0; i<len; i++)
		{	
			var str = '';
			var user = arr[i].split('|||');
			if ( (i+2)%2==1) bg_color = '#eee';
			else bg_color = '#fff';
			
			str = str + '<div class="author_box_item" style="background-color: '+bg_color+'">';
			str = str + '<div class="author_box_item_1">';
			str = str + '<a class="simplelink" href="http://'+user[2]+'.'+host[1]+'">'+user[1]+'<a>';
			str = str + '</div>';
			str = str + '<div class="author_box_item_2">';
			str = str + '<a class="simplelink" >'+user[0]+'<a>';
			str = str + '</div>';
			str = str + '</div>';
				
			html = html+str;
		}
		parent.innerHTML = html;	
	}
}

function loadTwitter(manual)
{
	manualClick_2 = manual;
	var posturl="/twitter.txt"; 
	var myAjax = new Ajax.Request(posturl, {method: 'get', onComplete:function(response) {changeTwitter(response); }});
}

function changeTwitter(response)
{
	var resp = response.responseText;
	//alert(resp);

	if (resp>'')
	{
		document.getElementById('authorTab').className='news_box_tab_passive';
		document.getElementById('twitterTab').className='news_box_tab_active';
	
		var arr = resp.split('||||');

		var parent = document.getElementById('author_box_body');
			
		var html = ''; 
		var len = arr.length;

		for (i=0; i<len; i++)
		{	
			var str = '';
			var item = arr[i].split('|||');
			if ( (i+2)%2==1) bg_color = '#eee';
			else bg_color = '#fff';

			str = str + '<div class="twitter_box_item" onMouseOver="this.style.textDecoration=\'underline\'" onMouseOut="this.style.textDecoration=\'none\'" onClick="window.location.href=\'http://twitter.com/'+item[1]+'\'" style="background-color: '+bg_color+'; overflow: hidden; cursor: pointer">';

			str = str + '<div class="twit_img">';
			str = str + '<img border=0 src="'+item[2]+'" width=36 height=36>';
			str = str + '</div>';
			str = str + '<div class="twit_text">';
			str = str + item[0];
			str = str + '</div>';

			str = str + '</div>';
				
			html = html+str;
		}
		parent.innerHTML = html;	
	}
}

function keywordHint(element)
{
	var str = document.getElementById(element).value;
	var arr = str.split(',');
	var word = arr[arr.length-1];

	//document.getElementById('keywordHint').innerHTML = word;
	if (word.length>=3)
	{
		var poststr="word="+word;
		var posturl="/getKeywordHint.php"; 
		var myAjax = new Ajax.Request(posturl, {method: 'post', parameters: poststr, onComplete:function(response) { changeHint(response); }});
	}
}

function changeHint(response)
{
	var resp = response.responseText;
	document.getElementById('keywordHint').innerHTML = resp;
}

function confirm_link(msg, link)
{
	input_box=confirm(msg);
	if (input_box==true)
	{ 
		document.location.href=link;
	}
}

function execute_nrlt_advert()
{
	//var el = document.getElementById('nrlt_advert');
	//el.style.background = 'url(/img/frame_160x150.png) 0 0 no-repeat';
	//el.style.width = '160px';
	//el.style.height = '150px';
	//el.style.margin = '5px 0 10px 5px';
	//el.style.padding = '1px';
	
	//el.innerHTML = '<div style="width: 150px; height: 115px; margin: 20px 5px 0 5px" >';
		 
	var str='if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Teksta_reklama.160x150_kreisa" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=3WQ20aOHgi.hQ7rZqsm8PNJh.A1PcvCgVGGWLHVZtDz.N7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
	eval(str);

	//str='</div><div style="width: 100px; height: 10px; cursor: pointer;" onClick="window.location.href=\'http://www.nerealitate.lv/faili/teksta_reklamas_nrlt.pdf\'">&nbsp;</div>';
	//el.innerHTML = el.innerHTML+str;
	
}

function execute_advert(num)
{
	// lai nav 2 lentas
	if (num==6 && loadedAdverts[12]) return false;

	var str = '';
	switch(num)
	{
	
	case 1: //nrlt txt adverts kreisajaa malaa
		//str='if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Teksta_reklama.160x150_kreisa" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=3WQ20aOHgi.hQ7rZqsm8PNJh.A1PcvCgVGGWLHVZtDz.N7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 2: // virs tornja
		//str = 'if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Visas_lapas.Kreisas_malas_160x90" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=PnLnwAXnd593lOue3H00TmTUnqtNsXrTMjs40gxfCRf.A7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 3: // tornis
		//str = 'if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Visas_lapas.Kreisas_malas_tornis_160x600" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=iGWVDr3Z7Qd2NCpz27cTVunXsJr70YLqgKGgIcLs.dr.D7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 4: // vidus augsha
		//str= 'if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Visas_lapas.Vidus_augsa_468x60" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=0xUMV5o.7djBaV42q718YsJsHc9P.EcMHkN1YY.1Yyv.E7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 5: // peldoshais
		//str = 'if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Visas_lapas.Vidus_uzzinam_aizveramais_180x150" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=rf.suaYoLTpcM53ZGIqCKPy9jOU_jpCvRYymwQJ6tJr.U7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 6: // lenta citaas lapaas ne pirmajaa
		//str = 'if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Visas_lapas.Apaksa_800x35" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=K07n9yead97Jw1E46hZoZ.dSz33QI4_YR3KkTuuciqn.s7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 7: // karogs labas malas augshaa
		//str = 'if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Visas_lapas.Labas_malas_karogs_250x35(250x265)" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=aRvnNXEQd6ht1VFWMxZoI78KsUxJCOcug726FYlpKwP.R7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 8:  // laba mala apaksha
		//str = 'if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Visas_lapas.Labas_malas_275x175" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=QF1bjevE_NNk3EkuLtoK81QtguGyvx4B0gDg9Iezduz.A7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 9: // raksta lapaa
		//str = 'if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Zinas.Zem zinam_500x130" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=jBflvBCiLToKy8oEZgbj10DRP2a3oO847CPRjEirfuv.17/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 10: //giga
		str='if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Visas_lapas.GIGA_980x90" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=ZldL5hN4m.EQjiLv.VKnHMoQImIr0F4cjY79HoqIZcP.17/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 11: // labas malas reklama ieksh raamja
		str='if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Visas_lapas.Labas_malas_180x150" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=d988.orE3eE1NePqhWjQ1QP2EwuSN.Iy75EW1X7_KMP.M7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 12: // lenta tikai pirmajaa lapaa
		//str = 'if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Visas_lapas.Apaksa_800x35_tikai1_lapa" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=YZDlld8eLTG.KMo7edtDGjamoUhVp0d1tCWNWXShd8..U7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	case 13: // tas pac kas 4, tikai meitenju daljaa
		//str = 'if(location.protocol.substr(0,4)==\'http\')document.write(unescape(\'%3C\')+\'script id="Nerealitate_lv.Tribine.Meitenu_sadala.Vidus_468x60" src="\'+location.protocol+\'//nerealitatelv.adocean.pl/_\'+(new Date()).getTime()+\'/ad.js?id=MIvmBfvV0lx_WZ.xZm6Y5vGy8v5H_7ICVfQpqH5.tLv.m7/x=\'+screen.width+\'/y=\'+screen.height+\'" type="text/javascript"\'+unescape(\'%3E%3C\')+\'/script\'+unescape(\'%3E\'));';
		break;
	}
	loadedAdverts[num] = true;

	if (str) eval(str);
}

function loadDate()
{
	var curdate = new Date();
	var date = curdate.getDate();
	var month = curdate.getMonth()+1;
	var year = curdate.getFullYear();

	date = date.toString();
	var str = '';
	if (date.length==1) date = '0'+date;
	str = str + '<img width="18" height="18" class="date" src="/img/numbers/'+date.charAt(0)+'.png" border="0" />';
	str = str + '<img width="18" height="18" class="date" src="/img/numbers/'+date.charAt(1)+'.png" border="0" />';
	str = str + '<img width="6" height="6" class="dot" src="/img/numbers/dot.png" />';
	
	month = month.toString();
	if (month.length==1) month = '0'+month;

	str = str + '<img width="18" height="18" class="month" src="/img/numbers/'+month.charAt(0)+'.png" border="0" />';
	str = str + '<img width="18" height="18" class="month" src="/img/numbers/'+month.charAt(1)+'.png" border="0" />';
	str = str + '<img width="6" height="6" class="dot" src="/img/numbers/dot.png"/>';

	year = year.toString();

	str = str + '<img width="18" height="18" class="year" src="/img/numbers/'+year.charAt(0)+'.png" border="0" />';
	str = str + '<img width="18" height="18" class="year" src="/img/numbers/'+year.charAt(1)+'.png" border="0" />';
	str = str + '<img width="18" height="18" class="year" src="/img/numbers/'+year.charAt(2)+'.png" border="0" />';
	str = str + '<img width="18" height="18" class="year" src="/img/numbers/'+year.charAt(3)+'.png" border="0" />';

	document.getElementById('date_span').innerHTML = str;
}

function load_eshops(server)
{
	var myRand=parseInt(Math.random()*99999999);
	var poststr="myRand="+ myRand;

	var posturl="/leftData.txt";
	var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete:function(response) {
		write_eshops(response, server);
		}});

}

function write_eshops(originalRequest, server)
{
	var str = '';
	var response = originalRequest.responseText;

	var respArr = response.split('|||||');
	var eshops = respArr[3];
	//var izp = respArr[8];


	// eshops modulis
	str = '';
	var shop = eshops.split('||||');

	var len = shop.length;
	var i=0;
	str = str + '<div class="eshop_body">';
	str = str + '<table class="eshop_table" cellPadding="0" cellSpacing="0">';

	for(i=0;i<len;i++)
	{
		var s = shop[i].split('|||');
		//alert(i+' '+i%2);
		if (i%2==0) str = str + '<tr>';
		str = str + '<td><a target="_blank" id="eshop_'+i+'" href="http://www.spoki.lv/eshop.php?id='+s[0]+'">'+s[1]+'</a></td>';
		if (i%2==1) str = str + '</tr>';
		//alert(str);
		e_shops[i] = new Array(2);
		e_shops[i][0] = s[1];
		e_shops[i][1] = s[2];
		
	}

	str = str + '</table><div class="clearer"></div></div>';

	document.getElementById('eshops_body').innerHTML = str;
	/*	
	str = '';
	var izpardosanas = izp.split('||||');
	if (izpardosanas != '---')
	{
		var len = izpardosanas.length;
		var i=0;

		for(i=0;i<len;i++)
		{
			var iz = izpardosanas[i].split('|||');

			str = str + '<div style="background-color: #F1F1E0; width: 270px; margin: 0 0 5px 1px; font-family: verdana">';
			str = str + '<div style="float: left; text-align: center; width: 100px; margin-top: 5px; ">';
			str = str + '<a href="http://www.spoki.lv/eshop.php?id='+iz[3]+'&link='+iz[1]+'">';
			str = str + '<img width=58 height=43 src="'+iz[2]+'" style="border: 1px solid #DEDEDE">';
			str = str + '</a>';
			str = str + '</div>';
			str = str + '<div style="float: left; width: 165px; margin-top: 5px; font-size: 10px">';
			str = str + '<a style="color: #333333; text-decoration: none;" href="http://www.spoki.lv/eshop.php?id='+iz[3]+'&link='+iz[1]+'">'+iz[0]+'</a><br>';
			str = str + '<a style="color: #0377B1; text-decoration: none;" href="http://www.spoki.lv/eshop.php?id='+iz[3]+'&link='+iz[1]+'">izpardosanas.lv</a>';
			str = str + '</div>';
			str = str + '<div style="clear:both"></div>';
			str = str + '</div>';

		}

		//document.getElementById('eshop_box_body').style.paddingBottom = '5px';
		document.getElementById('eshops_body').innerHTML = document.getElementById('eshops_body').innerHTML + str;
		//document.getElementById('mod_izp').style.display='';
		
	}*/

	setTimeout('change_eshops(1)', 10000);
}

function change_eshops(x)
{
	var len = e_shops.length;
	//alert(len);
	var i =0;
	for (i=0; i<len; i++)
	{
		document.getElementById('eshop_'+i).innerHTML = e_shops[i][x];
	}

	setTimeout('change_eshops('+((x+1)%2)+')', 10000);
}

function setOldMethod()
{
	document.getElementById('image_loader_new').style.display = 'none';
	document.getElementById('image_loader_old').style.display = '';
	document.getElementById('upload_method').value='old';
}

function DraugiemSay( title, url, titlePrefix ){
 window.open(
  'http://www.draugiem.lv/say/ext/add.php?title=' + encodeURIComponent( title ) +
  '&link=' + encodeURIComponent( url ) +
  ( titlePrefix ? '&titlePrefix=' + encodeURIComponent( titlePrefix ) : '' ),
  '',
  'location=1,status=1,scrollbars=0,resizable=0,width=530,height=400'
 );
 return false;
}

function drawDaySelect()
{
	var str = '';
	var i=0;
	str = '<select name="day" style="width: 90px;">';
	str = str + '<option value="1">1</option>';
	for (i=2; i<=31; i++)
		str = str + '<option value="'+i+'">'+i+'</option>';
	str = str + '</select>';

	document.write(str);
}

function drawMonthSelect()
{
	var mon_arr = Array();
	mon_arr[1] = 'Janvāris';
	mon_arr[2] = 'Februāris';
	mon_arr[3] = 'Marts';
	mon_arr[4] = 'Aprīlis';
	mon_arr[5] = 'Maijs';
	mon_arr[6] = 'Jūnijs';
	mon_arr[7] = 'Jūlijs';
	mon_arr[8] = 'Augusts';
	mon_arr[9] = 'Septembris';
	mon_arr[10] = 'Oktobris';
	mon_arr[11] = 'Novembris';
	mon_arr[12] = 'Decembris';

	var str = '';
	var i=0;
	str = '<select name="month" style="width: 90px;">';
	str = str + '<option value="1">Janvāris</option>';
	for (i=2; i<=12; i++)
		str = str + '<option value="'+i+'">'+mon_arr[i]+'</option>';
	str = str + '</select>';

	document.write(str);
}

function drawYearSelect()
{
	var str = '';
	var i=0;
	str = '<select name="year" style="width: 90px;">';
	str = str + '<option value="2009">2009</option>';
	for (i=2008; i>=1909; i--)
		str = str + '<option value="'+i+'">'+i+'</option>';
	str = str + '</select>';

	document.write(str);
}

function add_cw_vote_comments(userExists, commentId, cwId, voteType1, submitterId){
	var voteCwId = cwId;
	var voteType = voteType1;
	var voterId = 0;
	voterId = readCookie('voteId');
	if(voterId == null){
		voterId = genId();
		createCookie('voteId', voterId, 365);
	}
	if(userExists==submitterId && submitterId>0){
		alert('Nevar balsot par savu komentāru');
		return false;
	}else{
		var myRand=parseInt(Math.random()*99999999);
		var poststr="myRand="+ myRand + "&voterId="+voterId+ "&cwId="+encodeURI(voteCwId)+"&voteType=" +voteType+"&commentId=" + commentId +"&submitterId=" + submitterId;
		var posturl="/votesCwComments.php";
		var myAjax = new Ajax.Request(posturl, {method: 'get', parameters: poststr, onComplete:function(response) {
			xmlhttpChangeVoteComment(response,commentId, voteType);
				}});
	}
}

function quiz_check()
{
	var i = 0;
	var msg = '';
	var type = document.getElementById('q_type').value;
	if (type=='1')
	{
		if (document.getElementById('q_email').value == '' || document.getElementById('q_username').value == '' || document.getElementById('q_region').value == '' || document.getElementById('q_phone').value == '' || (document.getElementById('q_bibName').value == '' && document.getElementById('q_group_2').checked) )
			msg = 'Jāaizpilda visi lauki!';
	}
	if (type=='2')
	{
		for (i=1; i<=20; i++)
		{
			var elems = document.getElementsByName('q_'+i);
			if ( (elems[0].checked == false && elems[1].checked == false && elems[2].checked == false && i<20) || (i==20 && elems[0].value==''))
			{
				msg = 'Nav izvēlēta atbilde uz '+i+'. jautājumu';
				break;
			}
		}
	}

	if (msg=='') document.quiz_form.submit();
	else alert(msg);
}