
function MerchantInquiryBean(mi)
{
	//alert("MerchantInquiryBean");
	
	if (mi != null) {
		
		if (typeof(mi.inquiryReference) != 'undefined'){
			this.inquiryReference = mi.inquiryReference ;
			}
		
		if (typeof(mi.inquiryText) != 'undefined'){
			this.inquiryText = mi.inquiryText ;
			}
		
		if (typeof(mi.mobileOrderFlag) != 'undefined'){
			this.mobileOrderFlag = mi.mobileOrderFlag;
		}
		if (typeof(mi.webOrderFlag) != 'undefined'){
			this.webOrderFlag = mi.webOrderFlag ;
		}
		if (typeof(mi.waiterOrderFlag) != 'undefined'){
			this.waiterOrderFlag = mi.waiterOrderFlag ;
		}
		
		//Contact
		if (typeof(mi.contactFirstName) != 'undefined'){
			this.contactFirstName = mi.contactFirstName;
		}
		if (typeof(mi.contactLastName) != 'undefined'){
			this.contactLastName = mi.contactLastName ;
		}
		if (typeof(mi.contactPhone) != 'undefined'){
			this.contactPhone = mi.contactPhone ;
		}
		if (typeof(mi.contactEmail) != 'undefined'){
			this.contactEmail = mi.contactEmail;
		}
		
		//Merchant
		if (typeof(mi.merchantName) != 'undefined'){
			this.merchantName = mi.merchantName;
		}
		if (typeof(mi.merchantWebsite) != 'undefined'){
			this.merchantWebsite = mi.merchantWebsite ;
		}
		if (typeof(mi.merchantAddress1) != 'undefined'){
			this.merchantAddress1 = mi.merchantAddress1 ;
		}
		if (typeof(mi.merchantAddress2) != 'undefined'){
			this.merchantAddress2 = mi.merchantAddress2;
		}
		if (typeof(mi.merchantCity) != 'undefined'){
			this.merchantCity = mi.merchantCity ;
		}
		if (typeof(mi.merchantState) != 'undefined'){
			this.merchantState = mi.merchantState ;
		}
		if (typeof(mi.merchantZip) != 'undefined'){
			this.merchantZip = mi.merchantZip ;
		}
		if (typeof(mi.merchantPhone) != 'undefined'){
			this.merchantPhone = mi.merchantPhone ;
		}
		
		if (typeof(mi.merchantCountry) != 'undefined'){
			this.merchantCountry = mi.merchantCountry ;
		}
		if (typeof(mi.workPhone) != 'undefined'){
		this.workPhone = mi.workPhone ;
		}
		if (typeof(mi.alternatePhone) != 'undefined'){
		this.alternatePhone = mi.alternatePhone ;
		}
		if (typeof(mi.active) != 'undefined'){
		this.active = mi.active ;
		}
		if (typeof(mi.createdBy) != 'undefined'){
		this.createdBy = mi.createdBy ;
		}
		if (typeof(mi.createdOn) != 'undefined'){
		this.createdOn = mi.createdOn ;
		}
		if (typeof(mi.updatedBy) != 'undefined'){
		this.updatedBy = mi.updatedBy ;
		}
		if (typeof(mi.updatedOn) != 'undefined'){
		this.updatedOn = mi.updatedOn ;
		}
	}
	
	
	this.toString()
	{
		return this.name ;
	}
	
	
	
	this.toJSONString = function()
	{
		return("{\"MerchantInquiryBean\":" + dojo.toJson(this) + "}") ;
	}
}


function CreateMerchantInquiryRequest()
{
	//alert("CreateMerchantInquiryRequest");
	this.requestID = "" ;
	this.clientID  = "" ;
	this.userID = "" ;
	this.requestTime = new Date();
	
	this.merchantInquiryBean = new MerchantInquiryBean() ;

	this.toJSONString = function() {
		var oldReqTime;
		if (typeof this.requestTime == undefined) {
			this.requestTime = new Date();
		}

		oldReqTime = this.requestTime;
		// TODO: Include timezone later
		this.requestTime = this.requestTime.toString("yyyy-MM-dd HH:mm:ss");
		var jsonStr = '{"createMerchantInquiryRequest":' + dojo.toJson(this) + '}';
		this.requestTime = oldReqTime;
		return jsonStr;
	}
	
}

