function iwayOpenDialog(urlDest, title, newPageParams, width, height, isModal, callbackName)
{
	var dialogParams = new NameValueSerializer('=', '&', true);
	dialogParams.add('dialogURL', urlDest);
	dialogParams.add('dialogTitle', title);
	dialogParams.add('newPageParams', newPageParams);
	var callbackFunc;
	if (callbackName)
	{
		dialogParams.add('dlgCallback', callbackName);
		callbackFunc = window[callbackName];
	}

	var dialogURL = '/NewsWay/Versions/250/Site/IWay/General/OpenDialog.asp' + '?' + dialogParams;

	var modalParams = new NameValueSerializer(':', ';', false);
	modalParams.add('dialogWidth', width + 'px');
	modalParams.add('dialogHeight', height + 'px');
	modalParams.add('status', 'no');
	modalParams.add('resizable', 'yes');
	modalParams.add('help', 'no');
	
	var windowParams = new NameValueSerializer('=', ',', false);
	windowParams.add('width', width);
	windowParams.add('height', height);
	windowParams.add('status', 'no');
	windowParams.add('menubar', 'no');
	windowParams.add('resizable', 'yes');
	windowParams.add('help', 'no');

	if(isModal && window.showModalDialog)
	{
		var ret = clientOpenDialog(dialogURL, "", width, height, callbackName)
		if (typeof(ret) != "undefined" && callbackFunc && typeof(callbackFunc) == "function" && ret != null)
		{
			callbackFunc(ret);
		}
	}
	else
	{
		clientOpenWindow(dialogURL, "_blank", windowParams);
	}
}


function iwayCloseDialog()
{
	if (parent && parent.callback) 
	{
		parent.callback();
	} 
	else 
	{
		window.top.close();
	}
}


function NameValueSerializer(inDelim, outDelim, isToEncode)
{
	var paramArray = new Array();

	function MakeNVPair(name, value)
	{
		var newValue = (isToEncode) ? encodeURIComponent(value) : value;
		return [name, newValue].join(inDelim);
	}

	this.add = function(name, value)
	{
		paramArray.push( MakeNVPair(name, value) );
	}

	this.toString = function(delim)
	{
		if(!delim)
			delim = outDelim;
		
		return paramArray.join(delim);
	}
}



function MakeShortWebDAVCustomerPath(virtPath)
{
	var prefix = window.location.protocol + "//" +  window.location.hostname;
	var firstPathSeperator = (virtPath.charAt(0) == '/') ? '' : '/';
	var shortVirtPath = virtPath.replace(/EditionManager\/ipanel\/profiles\/business\/customers/i, "EditionManager/V");
	var webDAVPath = prefix + firstPathSeperator + shortVirtPath;	
	return webDAVPath;
}


function OpenDavFolder(folderVirtPath)
{
	var shortFolderURL = MakeShortWebDAVCustomerPath(folderVirtPath);

	var topWindow = clientFindTopLevelFromDialog();
	var dataFrame = topWindow.frames["dataFrame"];
	dataFrame.WebDAVNavigator.navigateFrame(shortFolderURL, "_blank");
}


function Dialog_OpenEmptyWindow(name, width, height, callbackName)
{
	var LeftPos = null;
	var TopPos = null;
	try {	
		var topWindowScreen = window.top.screen
		LeftPos = (topWindowScreen.width) ? (topWindowScreen.width-width)/2 : 0;
		TopPos = (topWindowScreen.height) ? (topWindowScreen.height-height)/2 : 0;
	} catch (ex) {
	}
	
	var sFeatures = 'width=' + width + 'px';
	sFeatures += ',height=' + height + 'px';
	if (TopPos && LeftPos) {
		sFeatures += ',top=' + TopPos + 'px';
		sFeatures += ',left=' + LeftPos + 'px';
	}
	sFeatures += ',directories=no';
	sFeatures += ',location=no';
	sFeatures += ',menubar=no';
	sFeatures += ',resizable=yes';
	sFeatures += ',status=yes';
	sFeatures += ',toolbar=no';
	sFeatures += ',scrollbars=yes';
	sFeatures += ',modal=yes';
	sFeatures += ',alwaysRaised=yes';
	sFeatures += ',dependent=yes';
	sFeatures += ',dialog=yes';
	
	return openWindow("/newsway/versions/250/site/iway/empty.htm", name, sFeatures,callbackName) 
}


