function ajaxFunction(a,b)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  

xmlhttp.onreadystatechange=function()
{

if(xmlhttp.readyState==1)
  {
document.getElementById(b).innerHTML='<div style="margin:100px 0 0 200px"><img src="images1/loader.gif" alt="loading" /></div>';
  }

if(xmlhttp.readyState==4)
  {
document.getElementById(b).innerHTML=xmlhttp.responseText;
  }
 
}
xmlhttp.open("GET",a,true);
xmlhttp.send(null);
}  
//ajax end

function printals()  {

document.getElementById("print_title").innerHTML = document.form_add.title.value;


}
function printals_d()  {

document.getElementById("print_text").innerHTML = document.form_add.descr.value;


}

function printals_u()  {

document.getElementById("print_url").innerHTML = document.form_add.url.value;


}
//popup
function popup_w()
{
window.open("inc/recip.php", "myWindow", "dependent=1, status=0, height=640, width=600, left=350");

}

function popup_p()
{
window.open("inc/cond_p.php", "myWindow", "dependent=1, status=0, height=640, width=600, left=350");

}

function popup_re()
{
window.open("inc/cond_re.php", "myWindow", "dependent=1, status=0, height=640, width=600, left=350");

}

//

function changes(a,b){

document.getElementById(b).style.display="block";
//firefox
document.getElementById(b).style.opacity=0.2;
document.getElementById(a).style.opacity=1;

document.getElementById(a).style.display="block";
//ie
document.getElementById(b).style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+20+")";
document.getElementById(a).style.filter ="progid:DXImageTransform.Microsoft.Alpha(opacity="+100+")";

}
//

function disap(a,b){
	document.getElementById(a).style.display="none";
	document.getElementById(b).style.display="none";
	value=0;
	ivalue=0;
	fvalue=0;
	ifvalue=0;
}

//city dropdown
function show_drop_menu(){
document.getElementById('drop_absol').style.visibility="visible";
}
function hide_drop_menu(){
document.getElementById('drop_absol').style.visibility="hidden";
}
//url redirection
function go_to(newloc){
this.location.href = newloc;
}

//autocomplete
function showHint(str)
{
if (str.length==0)
  {
  document.getElementById("txtHint").innerHTML="";
  document.getElementById("txtHint").style.visibility="hidden";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200 && xmlhttp.responseText != "no suggestion")
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
	document.getElementById("txtHint").style.visibility="visible";
    }

  }
  
xmlhttp.open("POST","auto_complete.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("q="+str);
}

function acomplete(a)
{
	document.getElementById("q").value=a;
	document.getElementById("txtHint").style.visibility="hidden";
}
function closea()
{
document.getElementById("txtHint").style.visibility="hidden";
}

$('document').ready(function(){

  $('body').click(function(){
  if(document.getElementById("txtHint").style.visibility == 'visible')
   document.getElementById("txtHint").style.visibility="hidden";
  });
  
});

//news & sport white
function b_white(a,b)
{
document.getElementById(a).style.background = "white";
document.getElementById(b).style.background = "";
}
//quick view section
function univ_show(a,b)                      // show content
{
document.getElementById(a).style.visibility="visible";
document.getElementById(a).innerHTML = b;
}
function show_op()
{
document.getElementById("op").style.visibility="visible";
}

function univ_close(a)                       // close content
{
document.getElementById(a).innerHTML = "";
}
function close_qop(a)
{
document.getElementById(a).style.visibility="hidden";
document.getElementById(a).innerHTML = "";
}