function submitCreateMerchantInquiryRequest(formID) 
{
	var theDjitForm = dijit.byId(formID) ; 
	// Check if the form elements have valid values
	if ( !validateForm(theDjitForm) ) {
		return false ;
	}
	clearErrorsDisplay("errorMsgsDiv") ;

	//alert("submitCreateMerchantInquiryRequest");
	// Instantiate CreateMerchantInquiryRequest object
	var crMIReq = new CreateMerchantInquiryRequest() ;
	
	// Set the header
	crMIReq.requestID = "123" ;
	crMIReq.clientID = "ROSWEB" ;
	crMIReq.userID = "someone"  // TODO:  Fix this later
	this.requestTime = new Date();
	
	// Create the MerchantInquiryBean in the request object and copy the user input 	
	crMIReq.merchantInquiryBean = new MerchantInquiryBean() ;
	var theForm = dojo.byId(formID) ;
	var userInput = dojo.formToObject(theForm) ;
	
	//alert(document.getElementById("inquiryText").value);
	//alert(document.getElementById("contactEmail").value);
	
	if (typeof(userInput.inquiryText) != 'undefined'){
		crMIReq.merchantInquiryBean.inquiryText = userInput.inquiryText ;
	}else
	{
		crMIReq.merchantInquiryBean.inquiryText = document.getElementById("inquiryText").value;
	}
	
	// Order Flag
	if ( typeof userInput.mobileOrderFlag != "undefined" ) {
		if (userInput.mobileOrderFlag == "on"){
			crMIReq.merchantInquiryBean.mobileOrderFlag = "1" ;
		}
	}else
	{
		if (document.getElementById("mobileOrderFlag").value == "on"){
			crMIReq.merchantInquiryBean.mobileOrderFlag = "1" ;
		}else
		{	
			crMIReq.merchantInquiryBean.mobileOrderFlag = "0";
		}
	} 
	
	if ( typeof userInput.webOrderFlag != "undefined" ) {
		if (userInput.webOrderFlag == "on"){
			crMIReq.merchantInquiryBean.webOrderFlag = "1" ;
		}
	}else
	{
		if (document.getElementById("webOrderFlag").value == "on"){
			crMIReq.merchantInquiryBean.webOrderFlag = "1" ;
		}else
		{	
			crMIReq.merchantInquiryBean.webOrderFlag = "0";
		}
	}
	
	//alert("1");
	if ( typeof userInput.waiterOrderFlag != "undefined" ) {
		if (userInput.waiterOrderFlag == "on"){
			crMIReq.merchantInquiryBean.waiterOrderFlag = "1" ;
		}
	}else
	{
		if (document.getElementById("waiterOrderFlag").value == "on"){
			crMIReq.merchantInquiryBean.waiterOrderFlag = "1" ;
		}else
		{	
			crMIReq.merchantInquiryBean.waiterOrderFlag = "0";
		}
	}
	
	//alert("First Name ="+userInput.contactFirstName);
	// Contact
	if (typeof(userInput.contactFirstName) != 'undefined'){
		crMIReq.merchantInquiryBean.contactFirstName = userInput.contactFirstName;
	}else
	{
		crMIReq.merchantInquiryBean.contactFirstName=document.getElementById("contactFirstName").value;
	}
	
	if (typeof(userInput.contactLastName) != 'undefined'){
		crMIReq.merchantInquiryBean.contactLastName = userInput.contactLastName ;
	}else
	{
		crMIReq.merchantInquiryBean.contactLastName=document.getElementById("contactLastName").value;
	}
	if (typeof(userInput.contactPhone) != 'undefined'){
		crMIReq.merchantInquiryBean.contactPhone = userInput.contactPhone ;
	}else
	{
		crMIReq.merchantInquiryBean.contactPhone=document.getElementById("contactPhone").value;
	}
	if (typeof(userInput.contactEmail) != 'undefined'){
		crMIReq.merchantInquiryBean.contactEmail = userInput.contactEmail;
	}else
	{
		crMIReq.merchantInquiryBean.contactEmail = document.getElementById("contactEmail").value;
	}
	//alert("Email ="+userInput.contactEmail);
	//alert("2");
	
	// Merchant
	if (typeof(userInput.merchantName) != 'undefined'){
		crMIReq.merchantInquiryBean.merchantName = userInput.merchantName ;
	}else
	{
		crMIReq.merchantInquiryBean.merchantName=document.getElementById("merchantName").value;
	}
	if (typeof(userInput.merchantWebsite) != 'undefined'){
		crMIReq.merchantInquiryBean.merchantWebsite = userInput.merchantWebsite ;
	}else
	{
		crMIReq.merchantInquiryBean.merchantWebsite=document.getElementById("merchantWebsite").value;
	}
	if (typeof(userInput.merchantAddress1) != 'undefined'){
		crMIReq.merchantInquiryBean.merchantAddress1 = userInput.merchantAddress1 ;
	}else
	{
		crMIReq.merchantInquiryBean.merchantAddress1=document.getElementById("merchantAddress1").value;
	}
	if (typeof(userInput.merchantAddress2) != 'undefined'){
		crMIReq.merchantInquiryBean.merchantAddress2 = userInput.merchantAddress2 ;
	}else
	{
		crMIReq.merchantInquiryBean.merchantAddress2=document.getElementById("merchantAddress2").value;
	}
	if (typeof(userInput.merchantCity) != 'undefined'){
		crMIReq.merchantInquiryBean.merchantCity = userInput.merchantCity ;
	}else
	{
		crMIReq.merchantInquiryBean.merchantCity=document.getElementById("merchantCity").value;
	}
	if (typeof(userInput.merchantState) != 'undefined'){
		crMIReq.merchantInquiryBean.merchantState = userInput.merchantState ;
	}else
	{
		crMIReq.merchantInquiryBean.merchantState=document.getElementById("merchantState").value;
	}
	if (typeof(userInput.merchantZip) != 'undefined'){
		crMIReq.merchantInquiryBean.merchantZip = userInput.merchantZip ;
	}else
	{
		crMIReq.merchantInquiryBean.merchantZip=document.getElementById("merchantZip").value;
	}
	if (typeof(userInput.merchantPhone) != 'undefined'){
		crMIReq.merchantInquiryBean.merchantPhone = userInput.merchantPhone ;
	}else
	{
		crMIReq.merchantInquiryBean.merchantPhone=document.getElementById("merchantPhone").value;
	}
	//alert("3");
	
	//alert("Json:\n" + crMIReq.toJSONString()) ;
	
	dojo.xhrPost( {
		url : MSCONSTANTS.SERVICE_URL,
		handleAs : "json",
		postData : "serviceReq=" + escape(crMIReq.toJSONString()),
		load : function(response) {
		handleCreateMerchantInquiryResponse(response);
		}
	});
	
}

