function ParseInteger(input, defaultvalue)
{
	return ((input == "") || (input == null) || (input == undefined)|| (input == NaN)) ? 0 : parseInt(input);
}

function isExternal(url) {
    var match = url.match(/^([^:\/?#]+:)?(?:\/\/([^\/?#]*))?([^?#]+)?(\?[^#]*)?(#.*)?/);
    if (typeof match[1] === "string" && match[1].length > 0 && match[1].toLowerCase() !== location.protocol) return true;
    if (typeof match[2] === "string" && match[2].length > 0 && match[2].replace(new RegExp(":("+{"http:":80,"https:":443}[location.protocol]+")?$"), "") !== location.host) return true;
    return false;
}

function StrFromChar(textstring)
{
	var regexp = /(\&{1}\#{1}\d+\;{1})/gi; //FIND ISO LATIN CODE
	var regexpnumbers = /(\d+)/gi; //FIND DIGITS

	var match_arr = textstring.match(regexp);
	if(match_arr)
	{
		for(i=0;i<match_arr.length;i++)
		{
			var foundiso = match_arr[i];
			var foundisonumber = foundiso.replace("&#","").replace(";","")
			textstring = textstring.replace(foundiso, String.fromCharCode(foundisonumber));
		}
	}
	return textstring;
}

$(document).ready(function() {
	$('.tabs').tabs({
		fx: { height: 'toggle', opacity: 'toggle' }	,
		selected : ParseInteger( jQuery.cookie("ChosenUserType"), 0)
	 });

	//if chosenusertype unknown, ask for it again.
	if(jQuery.cookie("ChosenUserType") == '' || jQuery.cookie("ChosenUserType") == null)
		jQuery.cookie("show_box_ChosenUserTypeDialog", null);


	jQuery("").WG_CookiedAction({
		name_of_cookie_prefix: '',
		name_of_cookie_id: 'show_box_ChosenUserTypeDialog', //for wg pagemappingid
		expiration_cookie: 7,
		show_box_type: 'once',
		callback_action: function(){
				var url = "popup_patient_of_behandelaar.htm";
        		var iframeobj = $("<iframe src =\""+url+"\" frameborder=\"auto\" id=\"iframe_cookied\"><p>Your browser does not support iframes.</p></iframe>");
				iframeobj.dialog({
					bgiframe: true,
					height: 350,
					width: 500,
					modal: true,
					draggable: false,
					title: StrFromChar("Informatiekeuze"),
					resizable: false,
					open: function(event, ui) {
						$(this).css("width","97%");
					},
					buttons:
					[
					    {
					        text: StrFromChar("Ik ben pati&#235;nt"),
					        click: function() {

												$.cookie("ChosenUserType", "0", {expires: 30});
												$('.tabs').tabs( "select" ,0, 0) ;
												$( this ).dialog( "close" );
					        }
					    },
					    {
					        text: StrFromChar("Ik ben behandelaar"),
					        click: function() {

												$.cookie("ChosenUserType", "1", {expires: 30});
												$('.tabs').tabs( "select" ,1, 0) ;
												$( this ).dialog( "close" );
					        }
					    }
					]
				});
		}
	});




    jQuery("a").click(function () {
    	var clickedUrl = $(this).attr('href')
    	var isExternalUrl = isExternal(clickedUrl);
      	if(isExternalUrl)
      	{
			var url = "popup_externe_link.htm";
    		var iframeobj = $("<iframe src =\""+url+"\" frameborder=\"auto\" id=\"iframe_cookied\"><p>Your browser does not support iframes.</p></iframe>");
			iframeobj.dialog({
				bgiframe: true,
				height: 350,
				width: 500,
				modal: true,
				draggable: false,
				title: StrFromChar("U verlaat de site"),
				resizable: false,
				open: function(event, ui) {
					$(this).css("width","97%");
					$(this).focus();
				},
				buttons:
				[
				    {
				        text: StrFromChar("Doorgaan"),
				        click: function() {
							window.open(clickedUrl, '_blank');
							$( this ).dialog( "close" );
				        }
				    },
				    {
				        text: StrFromChar("Negeren"),
				        click: function() {
							$( this ).dialog( "close" );
				        }
				    }
				]


			});
			return false;
      	}

    });
});







