Framework.Modules.Notification=function(){
	this.loadTable=loadTable;
	var self=this;
    var confSent;
	var contacts = {};
	this.initSelf(this);
	var loading="<img src='images/loader.gif' width='16px' height='16px' style=\"padding-right: 5px\"/>Loading...";
	this.startWidget=function(){
		self.sendRequest("data");
	}
		
	function loadTable(){

        self.el.self = self;
		contacts = getJsonObj(self.data);
		self.el.innerHTML="";
		var h = self.el.parentNode.offsetHeight;
		//self.el.style.height = h+"px";
		
		//var moduleTable=new ContactTable(self.el,self.headerConfig,self.data, false, true, self.doDelete, self.showEdit, self.doActivate, self.sendConf);
//		moduleTable.drawTable();		
		self.info.view=TABLE_VIEW;

		
		
				var headers = [{ dataField: "firstName", headerText: Framework.Lang.AddContact_name ,  width: 100 ,sortable: true},
					       { dataField: "account",   headerText: Framework.Lang.AddContact_account ,width: 100 ,sortable: true },
						   { dataField: "types",    headerText: Framework.Lang.Type , width: 50 ,sortable: true },
						   { dataField: "confirmed",   headerText: "Confirmation" , width: 70 ,sortable: false, type: 'url' },
						   { dataField: "active",    headerText: " " , width: 70,sortable: false ,  type: 'activeMonitis'}
						 ];

		var contactModuleTable=new ContactFlashTable()
		contactModuleTable.drawTable(self.el,headers,self.data,self.doDelete, self.showEdit, self.doActivate, self.sendConf,confSent,ACCOUNT_NAMES);

		if (selectedContact){
			selectedContact = contacts[selectedContact.id];
			self.edit();
		}

	}
	
	this.doDelete = function(contactId){
		
		 var deleteConfirm = window.confirm("Are you sure to delete contact?");
          if(deleteConfirm==true){
          self.el.innerHTML=loading;
          RequestDispatcher.sendDEL(Framework.contactURL+User.userId+"/"+contactId, resp_delete, contactId);
        }
		
	}
	
	function resp_delete(resp, params){
		
		var responseStatus = eval("("+resp.responseText+")");
		
  	  	if(responseStatus.status==STATUS_OK){
   			if (selectedContact && params == selectedContact.id) self.closeEdit();

            self.sendRequest("data");
		}

	}
	
	this.doActivate = function(id,contId, checked){
		var contact = contacts[contId];
		var name = getName(contact.name);
		var email = contact.account;
		var typ =  ACCOUNT_TYPES[contact.type];
		var portab = contact.portable;
	    var countryId = contact.countryId;
    	contact.active = checked ? 1 : 0;
    	RequestDispatcher.sendPUT(Framework.contactURL+User.userId+"/"+contId+"/"+encode(name.firstName)+"/"+encode(name.lastName)+"/"+email+"/"+contact.active+"/"+typ+"//"+portab+"/"+countryId, resp_activate);
       	      	
       	for(var i=0;i<self.data.length; i++){
			if (self.data[i][0] == id){
				self.data[i][4] = contact.active;
			}
		}
	}
	
	function resp_activate(resp){
		var responseStatus = eval("("+resp.responseText+")");
  	  	if(responseStatus.status!=STATUS_OK){
   			alert("Error activating contact");
        }
	}
	
	 this.sendConf = function(id){
	  	var contact = contacts[id];
		 if (!contact.confSent){
	    	contact.confSent = true;
			confSent=true;
	    	var msg = "";
			switch(contact.type){
				case ACCOUNT_NAMES[0]: msg = "Confirm Email";break;
				case ACCOUNT_NAMES[1]:  msg = "Confirm mobile number";break;
				default: msg = "Confirm IM";break;
			}
	    	showHelp(msg);
			RequestDispatcher.sendPUT(Framework.contactURL+User.userId+"/sendConfirmation/"+id, self.resp_sendConf);
		}else{
			showHelp('Confirmation is sent');
		}
	}
	
	this.resp_sendConf = function(response){
		self.sendRequest("data");
	}
	
	var ACCOUNT_TYPES = {'Email':1,'SMS':2,'ICQ':3,'Yahoo':4,'MSN':5,'AOL':6,'Google':7};
	var ACCOUNT_NAMES = ['Email','SMS','ICQ','Yahoo','MSN','AOL','Google'];
	var IM_TYPES = [{name:"ICQ", id:3}, {name:"Yahoo", id:4}, {name:"MSN", id:5}, {name:"Google", id:7}]
	/////edit elements
	var editInputFirstname;
	var editInputLastName;
	var editInputAccount;
	var editInputIM;
	var editSelectIMType;
	var editInputMobile;
	var editAccountyType;
	var editMobileCode;
	var selectedContact;
	var portableTR;
	var inputWidth = "150px";
	var errorDiv;
	
	this.showEdit=function(id){
		if(!selectedContact || id != selectedContact.id ){
     		selectedContact = contacts[id];
      		self.editCollapse();
     	}else {
     		self.closeEdit();
     	}
    }
      
	this.closeEdit = function(){
		self.editExpand();
		selectedContact = null;
	}
	
	this.edit = function(){
		self.editContent.innerHTML = "";
    	var t;
    	var tab;//tbody
	    var tr;

    	errorDiv=document.createElement("div");
	    errorDiv.style.color = "red";
		errorDiv.style.padding = "2px";
		self.editContent.appendChild(errorDiv);

	    t=document.createElement("table");
		t.setAttribute("id", "notificationTABLE");
		t.setAttribute("class", "text");
    	t.setAttribute("cellpadding",1);
	    t.setAttribute("cellspacing","3");

    	tab=document.createElement("tbody");
	    t.appendChild(tab);
		  
		tr=document.createElement("tr");
	    tr.className = "editTableRow";
	    txt=document.createTextNode(Framework.Lang.AddContact_firstName + ":");
	    td=document.createElement("td");
	    td.className= "formLabelWrap";
	    td.appendChild(txt);
    	tr.appendChild(td);
	    td=document.createElement("td");

    	editInputFirstName = document.createElement("input");
		editInputFirstName.style.width = inputWidth;
        editInputFirstName.value = getName(selectedContact.name).firstName;
	    td.appendChild(editInputFirstName);
    	tr.appendChild(td);
    	tdForText = document.createElement("td");
    	tdForText.appendChild(document.createTextNode("*"));
    	tr.appendChild(tdForText);
    	tab.appendChild(tr);

    	tr=document.createElement("tr");
    	tr.className = "editTableRow";
      	txt=document.createTextNode(Framework.Lang.AddContact_lastName + ":");
      	td=document.createElement("td");
      	td.className= "formLabelWrap";
    	td.appendChild(txt);
	    tr.appendChild(td);
    	td=document.createElement("td");
	    editInputLastName=document.createElement("input");
       	editInputLastName.value=getName(selectedContact.name).lastName;
     	td.appendChild(editInputLastName);
		editInputLastName.style.width = inputWidth;
        tdForText = document.createElement("td");
    	tdForText.appendChild(document.createTextNode("*"));
    	tr.appendChild(td);
    	tr.appendChild(tdForText);
    	tab.appendChild(tr);
		var contactType = selectedContact.type;
		var account = selectedContact.account;
		if(contactType == "Email"){ 
			tr=document.createElement("tr");
			tr.name = "Email";
		    tr.className = "editTableRow";
	    	txt=document.createTextNode(Framework.Lang.AddContact_account + ":");
		    td=document.createElement("td");
		    td.className= "formLabelWrap";
		   	td.appendChild(txt);
		    tr.appendChild(td);
	    	td=document.createElement("td");
		    editInputAccount=document.createElement("input");  
	    	editInputAccount.value = account;
			editInputAccount.style.width = inputWidth;
	     	td.appendChild(editInputAccount);
			tdForText = document.createElement("td");
			tdForText.appendChild(document.createTextNode("*"));
			tr.appendChild(td);
			tr.appendChild(tdForText);
			/*var EmailHelp = document.createElement("td");
    	  	EmailHelp.innerHTML = '<img src="images/homePage/Help.gif" onclick="showHelp(\'Email contact\')" style="cursor:pointer" />';
    	  	tr.appendChild(EmailHelp);*/
		   	tab.appendChild(tr);
		}else if (contactType == "SMS"){
			//country
			var  trc=document.createElement("tr");
			tr.name = "SMS";
			trc.className = "editTableRow";
		    txt=document.createTextNode(Framework.Lang.countryCode + ":");
	
	    	td=document.createElement("td");
	    	td.className= "formLabelWrap";
		    td.appendChild(txt);
	    	trc.appendChild(td);
		    countryTD=document.createElement("td");
	    	trc.appendChild(countryTD);
			tab.appendChild(trc);
			
					  
			// phone number
			  tr=document.createElement("tr");
			  tr.name = "SMS";
			  tr.className = "editTableRow";
		      txt=document.createTextNode(Framework.Lang.AddContact_mobilePhone + ":");
	
	    	  td=document.createElement("td");
	    	  td.className= "formLabelWrap";
	    	  td.vAlign="top";
	    	  td.style.paddingTop="2px";
		      td.appendChild(txt);
	    	  tr.appendChild(td);
		      td=document.createElement("td");
	    	  editInputMobile=document.createElement("input");
			  editInputMobile.style.width = inputWidth;
			  editInputMobile.value=account;
			  
			  helpText=document.createElement("div");
			  helpText.className = "chars";
			  helpText.innerHTML = Framework.Lang.mobileHelp;
	
		      td.appendChild(editInputMobile);
		      td.appendChild(helpText);
	    	  tr.appendChild(td);
			  tdForText = createAsterixTD();
	    	  tr.appendChild(tdForText);
			 
	    	  tab.appendChild(tr);
			  
			  /// portable 
			  var trp=document.createElement("tr");
			  trp.name = "SMS_p";
			  trp.className = "editTableRow";
		      txt=document.createTextNode(Framework.Lang.AddContact_portable + ":");
	
	    	  td=document.createElement("td");
	    	  td.className= "formLabelWrap";
		      td.appendChild(txt);
	    	  trp.appendChild(td);
		      td=document.createElement("td");
	    	  editCheckboxPortable=document.createElement("input");
	    	  editCheckboxPortable.checked=selectedContact.portable;
			  editCheckboxPortable.setAttribute("type","checkbox");
	
		      td.appendChild(editCheckboxPortable);
	    	  trp.appendChild(td);
			 			  
			  var phoneHelp = document.createElement("td");
	    	  phoneHelp.innerHTML = '<img src="images/homePage/Help.gif" onclick="showHelp(\'Portable phone number\')" style="cursor:pointer" />';
	    	  trp.appendChild(phoneHelp);
	    	  tab.appendChild(trp);
			  portableTR = trp;
			  /// end portable
		 	
		 	
			if (selectedContact.confirmed == 0){ 
				 tr=document.createElement("tr");
				  tr.name = "SMS";
				  tr.className = "editTableRow";
			      txt=document.createTextNode(Framework.Lang.AddContact_code + ":");
		
		    	  td=document.createElement("td");
		    	  td.className= "formLabelWrap";
			      td.appendChild(txt);
		    	  tr.appendChild(td);
			      td=document.createElement("td");
		    	  editMobileCode=document.createElement("input");
				  editMobileCode.style.width = inputWidth;
				  td.appendChild(editMobileCode);
		    	  tr.appendChild(td);
		    	  var ActiveCodeHelp = document.createElement("td");
    	  		  ActiveCodeHelp.innerHTML = '<img src="images/homePage/Help.gif" onclick="showHelp(\'SMS activation\')" style="cursor:pointer" />';
	    	  	  tr.appendChild(ActiveCodeHelp);
		    	  tab.appendChild(tr);
		    	  verCode_tr = tr;
	    	}else{
	    		editMobileCode = null;
	    	}
	    	
	    	if (COUNTRY_CODES){
	        	fillCountryCodes();
	        } else {
	        	request_getCountryCodes();
	        }
		}else{
	     	tr=document.createElement("tr");
		  	tr.name = "IM";
	      	tr.className = "editTableRow";
	      	txt=document.createTextNode(Framework.Lang.AddContact_IMType + ":");
	    	td=document.createElement("td");
    	    td.className= "formLabelWrap";
	        td.appendChild(txt);
    	   	tr.appendChild(td);
	       	td=document.createElement("td");
			editSelectIMType=document.createElement("select");
    	  	editSelectIMType.style.width = inputWidth;
    	  	editSelectIMType.id = "selectIMType";
		    
		    for (var i=0; i<IM_TYPES.length;i++){
			    txt=document.createTextNode(IM_TYPES[i].name);
	    	  	var option=document.createElement("option");
		      	option.value=IM_TYPES[i].id;
	    	  	option.appendChild(txt);
		      	editSelectIMType.appendChild(option);
		      	if (contactType == IM_TYPES[i].name){
		      		option.selected = true; 
		      	}
	      	}

			td.appendChild(editSelectIMType);
	      	tr.appendChild(td);
	      	tab.appendChild(tr);
	      		
		  	tr=document.createElement("tr");
		 	tr.name = "IM";
		  	tr.className = "editTableRow";
	      	txt=document.createTextNode(Framework.Lang.AddContact_IM + ":");

    	  	td=document.createElement("td");
    	  	td.className= "formLabelWrap";
	      	td.appendChild(txt);
    	  	tr.appendChild(td);
	      	td=document.createElement("td");
    	  	editInputIM=document.createElement("input");
		  	editInputIM.style.width = inputWidth;
    	  	editInputIM.value = account;

	      	td.appendChild(editInputIM);
    	  	tr.appendChild(td);
		  	tdForText = document.createElement("td");
    	  	tdForText.appendChild(document.createTextNode("*"));
		  	tr.appendChild(tdForText);
		  
		  	/*var IMHelp = document.createElement("td");
    	  	IMHelp.innerHTML = '<img src="images/homePage/Help.gif" onclick="showHelp(\'IM contact\')" style="cursor:pointer" />';
    	  	tr.appendChild(IMHelp);*/
    	  	tab.appendChild(tr);
		}
		  
      
	      tr=document.createElement("tr");
		  tr.style.paddingTop = "7px";
	      td=document.createElement("td");
	      td.colSpan="2";
	      editButton = document.createElement("INPUT");
		  editButton.setAttribute( "type", "button" );
		  editButton.setAttribute( "value", Framework.Lang.editApply );
		  editButton.setAttribute( "id", "editButton" );
		  td.appendChild(editButton);
	      
	      closeButton = document.createElement("INPUT");
		  closeButton.setAttribute( "type", "button" );
		  closeButton.setAttribute( "value", Framework.Lang.close );
		  closeButton.setAttribute( "id", "closeButton" );
		  td.appendChild(closeButton);
	      	      
	      tr.appendChild(td);
    	  tab.appendChild(tr);
		  
		  closeButton.onclick = self.closeEdit;
		  
	      editButton.onclick=function(){
    	    errorDiv.innerHTML = "";
			var first=editInputFirstName.value.trim();
	       	var second=editInputLastName.value.trim();
		    var portable = false;
			var ttype = contactType;
			var account;
			var accountType;
			var verCode = "";
			var country = 1;
			var sendWeeklyReport = selectedContact.sendWeeklyReport;
			if (ttype == 'Email'){
				account = editInputAccount.value.trim();
				accountType = 1;
				validateMessage = validateMail(first, second, account);
			}else if(ttype == 'SMS'){
				account = editInputMobile.value;
				accountType = 2;
				verCode = editMobileCode ? editMobileCode.value : "";
				portable = editCheckboxPortable.checked;
				country = $id("selectCountry").value;
				validateMessage=validateSMS(first, second, account);
			}else{
     	   		account = editInputIM.value;
				accountType = $id("selectIMType").value;
				validateMessage=validateIM(first, second, account);
			}
			if(validateMessage!="success"){
           		errorDiv.innerHTML = validateMessage;
    	      	return;
            }
		   	var status=1;

 	       	var notification_data="/"+selectedContact.id+"/"+encode(first)+"/"+encode(second)+"/"+encode(account)+"/"+
 	       						status+"/" + accountType+"/"+verCode+"/"+portable+"/"+country+"/"+sendWeeklyReport;
	 	  	//self.el.innerHTML=loading;
		    RequestDispatcher.sendPUT(Framework.contactURL+User.userId+notification_data, resp_edit);
	   }
	  
       self.editContent.appendChild(t);
       if (selectedContact.countryId == AUSTRALIA_ID) {
			portableTR.style.display = "";
	   }else if (portableTR){
			portableTR.style.display = "none";
	   }
	}
	
	var getName=function(name){
		var ind=name.indexOf(" ");
  	 	var first=name.substring(0,ind) ;
		var last=name.substring(ind+1);
   		var r=new Object();
   		r.firstName=first;
  		r.lastName=last;
   		return r;
	}
	
	var resp_edit=function(response){
		var responseStatus=eval("("+response.responseText+")");
		if(responseStatus.status!=STATUS_OK){
			errorDiv.innerHTML = responseStatus.status;
	   }else {
	   		self.sendRequest("data");
	   }
	}
	
	var fillCountryCodes = function(){
		var countryChanged = function(countryId){
			if (countryId == AUSTRALIA_ID) {
				portableTR.style.display = "";
			}else{
				portableTR.style.display = "none";
			}
		}
		editSelectCountry = document.createElement("select");
		editSelectCountry.style.width = inputWidth;
		editSelectCountry.id = "selectCountry"; 
		editSelectCountry.onchange = function(){
			countryChanged($id("selectCountry").value);
		}
		var countryId = COUNTRY_CODES[0].value;
    	for (var i=0;i<COUNTRY_CODES.length;i++){
			txt=document.createTextNode(COUNTRY_CODES[i].label);
	        var option=document.createElement("option");
    	    option.value=COUNTRY_CODES[i].value;
	        option.appendChild(txt);
    	    editSelectCountry.appendChild(option);
    	    if (selectedContact.countryId == COUNTRY_CODES[i].value){
    	    	option.selected = true;
    	    	countryId = COUNTRY_CODES[i].value;
    	 	}
		}
		countryTD.innerHTML = "";
		countryTD.appendChild(editSelectCountry);
		countryChanged(countryId);
	}
	
	var process_getCountryCodes = function(response){
		COUNTRY_CODES = eval("("+response.responseText+")");
		Utils.sort(COUNTRY_CODES, "name", SORT_TYPE_DESC);
		fillCountryCodes();
	}
	 
	var request_getCountryCodes = function(){
		RequestDispatcher.sendGET(Framework.contactURL+User.userId+"/countrycode", process_getCountryCodes);
	}
	
	var createAsterixTD = function(){
		var tdForText = document.createElement("td");
    	tdForText.vAlign="top";
    	tdForText.style.paddingTop="2px";
    	tdForText.appendChild(document.createTextNode("*"));
    	return tdForText;
	}
	var getJsonObj = function(dataArray){

		var arr = {};
		for(var i=0;i<dataArray.length; i++){
			var cont = {};
			cont.id = dataArray[i][0];
			cont.name = dataArray[i][1];
			cont.account = dataArray[i][2];
			cont.type = dataArray[i][3];
			cont.confirmed = dataArray[i][4];
			cont.active = dataArray[i][5];
			cont.portable = dataArray[i][6];
			cont.countryId = dataArray[i][7];
			cont.sendWeeklyReport = dataArray[i][8];
			arr[cont.id] = cont;
		}
		return arr;
	}
}

Framework.Modules.Notification.prototype = new Framework.Module();
Framework.Modules.Notification.prototype.constructor = Framework.Modules.Notification;