//ajax for open quick view
function open_quick(a,b,dest_id)
{
var xmlhttp;
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  

xmlhttp.onreadystatechange=function()
{

if(xmlhttp.readyState==1)
  {
document.getElementById('quick_view').innerHTML='<div style="margin:100px 0 0 200px"><img src="images1/loader.gif" alt="loading" /></div>';
  }

if(xmlhttp.readyState==4)
  {
document.getElementById(b).innerHTML=xmlhttp.responseText;
  }
 
}
xmlhttp.open("POST",a,true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send('dest_id='+dest_id);
}  
//ajax end
//quick view end

//old bookmark script
function bookmark()
{
var BookmarkURL="http://na4alo.com/"
var BookmarkTitle="www.na4alo.com"
if (document.all)
{
window.external.AddFavorite(BookmarkURL,BookmarkTitle);
}
else
{
window.sidebar.addPanel(BookmarkTitle, BookmarkURL, '');
}
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function show(a)
{
document.getElementById(a).style.display='inline';
}
//return back
function go_back()
{
window.history.back();
return false;
}

//start VALIDATION FUNCTIONS
//registration form
function valExcl(text)
{
var excl = /[*#$\|!\^%&,]+/i;

return !(excl.test(text));
}

function valName(n, name, c)
{
  var form = document.form1;
  var nameVal = form.elements[n];

  if(nameVal.value.length > c && valExcl(nameVal.value) == true){
	document.getElementById(name).innerHTML = '&nbsp;<img src="images/ok_tick.png" />';
  } 
  else
  {
    document.getElementById(name).innerHTML = '<span class="red_star">&nbsp;*</span>';
  }
}

function valPass()
{
  var form = document.form1;
  var reVal = 'reVal';
  if(form.repswrd.value == form.pswrd.value)
  {
    document.getElementById(reVal).innerHTML = '&nbsp;<img src="images/ok_tick.png" />';
  }else{
    document.getElementById(reVal).innerHTML = '<span class="red_star">&nbsp;*</span>';
  }
}

function rulesVal()
{
  var form = document.form1;
  if(form.cond.checked == true)
  {
    document.getElementById('condVal').innerHTML = '&nbsp;<img src="images/ok_tick.png" />';
  }else{
    document.getElementById('condVal').innerHTML = '<span class="red_star">&nbsp;*</span>';
  }

}

function emailVal()
{
  var form = document.form1;

  var myReg = /^[\w\.\-]+@[a-z\d\.\-]+\.[a-z\d]+$/gi; //name@mail.com
  
  if(myReg.test(form.email.value) == true)
  {
    document.getElementById('mVal').innerHTML = '&nbsp;<img src="images/ok_tick.png" />';
  }else if(myReg.test(form.email.value) == false){
    document.getElementById('mVal').innerHTML = '<span class="red_star">&nbsp;*</span>';
  }

}

//add site form
function addrVal(name_div, c)
{
  var form = document.form_add;

  if(form.addr.value.length > c)
  {
    document.getElementById(name_div).innerHTML = '&nbsp;<img src="images/ok_tick.png">';
  }
  else
  {
    document.getElementById(name_div).innerHTML = '';
  }
}

function titleVal(name_div, c)
{
  var form = document.form_add;

  if(form.title.value.length > c)
  {
    document.getElementById(name_div).innerHTML = '&nbsp;<img src="images/ok_tick.png">';
  }
  else
  {
    document.getElementById(name_div).innerHTML = '';
  }
}

function textVal(name_div, c)
{
  var form = document.form_add;

  if(form.descr.value.length > c)
  {
    document.getElementById(name_div).innerHTML = '&nbsp;<img src="images/ok_tick.png">';
  }
  else
  {
    document.getElementById(name_div).innerHTML = '';
  }
}

function add_url()
{
  var form = document.form_add;
  var tries = /^[\w\.\-\/:]+\.[a-z\d\/]+$/i;
  var patt = /^http:\/\//i;
  
  if(tries.test(form.url.value) == true)
  {
    document.getElementById('urlVal').innerHTML = '&nbsp;<img src="images/ok_tick.png" />';
	
	 if(patt.test(form.url.value) == false)
     {
       form.url.value = 'http://' + form.url.value;
     }
  }
  else
  {
    document.getElementById('urlVal').innerHTML = '';
  }
}

function selectVal()
{
  var form = document.form_add;
if(form.categ.value != '')
  {
    document.getElementById('selVal').innerHTML = '&nbsp;<img src="images/ok_tick.png">';
  }
  else
  {
    document.getElementById('selVal').innerHTML = '';
  } 

}


function rulesVal2()
{
  var form = document.form_add;
  if(form.conditions.checked == true)
  {
    document.getElementById('condVal').innerHTML = '&nbsp;<img src="images/ok_tick.png" />';
  }else{
    document.getElementById('condVal').innerHTML = '';
  }

}

//add offer form

function offerCat()
{
 var form = document.form_offer;
 if(form.categ.value != '')
 {
  document.getElementById('selVal').innerHTML = '&nbsp;<img src="images/ok_tick.png">';
 }
 else
 {
  document.getElementById('selVal').innerHTML = '';
 }

}

function ofTitleVal(c)
{
  var form = document.form_offer;

  if(form.title.value.length > c)
  {
    document.getElementById('titleVal').innerHTML = '&nbsp;<img src="images/ok_tick.png">';
  }
  else
  {
    document.getElementById('titleVal').innerHTML = '';
  }
}

function ofTextVal(c)
{
  var form = document.form_offer;

  if(form.desc.value.length > c)
  {
    document.getElementById('textVal').innerHTML = '&nbsp;<img src="images/ok_tick.png">';
  }
  else
  {
    document.getElementById('textVal').innerHTML = '';
  }
}

function priceVal()
{
 var form = document.form_offer;
 if(!isNaN(form.price.value) && form.price.value != '')
  {
    document.getElementById('priceVal').innerHTML = '&nbsp;<img src="images/ok_tick.png">';
  }
  else
  {
    document.getElementById('priceVal').innerHTML = '';
  }

}

function reducedVal()
{
 var form = document.form_offer;
 if(!isNaN(form.reduced.value) && form.reduced.value != '')
  {
    document.getElementById('reducedVal').innerHTML = '&nbsp;<img src="images/ok_tick.png">';
  }
  else
  {
    document.getElementById('reducedVal').innerHTML = '';
  }

}

function offerCalc(a,b,c)
{
 var price = a.value;
 var reduced = b.value;
 if(isNaN(price) == false && isNaN(reduced) == false && price != '' && reduced != '')
 {
  var endPrice = (parseFloat(price) / 100) * (100 - parseInt(reduced))
  document.getElementById(c).innerHTML = '<b>Цена на офертата: <span style="color:red">'+endPrice.toFixed(2)+' лв.</span></b>';
 }else
 {
 document.getElementById(c).innerHTML = '';
 }
 
}

function ofCondVal()
{
 var form = document.form_offer;
 if(form.rules.checked == true)
 {
  document.getElementById('condVal').innerHTML = '&nbsp;<img src="images/ok_tick.png">';
 }
 else
 {
  document.getElementById('condVal').innerHTML = '';
 }
}

function ofValPhone(a, b)
{

 var myReg = /[^\d\s]+/;

 if(myReg.test(a.value) != true && a.value.length >= 3)
 {
  document.getElementById(b).innerHTML = '&nbsp;<img src="images/ok_tick.png">';
 }
 else
 {
  document.getElementById(b).innerHTML = '';
 }

}

function of_add_url(a, b)
{
  var form = document.form_offer;
  var tries = /^[\w\.\-\/:]+\.[a-z\d\/]+$/i;
  var patt = /^http:\/\//i;
  
  if(tries.test(a.value) == true)
  {
    document.getElementById(b).innerHTML = '&nbsp;<img src="images/ok_tick.png" />';
	
	 if(patt.test(a.value) == false)
     {
       a.value = 'http://' + a.value;
     }
  }
  else
  {
    document.getElementById(b).innerHTML = '';
  }
}

function ofAddr(c)
{
  var form = document.form_offer;

  if(form.addr.value.length > c)
  {
    document.getElementById('urlAdr').innerHTML = '&nbsp;<img src="images/ok_tick.png">';
  }
  else
  {
    document.getElementById('urlAdr').innerHTML = '';
  }

}

function valNumChar(a,b,c)
{

  if(a.value.length > c)
  {
    document.getElementById(b).innerHTML = '&nbsp;<img src="images/ok_tick.png">';
  }
  else
  {
    document.getElementById(b).innerHTML = '';
  }

}

function endDate() 
{
  var ret = true;
  var mySelect = document.form_offer.end_year;
  
  if(mySelect.options[mySelect.selectedIndex].value == '')
  {
   alert('Трябва да попълните валидна дата на офертата');
   document.getElementById('valYear').innerHTML = '<span class="red_star">&nbsp;***</span>';
   mySelect.focus();
   ret = false;
  }
 return ret;
}
  
//COOKIES FUNCTIONS

function createCookie(cookieName, cookieValue, cookieExpires, cookiePath)
{
 cookieValue = escape(cookieValue);

 if(cookieExpires == '')
 {
  var nowDate = new Date();
  nowDate.setMonth(nowDate.getMonth() + 6);
  cookieExpires = ';expires=' + nowDate.toGMTString();
 }
 
 if(cookiePath != '')
 {
  cookiePath = ';Path=' + cookiePath;
 }

 document.cookie = cookieName + '=' + cookieValue + cookieExpires + cookiePath;
 
 return false;

}



function getCookie(cookieName)
{
 var cookieValue = document.cookie;

 var cookieStart = cookieValue.indexOf(' ' + cookieName + '='); //checks the middle cookies

 if(cookieStart == -1)
 {
   cookieStart = cookieValue.indexOf(cookieName + '='); // checks in the beggining
   
 }   
   
 if(cookieStart == -1) 
 {
   cookieValue = null; // no cookie with such name
 }
 else
 {
   cookieStart = cookieValue.indexOf('=', cookieStart) + 1;
   var cookieEnd = cookieValue.indexOf(';', cookieStart); // if cooke it is not the last cookie
   
   if(cookieEnd == -1) 
   {
    cookieEnd = cookieValue.length; //if it is the last cookie and no ; after that
   }
   
   cookieValue = unescape(cookieValue.substring(cookieStart, cookieEnd));
 
 }

 return cookieValue;

}  

function upendCookie(cookieName, cookieValue, cookieExpires, cookiePath)
{
 var num = 0;
 var divid = cookieValue;
 
 if(getCookie(cookieName) != null)
 {
  var rep = getCookie(cookieName).split(';'); //escape repetitions
  var kValue = cookieValue;
  var i;

  for(i=0; i<rep.length; i++)
  { 
   if(rep[i] != kValue)
   {
    cookieValue += ';' + rep[i]; 
   }
  }
  
 }


 createCookie(cookieName, cookieValue, cookieExpires, cookiePath);
 
 document.getElementById(divid).innerHTML = '<a href="saved.php"><span style="color:#C21818">Запазен</span></a> <img src="images/pin_point.png" alt="" />';
 
 var saved = savedNum() + num;
 document.getElementById('saved').innerHTML = 'Запазени(' + saved + ')';
 
 num++;
}

//link name
function saveText(cookName, cookVal)
{
 var isSet = false;
 
 if(getCookie(cookName) != null){
  var rep = getCookie(cookName).split(';');
  
  for(i=0; i<rep.length; i++)
  { 
   if(rep[i] == cookVal)
   {
     isSet = true;
	 break;
   }
  }
  
 }

 var ret;

 if(isSet == false)
 {
  ret = 'Запази';
 }
 else
 {
  ret = '<a href="saved.php"><span style="color:#C21818">Запазен</span></a> <img src="images/pin_point.png" alt="" />';
 }
 
 document.write(ret);
}

//show number of saved
function savedNum()
{
  var n = 0;
  
  if(getCookie('saved') != null){
  var rep = getCookie('saved').split(';');
    
    for(i=0; i<rep.length; i++)
    { 
     n++;
    }
    
  }
  
  return n;
  
}

function removeCook(cookieName, substractValue, cookieExpires, cookiePath)
{
 var divid = 'saved_list' + substractValue;
 
 var newValue = '';
 
 if(getCookie(cookieName) != null)
 {
  var rep = getCookie(cookieName).split(';'); //escape repetitions
  var i;

  for(i=0; i<rep.length; i++)
  { 
   if(rep[i] != substractValue)
   {
    newValue +=  rep[i] + ';'; 
   }
  }
  
  newValue = newValue.substring(0, newValue.length-1);
  
  document.getElementById(divid).style.display = 'none';
  
  	 var saved = savedNum() - 1;
	 document.getElementById('saved').innerHTML = 'Запазени(' + saved + ')';
	 document.getElementById('saved_no').innerHTML = '&nbsp;&nbsp;Запазени сайтове:&nbsp;&nbsp;<b>' + saved + '</b>';
  
    if(newValue != '')
    {
     //alert(newValue.substring(0, newValue.length-1));
     createCookie(cookieName, newValue, cookieExpires, cookiePath);
 

 
    }
	else
	{
	  createCookie(cookieName, newValue, ';expires=mon, 30 Jan 1980', cookiePath);
	  //document.getElementById('saved').innerHTML = 'Запазени(0)';
	}

 }

}

//banner_rotator
var tsec = 4000;
var bannTimerID = setInterval('bR.change(0)', tsec);

$(function(){		
		$('#photoShow a').attr('target', '_blank');		
});

bR = {
	change: function(selimg){
		var currentImage = $('#photoShow div.current');
		
		var back = currentImage.prev();
		var ahead = currentImage.next();
		var nextImage;
		
		var currentNum = $('#cnum'); //numbers
		var nextNum;
		
		//controllers - back and fwd
		if(selimg == 0 || selimg == 2){
		  nextImage = currentImage.next();
		  nextNum = currentNum.next();
		}else if(selimg == 1){
		  nextImage = currentImage.prev();
		  nextNum = currentNum.prev();
		  clearInterval(bannTimerID);
		  $('#ctrls').html('<img src="images/play.png" alt="" border="0" />');
		}
		
		if(selimg == 2){
		  clearInterval(bannTimerID);	
		  $('#ctrls').html('<img src="images/play.png" alt="" border="0" />');
		}
		
		//if last or first element loop numbers
		if(nextNum.length == 0 && selimg != 1){
		  nextNum = $('#numbs span:first');
		}else if(nextNum.length == 0 && selimg == 1){
		  nextNum = $('#numbs span:last');
		}
		//if last or first element loop elements
		if(nextImage.length == 0 && selimg != 1){
		  nextImage = $('#photoShow div:first');
		}else if(nextImage.length == 0 && selimg == 1){
		  nextImage = $('#photoShow div:last');
		}
			  
		currentImage.removeClass('current').addClass('previous');
		nextImage.css({opacity: 0.0}).addClass('current').animate({opacity: 1.0}, 1000, 
		function(){
			currentImage.removeClass('previous');
		});	
		
		nextNum.attr('id', 'cnum');
		currentNum.attr('id', 'nnum');
	
		return false;

	}
//---
}

//result page LISTING effects
$(function(){
 $('TD[class=result_page_urls]').hover(function(){
	 $(this).css('background', '#FFFFCC'); 
	}, function(){
	 $(this).css('background', '#ffffff');
	});

});
