var CIC = CIC || {};
var YAHOO;

( function () {

var loadedTab1 = false,
 dom = YAHOO.util.Dom,
 event = YAHOO.util.Event,
 connect = YAHOO.util.Connect;
var prevFlags = [];

CIC.TAB1 =  {
 
 	resetTab : function(doInit) {
 		loadedTab1 = false;
 		if ( typeof(doInit) === 'undefined' || doInit ) {
 			this.initialize();
 		}
	},
 	
 	initialize : function() {
		if ( !loadedTab1 )  {
			CIC.util.loadingApplication(true);
			CIC.util.loadingDisplay('tab1');
			connect.asyncRequest('GET', '/cic/PR?app=1&action=3', {
								success:this.handleSuccess,
								failure:this.handleFailure,
							  	cache:false,
							  	timeout: 30000, 
							  	argument: ['tab1',null,null], 
								scope: this });
						
		}
		
	},
 
	handleSuccess:function(o){
 		var html = null, msg = null,
   			dobj = dom.get( o.argument[0] ),
   			msgIdx = o.responseText.indexOf("--MESSAGE--",0),
   			oSubmitButton1, oSubmitButton2;
   		
   		html = o.responseText;
   		
   		//alert(html);
   		if ( msgIdx >= 0 ) {
   			html = o.responseText.substring(0,msgIdx);
   			msg = o.responseText.substring(msgIdx+11);
    	} 

		if ( msg !== null ) {
			CIC.util.loadingApplication(false);
			alert(msg);
			return;
		}
			
		dobj.innerHTML = html;	
		CIC.util.loadingApplication(false);

		scroll(0,0);

		if (o.argument[0] === "tab1" && !loadedTab1 ) { 
		 	loadedTab1 = true;
			oSubmitButton1 = new YAHOO.widget.Button("submitbutton11");
		    oSubmitButton1.addListener("click", this.saveProfile,this,true);   
	
			oSubmitButton1 = new YAHOO.widget.Button("refreshUserProfile");
		    oSubmitButton1.addListener("click", this.resetTab,this,true);   

			if ( document.getElementById( 'submitbutton12' ) ) {		  
				oSubmitButton2 = new YAHOO.widget.Button("submitbutton12");  
		    	oSubmitButton2.addListener("click", this.sendConfirm,this,true);
		    }   
	 	} 
		
	},

	handleFailure:function(o){
		CIC.util.loadingError();
		loadedTab1 = false;
	},

	handleJSON:function(o){
   		var response = [];   
   		var messages = [];

   		try {
        	response = YAHOO.lang.JSON.parse(o.responseText);
        	messages = CIC.util.arrayify(response.response.message);
        	prevFlags = CIC.util.arrayify(response.response.flagError);
        	
    		var alertmsg = "";
    		
        	
        	if ( response.response.verror ) {
        		alertmsg = "Errors:\n\n";
        	} 
    		for ( var i = 0; i < messages.length; i++ ) {
    			alertmsg += messages[i]+"\n\n";
    		}
    		
    		CIC.util.setFieldWarnings(prevFlags,"1-3");
    		
    		if ( messages.length > 0 )
    			alert(alertmsg);

        	if ( !response.response.verror ) {
        		this.resetTab(true);
        	}

        } catch (x) {   
            alert("Error - "+x.message);   
            return;   
        }   

		
	},


	saveProfile : function () {
		CIC.util.clearFieldWarnings(prevFlags,"1-3");
		var cObj, formObject = document.getElementById('profileform');
		connect.setForm(formObject);
		cObj = connect.asyncRequest('POST', '/cic/PR?out=json&button=1', {
								success:this.handleJSON,
								failure:this.handleFailure,
							  	cache:false,
							  	timeout: 30000, 
							  	argument: ['tab1',null,null], 
								scope: this });
	},
	
	sendConfirm : function () {
		CIC.util.clearFieldWarnings(prevFlags,"1-3");
		var cObj, formObject = document.getElementById('profileform');
		connect.setForm(formObject);
		cObj = connect.asyncRequest('POST', '/cic/PR?out=json&button=2', {
								success:this.handleJSON,
								failure:this.handleFailure,
							  	cache:false,
							  	timeout: 30000, 
							  	argument: ['tab1',"verification",null], 
								scope: this });
	}
	
	
};	

})();

