function validate(url, id)
{
	new Ajax.Request(url, {
		parameters: "id=" + id+
					"&value="+$(id).value
					+"&flag=1"
					+"&choose=0"
					+"&reviewAdd=0"
					+"&isRated=0"
					+"&rate=0",
		onSuccess: function(resp) { 
		 var idErr="err"+id;
		 var idTabel=id+"Td";
		 if($(idErr)==null)
		 {
		 if($(idTabel).lastChild.nodeType!=1)
			$(idTabel).lastChild.previousSibling.firstChild.nodeValue="";
		 new Insertion.After(id, '<span id='+idErr+' style="color:red">  '+resp.responseText+'</span>');
		 }
		 else
		 {
			Element.replace(idErr, '<span id='+idErr+' style="color:red">  '+resp.responseText+'</span>');
		 }
		 } 
		 
	});
} 

function rating(url, id, listingId)
{
	var node = document.getElementsByName(id)[0];
	new Ajax.Request(url, {
		parameters: "value=" + node.firstChild.nodeValue+
					+"&listingId=" + listingId
					+"&flag=0"
					+"&choose=0"
					+"&reviewAdd=0"
					+"&isRated=1"
					+"&rate=1",
		onSuccess: function(resp) {
		 var length=node.firstChild.nodeValue;
		 for(i=1; i<=length; i++)
		 {
		  document.getElementsByName("star"+i)[0].setAttribute("class", "post-star-"+i);
		  document.getElementsByName("star"+i)[0].className= "post-star-"+i;
		  document.getElementsByName("star"+i)[0].setAttribute("onclick", "return false;");
		 }
		 for(j=parseInt(length)+1; j<=5; j++)
		 {
		  document.getElementsByName("star"+j)[0].setAttribute("class", "post-down-star-"+j);
		  document.getElementsByName("star"+j)[0].className= "post-down-star-"+j;
		  document.getElementsByName("star"+j)[0].setAttribute("onclick", "return false;");
		 }
		 var temp = new Array();
		 temp = resp.responseText.split(' ');
		 $("currentRatingDiv").innerHTML="";
		 var list=document.createElement("ul");
		 list.className="star-rating";
		 for(i=1; i<=5; i++)
		 {
		  if(i<=parseInt(temp[0])) 
		  { 
		   var item=document.createElement("li");
		   var link = document.createElement("a");
		   link.setAttribute("href", "#");
		   link.className="post-star-"+i;
		   link.setAttribute("onclick", "return false;");
		  }
		  if(i==temp[1] && temp[2]==1)
		  {
		   var item=document.createElement("li");
		   var link = document.createElement("a");
		   link.setAttribute("href", "#");
		   link.className="half-star-"+i;
		   link.setAttribute("onclick", "return false;");
		  }
		  else if((i==temp[1] && temp[2]==0) || i>temp[1])
		  {
		   var item=document.createElement("li");
		   var link = document.createElement("a");
		   link.setAttribute("href", "#");
		   link.className="post-down-star-"+i;
		   link.setAttribute("onclick", "return false;");
		  }
		  item.appendChild(link);
		  list.appendChild(item);
		 }
		 var textNode=document.createElement("h3");
		 textNode.appendChild(document.createTextNode("The current rating is:  "));
		 $("currentRatingDiv").appendChild(textNode);
		 $("currentRatingDiv").appendChild(list);
	    }
	});
} 

function populate(categId, url, id)
{
	new Ajax.Request(url, {
		parameters: "&categId="+categId
		+"&flag=0"
		+"&choose=0"
		+"&reviewAdd=0"
		+"&isRated=0"
		+"&rate=0",
		onSuccess: function(resp) {
		var mesaj="";
		var mesaj2="";
		var idCateg="";
		var searchResults = eval('(' + resp.responseText + ')');
		var nrIteratii=searchResults['categorii']['categ'].length;
		var node=document.createElement("option");
			node.setAttribute("value", 0);
			var textNode=document.createTextNode("Select a category");
			node.appendChild(textNode);
			$(id).appendChild(node);
		
		//$('thirdCateg').innerHTML = ''; 
		//$('thirdCateg').appendChild(node); 
		$('addCateg').disabled=true;
		$(id).innerHTML="";
		
		if($('firstCateg').childNodes[0].nodeType==1 && ($('firstCateg').childNodes[0].getAttribute("value")==0)) 
		 {
		   $('firstCateg').removeChild( $('firstCateg').childNodes[0]);
		 }
		else if($('firstCateg').childNodes[1].nodeType==1 && ($('firstCateg').childNodes[1].getAttribute("value")==0)) 
		 {
		   $('firstCateg').removeChild( $('firstCateg').childNodes[1]);
		 }
		var node=document.createElement("option");
			node.setAttribute("value", 0);
			var textNode=document.createTextNode("Select a category");
			node.appendChild(textNode);
			$(id).appendChild(node);
			
		for (i = 0; i <nrIteratii ; i++)
		{
			var node=document.createElement("option");
			node.setAttribute("value", searchResults['categorii']['categ'][i].id);
			var textNode=document.createTextNode(searchResults['categorii']['categ'][i].name);
			node.appendChild(textNode);
			$(id).appendChild(node);
		}
		}
	});
} 

