

function checkForm(form){

	if (form.project.value == "Project name" && 
	    form.uri.value == "URL" && 
	    form.description.value == "Description" && 
	    form.comment.value == "E-mail address"){
		return false;
	}else{
	    window.scroll(0,0);
	    return true;
	}
}


function showWarning(evt) {
	var tmp = evt.target.getAttribute('id').substr(5);
    var appid = tmp.substr(0, tmp.indexOf('_'));
    var stars = tmp.substr(tmp.indexOf('_')+1);

    var textdiv = document.getElementById("messagebox_" + appid);
    textdiv.innerHTML = "<span id='app_messagebox_inner'> Give this program " + stars + " stars ?  " + 
    	    "<span class='yes' onclick='submitRating(" + appid + "," + stars + ")' >Yes</span> " + 
            "<span class='no'  onclick='clearMessage(" + appid + ")' >no</span></span> "; 
}

function clearMessage(appid) {
	var textdiv = document.getElementById("messagebox_" + appid);
    textdiv.innerHTML = ""; 
}

function submitRating(appid, stars) {
    $.getJSON("./submitRating.php", {'appid': appid, 'stars': stars}, updateProgram);
}

function updateProgram(data) {

	//alert(data.appid + " " + data.average + " "  + data.votes);

    if (data.average == "votedlastweek") {

    	//Show 'You have already voted for ths program' message
        var messagebox = document.getElementById("messagebox_" + data.appid);
        messagebox.innerHTML = "<span id='app_messagebox_inner'>You have already voted for this program!</span>";

	} else if (data.average == "reachedlimit") {

    	//Show 'You can only vote three times a day' message
        var messagebox = document.getElementById("messagebox_" + data.appid);
        messagebox.innerHTML = "<span id='app_messagebox_inner'>Sorry, you can only vote 3 times a day!</span>";

	} else {
            
	    //Get the ratings division
    	var division = document.getElementById("rating_" + data.appid);            
                                 
    	//Get the new average rating
    	var rating = data.average;
    
   		//Remove the current stars
    	division.innerHTML = "";

    	//Attach the new stars
    	attachStars(division, rating);  
    
    	//Update the division with the text
    	var textdiv = document.getElementById("rating_text_" + data.appid);
    	textdiv.innerHTML = data.average + " stars - " + data.votes + " votes";
        
    	//Show 'Thank you' message
    	var messagebox = document.getElementById("messagebox_" + data.appid);
    	messagebox.innerHTML = "<span id='app_messagebox_inner'>Thank you for your vote!</span>";

    } 


}


function  init_rating(){

	var divisions = getElementsByClass("rating")

	for(var i = 0; i < divisions.length; i++) {

		//Get rating		
		var rating = divisions[i].firstChild.nodeValue;
		attachStars(divisions[i], rating);
		
	}

}

function attachStars(division, rating) {

	//Get rating            
        var rating = rating;

	//Remove the stars or the text
        division.innerHTML = "";

	
        var NUMBER_OF_STARS = 5;

        if (rating > NUMBER_OF_STARS || rating < 0){
        	//Do nothing
        } else {

       		for (var j = 1; j <= NUMBER_OF_STARS; j++) {

                var star = document.createElement('img');

                if (rating >= 1) {
                	star.setAttribute('src', './img/rating_on.gif');
                        star.className = 'on';
                        rating--;
                }
                else if(rating == 0.5) {
                        star.setAttribute('src', './img/rating_half.gif');
                        star.className = 'half';
                        rating = 0;
                }
                else {
                        star.setAttribute('src', './img/rating_off.gif');
                        star.className = 'off';
                }
                
				var widgetId = division.getAttribute('id').substr(7);
                star.setAttribute('id', 'star_' + widgetId + '_' + j);

				star.onmouseover = new Function("displayHover(" + widgetId + ", " + j + ")" );				
				star.onmouseout  = new Function("displayNormal(" + widgetId + ", " + j + ")" );				

                division.appendChild(star);
                }
	}

}