function handleCreateMerchantInquiryResponse(response) {

	//alert("Handle handleCreateMerchantInquiryResponse");
	var respObj = null;
	if (typeof response.createMerchantInquiryResponse != "undefined") {
		respObj = response.createMerchantInquiryResponse;
	} else {
		respObj = response.serviceResponse;
	}
	if (respObj.serviceStatus == MSCONSTANTS.SUCCESS) {
		//alert("Success");
		//merchantRepDetails = respObj.userBean;
		merchantRepDetails = respObj.merchantInquiryBean;
		MSCONSTANTS.MERCHANT_INQUIRY_REFERENCE = merchantRepDetails.inquiryReference;
		
		
		displayPage(MSCONSTANTS.CREATE_MERCHANT_INQUIRY_SUCCESS_PAGE);
	}else {
		showServiceErrors(respObj.errors, "errorMsgsDiv", null) ;
	}
}

function test()
{
	alert("Hello");
}

//************  GET MERCHANT INQUIRIES ****************//
function GetMerchantInquiries() 
{
	//alert("submitCreateMerchantInquiryRequest");
	// Instantiate CreateMerchantInquiryRequest object
	var crMIReq = new CreateMerchantInquiryRequest() ;
	
	// Set the header
	crMIReq.requestID = "123" ;
	crMIReq.clientID = "ROSWEB" ;
	crMIReq.userID = "someone"  // TODO:  Fix this later
	this.requestTime = new Date();
	
	// Create the MerchantInquiryBean in the request object and copy the user input 	
	crMIReq.merchantInquiryBean = new MerchantInquiryBean() ;
	//var userInput = dojo.formToObject(theForm) ;
	crMIReq.action = "GET";
	
	//alert("Json:\n" + crMIReq.toJSONString()) ;
	
	dojo.xhrPost( {
		url : MSCONSTANTS.SERVICE_URL,
		handleAs : "json",
		postData : "serviceReq=" + escape(crMIReq.toJSONString()),
		load : function(response) {
		handleGetMerchantInquiriesResponse(response);
		}
	});
	
}