function populateThird(categId, url, id)
{
		new Ajax.Request(url, {
		parameters: "&categId="+categId
		+"&flag=0"
		+"&choose=0"
		+"&reviewAdd=0"
		+"&isRated=0"
		+"&rate=0",
		onSuccess: function(resp) {
		var mesaj="";
		var idCateg="";
		var searchResults = eval('(' + resp.responseText + ')');
		$(id).innerHTML="";
		if($('secondCateg').childNodes[0].nodeType == 1 && ($('secondCateg').childNodes[0].getAttribute("value")==0) )
		{
		   $('secondCateg').removeChild( $('secondCateg').childNodes[0]);
		}
		
		else if($('secondCateg').childNodes[1].nodeType == 1 && ($('secondCateg').childNodes[1].getAttribute("value")==0))
		{		
		   $('secondCateg').removeChild( $('secondCateg').childNodes[1]);
		}
		var nrIteratii2=searchResults['categorii']['categ2'].length;
		
		var node=document.createElement("option");
			node.setAttribute("value", 0);
			var textNode=document.createTextNode("Select a category");
			node.appendChild(textNode);
			$(id).appendChild(node);
		for (i = 0; i <nrIteratii2 ; i++)
		{
			var node=document.createElement("option");
			node.setAttribute("value", searchResults['categorii']['categ2'][i].id);
			var textNode=document.createTextNode(searchResults['categorii']['categ2'][i].name);
			node.appendChild(textNode);
			$(id).appendChild(node);
		}
		}
	});
} 

function deleteFirst(selectId)
{
 if($(selectId).childNodes[0].nodeType == 1 && ($(selectId).childNodes[0].getAttribute("value")==0))
 {
   $(selectId).removeChild( $(selectId).childNodes[0]);
 }
 
 else if($(selectId).childNodes[1].nodeType == 1 && ($(selectId).childNodes[1].getAttribute("value")==0))
 {
   $(selectId).removeChild( $(selectId).childNodes[1]);
 }
 $('addCateg').disabled=false;
}
		 
function validatePass(url, id, cid)
{
    if($(id).value=='')  
	{
	validate(url, id); 
	return;
	}
	new Ajax.Request(url, {
		parameters: "id=" + id+
					"&cid=" +cid+
					"&value="+$(id).value+
					"&cvalue="+$(cid).value
					+"&reviewAdd=0"
					+"&isRated=0"
					+"&choose=0",
		onSuccess: function(resp) { 
		 var idErr="err"+id;
		 var idTabel=id+"Td";
		 if($(idErr)==null)
		 {
		 if($(idTabel).lastChild.nodeType!=1)
			$(idTabel).lastChild.previousSibling.firstChild.nodeValue="";
			new Insertion.After(id, '<span id='+idErr+' style="color:red">  '+resp.responseText+'</span>');
		 }
		 else
		 {
			Element.replace(idErr, '<span id='+idErr+' style="color:red">  '+resp.responseText+'</span>');
		 }
		 } 
		 
	});
} 


function hideElement()
{
 if( $('thirdCateg')!=null && $('thirdCateg').childNodes[0].nodeType == 1)
 {
  if($('thirdCateg').childNodes[0].getAttribute("value")==0)
  {
	$('addCateg').disabled=true;
  }
  else if($('addCateg')!=null) 
  {
   $('addCateg').disabled=false;
  }
  
 }
  else if($('thirdCateg')!=null &&  $('thirdCateg').childNodes[1].nodeType == 1)
  {
   if($('thirdCateg').childNodes[1].getAttribute("value")==0)
  {
	$('addCateg').disabled=true;
  }
   else if($('addCateg')!=null) 
  {
   $('addCateg').disabled=false;
  }
 }
 
 if($('selectList')!=null)
 {
  if($('selectList').options.length == 0)
    new Element.hide( $('chosenCategory'));
 }
}

function hideReview()
{
if($('reviewField')!=null)
 {
   new Element.hide( $('reviewField'));
 }
if ($('ratingField')!=null)
 {
   new Element.hide( $('ratingField'));
 }
}