function displayHover(ratingId, star) {

    for (var i = 1; i <= star; i++) {
        document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './img/rating_over.gif');
    }

}


function displayNormal(ratingId, star) {

    for (var i = 1; i <= star; i++) {
        var status = document.getElementById('star_'+ratingId+'_'+i).className;
        document.getElementById('star_'+ratingId+'_'+i).setAttribute('src', './img/rating_'+status+'.gif');
    }

}

var sUserAgent = navigator.userAgent;

function detectIE(){

	var isIE = false;

	var isOpera = sUserAgent.indexOf("Opera") > -1;
	var isIE = sUserAgent.indexOf("compatible") > -1 
		       && sUserAgent.indexOf("MSIE") > -1
		       && !isOpera;        

	if (isIE) {
		var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
		reIE.test(sUserAgent);
		var fIEVersion = parseFloat(RegExp["$1"]);

		var isMinIE7 = false;

		isMinIE7 = fIEVersion >= 7.0;

		if (isMinIE7 != true) {
			//IE6
		}
	}

	return isIE;
}

function detectFirefox(){

	var isFirefox = false;
	var browserName = navigator.appName;
	if (browserName == "Netscape") {
		isFirefox = true;
	}
	return isFirefox;
}


function bookmarkPage(bookmarkDiv, supercategory, category, subcategory){

	bookmarkDiv.style.cursor = "auto";

	var bookmarkIcon = bookmarkDiv.firstChild;
	var bookmarkText = bookmarkDiv.lastChild;

	bookmarkIcon.setAttribute("src", "img/bookmarked.jpg");
	bookmarkIcon.setAttribute("title", "");
	bookmarkText.style.color = "#A0A0A0";
	bookmarkText.setAttribute("title", "");

	setHashcode(supercategory, category, subcategory);

	var supercategory = supercategory.replace(/\+/g," ");
	var category = category.replace(/\+/g," ");
	var subcategory = subcategory.replace(/\+/g," ");

	parent.document.title = "OSSD - " + supercategory + " : " + category + " / " + subcategory;

	var messageBox = document.getElementById("messageBox");
	messageBox.style.display = "block";
}

function setHashcode(supercategory, category, subcategory){

	parent.window.location.hash = supercategory + "_" + category + "_" + subcategory;
}

function processHashcode(){

	var hashcode = window.location.hash;

	if (hashcode != "") {

		hashcode = hashcode.substr(1);

		var array = hashcode.split("_") 
		var supercategory = array[0];
		var category = array[1];
		var subcategory = array[2];

		if( supercategory != undefined && category != undefined && subcategory != undefined ) {
			collapseAndOpen(category);
			reloadIframe(supercategory, category, subcategory);	
			makeBold(category, subcategory);
		}
	}
}


function resetForm() {

	parent.document.getElementById("form_form").reset();
}


function Toggle(idItem) {

   	var div = document.getElementById(idItem);
   	var invisible = (div.style.display == "none");

	if (invisible) {
		open(idItem);
	} else {
		close(idItem);
	}
}

function collapseAndOpen(category){

	category = category.replace(/\+/g,"");

	var allItems = getElementsByClass("togglediv", "parent");
	for(i=0; i < allItems.length; i++){

	//alert(allItems[i].id + " " + category);

		if(allItems[i].id == category){

			if(allItems[i].style.display == "none"){
				open(allItems[i].id);
			} 

		} else {
			
			if(allItems[i].style.display == "block"){
				close(allItems[i].id);
			}
		}
	}
}


function open(idItem){

   	var div = parent.document.getElementById(idItem);
	var text = parent.document.getElementById("text" + idItem);

	div.style.display = "block";
	text.style.fontWeight = "bold";
	text.style.color = "green";
}

function close(idItem){

   	var div = parent.document.getElementById(idItem);
	var text = 	parent.document.getElementById("text" + idItem);

	div.style.display = "none";	
	text.style.fontWeight = "normal";
	text.style.color = "black";
}