function SearchMerchantInquiriesBean(sMerchant) {
	if (sMerchant != null) {
		if (typeof (sMerchant.name) != 'undefined') {
			this.name = sMerchant.name;
		}
		if (typeof (sMerchant.inquiryReference) != 'undefined') {
			this.inquiryReference = sMerchant.inquiryReference;
		}
		if (typeof (sMerchant.new) != 'undefined') {
			this.new = sMerchant.name;
		}
		if (typeof (sMerchant.inquiryId) != 'undefined') {
			this.inquiryId = sMerchant.inquiryId;
		}
	}
	this.toString()
	{
		return this.name;
	}
	this.toJSONString = function() {
		return ("{\"merchantBean\":" + dojo.toJson(this) + "}");
	}
}

function SearchMerchantInquiriessRequest() {
	this.requestID = "";
	this.clientID = "";
	this.userID = "";
	this.requestTime = new Date();

	this.merchantBean = new SearchMerchantsBean();

	/*
	 * this.toJSONString = function() { return("{\"searchMerchantsRequest\":" +
	 * dojo.toJson(this) + "}") ; }
	 */

	this.toJSONString = function() {
		var oldReqTime;
		if (typeof this.requestTime == undefined) {
			this.requestTime = new Date();
		}

		oldReqTime = this.requestTime;
		// TODO: Include timezone later
		this.requestTime = this.requestTime.toString("yyyy-MM-dd HH:mm:ss");
		var jsonStr = '{"searchMerchantsRequest":' + dojo.toJson(this) + '}';
		this.requestTime = oldReqTime;
		return jsonStr;
	}
}

function submitSearchMerchantInquiryRequest() 
{
	//alert("submitSearchMerchantInquiryRequest");
	// Instantiate CreateMerchantInquiryRequest object
	var sMIReq = new SearchMerchantInquiryRequest() ;
	
	// Set the header
	crMIReq.requestID = "123" ;
	crMIReq.clientID = "ROSWEB" ;
	crMIReq.userID = "someone"  // TODO:  Fix this later
	this.requestTime = new Date();
	
	// Create the MerchantInquiryBean in the request object and copy the user input 	
	crMIReq.merchantInquiryBean = new MerchantInquiryBean() ;
	//var userInput = dojo.formToObject(theForm) ;
	crMIReq.action = "GET";
	
	//alert("Json:\n" + crMIReq.toJSONString()) ;
	
	dojo.xhrPost( {
		url : MSCONSTANTS.SERVICE_URL,
		handleAs : "json",
		postData : "serviceReq=" + escape(crMIReq.toJSONString()),
		load : function(response) {
		handleGetMerchantInquiriesResponse(response);
		}
	});
	
}

