﻿var EnableCLog = false;
var EnableStopWatch = true;

//var _krak = true;
//function krak()
//{
//	if(_krak)
//	{
//		throw();
//	}
//}

function NYC(msg)
{
	alert("Not Yet Completed:\r\n\t" + msg);
}
function NYI(msg)
{
	alert("Not Yet Implemented:\r\n\t" + msg);
	//throw;
}

function CLog()
{
	if(EnableCLog)
	{
		GLog.write(arguments[0]);
	}
}

function $(id)
{
    return document.getElementById(id);
}

function $$(name)
{
    return document.getElementsByName(name);
}

function Stopwatch()
{
	this._start = 0;
	this.msgQueue = [];
	
	this.Start = function()
	{
		this.msgQueue = [];
		this._start = new Date().getTime();
	}
	
	this.Tick = function(message)
	{
		var time = new Date().getTime() - this._start;
		this.msgQueue[this.msgQueue.length] = time + ' : ' + message;
	}
	this.Stop = function()
	{
		if(EnableStopWatch)
		{
			for(var i = 0; i < this.msgQueue.length; i++)
			{
				GLog.write(this.msgQueue[i]);
			}
		}
	}
}

//String.prototype.Format = function()
function formatString()
{
	var format = arguments[0];
	
	for(var i = 0; i < arguments.length - 1; i++)
	{
		format = format.replace('{' + i + '}', arguments[i + 1]);
	}
	
	return format;
}

function IsDefined(subject)
{
	if(subject == null)
	{
		return false;
	}
	else
	{
		if(typeof(subject) == 'undefined')
		{
			return false;
		}
	}
	
	return true;
}

function chooseViewAndSetLink(sender, link, url)
{
//	url = prompt('Enter a URL:', (url != '') ? url : 'http://');
//	
//	if (url != null) {
//		if (!link) {
//			var tempUrl = 'http://tempuri.org/tempuri.html';
//			this.ExecuteCommand('createlink',null,tempUrl);
//			var links = this.designEditor.document.getElementsByTagName('a');
//			for (var i=0;i<links.length;i++) {
//				if (links[i].href == tempUrl) {
//					link = links[i];
//					break;
//				}
//			}		
//		}
//		link.href = url;
//		link.setAttribute('temp_href',url);
//	}
	
	sender.chooseLinkCallBack = setLink;
	
	chooseView(sender, link, url, setLink);
}

function chooseView(sender, link, url, chooseLinkCallBack)
{
//	if(!sender.viewList)
//	{
//		sender.viewList = document.createElement('div');
//		document.appendChild(sender.viewList);
//	}
	sender.link = link;
	sender.url = url;

	var modalPopupBehavior = $find('ClientShowSelectView');
	modalPopupBehavior.show();
	modalPopupBehavior.sender = sender;
	window.sender = sender;
	
//	sender.viewList.innerHTML = '<b>Dit is de viewList</b>';
//	sender.viewList.link = link;
//	sender.viewList.chooseLinkCallBack = chooseLinkCallBack;
//	
//	sender.viewList.style.visibility = 'visible';
//	sender.viewList.style.position = 'absolute';
//	sender.viewList.style.display = 'block';
//	sender.viewList.style.zIndex = '100';
//	sender.viewList.style.left = '400px';
//	sender.viewList.style.top = '200px';
}

function onOkSelectView(viewListId)
{
	var snd = window.sender;
	
	var viewName = snd.url;
	
	// Find the correct view (the selected one, if any)
	//viewName = getSelectedValueFromRadioList(viewListId);
	viewName = getSelectedValueFromCheckboxList(viewListId);
	
	//setLink(snd, snd.link, '[InternalLink:' + viewName + ']');
	if(viewName != null)
	{
		setLink(snd, snd.link, '\|View\|' + viewName + '\|/View\|');
		//setLink(snd, snd.link, '\|ViewTitle\|' + viewName + '\|/ViewTitle\|');
	}
	else
	{
	    alert('De geselecteerde pagina is niet gevonden.\r\nNeem contact op met de website beheerder.');
	}
	//setLink(snd, snd.link, '[View]' + viewName + '[/View]');
}

function setLink(sender, link, url)
{
	if (url != null) 
	{
		if (!link) 
		{
			var tempUrl = 'http://tempuri.org/tempuri.html';
			sender.ExecuteCommand('createlink',null,tempUrl);
			var links = sender.designEditor.document.getElementsByTagName('a');
			for (var i=0;i<links.length;i++) {
				if (links[i].href == tempUrl) {
					link = links[i];
					break;
				}
			}		
		}
		link.href = url;
		link.setAttribute('temp_href',url);
	}
}

function getSelectedValueFromRadioList(viewListId)
{
	var found = false;
//	var index = 0;
//	while(!found)
//	{
//		var opt = $(viewListId + '_' + index);
//		
//		if(opt == null)
//		{
//			return null;
//		}
//		
//		if(opt.checked)
//		{
//			found = true;
//			return opt.value;
//		}
//		
//		index++;
//	}

	var nodes = $$(viewListId);
	
	for(var i=0; i < nodes.length; i++)
	{
		if(nodes[i].checked)
		{
			return nodes[i].value;
		}
	}
	
	return null;
}

function getSelectedValueFromCheckboxList(viewListId)
{
	var found = false;
	//alert($(viewListId).length);
	//var nodes = $$(viewListId)[0].getElementsByTagName('input');
	var nodes = $(viewListId).getElementsByTagName('input')
	
	for(var i=0; i < nodes.length; i++)
	{
		if(nodes[i].checked)
		{
		    //alert(nodes[i].nextSibling.href);
			return getResourceKey(nodes[i].nextSibling.href);
		}
	}
	
	return null;
}

function getResourceKey(action)
{
    var len = action.length;
    var lastSlash = action.lastIndexOf('\\');
    
    var result = action.substr(lastSlash + 1, len - lastSlash - 3);
    
    //alert(result);
    return result;
}