function openWindow(openURL, target, winParams, backFunc)
{
	var winObj = window
	while (winObj.dialogArguments && typeof(winObj.dialogArguments)=="object" && typeof(winObj.dialogArguments.open)!="undefined")
	{
		winObj = winObj.dialogArguments
	}
	if(typeof(window[backFunc]) != "undefined")
	    winObj[backFunc] = window[backFunc];
	winObj.open(openURL, target, winParams);
}


function OpenOfficeDocumentWithWebDAV(virtPath)
{
  var urlFile = MakeShortWebDAVCustomerPath(virtPath);
	try {
		var oSPOD = new ActiveXObject("SharePoint.OpenDocuments");
		var ret = oSPOD.EditDocument2(window, urlFile);
	} catch (ex){
		iwayMessageBoxWithParams("Error", "Could not open the target file for editing on this client computer.", [], "error");
	}
	if (!ret) {
		iwayMessageBoxWithParams("Error", "Could not open the target file. Check security permissions.", [], "error");
	}
}

function selectFileDialog(path, selectBox, folderName, fileTypes, mode,allowFileSelection, imageTarget, allowUpload)
{
	var newimage = null
	var urlParams = "path=" + clientEncodeUrlAsUTF8( path)
	
	if (typeof(folderName) != "undefined" && folderName != null && folderName != "") {
		urlParams += "&foldername=" + clientEncodeUrlAsUTF8(folderName)  
	}
	if (typeof(fileTypes) != "undefined" && fileTypes != null && fileTypes != "") {
		var fileTypesEncoded = clientEncodeUrlAsUTF8(fileTypes)
		urlParams += "&filterString=" + fileTypesEncoded + "&uploadFilter=" + fileTypesEncoded 
	}
	
	if (typeof(selectBox)!="undefined" && selectBox!=null ) {
		urlParams += "&imageName=" + clientEncodeUrlAsUTF8( selectBox.value)
	}		
	if (typeof(imageTarget)!="undefined" && imageTarget!=null && imageTarget!="")
	{
		urlParams += "&imageTarget="+imageTarget
    }
   
	var allowUploadStr = "";
	var selectFileStr = "";
	if (allowFileSelection != null) 
	{
	    allowFileSelection = eval(allowFileSelection.toLowerCase())
	    if (allowFileSelection)
	    {
	       selectFileStr += "&selectImage=true" 
	    }
	    else
	    {
	        selectFileStr += "&selectImage=false"
	    }
	}

	if (allowUpload != null) {
	    allowUpload = eval(allowUpload.toLowerCase())

	    if (allowUpload) {
	        if (selectFileStr == "")
	            selectFileStr = "&selectImage=true"
	        allowUploadStr += "&allowUpload=true" + selectFileStr;
	    }
	    else {
	        if (selectFileStr == "")
	            selectFileStr = "&selectImage=false"
	        allowUploadStr += "&allowUpload=false" + selectFileStr;
	    }
	    urlParams += allowUploadStr
	}
	else 
	{
	    urlParams += selectFileStr
	}

    	
	if (mode!="general") {
		newimage=window.showModalDialog("/newsway/versions/250/site/iway/app/browseImages/default.asp?" + urlParams,window,"status:no;help:no;resizable:yes;dialogWidth:540px;dialogHeight:545px" )
	}
	else {
		newimage=window.showModalDialog("/newsway/versions/250/site/iway/app/browseImages/general.asp?" + urlParams,window,"status:no;help:no;resizable:yes;dialogWidth:540px;dialogHeight:545px" )
	}	
		
	if ( newimage!=null && typeof(newimage)!="undefined"  &&  newimage != "")
	{
	    newimage = clientGetFileName(newimage)

	    if (selectBox != null)
	    {
	        var options = selectBox.options
	        for (var i = 0; i < options.length; i++) {
	            if (options[i].value.toLowerCase() == newimage.toLowerCase()) {
	                selectBox.selectedIndex = i;
	                return
	            }
	        }
	        options.add(new Option(newimage, newimage))
	        selectBox.selectedIndex = i;
	    }
	} 
	return newimage
}