//var SITE_URL = 'http://andrei:86/Andrei/Martie-work/SoftBrowse/';
var SITE_URL = 'http://www.softbrowse.com/';

/*-----------------------------------------------------------
    Toggles element's display value
    Input: any number of element id's
    Output: none 
    ---------------------------------------------------------*/
function toggleDisp() {
    for (var i=0;i<arguments.length;i++){
        var d = $(arguments[i]);
        if (d.style.display == 'none')
            d.style.display = 'block';
        else
            d.style.display = 'none';
    }
}
/*-----------------------------------------------------------
    Toggles tabs - Closes any open tabs, and then opens current tab
    Input:     1.The number of the current tab
                    2.The number of tabs
                    3.(optional)The number of the tab to leave open
                    4.(optional)Pass in true or false whether or not to animate the open/close of the tabs
    Output: none 
    ---------------------------------------------------------*/

function toggleTab(tabId,num,numelems,opennum,animate) {

	var tabIds = new Array(4);
	tabIds[1] = "td_related";
	tabIds[2] = "td_new_added";
	tabIds[3] = "td_top_rated";
	tabIds[4] = "td_review";

	$(tabId).removeAttribute('onmouseover');
	$(tabId).removeAttribute('onmouseout');
	$(tabId).className = 'Highlight';
	
    if ($('__tabContent'+num).style.display == 'none'){
        for (var i=1;i<=numelems;i++){
            if ((opennum == null) || (opennum != i)){

								if ( tabIds[i] != tabId ) {
									$(tabIds[i]).className = 'Normal';
									$(tabIds[i]).setAttribute('onmouseover', 'this.className="Highlight"');
									$(tabIds[i]).setAttribute('onmouseout', 'this.className="Normal"');
								}
								
                var tempc = '__tabContent'+i;
                var c = $(tempc);
                if(c.style.display != 'none'){
                    if (animate || typeof animate == 'undefined')
                        Effect.toggle(tempc,'blind',{duration:0.5, queue:{scope:'menus', limit: 3}});
                    else
                        toggleDisp(tempc);
                }
            }
        }

        var c = $('__tabContent'+num);
        c.style.marginTop = '2px';
        if (animate || typeof animate == 'undefined'){
            Effect.toggle('__tabContent'+num,'blind',{duration:0.5, queue:{scope:'menus', position:'end', limit: 3}});
        }else{
            toggleDisp('__tabContent'+num);
        }
    }
}

function tooglePopularVoted() {
	if ( $('__popular').style.display == 'none' ) {
			$('__popular').style.display = 'block';
			$('__voted').style.display = 'none';
			$('most_popular').className = 'class_font link_most_popular_selected';
			$('most_voted').className = 'class_font link_most_popular';
	} else {
		$('__popular').style.display = 'none';
		$('__voted').style.display = 'block';
		$('most_popular').className = 'class_font link_most_popular';
		$('most_voted').className = 'class_font link_most_popular_selected';
	}
}

function software() {
	$('software_id').className = 'class_font link_software_selected';
	$('publisher_id').className = 'class_font link_software';
	new Ajax.Request
			(
				SITE_URL +'inc/ajax/ajaxSoftwareAward.php',
					{
						method : 'post',
						parameters : {},
						onLoad: function() { 
							//$('_software_publisher').innerHTML = 'Loading...';
						},
						onSuccess: function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('__software_publisher').innerHTML = response;
								}
							},
						onFailure : function () {alert("Error retreving software award");}
					}
			);
}

function publisher() {
	$('software_id').className = 'class_font link_software';
	$('publisher_id').className = 'class_font link_software_selected';
	new Ajax.Request
			(
				SITE_URL +'inc/ajax/ajaxPublisherAward.php',
					{
						method : 'post',
						parameters : {},
						onLoading: function(){ 
							//$('_software_publisher').innerHTML = 'Loading...';
						},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('__software_publisher').innerHTML = response;
								}
							},
						onFailure : function () {alert("Error retreving publisher award");}
					}
			);
}

function relocate( a_link ) {
	if ( a_link.substring(0,7) == 'http://' ) {
		window.location = a_link;
	} else {
		window.location = 'http://'+a_link;
	}
	
}