function showReview()
{
   new Effect.SlideDown($('reviewField'));
   new Effect.SlideDown($('ratingField'));
  if($('firstRating')!=null)
     new Element.hide($('firstRating'));
}

function addUserReview(url,listingId)
{
 new Ajax.Request(url, {
		parameters: "title=" + $('titleReview').value+
		"&descr=" + $('descriptionReview').value
		+"&listId=" + listingId
		+"&flag=0"
		+"&choose=0"
		+"&reviewAdd=1"
		+"&isRated=0"
		+"&rate=0",
		onSuccess: function(resp) { 
		var temp = new Array();
		temp = resp.responseText.split(' ');
		if(temp[0] == "title")
		{
		 if($("errtitleReview")==null)
		 {
		 if($("titleReviewTd").lastChild.nodeType!=1)
			$("titleReviewTd").lastChild.previousSibling.firstChild.nodeValue="";
			new Insertion.After("titleReview", '<span id="errtitleReview" style="color:red">  Please enter a title</span>');
		 }
		 else
		 {
			Element.replace("errtitleReview", '<span id="errtitleReview" style="color:red">   Please enter a title</span>');
		 }
		}
		if(temp[1] == "description")
		{
		 if($("errdescriptionReview")==null)
		 {
		 if($("descriptionReviewTd").lastChild.nodeType!=1)
			$("descriptionReviewTd").lastChild.previousSibling.firstChild.nodeValue="";
			new Insertion.After("descriptionReview", '<span id="errdescriptionReview" style="color:red">  Please enter a description</span>');
		 }
		 else
		 {
			Element.replace("errdescriptionReview", '<span id="errdescriptionReview" style="color:red">   Please enter a description</span>');
		 }
		}
		else if(temp[0]=='' && (temp[1]=='' || temp[1]==null))
		{
		 var currentTime = new Date();
		 var titleNode=document.createElement("p");
		 titleNode.className="informations";
		 titleNode.setAttribute("class", "informations");
		 var titleNodeText=document.createTextNode($('titleReview').value);
		 titleNode.appendChild(titleNodeText);
		 var descriptionNode=document.createElement("p");
		 var month=currentTime.getMonth() + 1;
		 var day=currentTime.getDate();
		 var text=$('descriptionReview').value+" , posted on "+currentTime.getFullYear()+"-"+((month<10)?"0"+month:month)+"-"+((day<10)?"0"+day:day);
		 var descrNodeText=document.createTextNode(text);
		 descriptionNode.appendChild(descrNodeText);
		 $('userReviews').appendChild(titleNode);
		 $('userReviews').appendChild(descriptionNode);
		 $('titleReview').value='';
		 $('descriptionReview').value='';
		 
		}
		}});
}

function removeCategory()
{
 var elem=$('selectList');
 for (var i=0; i<elem.options.length; i++) 
 {
  if (elem.options[i].selected)
  {
   elem.removeChild(elem.options[i]);
   if(elem.options.length==0) 
    {
     hideElement();
     return;
    }
	--i;
  }
 }
}

function addCategory(url)
{
 var categorie=$('secondCateg');
 var chosenCateg=$('chosenCategory');
 var optionExists=false;
 var text=categorie.options[categorie.selectedIndex].text;
 var idVal=categorie.options[categorie.selectedIndex].value;
 var elem=$('selectList');
 new Ajax.Request(url, {
		parameters: "&flag=0"
		+"&choose=1",
		onSuccess: function(resp) { 
		if(idVal == 0)
		{
		  return;
		}
		
		if(chosenCateg.style.display == 'none')
		  chosenCateg.style.display = '';
		var nrOptions=$('selectList').options.length;
		
		if(nrOptions <= 9)
		{ 
		 for(var i=0; i<nrOptions; i++)
		 {
		  if(elem.options[i].value == idVal) 
		  optionExists=true;
		 }
		 if(optionExists == false)
		 {
		  var optionNode=document.createElement("option");
		  optionNode.setAttribute('value', idVal);
		  var optiune=document.createTextNode(text);
		  optionNode.appendChild(optiune);
		  $('selectList').appendChild(optionNode);
		 }
		}
		else
		{
		  var idErr="errselectList";
		  var idTabel="selectListTd";
		  if($(idErr)==null)
		  {
		  if($(idTabel).lastChild.nodeType!=1)
			$(idTabel).lastChild.previousSibling.firstChild.nodeValue="";
		  new Insertion.After('selectList', '<span id='+idErr+' style="color:red">  You have reached 10 options</span>');
		  }
		  else
		  {
			Element.replace(idErr, '<span id='+idErr+' style="color:red">  You have reached 10 options</span>');
	      }
		} 
		
		 if($('errAddCateg')!=null)
		 {
		    Element.replace($('errAddCateg'), '');
		 }
		} 
	});
}

