<HTML xmlns:stb="urn:guimachine-com:storyboard" xmlns:gml="urn:guimachine-com:gml">
	<HEAD>
	<@program name="NewDCDlg_Comp" exec="STUDIO" mode="MODAL">
		The <i>Create New DC</i> popup window is used for creating new DCs to store VC models.
		<param name="repository:str" >IN: the repository id in which the new DC will be created. </param>
		<param name="scName:str" default="#TEXT[XLST_LOCAL_SC_NAME]">IN: the Software component name in which the new DC will be created. </param>
		<param name="isLocal:b" default="true">IN: indicates whether to the new model is created locally or in DTR. </param>
		<return type="true">Returns ~true iff a DC was actually created</return>
	
	   <copyright>(c) SAP AG 2003-2006. All rights reserved.</copyright>
	</program@>
	
	
	<META HTTP-EQUIV="MSThemeCompatible" CONTENT="no">
	#INCLUDE[gml:defs.inc]
	
	#USING[lib:Global.js]
	#USING[lib:Xml.js]
	#USING[dev:urn.UrnUtils.js]
	#USING[lib:Box.htc]
	#USING[lib:Button.htc]
	#USING[lib:Choice.htc]
	#USING[lib:Dialog.htc]
	#USING[lib:Input.htc]
	#USING[lib:Splitter.htc]
	#USING[lib:LocationSelector.htc]
	#INCLUDE[env:global_hotkeys.inc]
	
	<SCRIPT language="JavaScript">
	
	/////////////////////////////////////////////////////////////////////
	// SETUP WINDOW
	
	var mode = WPARAM("mode");
	if (!mode)
		mode = "new";
		
	if (mode == "saveas")
		WSETUP('#TEXT[XTIT_SAVE_AS_MDL]', 400, 175);
	else if (mode == "move")
		WSETUP('#TEXT[XTIT_MOVE_TO_LOCAL]', 400, 175 );
	else //"new"
		WSETUP('#TEXT[XTIT_CREATE_NEW_DC]', 400, 175 );
	
	LISTEN('onInit', initDisplay);
	
	//patch, because for some reason the params are not passed well
	var dstLocation = null;
	var orig_location = null;
	var g_doSync = false;
	
	/////////////////////////////////////////////////////////////////////
	// GKB SERVER CONNECT
	

	////// Initialization functions
	function initDisplay()
	{

		dstLocation = WPARAM("location");
		orig_location = WPARAM("orig_location");
		g_doSync = BOOL(WPARAM("doSync"));
	
		fldDevelComp.value = dstLocation.getDCId();
		if(orig_location){
		var validLoc = orig_location.isLocationValid();
			if (mode == "saveas"  && validLoc) {
				ls.initLocation(ls.REPOSITORY_DOMAIN.LOCAL, ls.USING_USER_DATA.YES); //we cannot create a DC directly in DTR. Hence forcing local location
				}
		}
		if (mode == "new" && dstLocation && dstLocation.repository){
			//new DC can be only called from tree or new model dialog, and therefore doesn't allow changing SC
			ls.initLocation(null, null, dstLocation);
		}      
		setTimeout(initVendorsList, 50);
	}
	
	
	
	function initVendorsList() {
		var ch=$ENV.channel2;
		var vendors = null;
		var loc = ls.getLocation();
		if(loc){
			var vendors = ch.listVendors(loc);
		}
		if (!vendors) {
			PROMPT(ch.error, {icon:'ERROR'});
			return false;
		}
		else{
			fldVendor.domain = vendors;
			// set the default value to be the first vendor
			//fldVendor.selectFirst();
			fldVendor.value = vendors.substring(0, vendors.indexOf(":"));
		}
		setTimeout(finishInit);
	}
	
	function initDomainsList() {
		var ch=$ENV.channel2;
		var domains = ch.listDomains(false);
		if (!domains) {
			PROMPT(ch.error, {icon:'ERROR'});
			return false;
		}
		else {
			fldDomains.domain = domains;
	
			// set the default value to be the first domain
			//fldDomains.selectFirst();
			fldDomains.value = domains.substring(0, domains.indexOf(":"));
	
		}
	}
	
	function finishInit() {
		// Langs
		masterLang.domain = #[SUPPORTED_LANGS];
		masterLang.value = GETVAR('masterLanguage') || "en";
		if(!masterLang.text){
			masterLang.value = "en";
			SETVAR('masterLanguage',"en");
		}
	
		FOCUS(fldDevelCompName);
		btnOk.enabled = true;
	}
	
	/////////////////////////////////////////////////
	// DATA INPUT
	
	document.onkeydown = function() {
		switch (event.keyCode) {
			case #[ENTER]: accept(); break;
			case #[ESC]: WCLOSE(); break;
		}
	}
	
	/////////////////////////////////////////////////
	// CREATE MODEL
	
	function accept(){
			changeBtnState(false);
			setTimeout(create, 50);
	}
	
	function changeBtnState(state){
		btnCancel.enabled=state;
		btnOk.enabled=state;
	}

	function create() {
	
		SETVAR('masterLanguage', masterLang.value);
	
		// validate DC name
		var dcName=fldDevelCompName.value||'';
		if (!dcName) {
			FOCUS(fldDevelCompName);
			PROMPT('#TEXT[XMSG_DC_NAME_MISSING]', {icon:'ERROR'});
			changeBtnState(true);
			return;
		}
	
		var isDC = true;
		var err = $ENV.isValidName(dcName, isDC);
		if (err) {
			FOCUS(fldDevelCompName);
		    PROMPT(err, {icon:'ERROR'});
		    changeBtnState(true);
			return;
		}
	
		// connect to models library
		var ch=$ENV.channel2;
		var lsLoc = ls.getLocation();
		var location = $ENV.createObject('dev:Location',
						'',
						'',
						lsLoc.getRepository(),
						getSoftwareComponentName(lsLoc.getSCFullName()),
						getSoftwareComponentVendor(lsLoc.getSCFullName()),
						this.fldDevelCompName.value,
						'',
						fldVendor.value);
		var res = null;
		
		
		if (mode=="saveas")	{
			//save dc as
			res = ch.createDevComponent(location, fldDomains.value, masterLang.value);
			if (res) {
				$ENV.model.getSourceControlHelper().saveAsDevComponent(orig_location, location, g_doSync, fldDomains.value, masterLang.value, saveAsDevComponentCallback );
			}

			function saveAsDevComponentCallback (response) {
			res = response;
			}
		}
		else if (mode=="new") {
			//create new dc
			res = ch.createDevComponent(location, fldDomains.value, masterLang.value);
		}
		else { //"movetolocal"
			//move dc to local
			var locations=[];
			locations[0] = orig_location;
			locations[1] = location;
	
			$ENV.model.getSourceControlHelper().revertDcChanges( locations, false, revertCallback );
	
			function revertCallback(dstDCLocation) {
			}
		}
	
		if (!res) {
			FOCUS(fldDevelCompName);
			PROMPT(ch.error, {icon:'ERROR'});
			changeBtnState(true);
			return;
		}
		var loc = CLONE(location);
		loc.setDCName(InterfaceToImplementationDC(location.getDCName()));
		ls.LowerBoundFlavor = 'DC';
		ls.saveUserData(loc);
		
	
		// update the manage models panel about the new DC
		var evt = $ENV.createEvtObject();
		evt.type = 'onTreeRefreshEvt';
		evt.location = CLONE(location);
		evt.location.dc = '';
		$ENV.fireModelEvent( evt );
		$ENV.recordTreeRefreshEvent( evt );
		
		var retObj = {};
		retObj.name = fldDevelCompName.value;
		retObj.urn = res.getDCId();
		retObj.dcVendor = fldVendor.value;
		retObj.dcPrefix = fldPrefix.value;
		retObj.id = namespaceFromDC({name:res.getDCId(), vendor:fldVendor.value, prefix:fldPrefix.value});
		if ($ENV.isLocalRepositoryKey(location.repository)) {
			SETVAR('RecentDCKey', retObj.id);
		}
		retObj.softwareComponent = lsLoc.getSCFullName();
		retObj.repositoryKey = lsLoc.getRepository();
		WCLOSE(retObj);
	}


	</SCRIPT>
	
	
	</HEAD>
	<BODY>
	<STB:DIALOG id=elmDialog>
	
		<STB:BOX glyph="DIALOG-AREA">
	
			<STB:BOX box="0 0 W H">
				<STB:LocationSelector box="2 4 W-4 40" id="ls" LowerBoundFlavor="SC" allowNewDC="false" showIcons="true""/>
				<STB:INPUT  box="0 48 W 20" id=fldVendor type="enum" label="#TEXT[XFLD_VENDOR]" onchange="setPrefix()" indent="100" style="visibility:hidden"/>
				<STB:INPUT  box="0 72 W 20" id=fldPrefix type="enum" label="#TEXT[XFLD_PREFIX]" onchange="updateValues()" indent="100" style="visibility:hidden"/>
				<STB:INPUT  box="0 96 W 20" id=fldDomains type="enum" label="#TEXT[XFLD_DOMAINS]" indent="100" tooltip="#TEXT[XTOL_INSERT_DOMAIN_DESC]" style="visibility:hidden"/>
				<STB:INPUT  box="0 48 W 20" id=masterLang type="enum" label="#TEXT[XFLD_MASTER_LANG]" indent="130" />
				<STB:INPUT  box="0 72 W 20" id=fldDevelCompName type="str" label="#TEXT[XFLD_DEVELOPMENT_COMPONENT]" indent="130" />
				<STB:INPUT  box="0 168 W 20" id=fldDevelComp type="str" label="#TEXT[XFLD_DEVELOPMENT_COMPONENT]" indent="100" mask="LOWERCASE" enabled="false" readonly="true" style="visibility:hidden"/>
				<STB:INPUT  box="0 190 W 20" id=fldNamespace type="str" label="#TEXT[XFLD_NAMESPACE]" indent="100" mask="LOWERCASE" enabled="false" readonly="true" style="visibility:hidden"/>
			</STB:BOX>
	
		</STB:BOX>
	
		<STB:BOX glyph="BUTTONS-AREA">
			<STB:BUTTON id=btnOk enabled="false" onclick="accept()">#TEXT[XBUT_OK]</STB:BUTTON>
			<STB:BUTTON id=btnCancel onclick="WCLOSE()">#TEXT[XBUT_CANCEL]</STB:BUTTON>
		</STB:BOX>
	
	</STB:DIALOG>
	</BODY>
	
	</HTML>