﻿var effectiveDialogPath = './Controls/CommonDialog/CommonDialog.aspx';
var effectiveWidth = 350;
var effectiveHeight = 300;

var __CommonDialog = {
	launchArgs: new Object(),
	userAgent: {
		isGecko: (navigator.userAgent.search(/Gecko/) >= 0),
		isIE: (navigator.userAgent.search(/MSIE/) >= 0),
		isIE6: (navigator.userAgent.search(/MSIE 6/) >= 0),
		isIE7: (navigator.userAgent.search(/MSIE 7/) >= 0),
		isOpera: (navigator.userAgent.search(/Opera/) >= 0)
	}
}

function __cdbLaunch() {
	if (arguments == null || arguments.length == 0) {
		return;	//below the legal limit; throw it back
	}

	var ua = __CommonDialog.userAgent;

	//build launchArgs from function arguments
	var launchArgs = __CommonDialog.launchArgs;	//reference to global variable
	for (var i = 0; i < arguments.length; i++) {
		//split on equals sign
		if((arg = arguments[i].match(/^([^=]*)=(.*)$/)) != null) {
			if (isNaN(arg[2])) {	//ensure numbers are numbers
				launchArgs[arg[1]] = arg[2];
			} else {
				launchArgs[arg[1]] = arg[2] * 1;
			}
		} else {
			launchArgs[arguments[i]] = null;
		}
	}

	//required arguments
	if (!launchArgs.hasOwnProperty('dialogType') || launchArgs.dialogType == undefined || launchArgs.dialogType == '') {
		return;
	}
	if (!launchArgs.hasOwnProperty('dialogMessage') || launchArgs.dialogMessage == undefined || launchArgs.dialogMessage == '') {
		return;
	}
		
	if (launchArgs.hasOwnProperty('width') && !isNaN(launchArgs.width) && launchArgs.width > 0) {
		effectiveWidth = launchArgs.width;
	}
	if (launchArgs.hasOwnProperty('height') && !isNaN(launchArgs.height) && launchArgs.height > 0) {
		effectiveHeight = launchArgs.height;
	}
	launchArgs.dialogHeight = effectiveHeight;

	//default == modal
	if (!launchArgs.hasOwnProperty('nonModal') || launchArgs.nonModal == undefined || launchArgs.nonModal == '') {
		launchArgs.nonModal = 0;
	}

	var querystring = '';
	var url = '';
	var diagOpts = {
		center: 1,
		help: 0, 
		left: 0, top: 0,
		resizable: 0,
		scroll: 0,
		status: 1, 
		width: 0, height: 0
	}
	diagOpts.toString = function () {
		var retval = '';
		//if (ua.isIE) {	//effective(Width|Height) += 6;	// IE box model bug
			if (launchArgs.nonModal) {	//IE normal popup
				if (this.width > 0)  { retval =  'width=' + (this.width * 1 + 6); }
				if (this.height > 0) { retval += ',height=' + (this.height * 1 + 6); }
				if (this.center == 1) {
					var posval = 0;
					
					retval += ',screenX=';
					posval = top.screenX + Math.round((top.outerWidth - this.width) / 2);
					posval = (posval < 0) ? 0 : posval;
					retval += posval;
	
					retval += ',screenY=';
					posval = top.screenY + Math.round((top.outerHeight - this.height) / 2);
					posval = (posval < 0) ? 0 : posval;
					retval += posval;
				} else {
					if (this.left > -1) { retval += ',screenX=' + this.left; }
					if (this.top > -1)  { retval += ',screenY=' + this.top; }
				}
				if (this.resizable == 1) { retval += ',resizable'; }
				if (this.scroll == 1)    { retval += ',scrollbars'; }
				if (this.status == 1)    { retval += ',status'; }
			} else {	//IE modal dialog
				retval = 'dialogWidth:' + (this.width * 1 + 6)
					+ 'px;dialogHeight:' + (this.height * 1 + 6)
					+ 'px';
				if (this.center) {
					retval += ';center:' + this.center;
				} else {
					retval += ';dialogLeft:' + this.left + 'px;dialogTop:' + this.top;
				}
				retval += 'px;status:' + this.status
					+ ';help:' + this.help
					+ ';resizable:' + this.resizable
					+ ';scroll:' + this.scroll + ';';
			}
		//} else {	//popup
		//	if (this.width > 0)  { retval =  'outerWidth=' + this.width; }
		//	if (this.height > 0) { retval += ',outerHeight=' + this.height; }
		//	if (this.center == 1) {
		//		var posval = 0;
		//		
		//		retval += ',screenX=';
		//		posval = top.screenX + Math.round((top.outerWidth - this.width) / 2);
		//		posval = (posval < 0) ? 0 : posval;
		//		retval += posval;
		//
		//		retval += ',screenY=';
		//		posval = top.screenY + Math.round((top.outerHeight - this.height) / 2);
		//		posval = (posval < 0) ? 0 : posval;
		//		retval += posval;
		//	} else {
		//		if (this.left > -1) { retval += ',screenX=' + this.left; }
		//		if (this.top > -1)  { retval += ',screenY=' + this.top; }
		//	}
		//	if (this.resizable == 1) { retval += ',resizable'; }
		//	if (this.scroll == 1)    { retval += ',scrollbars'; }
		//	if (this.status == 1)    { retval += ',status'; }
		//}
		
		return retval;
	}

	//width/height
	diagOpts.width  = effectiveWidth;
	diagOpts.height = effectiveHeight;	//+ 20 for status bar??
	
	if (launchArgs.hasOwnProperty('dialogLeft') && !isNaN(launchArgs.dialogLeft) && launchArgs.dialogLeft >= 0) {
		diagOpts.left = launchArgs.dialogLeft;
	}
	if (launchArgs.hasOwnProperty('dialogTop') && !isNaN(launchArgs.dialogTop) && launchArgs.dialogTop >= 0) {
		diagOpts.top = launchArgs.dialogTop;
	}

	//querystring
	querystring += 'title=';
	if (launchArgs.hasOwnProperty('title') || launchArgs.title != undefined || launchArgs.title != '') {
		querystring += encodeURIComponent(launchArgs.title);
	}

	//open the thing
	if (launchArgs.hasOwnProperty('dialogPath') && launchArgs.dialogPath != undefined && launchArgs.dialogPath != '') {
		effectiveDialogPath = launchArgs.dialogPath;
	}

	url = effectiveDialogPath + '?' + querystring;

	if (launchArgs.nonModal) {
		return window.open(url, 'CommonDialog', diagOpts.toString(), false);
		//return window.showModelessDialog(url, launchArgs, diagOpts.toString());
	} else {
		return window.showModalDialog(url, launchArgs, diagOpts.toString());
	}
}//__cdbLaunch()