function handleGetMerchantInquiriesResponse(response) {

	//alert("Handle handleGetMerchantInquiriesResponse");
	var respObj = null;
	if (typeof response.createMerchantInquiryResponse != "undefined") {
		respObj = response.createMerchantInquiryResponse;
	} else {
		respObj = response.serviceResponse;
	}
	if (respObj.serviceStatus == MSCONSTANTS.SUCCESS) {
		//alert("Success");
		merchantInquiryListObj = respObj.merchantInquiryList;
		var merchantInquiryList = {
				identifier : "id",
				label : "id",
				items : merchantInquiryListObj
			};
			if (typeof fileReadStore == "undefined"){
				var fileReadStore = new dojo.data.ItemFileReadStore( {
				data : merchantInquiryList, clearOnClose: true
				}); 
				//fileWriteStore.fetch();
			}else{
				fileReadStore.close();
				fileReadStore.data = merchantInquiryList;
				fileReadStore.clearOnClose = true;
				fileReadStore.fetch();
			}
			var layout = [ {
				cells : [ [  {
					field : "inquiryReference",
					name : "Inquiry Id",
					width : "45px"
				},{
					field : "merchantName",
					name : "Merchant Name",
					width : "80px"
				}, {
					field : "contactFirstName",
					name : "First Name",
					width : "70px"
				},{
					field : "contactLastName",
					name : "Last Name",
					width : "70px"
				},{
					field : "inquiryDate",
					name : "Inquiry Date/Time",
					width : "80px"
				},{
					field : "contactPhone",
					name : "Phone",
					width : "70px"
				}, {
					field : "contactEmail",
					name : "Email",
					width : "120px"
				} ] ]
			} ];
			//alert ("layout")
			
			//widget.clear();
			if (typeof grid == "undefined"){
				//alert("First grid000");
				grid = new dojox.grid.DataGrid({
	            store: fileReadStore,
	            clientSort: true,
	            rowSelector: '5px',
	            structure: layout,
	            height: '350px',
	            noDataMessage: 'No New Inquiries.'
		        },"list");
			}else{
				if (MSCONSTANTS.gridview ==1){
					//alert("grid00 view");
					grid = new dojox.grid.DataGrid({
		            store: fileReadStore,
		            clientSort: true,
		            rowSelector: '5px',
		            structure: layout,
		            height: '350px',
		            noDataMessage: 'No Matching Inquiries.'
			        },"list");
					MSCONSTANTS.gridview = 0;
				}else{
					 fileReadStore.fetch();
					 grid.setStore(fileReadStore);
					 grid.setStructure(layout);
					 grid.setNoDataMessage('No Matching Inquiries.');
					 grid.setHeight('350px');
					 grid._refresh();
					 handle = dojo.connect(grid, "onRowClick", showDetail);

				}
			}
				   handle = dojo.connect(grid, "onRowClick", MIshowDetail);

			grid.startup();
	}else {
		for(i = 0; i < respObj.errors.length; ++i) {
			if ( dijit.byId(respObj.errors[i].attributeName) != null ) {
			  dijit.byId(respObj.errors[i].attributeName).displayMessage(respObj.errors[i].message);
			  // dijit.byId(respObj.errors[i].attributeName).displayMessage("Error here!");
			}
		}
	}
}

var MIshowDetail = function(e){
    var value = e.cellNode.firstChild.data;
//alert('value:' + value + " column:" + e.cellIndex + " row:" + e.rowIndex);
merchantInquiryDetails = this.getItem(e.rowIndex);
displayPage(MSCONSTANTS.VIEW_MERCHANT_INQUIRY_PAGE);
//alert (currItem.address1 + "curr Item");
}

function MIcleanUp(){
	if (typeof fileReadStore != "undefined"){
		//alert("clean store");
		fileReadStore = null;
	}
	if (typeof grid != "undefined"){
		//alert("inclane grid");
		MSCONSTANTS.gridview=1;
		grid.destroy();
	}
}

function populateMerchantInquiries()
{
	//alert("populateMerchantInquiries");
	miBeanList = merchantInquiryListObj.length;
	//alert(miBeanList);
	
	miBeanDisplay = "<table border='1'><tr><th>ID</th><th>Inquiry Text</th><th>Inquiry Status</th><th>Notes</th></tr>";
	
	
	if ( miBeanList > 0)
	  {	  
	  
		  for (counter=0; counter<miBeanList; counter++)
		  {
			 //alert("MerchantInquiryBean["+counter+"] Text ="+merchantInquiryListObj[counter].inquiryText);
			 miBeanDisplay = miBeanDisplay + "<tr>";
			 miBeanDisplay = miBeanDisplay + "<td>" + merchantInquiryListObj[counter].id +"</td>";
			 miBeanDisplay = miBeanDisplay + "<td>" + merchantInquiryListObj[counter].inquiryText +"</td>";
			 miBeanDisplay = miBeanDisplay + "<td>" + merchantInquiryListObj[counter].status +"</td>";
			 miBeanDisplay = miBeanDisplay + "<td>" + merchantInquiryListObj[counter].notes +"</td>";
			 miBeanDisplay = miBeanDisplay + "</tr>";
				
		  }
		  
		  miBeanDisplay = miBeanDisplay + "</table>";
		  //alert(miBeanDisplay);
		 document.getElementById("merchantInquiriesDiv").innerHTML=miBeanDisplay;
	  }
	
	
}