function setRecomended( number, section ) {
	new Ajax.Request
			(
				SITE_URL+'inc/ajax/ajaxSetRecomended.php',
					{
						method : 'post',
						parameters : { number: number, section: section},
						//onLoading: function(){ $('_recomended').innerHTML = 'Loading...';},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								$('__recomended').innerHTML = response;
							},
						onFailure : function () {alert("Error retreving software");}
					}
			);
}

function setRecomendedSection( section ) {
	new Ajax.Request
			(
				SITE_URL+'inc/ajax/ajaxSetRecomended.php',
					{
						method : 'post',
						parameters : { section: section},
						//onLoading: function(){ $('_recomended').innerHTML = 'Loading...';},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								$('__recomended').innerHTML = response;
							},
						onFailure : function () {alert("Error retreving software");}
					}
			);
}

function addPoints( divId ) {	
	var softwareId = $('software_id').value;
	var pct = 0;
	switch ( divId ) {
		case "point1":
		pct = 1;
		break;
		case "point2":
		pct = 2;
		break;
		case "point3":
		pct = 3;
		break;
		case "point4":
		pct = 4;
		break;
		case "point5":
		pct = 5;
		break;
		case "point6":
		pct = 6;
		break;
		case "point7":
		pct = 7;
		break;
		case "point8":
		pct = 8;
		break;
		case "point9":
		pct = 9;
		break;
		case "point10":
		pct = 10;
		break;
		default:
			pct = 0;
	}
	if ( softwareId != "" ) {
		new Ajax.Request
			(
				SITE_URL+'inc/ajax/ajaxAddPoints.php',
					{
						method : 'post',
						parameters : {softwareId: softwareId, points: pct},
						onLoading: function(){ $('rating_message').update('Adding points...');},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response == 'DONE' ) {
									$('rating_message').update('Thanks for voting!');
									getPoints( softwareId );
								} else if ( response = 'VOTED' ) {
									$('rating_message').update('You voted in the last 24 hours.');
								}
							},
						onFailure : function () {alert("Error retreving the file");}
					}
			);
	}
}

function getPoints( softId ) {
	if ( softId != "" ) {
		new Ajax.Request
			(
				SITE_URL+'inc/ajax/ajaxGetPoints.php',
					{
						method : 'post',
						parameters : {softId: softId},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('__rating_points').update(response);
								}
							},
						onFailure : function () {alert("Error retreving the file");}
					}
			);
	}
}

function colorRating( divId ) {
	var points = new Array(10);
	points[0] = "point1";
	points[1] = "point2";
	points[2] = "point3";
	points[3] = "point4";
	points[4] = "point5";
	points[5] = "point6";
	points[6] = "point7";
	points[7] = "point8";
	points[8] = "point9";
	points[9] = "point10";
	
	var idx = 0;
	for ( i = 0; i < points.length; i++ ) {
		if ( points[i] == divId ) {
			idx = i;
		}
	}
	
	for ( i = 0; i < points.length; i++ ) {
		if ( i <= idx ) {
			$(points[i]).style.background = "#669933";
		} else {
			$(points[i]).style.background = "#66CC99";
		}
	}
	
}

function toogleSectionCheck( checkboxId ){
	var imgFalse = SITE_URL+'images/radio_button.jpg';
	var imgTrue = SITE_URL+'images/radio_button_selected.jpg';
	
	var checkboxs = new Array(6);
	checkboxs[0] = "checkbox1";
	checkboxs[1] = "checkbox2";
	checkboxs[2] = "checkbox3";
	checkboxs[3] = "checkbox4";
	checkboxs[4] = "checkbox5";
	checkboxs[5] = "checkbox6";
	
	if ( checkboxId != "" ) {
		for ( i = 0; i < checkboxs.length; i++ ) {
			if ( checkboxs[i] != checkboxId ) {
				$(checkboxs[i]).src = imgFalse;
			}
		}
		
		if ( $(checkboxId).src == imgTrue ) {
			$(checkboxId).src = imgFalse;
			$('checkbox_value').value = "checkbox1";
			$('checkbox1').src = imgTrue;
		} else {
			$(checkboxId).src = imgTrue;
			$('checkbox_value').value = checkboxId;
		}

	}
}

