﻿// JScript File
//addNamespace('incidentReport');
//addNamespace('incidentReport.NetOnNet');
addNamespace('incidentReport.NetOnNet.view');
addNamespace('incidentReport.NetOnNet.model');
addNamespace('incidentReport.NetOnNet.controller');

incidentReport.NetOnNet.view = {
	init: function(){
		alert('sdf');
	},
	
	getFormValues: function(){
		var formItems = new Array();
		var item;
		
		var controls = $('#pop-content input:text,#pop-content textarea');
		
		controls.each(function(index, value) { 
			item = new IkanoInsurance.Web.Service.InsuranceReport.formItem();
			item.property = $(value).attr('id');
			item.value = $(value).val();
			formItems.push(item);	
		});
		
		//checkboxes
		item = new IkanoInsurance.Web.Service.InsuranceReport.formItem();
		item.property = 'errorShop';
		item.value = $("#pop-content input[name='errorShop']:checked").val();
		formItems.push(item);
							
		item = new IkanoInsurance.Web.Service.InsuranceReport.formItem();
		item.property = 'reportedIncident';
		item.value = $("#pop-content input[name='reportedIncident']:checked").val();
		formItems.push(item);			

		item = new IkanoInsurance.Web.Service.InsuranceReport.formItem();
		item.property = 'accept_terms';
		item.value = $("#pop-content #accept_terms:checked").val();
		formItems.push(item);			

		item = new IkanoInsurance.Web.Service.InsuranceReport.formItem();
		item.property = 'errorStore';
		item.value = $("#pop-content #errorStore option:selected").val();
		formItems.push(item);	
		
		return formItems;
	}
};

incidentReport.NetOnNet.controller = {
    /**
     * Initialization of Controller.
	 *
    */
    init: function() {
		//wire events
        this.modelFormValidated.handle(incidentReport.NetOnNet.model.formValidatedEvent);
        this.modelFormSubmited.handle(incidentReport.NetOnNet.model.formSubmitEvent);
    },
    
    modelFormValidated: function() {
    
    },
    
    modelFormSubmited: function() {
    
    },

	validateForm: function() {
	
	},
	
	submitForm: function() {
		var formItems;
		
		formItems = incidentReport.NetOnNet.view.getFormValues();
		
		$('#pop-content .error').parent().parent().removeClass('error');
		$('#pop-content .error').parent().hide();
					
		var success = function(msg) { 
			
			if(msg.length > 0) {
				$('#pop-content #ajax-loader').hide();
				
				//highlight errors
				$.each(msg, function(index, value) { 
					$('#pop-content #' + value.property + '_error').parent().parent().addClass('error');
					$('#pop-content #' + value.property + '_error').parent().show();
				});
			
				//show error statement
				$('#pop-content #summary_error').parent().show();
				
				//scroll to submit button
				var destination = $('#pop-content #summary_error').offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 20); 
			}
			else {
				var success1 = function(msg){
					$('#pop-content #ajax-loader').hide();
					popup.displayElem('netonnet_done');
					
					$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 1000); 
					popup.displayElem('netonnet_done');
				};
				var failed1 = function(msg){
					$('#pop-content #ajax-loader').hide();
					popup.displayElem('netonnet_error');
					
					$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 1000); 
					popup.displayElem('netonnet_done');
				};
				
				IkanoInsurance.Web.Service.InsuranceReport.Controller.sendNetOnNet(formItems,success1,failed1);
			}
			
			popup.resizeOverlay();
			$('#pop-content #ajax-loader').hide();
		}
		
		var failed = function(msg) { 
			$('#pop-content #ajax-loader').hide();
			popup.displayElem('netonnet_error');
			
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 1000); 
			popup.displayElem('netonnet_done');
		}
		
		$('#pop-content #ajax-loader').show();
		
		IkanoInsurance.Web.Service.InsuranceReport.Controller.validateNetOnNet(formItems,success,failed);	
	}
};

incidentReport.NetOnNet.model = {
	//declaration of public events
    formValidatedEvent: new Observer(),
    formSubmitEvent: new Observer(),
};

incidentReport.NetOnNet.defaults = {
	verb: 'POST',
	data: new Object(),
	success: function() { },
	other: function() { }
};