function selectCategories()
{
 var elem=$('selectList');
 if(elem.options.length==0)
 {
  if($('errAddCateg')==null)
  {
    new Insertion.After('addCateg', '<span id="errAddCateg" style="color:red">  Please add a category</span>');
  }
  else
  {
    Element.replace($('errAddCateg'), '<span id="errAddCateg" style="color:red">  Please add a category</span>');
  }
  return false;
 }
 for (var i=0; i<elem.options.length; i++) 
 {
   elem.options[i].selected=true;
 }
}

function areBothFieldsEmpty()
{
  if($('category').value=='' && $('address').value=='')
  {
   alert('Please fill in a search criteria');
   return false;
  }
  else
  { 
   return true;
  }
}

function load(address) {
	var map = null;
    var geocoder = null;
	var marker=null;
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"),{mapTypes:[G_NORMAL_MAP]});
		geocoder = new GClientGeocoder();
		 if (geocoder) {
		  geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
			  map.addControl(new GSmallZoomControl());
              map.setCenter(point, 13);
			  /*var icon = new GIcon();
			  icon.image = "../images/marker1.png";
			  icon.iconSize = new GSize(20, 34);
			  icon.iconAnchor = new GPoint(6, 20);
		   	  icon.infoWindowAnchor = new GPoint(10, 1);*/
              marker = new GMarker(point);
              map.addOverlay(marker);
              marker.openInfoWindowHtml(address);
			  GEvent.addListener(marker, "click", function() {
			  map.setCenter(point, 13);
			marker.openInfoWindowHtml(address);
			});
			GEvent.addListener(map, "zoomend", function(){
			  map.setCenter(point);
			 });
            }
          })
		  }
      }
    }
function  changeContent(id, firstTax, secondTax, thirdTax)
{
			var whatValue = $(id);
			var displayBlock = document.getElementById('display')
	        var totalOutput = document.getElementById('totalOutput');
	        
	        var postingsQTY = parseInt(whatValue.value)

            var re = /^[0-9]*$/;
            if (!re.test(whatValue.value)) 
            {
                alert('Only numbers are allowed!');
                whatValue.value = whatValue.value.replace(/[^0-9]/g,'');
            }

	        if (whatValue.value == '') 
            {
		        totalOutput.style.display = 'none';
		        return;
	        }
			else
			{
				 totalOutput.style.display = 'block';
				var taxValue = whatValue.value;
				if(taxValue < 5)
					totalOutput.innerHTML = whatValue.value * firstTax + ' $';
				else if (taxValue >=5 && taxValue <10)
					totalOutput.innerHTML = whatValue.value * secondTax + ' $';
				else if (taxValue >=10)
					totalOutput.innerHTML = whatValue.value * thirdTax + ' $';
			}
}
function Trim(str)
{ 
	str = LTrim(str); 
	return RTrim(str);
}
function RTrim(str)
{ 
	while(str.charAt((str.length -1))==" ")
	{
		str = str.substring(0,str.length-1);
	}
	return str;
}
function LTrim(str)
{
	while(str.charAt(0)==" ")
	{
		str = str.replace(str.charAt(0),"");
	}
	return str;
}

function checkToSeeIfEmpty(id)
{
	if(Trim($(id).value) == '')
	{
		$('fieldMsg').innerHTML = 'Please fill in the number of postings.'
		Element.show('fieldMsg');
		setTimeout("hideMessage('fieldMsg')",1000);
		return false;
	}
	return true;
}

function hideMessage(id)
{
	new Effect.Fade($(id));
	
}

function showDiv(id)
{
	new Effect.Appear($(id));
	$('viewAllCitiesDiv').setAttribute('onclick', 'hideDiv(\''+id+'\')');
	$('viewAllCitiesDiv').innerHTML = 'hide';
}

function hideDiv(id)
{
	new Effect.Fade($(id));
	$('viewAllCitiesDiv').setAttribute('onclick', 'showDiv(\''+id+'\')');
	$('viewAllCitiesDiv').innerHTML = 'view all';
}

function PerformSearch(url)
{
	clearTimeout(window.search_timer);	
			
	$j('#job-listings').load(url + $j('#keywords').fieldValue() + '/');	
	$j("#indicator").hide();	
	return false;
}


function deleteText(text, elem)
{
	if(elem.value == text)
	{
		elem.value='';
	}
}

function insertText(text, elem)
{
	if(elem.value=='')
	{
		elem.value= text;
	}
}