function populateMerchantInquiryDetails(){
	if (typeof (merchantInquiryDetails.inquiryReference) != 'undefined') {
		document.getElementById("inquiryReference").innerHTML=merchantInquiryDetails.inquiryReference;
	}
	if (typeof (merchantInquiryDetails.inquiryText) != 'undefined') {
		document.getElementById("inquiryText").innerHTML=merchantInquiryDetails.inquiryText;
	}
	if (typeof (merchantInquiryDetails.merchantAddress1) != 'undefined') {
		address1= merchantInquiryDetails.merchantAddress1;
	}
	if (typeof (merchantInquiryDetails.merchantAddress2) != 'undefined') {
		address2= merchantInquiryDetails.merchantAddress2;
	}
	if (typeof (merchantInquiryDetails.merchantCity) != 'undefined') {
		city= merchantInquiryDetails.merchantCity;
	}
	if (typeof (merchantInquiryDetails.merchantState) != 'undefined') {
		state= merchantInquiryDetails.merchantState;
	}
		country= 'USA';
	
	if (typeof (merchantInquiryDetails.merchantZip) != 'undefined') {
		zip= merchantInquiryDetails.merchantZip;
	}
		document.getElementById("address").innerHTML=address1 + " " + address2 + "<br>" + city + ", " + state + " " + country + " - " + zip;
		
	if (typeof (merchantInquiryDetails.mobileOrderFlag) != 'undefined') {
		if(merchantInquiryDetails.mobileOrderFlag == "1"){
			document.getElementById("mobileOrderFlag").innerHTML="Yes";
		}else{
			document.getElementById("mobileOrderFlag").innerHTML="No";	
		}
	}
	if (typeof (merchantInquiryDetails.webOrderFlag) != 'undefined') {
		if(merchantInquiryDetails.webOrderFlag == "1"){
			document.getElementById("webOrderFlag").innerHTML="Yes";
		}else{
			document.getElementById("webOrderFlag").innerHTML="No";	
		}
	}
	if (typeof (merchantInquiryDetails.waiterOrderFlag) != 'undefined') {
		if(merchantInquiryDetails.waiterOrderFlag == "1"){
			document.getElementById("waiterOrderFlag").innerHTML="Yes";
		}else{
			document.getElementById("waiterOrderFlag").innerHTML="No";	
		}
	}
	if (typeof (merchantInquiryDetails.contactFirstName) != 'undefined') {
		firstName=merchantInquiryDetails.contactFirstName;
	}
	if (typeof (merchantInquiryDetails.contactLastName) != 'undefined') {
		lastName=merchantInquiryDetails.contactLastName;
	}
	document.getElementById("fullName").innerHTML=lastName+", "+ firstName;
	if (typeof (merchantInquiryDetails.contactPhone) != 'undefined') {
		document.getElementById("contactPhone").innerHTML=merchantInquiryDetails.contactPhone;
	}
	if (typeof (merchantInquiryDetails.contactEmail) != 'undefined') {
		document.getElementById("contactEmail").innerHTML=merchantInquiryDetails.contactEmail;
	}
	if (typeof (merchantInquiryDetails.merchantName) != 'undefined') {
		document.getElementById("merchantName").innerHTML=merchantInquiryDetails.merchantName;
	}
	
	if (typeof (merchantInquiryDetails.merchantWebsite) != 'undefined') {
		document.getElementById("merchantWebsite").innerHTML=merchantInquiryDetails.merchantWebsite;
	}
	
	if (typeof (merchantInquiryDetails.merchantPhone) != 'undefined') {
		document.getElementById("merchantPhone").innerHTML=merchantInquiryDetails.merchantPhone;
	}
}