function makeBold(category, subcategory) {

	var category = category.replace(/\+/g,"");
	var subcategory = subcategory.replace(/\+/g,"");

	var idSubcategory = category.concat(subcategory);

	var subCategory = parent.document.getElementById(idSubcategory);
	parent.selectLink(subCategory);
}

<!-- Make link bold on click and reset last clicked link to normal -->
var selectedLink = null;

function selectLink(subcategory) {	

	if (selectedLink != null) {
		selectedLink.style.fontWeight = "normal";
		selectedLink.style.color = "black";
	}
	subcategory.style.fontWeight = "bold";
	subcategory.style.color = "green";
	selectedLink = subcategory;
}

function reloadIframe(supercategory, category, subcategory) {

	var iFrame = document.getElementById("iframe_content");
	iFrame.src = "content.php?sup=" + supercategory + "&cat=" + category + "&sub=" + subcategory;
}

function setFocus(){

	var	inputField = document.getElementById("searchTextfield");
	inputField.focus();

	//var	mostpopulartext = document.getElementById("mostpopulartext");
	//selectLink(mostpopulartext);

	var	lastaddedtext = document.getElementById("lastaddedtext");
	selectLink(lastaddedtext);
}

function resizeIframe() {

	//Get iFrame
	var iframe = parent.document.getElementById('iframe_content');

	//Set default height
	iframe.style.height = 340 + "px";

  	//Find the height of the page
  	var height = document.body.scrollHeight;

  	//Set the height of the iframe 
  	iframe.style.height = height + 10 + "px";

  	//Set the top of the footer
	setFooter();
}

function setFooter() {

	//Get height navigation
	var navigation = parent.document.getElementById('navigation');
	var height_navigation = navigation.scrollHeight;

	//Get height iFrame
	var iframe = parent.document.getElementById('iframe_content');
	var height_iframe = iframe.contentWindow.document.body.scrollHeight;
	if(height_iframe < 660){
		height_iframe = 660;
	}
	//Get footer
	var footer = parent.document.getElementById('footer');

	if(height_navigation > height_iframe) {
  		//Set the top of the footer
		footer.style.top = height_navigation + 140 + "px";
	} else {
		footer.style.top = height_iframe + 120 + "px";
	}
}


function getElementsByClass(searchClass,node,tag) {

	var classElements = new Array();
	if ( tag == null )
		tag = '*';
	
	if (node == "parent") {
		var els = parent.document.getElementsByTagName(tag);
	} else {
		var els = document.getElementsByTagName(tag);

	}

	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function showIndex(){

	if (parent.location.href == self.location.href) {

		var queryString = window.top.location.search.substring(1);

		if(queryString == "" || getParameter(queryString, "keyword") != null ){
			if (window.location.href.replace){
				window.location.replace("index.php");
			} else {
				window.location.href = "index.php";	
			}
		} else {
			var supercategory = getParameter(queryString, "sup");
			var category = getParameter(queryString, "cat");
			var subcategory = getParameter(queryString, "sub");

			if (window.location.href.replace){
				window.location.replace("index.php#" + supercategory + "_" + category + "_" + subcategory);
			} else {
				window.location.href = "index.php#" + supercategory + "_" + category + "_" + subcategory;	
			}
		}
	}
}

function getParameter(queryString, parameterName) {

   // Add "=" to the parameter name (i.e. parameterName=value)
   var parameterName = parameterName + "=";

   if ( queryString.length > 0 ) {
      // Find the beginning of the string
      begin = queryString.indexOf ( parameterName );
      // If the parameter name is not found, skip it, otherwise return the value
      if ( begin != -1 ) {
         // Add the length (integer) to the beginning
         begin += parameterName.length;
         // Multiple parameters are separated by the "&" sign
         end = queryString.indexOf ( "&" , begin );
      if ( end == -1 ) {
         end = queryString.length
      }
      // Return the string
      return unescape ( queryString.substring ( begin, end ) );
   }

   // Return "null" if no parameter has been found
   return "null";

   }
}


