var InventoryService = {
	
	getParam: function(q,s) { 
		s = s ? s : window.location.search; 
		var re = new RegExp('&'+q+'(?:=([^&]*))?(?=&|$)','i');
		
	    return (s=s.replace(/^\?/,'&').match(re)) ? (typeof s[1] == 'undefined' ? '' : decodeURIComponent(s[1])) : undefined; 
	},		
	
	emptyWCSession: function () {
		$j.ajax({
			url: "/webcomponents/wc/emptySession",
			async: false,
			cache: false,
			success: function() {
				$j("#documentsForm").submit();
			},
			failure: function() {
				alert("failure");
			},
			data: {
			},
			type: 'GET'
		});	
		
		return false;
	},
	
	gotoScreen: function (screen) {
		this.emptyWCSession();
		switch(screen) {
		
		case 'manageData' :
			
			window.location = '/adportal/flow.html?flowId=adportal-flow&action=manageData';
			break;
		}
		
		return false;
	},
	
	nextClick : function() {
		 
		$j(".inventoryContinue").attr('disabled', true);
		// DL change the style of the button to red??
		// alert('Button disabled');
		// save modified articles on non-layout groups
		AdBuilderService.selectGroup( -1 );  // -1 is special in that we only save rather than switch groups
		
		this.jobXml = "";
		
		$j.ajax({
			url : "/webcomponents/wc/getJobXml",
			async : false,
			success : function(data, textStatus, xmlRequest) {
				if (data == null || data.length == 0)
					this.jobXml = "";
				else
					this.jobXml = data;
			}.bind(this),
			failure : function() {
				alert("failure");
			},
			type : 'GET'
		});
		
		var flowExecutionKey = this.getParam('_flowExecutionKey');
		$j.ajax({
		url : "/adportal/setJobXml.html",
		async : false,
		success : function() {
			// ok
		},
		failure : function() {
			alert("failure");
		},
		data : {
			'jobXml' : this.jobXml,
			'_flowExecutionKey': flowExecutionKey
		},
		type : 'POST'
	});

		actionNext();
	},
	
	storeRunsheetInGlobalArticleList : function() {
		this.storeRunsheetInGlobalArticleList2(true);
	},
	
	storeRunsheetInGlobalArticleList2 : function(doActioNext) {
		$j.ajax({
			url : "/webcomponents/wc/makeGlobalArticlesFromRunsheet",
			async : false,
			success : function() {
				// ok
			},
			failure : function() {
				alert("failure");
			},
			data : {
			},
			type : 'POST'
		});

		if(doActioNext){	
			actionNext();
		}
	},
	
	storeAd: function() {
		// store away a runsheet associate with a new ad in myads
		var jobXml;
		$j.ajax({
			url : "/webcomponents/wc/getJobXml",
			async : false,
			success : function(data, textStatus, xmlRequest) {
				if (data == null || data.length == 0)
					jobXml = "";
				else
					jobXml = data;
				
				
			},
			failure : function() {
				alert("failure");
			},
			type : 'GET'
		});

		var flowExecutionKey = this.getParam('_flowExecutionKey');
		$j.ajax({
			url : "/adportal/storeAd.html",
			data : {
				'jobXml': jobXml,
				'_flowExecutionKey': flowExecutionKey
			},
			async : false,
			success : function(data, textStatus, xmlRequest) {
				alert("advert stored");
			},
			failure : function() {
				alert("failure");
			},
			type : 'POST'
		});
		
	},
	
	saveClick: function() {
		
		AdBuilderService.selectGroup( -1 );  // -1 is special in that we only save rather than switch groups
		
		this.getJobXml("layout");
	},
	
	getJobXml : function (view){
		// store away a runsheet associate with a new ad in myads
		var jobXml;
		$j.ajax({
			url : "/webcomponents/wc/getJobXml",
			async : false,
			data : {
				"view" : view
			},
			success : function(data, textStatus, xmlRequest) {
				if (data == null || data.length == 0)
					$j("#editorialForm input[name='jobXml']").val("");
				else
					$j("#editorialForm input[name='jobXml']").val(data);
				
				jobXml = data;
			},
			failure : function() {
				alert("failure");
			},
			type : 'GET'
		});
	},
	
	remakeClick : function() {
		 
		this.jobXml = "";
		
		$j.ajax({
			url : "/webcomponents/wc/getJobXml",
			async : false,
			success : function(data, textStatus, xmlRequest) {
				if (data == null || data.length == 0)
					this.jobXml = "";
				else
					this.jobXml = data;
			}.bind(this),
			failure : function() {
				alert("failure");
			},
			type : 'GET'
		});
		
		var flowExecutionKey = this.getParam('_flowExecutionKey');
		$j.ajax({
		url : "/adportal/setJobXml.html",
		async : false,
		success : function() {
			// ok
		},
		failure : function() {
			alert("failure");
		},
		data : {
			'jobXml' : this.jobXml,
			'_flowExecutionKey': flowExecutionKey
		},
			type : 'POST'
		});

		window.location.href = "flow.html?_flowExecutionKey="+flowExecutionKey+"&_eventId=remake";
		return false;
	}
	
};