function toogleGoogleCheck( checkboxId ){
	var imgFalse = SITE_URL+'images/radio_button.jpg';
	var imgTrue = SITE_URL+'images/radio_button_selected.jpg';

	var checkboxs = new Array(2);
	checkboxs[0] = "checkbox11";
	checkboxs[1] = "checkbox22";
	
	if ( checkboxId != "" ) {
		for ( i = 0; i < checkboxs.length; i++ ) {
			if ( checkboxs[i] != checkboxId ) {
				$(checkboxs[i]).src = imgFalse;
			}
		}
		
		if ( $(checkboxId).src == imgTrue ) {
			$(checkboxId).src = imgFalse;
			$('checkbox_value2').value = "checkbox11";
			$('checkbox11').src = imgTrue;
		} else {
			$(checkboxId).src = imgTrue;
			$('checkbox_value2').value = checkboxId;
		}

	}
}

function toogleCheck() {
	if ( $('updates').value == 'no' ) {
		$('updates').value = 'yes';
		$('toogle_image_check').src = SITE_URL+"images/checkbox_select.jpg";
	} else {
		$('updates').value = 'no';
		$('toogle_image_check').src = SITE_URL+"images/checkbox.jpg";
	}
}

/*
function toogleTab( tabId, divId ) {
	var tabIds = new Array(4);
	tabIds[0] = "td_related";
	tabIds[1] = "td_new_added";
	tabIds[2] = "td_top_rated";
	tabIds[3] = "td_review";
	
	var divIds = new Array(4);
	divIds[0] = "__related";
	divIds[1] = "__newadded";
	divIds[2] = "__toprated";
	divIds[3] = "__review";

	if ( tabId != "" && divId != "" ) {
		$(tabId).removeAttribute('onmouseover');
		$(tabId).removeAttribute('onmouseout');
		$(tabId).className = 'Highlight';

		for ( i = 0; i < tabIds.length; i++ ) {
			if ( tabIds[i] != tabId ) {
				$(tabIds[i]).className = 'Normal';
				$(tabIds[i]).setAttribute('onmouseover', 'this.className="Highlight"');
				$(tabIds[i]).setAttribute('onmouseout', 'this.className="Normal"');
			}
		}
		
		for ( i = 0; i < divIds.length; i++ ) {
			
			if ( divIds[i] != divId ) {
				$(divIds[i]).style.display = 'none';
			}
			
			
			if( divIds[i] != divId && Element.visible(divIds[i])) {
      //slide up
				Effect.SlideUp( divIds[i] );
      }
			
		}

		$(divId).style.display = 'block';
		Effect.SlideDown( divId );
	}
}
*/

function getReview( sectionName,  subcategoryId ) {
	if ( subcategoryId != '' && sectionName != "" ) {
		toogleTab('td_review', '__review');
		
		new Ajax.Request
			(
				SITE_URL+'inc/ajax/ajaxGetReview.php',
					{
						method : 'post',
						parameters : { sectionName: sectionName, subcategoryId: subcategoryId},
						//onLoading: function(){ $('_review').update('Loading... ');},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('__review').update(response);
								}
							},
						onFailure : function () {alert("Error retreving the file");}
					}
			);
	}
}

function getTopRated( sectionName, subcategoryId ) {
	if ( subcategoryId != '' && sectionName != "" ) {
		toogleTab('td_top_rated', '__toprated');
		
		new Ajax.Request
			(
				SITE_URL+'inc/ajax/ajaxGetTopRated.php',
					{
						method : 'post',
						parameters : { sectionName: sectionName, subcategoryId: subcategoryId},
						//onLoading: function(){ $('_toprated').update('Loading... ');},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('__toprated').update(response);
								}
							},
						onFailure : function () {alert("Error retreving the file");}
					}
			);
	}
}

function getNewAdded( sectionName, subcategoryId ) {
	if ( subcategoryId != '' && sectionName != "" ) {
		toogleTab('td_new_added', '__newadded');
		
		new Ajax.Request
			(
				SITE_URL+'inc/ajax/ajaxGetNewAdded.php',
					{
						method : 'post',
						parameters : { sectionName: sectionName, subcategoryId: subcategoryId},
						//onLoading: function(){ $('_newadded').update('Loading... ');},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('__newadded').update(response);
								}
							},
						onFailure : function () {alert("Error retreving the file");}
					}
			);
	}
}

