/* 
   Javascript functions for shop.php.
 */


var request = document.getElementById('iframe_request');

function verhuizing(domain) {
    if (confirm("Een verhuizing van domein is alleen mogelijk als de domeinnaam reeds in uw bezit is.\nHet betekent dat u de domeinnaam van uw huidige hosting provider naar Starthosting.nl wilt veranderen.\n\nIndien het domein NIET op uw naam staat worden er overdrachtskosten in rekening gebracht.") )
    {
        add_domain(domain);
    }
}

function add_domain(domainname, sug) {
	request.src = 'request_shop.php?action=domain_add&domain='+domainname+'&sug='+(sug == null ? 0 : 1);
    setTimeout("updateCart()", 500);
}

function delete_domain(domainname) {
    request.src = 'request_shop.php?action=domain_delete&domain='+domainname;
    setTimeout("updateCart()", 500);
}

/*
    Bepaald of winkelwagen weergegeven moet worden. Als er de domein_count ==
    0, dan 'verdwijnt' de shopping_cart.
 */
function updateCart() {
    if (document.getElementById('domeinen_count').innerHTML == "0") {
        document.getElementById('next_step').style.visibility="hidden";
        document.getElementById('toegevoegd').style.visibility="hidden";
    } 
    else {
        document.getElementById('next_step').style.visibility="visible";
        document.getElementById('toegevoegd').style.visibility="visible";
    }
}

/* Suggestion function.
 * There are three ways to call this function. By pushing the
 * button Suggestions, More Suggestions, Special Suggestions.
 * This function creates tablerows with buttons and with all 
 * the suggestions that are received from the XML response. 
 * This function will disable certain buttons and will display
 * the loading-icon and will toggle the table.
 */
 /* Variable that will be used for XML response data */
var domaincheck_special = null;
var url = 'request_shop.php?action=domain_suggestion';

function suggestions(mode, domain){
	var row_id		= domain;
	var tld			= domain.replace(/([a-zA-Z0-9\-]*)\./g, "");
	var table		= "sug_table_" + tld;
	var img 		= "img_" + tld;
	var button_more = "more_button_" + tld;
	var text_more	= "more_text_" + tld;
	var row_more 	= "more_" + tld;
	var a_more		= "a_meer_" + tld;
	var a_sug		= "a_sug_" + tld;
	

	var href_more = $("#" +a_more).attr('href');
	var href_sug = $("#" +a_sug).attr('href');
	
	if(mode == "suggestions") {
		$("#" +table).toggle();
		$("#" +a_sug).attr("href", "javascript:void(0);");
	}
	else if(mode == "more") {
		$("#tip").hide();
		$("#tip_background").hide();
		$("#" +a_more).attr("href", "javascript:void(0);");
	}
		
	/* Table must be hide OR mode must be 'more' */
	if ((mode != "special") && ($("#" +table).css('display') != 'none') || (mode == "more")) {
		$("#loading").show();
		$("#" +table).show();
		$("#" +img).attr("src", "/pics/suggestie/up.gif");
		
		var suggest_url = url+ '&domain=' +domain;
		$.post(suggest_url, { domain: domain },
			function(data){
				var total_limits = $('total',data).text();
				for(i=1;i<=total_limits;i++){
					var suggestion 	= $('sug'+i,data).text();
					if(suggestion != ""){
						var suggestion_id = suggestion;
						$("#" +row_more).before("<tr id=\"" +suggestion_id+ "\"><td width=\"187\" style=\"padding-left: 40px\">" +suggestion+ "</td><td align=\"left\" width=\"135\"><a href=\"javascript:add_domain('" +suggestion+ "', \'1\')\" ><b>Registreren</b><img src=\"/pics/a/add_.gif\"/></a></td></tr>");
					}

					if(i == total_limits) {
						$("#loading").hide();
						if(($("#tip").css('display') != 'none') || ($("#tip_background").css('display') != 'none')) {
							$("#tip").hide();
							$("#tip_background").hide();
						}
					}
				}
				
				var stop = $('stop',data).text();
				if(stop){
					$("#loading").hide();
					$("#" +button_more).attr("src", "/pics/suggestie/not_allowed.gif");
					$("#" +text_more).css("color", "#eb1c26");
					$("#" +text_more).attr('innerHTML', 'Geen suggesties meer mogelijk'); 
					$("#" +text_more).parent().removeAttr('href'); 
					$("#" +text_more).parent().addClass('geen'); 
					$("#" +a_more).css("text-decoration", "none");
					$("#" +a_more).css("cursor", "default");
					$("#" +a_more).attr("href", "javascript:void(0);");
				} else {
					$("#" +a_more).attr("href", href_more);
				}
			}
		);
	}
	else if($("#" +table).css('display') == 'none') {
		$("#loading").hide();
		$("#" +table).hide();
		$("#" +img).attr("src", "/pics/suggestie/down.gif");
		
		$("#" +a_more).attr("href", href_more);
	}
	
    $("#" +a_sug).attr("href", href_sug);
}

function tip(img_id, mode){
		if(mode == "mouseover") {
			if($("#" +img_id).attr('tip_action') == "down"){
				$("#tip_text").attr('innerHTML', 'Bekijk suggesties'); 
			} else if($("#" +img_id).attr('tip_action') == "up"){
				$("#tip_text").attr('innerHTML', 'Klap suggesties in');
			}
			
			var top = $("#" +img_id).offset().top;
			var left = $("#" +img_id).offset().left;
			
			/* BROWSER CHECK */
			jQuery.each(jQuery.browser, function(i) {
				if($.browser.msie){
					$("#tip").css({ top:top - 2, left:left + 9 });
					$("#tip_background").css({ top:top - 12, left:left + 39 });
				}else{
					$("#tip").css({ top:top, left:left + 11 });
					$("#tip_background").css({ top:top - 10, left:left + 41 });
				}
			});
			$("#tip").show(300);
			$("#tip_background").show(300);
		}
		else if(mode == "mouseout") {
			$("#tip").hide();
			$("#tip_background").hide();
		}
		else if(mode == "click") {
			if($("#" +img_id).attr('tip_action') == "up"){
				$("#tip_text").attr('innerHTML', 'Bekijk suggesties'); 
				
				$("#" +img_id).attr('tip_action', 'down');
				$("#" +img_id).attr('onclick', tip(img_id, 'mouseover'));
			} else if($("#" +img_id).attr('tip_action') == "down"){
				$("#tip_text").attr('innerHTML', 'Klap suggesties in'); 
				
				$("#" +img_id).attr('tip_action', 'up');
				$("#" +img_id).attr('onclick', tip(img_id, 'mouseover'));
			}
		}

}