//openCommonDialog() is deprecated in favor of __cdbLaunch()
function openCommonDialog(dialogType, dialogMessage, title, nonModal, heading, buttonSet, defaultButton, icon, customIconPath, dialogPath, width, height, button1Type, button2Type, button1CustomImagePath, button2CustomImagePath, buttonPath, iconPath, dialogLeft, dialogTop) {
	return __cdbLaunch(
		'dialogType=' + dialogType,
		'dialogMessage=' + dialogMessage,
		'title=' + title,
		'nonModal=' + nonModal,
		'heading=' + heading,
		'buttonSet=' + buttonSet,
		'defaultButton=' + defaultButton,
		'icon=' + icon,
		'customIconPath=' + customIconPath,
		'dialogPath=' + dialogPath,
		'width=' + width,
		'height=' + height,
		'button1Type=' + button1Type,
		'button2Type=' + button2Type,
		'button1CustomImagePath=' + button1CustomImagePath,
		'button2CustomImagePath=' + button2CustomImagePath,
		'buttonPath=' + buttonPath,
		'iconPath=' + iconPath,
		'dialogLeft=' + dialogLeft,
		'dialogTop=' + dialogTop
	);
}//openCommonDialog()

//String format similar to C String.Format(String, Object[]) method
function cdbStringFormat(text, args)
{
    //check if there are two arguments in the arguments list
    if ( args == null || args.length == 0 )
    {
        //just return the original text
        return text;
    }

    var tokenCount = arguments.length - 1;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), args[token] );
    }

    return text;
}