function getRelated( sectionName, subcategoryId ) {
	if ( subcategoryId != '' && sectionName != "" ) {
		toogleTab('td_related', '__related');
		
		new Ajax.Request
			(
				SITE_URL+'inc/ajax/ajaxGetRelated.php',
					{
						method : 'post',
						parameters : {sectionName: sectionName, subcategoryId: subcategoryId},
						//onLoading: function(){ $('_related').update('Loading... ');},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('__related').update(response);
								}
							},
						onFailure : function () {alert("Error retreving the file");}
					}
			);
	}
}

function switchView( divId ) {
	if ( $(divId).style.display == "none" ) {
		$(divId).style.display = "block";
	} else {
		$(divId).style.display = "none";
	}
}

function setCategory( section ) {
	if ( section != '' ) {
		new Ajax.Request
			(
				SITE_URL+'inc/ajax/ajaxSetCategory.php',
					{
						method : 'post',
						parameters : {section: section},
						onLoading: function(){ $('software_category').innerHTML = '<option value=""> Loading... </option>';},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('software_category').innerHTML = response;
								}
							},
						onFailure : function () {alert("Error retreving the file");}
					}
			);
	}
}

function setSubcategory( category ) {
	if ( category != '' ) {
		new Ajax.Request
			(
				SITE_URL+'inc/ajax/ajaxSetSubcategory.php',
					{
						method : 'post',
						parameters : {category: category},
						onLoading: function(){ $('software_subcategory').innerHTML = '<option value=""> Loading... </option>';},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('software_subcategory').innerHTML = response;
								}
							},
						onFailure : function () {alert("Error retreving the file");}
					}
			);
	}
}

function addView (softId) {
	if ( softId != "" ) {
		new Ajax.Request
			(
				SITE_URL +'inc/ajax/ajaxAddView.php',
					{
						method : 'post',
						parameters : { softId: softId},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									//alert(response);
								}
							},
						onFailure : function () {alert("Error retreving categories");}
					}
			);
	}
}

function addDownload(softId) {
	if ( softId != "" ) {
		new Ajax.Request
			(
				SITE_URL +'inc/ajax/ajaxAddDownload.php',
					{
						method : 'post',
						parameters : { softId: softId},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									//alert(response);
								}
							},
						onFailure : function () {alert("Error retreving categories");}
					}
			);
	}
}

function categories() {
	new Ajax.Request
			(
				SITE_URL +'inc/ajax/ajaxCategories.php',
					{
						method : 'post',
						parameters : {},
						onLoading: function(){ 
							$('categories').innerHTML = 'Loading...';
						},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								//alert(response);
								if ( response != '' ) {
									$('__categories').innerHTML = response;
								}
							},
						onFailure : function () {alert("Error retreving categories");}
					}
			);
}

function new_updated() {
	new Ajax.Request
			(
				SITE_URL +'inc/ajax/ajaxNewUpdated.php',
					{
						method : 'post',
						parameters : {},
						onLoading: function(){ 
							$('new_updated').innerHTML = 'Loading...';
						},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('__new_updated').innerHTML = response;
								}
							},
						onFailure : function () {alert("Error retreving new and updated software");}
					}
			);
}

function mostPopular() {
	$('most_popular').className = 'class_font link_most_popular_selected';
	$('most_voted').className = 'class_font link_most_popular';
	new Ajax.Request
			(
				SITE_URL +'inc/ajax/ajaxMostPopular.php',
					{
						method : 'post',
						parameters : {},
						onLoad: function() { 
							$('__popular_voted').update('Loading...');
						},
						onSuccess: function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('__popular_voted').update(response);
								}
							},
						onFailure : function () {alert("Error retreving most popular software");}
					}
			);
}

function mostVoted() {
	$('most_popular').className = 'class_font link_most_popular';
	$('most_voted').className = 'class_font link_most_popular_selected';
	new Ajax.Request
			(
				SITE_URL +'inc/ajax/ajaxMostVoted.php',
					{
						method : 'post',
						parameters : {},
						onLoading: function(){ 
							$('__popular_voted').innerHTML = 'Loading...';
						},
						onSuccess : function (transport)
							{
								var response = transport.responseText;
								if ( response != '' ) {
									$('__popular_voted').innerHTML = response;
								}
							},
						onFailure : function () {alert("Error retreving most voted software");}
					}
			);
}	