function DImportInfo(importInfo,container){
	$('dName').update(importInfo.Name);
	$('dId').value=importInfo.Id;
	$('deleteErrorContainer').hide();	
	container.show();
	$('deleteButton').focus();
}

function DeleteImportInfo(updateButtonId,container){
	var importInfoId=$F('dId');
	var editBox = {
			Container:container,
			UpdateButtonId:updateButtonId,
			ErrorBoxId:'deleteError'
		}			            
		Emails.DeleteImportInfo(importInfoId,OnSucceeded, OnError, editBox);
}
//################################################################# message FUNCTIONS
/**
 * Function is called on click when delete message button is clicked
 * It populates container with values of current (clicked) message
 * @param {Object} message
 * @param {Object} container
 */
function DMessage(message,container){
	$('dCaption').update(message.caption);
	$('dId').value=message.id;
	$('deleteErrorContainer').hide();	
	container.show();
	$('DeleteOkButton').focus();
}
/**
 * deletes a message or a  draft
 * @param {Object} messageId
 * @param {Object} updateButtonId
 */
function DeleteMessage(messageId,updateButtonId,container)
{   	
	if(messageId && messageId!=0){	
		var editBox = {
			Container:container,
			UpdateButtonId:updateButtonId,
			ErrorBoxId:'deleteError'
		}			            
		Messages.DeleteMessage(messageId,OnSucceeded, OnError, editBox);
	}
}
/**
 * Function is called on click when start ssending message button is clicked
 * It populates container with values of current (clicked) message
 * @param {Object} message
 * @param {Object} container
 */
function StartMessagePop(message,container){
	$('startCaption').update(message.caption);
	$('startMessageId').value=message.id;
	$('startErrorContainer').hide();
	container.show();
}
/**
 * starts sending a message 
 * @param {Object} messageId
 * @param {Object} updateButtonId
 */
function StartMessage(messageId,updateButtonId,container)
{   	
	if(messageId && messageId!=0){	
		var editBox = {
			Container:container,
			UpdateButtonId:updateButtonId,
			ErrorBoxId:'startError'
		}			            
		Messages.StartMessage(messageId,OnSucceeded, OnError, editBox);
	}
}
/**
 * Function is called on click when stop sending message button is clicked
 * It populates container with values of current (clicked) message
 * @param {Object} message
 * @param {Object} container
 */
function StopMessagePop(message,container){
	$('stopCaption').update(message.caption);
	$('stopMessageId').value=message.id;
	$('stopErrorContainer').hide();
	container.show();
}
/**
 * stops sending a message 
 * @param {Object} messageId
 * @param {Object} updateButtonId
 */
function StopMessage(messageId,updateButtonId,container)
{   	
	if(messageId && messageId!=0){	
		var editBox = {
			Container:container,
			UpdateButtonId:updateButtonId,
			ErrorBoxId:'stopError'
		}			            
		Messages.StopMessage(messageId,OnSucceeded, OnError, editBox);
	}
}


function CopyMessagePop(message,container){
	$('copyMessageSavingImage').hide();
	$('copyMessageCaption').update(message.caption);
	$('copyMessageId').value=message.id;
	$('copyMessageErrorContainer').hide();
	container.show();
}
function CopyMessage(messageId,updateButtonId,container,redirectUrl){
	$('copyMessageSavingImage').show();
	$('copyMessageErrorContainer').hide();
	var asTemplate=$('copyMessageTargetTemplate').checked == true;
	var editBox = {			
			ErrorBoxId:'copyMessageError',
			LoadingImg:'copyMessageSavingImage',			
			Container:container,
			UpdateButtonId:updateButtonId,
			RedirectUrl:redirectUrl,
			AsTemplate: asTemplate
		}
	Messages.CopyMessage(messageId,asTemplate,OnSucceededCopyMessage, OnError, editBox)
}
function OnSucceededCopyMessage(result, editBox)
{	
	if(editBox.RedirectUrl && result){
		if(editBox.AsTemplate){
			window.location=editBox.RedirectUrl+"TemplatesWizard.aspx?messageid="+result;
		}else{
			window.location=editBox.RedirectUrl+"Wizard.aspx?messageid="+result;
		}
	}	
	if(editBox.Container){	editBox.Container.hide();	}
	if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}
	if(editBox.UpdateButtonId){__doPostBack($(editBox.UpdateButtonId).id,'');}
}
function SaveMessage(inputControls, testBoxModal){
		$('SavingImage').show();
		$('messageSaveErrorContainer').hide();
		var messageWrapper = {
			Id:$(inputControls.MessageIdInput).value,
			OriginalBody:inputControls.WYSIWYGUnited.getTextFunction(),
			Caption:$(inputControls.CaptionInput).value,
			FromName:$(inputControls.FromNameInput).value,				
			FromEmail:$(inputControls.FromEmailInput).value,
			AttachmentsArray:$(inputControls.AttachmentsInput).value,
			AutoresponderCategoryId:$(inputControls.MessageAutoresponderCategoryInput).value,
			IsDraft:$(inputControls.IsDraftInput).value,
			MessageType:$(inputControls.MessageTypeInput).value						
		}
		var editBox = {			
			ErrorBoxId:'messageSaveError',
			LoadingImg:'SavingImage',
			TestBoxModal:testBoxModal,
			InputControls:inputControls
		}
		Messages.SaveMessage(Sys.Serialization.JavaScriptSerializer.serialize(messageWrapper),OnSucceededSaveMessage, OnError, editBox);
}

function OnSucceededSaveMessage(result, editBox)
{	
	
	$(editBox.InputControls.MessageIdInput).value=result;
	if($('SendTestMessageId')){$('SendTestMessageId').value=result;}
	if(editBox.Container){	editBox.Container.hide();	}
	if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}
	if(editBox.UpdateButtonId){__doPostBack($(editBox.UpdateButtonId).id,'');}
	if(editBox.TestBoxModal){$find(editBox.TestBoxModal).show();}
}
function GrabHTMLCode(container,WYSIWYGUnitedControl){
	$('GrabLoadingImg').show();
	var url=$F('grabUrlInput');
	if(url && url!=""){
		var editBox = {
				Container:container,
				WYSIWYGUnited:WYSIWYGUnitedControl,
				LoadingImg:'GrabLoadingImg',
				ErrorBoxId:'grabError'
		}			
		Messages.GrabHTMLCode(url,OnGrabSucceeded, OnError, editBox);
	}
}
/**
 */
function OnGrabSucceeded(result, editBox)
{			
	if(editBox.Container){	editBox.Container.hide();	}
	if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}	   
	editBox.WYSIWYGUnited.setTextFunction(result.toString()); 
}
/**
recreates system messages opt-in opt-out admin notifications
 */
function RecreateSystemMessages(updateButtonId,container){
	var editBox = {
			Container:container,
			UpdateButtonId:updateButtonId,
			ErrorBoxId:'recreateSystemMessagesError'
		}		
	Messages.RecreateSystemMessages(OnSucceeded, OnError, editBox);
}
/**
Sends test messages
 */
function SendTestMessage(container,ajaxedCheckBox){
$('sendTestDoneContainer').hide();
$('sendTestErrorContainer').hide();
$('sendTestLoading').show();
var result = []; 
$(ajaxedCheckBox).value.split(',').compact().each(function(s) {
val=s.split('=')[1];
if(!isNaN(parseInt(val))){
	result.push(parseInt(val)); 
}
}); 
if(result.length>0){
	var sendTestParamsWrapper = {
		Email:$F('testEmail'),
		CategoryIds:result,
		EmailsNum:$F('testNumber'),
		MessageId:$F('SendTestMessageId')
	}
	var editBox = {
			Container:container,			
			ErrorBoxId:'sendTestError',
			LoadingImg:'sendTestLoading'
		}		
	Messages.SendTest(Sys.Serialization.JavaScriptSerializer.serialize(sendTestParamsWrapper), OnSendTestSucceeded, OnError, editBox);
	}else{
	$('sendTestLoading').hide();
	$('sendTestErrorContainer').show();
	$('sendTestError').update("You need to select a list.");	
	}
}
//################################################################# contact FUNCTIONS
/**
 * Function is called on click when edit contact button is clicked
 * It populates edit container with values of current (clicked) contact
 * @param {Object} address json representation of a contact
 * @param {Object} container a modal container id to call show()
 */
function EContact(address, container, categoryId, categoryName, trnslon) {
		$('EmailCheckResult').removeClassName('editerror');
		$('EmailCheckResult').removeClassName('editok');
		if (address.email.blank()) {
		    $('EmailCheckRow').show();
		    $('EmailCheckResult').update(Translate(trnslon, 'TypeEmailFirst'));
		} else {
		    $('EmailCheckRow').hide();
		    $('EmailCheckResult').update('');
		}
		$('eFName').value=address.fname;
		$('eLName').value=address.lname;
		$('eEmail').value = address.email;
		if (address.softBounces == '' || isNaN(parseInt(address.softBounces))) {address.softBounces = 0; }
		if (address.hardBounces == '' || isNaN(parseInt(address.hardBounces))) {address.hardBounces = 0; }
		$('eSoftBounces').value = address.softBounces;
		$('eHardBounces').value = address.hardBounces; 
		
		$('eId').value=address.id;		
		if($('eCategoryId')){
			$('eCategoryId').value=categoryId;
		}
		if($('eCategoryName')){
			$('eCategoryName').update(categoryName);
		}
		$('editContactErrorContainer').hide();
		var emailAddressWrapper = {
			Id:$('eId').value,
			FirstName:$('eFName').value,
			LastName:$('eLName').value,
			Email:$('eEmail').value,				
			Categories:categoryId,
			Properties:eval($('propertiesTextBoxId').value)
		}
		var editBox = {
			Container:container,						
			ErrorBoxId:'pageError'
		}
		if(address.id!=0){
			Emails.EmailAddressDefaultPropertiesValues(Sys.Serialization.JavaScriptSerializer.serialize(emailAddressWrapper),
	 	EmailAddressDefaultPropertiesValuesOnSucceeded, OnError, editBox);
	 	}else{
			setProperties(emailAddressWrapper.Properties);
			editBox.Container.show();
		}						
}
/**
 * the callback function that processes the Web Service return value and hides the box.
 * also makes a postback to update the list
 * @param {Object} result server output
 * @param {Object} editBox a json object contains modal container and UpdateButton
 */
function EmailAddressDefaultPropertiesValuesOnSucceeded(result, editBox)
{			
	setProperties(eval(result));
	editBox.Container.show();
}

/**
 * Updates contact. Is called on Save button click
 * Creates a josn object from textboxess and calls server function
 * @param {Object} categoriesTextBoxId
 * @param {Object} updateButtonId
 * @param {Object} container
 */
function UContact(updateButtonId,container,categoryId)
{   			
	var emailAddressWrapper = {
		Id:$('eId').value,
		FirstName:$('eFName').value,
		LastName:$('eLName').value,
		Email:$('eEmail').value,
		Categories: $('eCategoryId').value,	
		SoftBounces: $('eSoftBounces').value,
		HardBounces: $('eHardBounces').value,
		Properties:collectProperties(eval($('propertiesTextBoxId').value))
	}
	var editBox = {
		Container:container,
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'editContactError'
	}
	Emails.UpdateEmailAddress(Sys.Serialization.JavaScriptSerializer.serialize(emailAddressWrapper), OnSucceeded, OnError, editBox);	
}
//Function to check email if address already exists
//if exists it binds contorls vith values
function CheckEmail(trnslon){
	if($('eEmail').value!=''){
		$('EmailCheckResult').removeClassName('editerror'); 
		$('EmailCheckResult').removeClassName('editok');
		$('EmailCheckResult').update(Translate(trnslon, 'TypeEmailFirst'));
		$('EmailManageImage').show();
		var emailAddressWrapper = {
			Id:$('eId').value,
			FirstName:$('eFName').value,
			LastName:$('eLName').value,
			Email:$('eEmail').value,		
			Categories:$('eCategoryId').value,			
			Properties:collectProperties(eval($('propertiesTextBoxId').value))
		}
		var editBox = {		
			LoadingImg:'EmailManageImage',
			ErrorBoxId:'editContactError'
		}
		Emails.CheckEmailAddress(Sys.Serialization.JavaScriptSerializer.serialize(emailAddressWrapper), CheckEmailOnSucceeded, OnError, editBox);
	}else{
		$('EmailCheckResult').toggleClassName('editerror');
		$('EmailCheckResult').update(Translate(trnslon, 'EmailAddressRequired'));
	}
}
function CheckEmailOnSucceeded(result, editBox){
    if (editBox.LoadingImg) { $(editBox.LoadingImg).hide(); }
    $('EmailCheckRow').show();	
	$('EmailCheckResult').update(result.CheckResult);
	if(result.CheckStatus==1){//valid	
		$('EmailCheckResult').toggleClassName('editok');
	}
	if(result.CheckStatus==2){//notvalid
		$('EmailCheckResult').toggleClassName('editerror');
	}
	if($('eId').value=='' || $('eId').value=='0'){//we update only for new email
		$('eId').value=result.Id;
		$('eFName').value=result.FirstName;
		$('eLName').value=result.LastName;
		$('eEmail').value=result.Email;
		setProperties(result.Properties);
	}
}

function ContactAddInCategory(properties,updateButtonId,container,editPropertiesContainer,categoriesContainer,contactId,categoryId){
	var emailAddressWrapper = {		
		Id:contactId,
		Categories:categoryId,			//this should be string
		Properties:collectProperties(properties)
	}
	var editBox = {
		Container:container,
		UpdateButtonId:updateButtonId,
		EditPropertiesContainer:editPropertiesContainer,
		CategoriesContainer:categoriesContainer,
		ErrorBoxId:'addInMoreCategoriesError'
	}
	Emails.EmailAddressAddInCategory(Sys.Serialization.JavaScriptSerializer.serialize(emailAddressWrapper),categoryId,
	 ContactAddInCategoryOnSucceeded, OnError, editBox);	
}
/**
 * @param {Object} result server output
 * @param {Object} editBox a json object contains modal container and UpdateButton
 */
function ContactAddInCategoryOnSucceeded(result, editBox)
{			
	$(editBox.EditPropertiesContainer).hide();
	$(editBox.CategoriesContainer).hide();	
	if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}	
}

/**
 * Shows delete container and populates inner controls
 * @param {Object} address
 * @param {Object} container
 */
function SDContact(address,container,trnslon){
	$('dId').value=address.id;
	$('deleteContactErrorContainer').hide();
	$('dFullName').update(address.fullName);		
	if($('dCatId').value=='0' || $('dCatId').value==''|| $('dCatId').value=='-1'){
	    $('dCatName').update(Translate(trnslon, 'AllCategoriesAndSystem'));
	}	
	container.show();
	$('deleteContactButton').focus();
}
function DContact(updateButtonId,container){
	var emailAddressId =$('dId').value;	
	var categoryId=$('dCatId').value;
	var editBox = {
		Container:container,
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'deleteContactError'
	}
	if(emailAddressId && emailAddressId!=0){			            
		if(categoryId=='0' || categoryId=='' || categoryId=='-1'){
		    	Emails.DeleteEmailAddress(emailAddressId,OnSucceeded, OnError, editBox);
		}else{
					Emails.DeleteEmailAddressFromCategory(emailAddressId,categoryId,OnSucceeded, OnError, editBox);
		}
	}
}
function ToggleContact(updateButtonId,emailAddressId){
	var editBox = {		
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'pageError'
	}
	if(emailAddressId && emailAddressId!=0){			            
    	Emails.ToggleEmailAddress(emailAddressId,OnSucceeded, OnError, editBox);
	}
}
function DFromCategory(emailToCategory,updateButtonId,container){
	$('DeleteEmailToCategoryErrorContainer').hide();
	$('eCategoryName').update(emailToCategory.Name);
	$('eEmailToCategoryId').value=emailToCategory.Id;
	container.show();
}
function DeleteFromCategory(updateButtonId,container){
	var editBox = {
		Container:container,		
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'DeleteEmailToCategoryError'		
	}
	Emails.DeleteEmailToCategory($('eEmailToCategoryId').value,OnSucceeded, OnError, editBox)
}
function PreviewContact(contactId, container){
	var editBox = {
		Container:container,		
		LoadingImg:$('previewContactLoading'),
		ErrorBoxId:'previewContactError'
	}
	$('previewData').update("");
	$('previewData').hide();
	$('previewContactErrorContainer').hide();
	editBox.LoadingImg.show();
	container.show();
	Emails.EmalAddressPreview(contactId,PreviewContactOnSucceeded, OnError, editBox)
}
function PreviewContactOnSucceeded(result,editBox){
	editBox.LoadingImg.hide();
	$('previewData').update(result);
	$('previewData').show();
}
//################################################################# Properties FUNCTIONS
function EProperty(property,container){
	$('eName').value=property.name;
	$A($('eType').options).each(function(n) { if(n.value==property.type){n.selected=true}else{n.selected=false}});
	$('eData').value=property.data;
	$('eRequired').checked=property.isRequired;	
	$('eId').value=property.id;	
	$('editPropertyErrorContainer').hide();
	showDdl();	
	container.show();
}
function MoveProperty(prpertyId,moveDown,updateButtonId){
	var editBox = {
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'pageError'
	}
	Properties.MoveProperty(prpertyId,moveDown,OnSucceeded, OnError, editBox);
}
function UpdateProperty(updateButtonId,container){
	var propertyWrapper = {
		Id:$('eId').value,
		Name:$('eName').value,		
		Data:$('eData').value,
		IsRequired:$('eRequired').checked,						
		PropertyType:$('eType').value,
		CategoryId:$('eCategoryId').value
	}
	var editBox = {
		Container:container,
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'editPropertyError'
	}
	Properties.UpdateProperty(Sys.Serialization.JavaScriptSerializer.serialize(propertyWrapper),
	 OnSucceeded, OnError, editBox);	
}
function DProperty(property,container){
	$('dId').value=property.id;
	$('dCaption').update(property.name);
	container.show();
}
function DeleteProperty(property,updateButtonId,container){
	var id=$('dId').value;	
	var editBox = {
		Container:container,
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'deleteError'
	}
	Properties.DeleteProperty(id, OnSucceeded, OnError, editBox);	
}
//################################################################# category FUNCTIONS
function UCategory(category, container){	
	$('eName').value=category.name;
	$('eDescription').value=category.description;
	$('eDoubleOptIn').checked=category.doubleOptIn;
	$('eDoubleOptOut').checked=category.doubleOptOut;
	$('eOptInAdmin').checked=category.optInAdmin;
	$('eOptOutAdmin').checked=category.optOutAdmin;
	$('eSubscriptionDoubleOptInThankYouUrl').value=category.subscriptionDoubleOptInThankYouUrl;
	$('eUnsubscriptionDoubleOptOutThankYouUrl').value=category.unsubscriptionDoubleOptOutThankYouUrl;
	$('eUnsubscriptionThankYouUrl').value=category.unsubscriptionThankYouUrl;
	$('eId').value=category.id;
	$('editCategoryErrorContainer').hide();
	ToggleUCategoryTds();
	container.show();
}
function ToggleUCategoryTds() {
    if ($('eDoubleOptIn').checked) {
        $('subscribeDoubleOptInThankYouUrlTd').show();
        $('subscribeDoubleOptInThankYouUrlFieldTd').show();
    } else {
        $('subscribeDoubleOptInThankYouUrlTd').hide();
        $('subscribeDoubleOptInThankYouUrlFieldTd').hide();
    }
    if ($('eDoubleOptOut').checked) {
        $('unsubscribeDoubleOptOutThankYouUrlFieldTd').show();
        $('unsubscribeDoubleOptOutThankYouUrlTd').show();
    } else {
        $('unsubscribeDoubleOptOutThankYouUrlFieldTd').hide();
        $('unsubscribeDoubleOptOutThankYouUrlTd').hide();
    }
}
function UpdateCategory(updateButtonId,container){				
	var categoryWrapper = {
		Id:$('eId').value,
		Name:$('eName').value,
		Description:$('eDescription').value,
		DoubleOptIn:$('eDoubleOptIn').checked,
		DoubleOptOut:$('eDoubleOptOut').checked,
		SubscribedAdminConfirmation:$('eOptInAdmin').checked,
		UnsubscribedAdminConfirmation:$('eOptOutAdmin').checked,
		SubscriptionDoubleOptInThankYouUrl:$('eSubscriptionDoubleOptInThankYouUrl').value,
	    UnsubscriptionThankYouUrl:$('eUnsubscriptionThankYouUrl').value,
	    UnsubscriptionDoubleOptOutThankYouUrl:$('eUnsubscriptionDoubleOptOutThankYouUrl').value
	}
	var editBox = {
		Container:container,
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'editCategoryError'
	}
	Emails.UpdateCategory(Sys.Serialization.JavaScriptSerializer.serialize(categoryWrapper),
							OnSucceeded, OnError, editBox);
}
/**
 * Function is called on click when delete category button is clicked
 * It populates container with values of current (clicked) category
 * @param {Object} message
 * @param {Object} container
 */
function DCategory(objectWrapper,container){
	$('dCaption').update(objectWrapper.caption);
	$('dId').value=objectWrapper.id;
	$('deleteErrorContainer').hide();
	container.show();
}
function DeleteCategory(updateButtonId,container)
{
	var categoryId=$('dId').value;
	if(categoryId && categoryId!=0){
			var editBox = {
				Container:container,
				UpdateButtonId:updateButtonId,
				ErrorBoxId:'deleteError'
			}			            
			Emails.DeleteCategory(categoryId,OnSucceeded, OnError, editBox);		
	}
}
//################################################################# Account FUNCTIONS
function UAccount(account, container){	
	$('eName').value=account.name;
	$('eAddress').value=account.address;
	$('eEmail').value=account.email;
	$('ePhone').value=account.phone;
	$('eState').value=account.state;
	$('eCity').value=account.city;
	$('eZip').value=account.zip;
	$('eActive').checked=account.active;
	$('eCreateDate').value=account.createDate;
	//account settings
	$('eCredits').value=account.credits;
	$('eBounceEmailAddress').value=account.bounceEmail;
	$('eBouncePOP3Server').value=account.bouncePOPServer;
	$('eBouncePop3User').value=account.bouncePOPUser;
	$('eBouncePOP3Password').value=account.bouncePOPPassword;
	$('eSMTPServer').value=account.smtpServer;
	$('eSMTPUser').value=account.smtpUser;
	$('eSMTPPassword').value=account.smtpPassword;
	$('eBatchSize').value=account.batchSize;
	$('ePoolSize').value = account.poolSize;
	$('eMessageTimeout').value = account.messageTimeout;
	$('eBatchTimeout').value = account.batchTimeout;
	$('eMaxHardBounces').value = account.maxHardBounces;
	$('eMaxSoftBounces').value = account.maxSoftBounces;
	$('eEnableGeoLocation').checked = account.enableGeoLocation;
	if(account.smtpPortNumber!='0'){
		$('eSMTPPort').value=account.smtpPortNumber;
	}else{
		$('eSMTPPort').value='';
	}
	
	$('eId').value=account.id;	
	if(account.id==0){
		$('FirstUserTable').show();
	}	   else{
		$('FirstUserTable').hide();
	}
	$('editAccountErrorContainer').hide();
	PoolSize_onchange();
	container.show();
}
/**
* a helper function to enable/disable poolsize eMessageTimeout field
**/
function PoolSize_onchange() {
    $('eMessageTimeout').disabled = 'disabled';
    //$('MessageTimeoutRow').hide();
    if ($('ePoolSize').value == 1) {
        //$('MessageTimeoutRow').show();
        $('eMessageTimeout').disabled = '';
    } else {
        $('eMessageTimeout').value = '0';
    }
}
function UpdateAccount(updateButtonId,container){

	var accountWrapper = {
		id:$('eId').value,
		name:$('eName').value,
		address:$('eAddress').value,
		email:$('eEmail').value,
		phone:$('ePhone').value,
		state:$('eState').value,
		city:$('eCity').value,
		zip:$('eZip').value,
		active:$('eActive').checked,
		createDate:$('eCreateDate').value,
		credits:$('eCredits').value,
		bounceEmail:$('eBounceEmailAddress').value,
		bouncePOPServer:$('eBouncePOP3Server').value,
		bouncePOPUser:$('eBouncePop3User').value,
		bouncePOPPassword:$('eBouncePOP3Password').value,
		smtpServer:$('eSMTPServer').value,
		smtpUser:$('eSMTPUser').value,
		smtpPassword:$('eSMTPPassword').value,
		smtpPortNumber:$('eSMTPPort').value,
		batchSize:$('eBatchSize').value,
		poolSize: $('ePoolSize').value,
		messageTimeout: $('eMessageTimeout').value,		
		batchTimeout:$('eBatchTimeout').value,
		userLoginEmail:$('eUserEmail').value,
		userPassword:$('eUserPassword').value,
		maxHardBounces: $('eMaxHardBounces').value,
		maxSoftBounces: $('eMaxSoftBounces').value,
		enableGeoLocation: $('eEnableGeoLocation').checked
	}
	if (accountWrapper.smtpPortNumber=='' || isNaN(parseInt(accountWrapper.smtpPortNumber))){accountWrapper.smtpPortNumber = 0;}
	if (accountWrapper.credits=='' || isNaN(parseInt(accountWrapper.smtpPortNumber))){accountWrapper.credits = 0;}
	if (accountWrapper.batchSize=='' || isNaN(parseInt(accountWrapper.batchSize))){accountWrapper.batchSize = 25;}
	if (accountWrapper.batchTimeout == '' || isNaN(parseInt(accountWrapper.batchTimeout))) {accountWrapper.batchTimeout = 0; }
	if (accountWrapper.maxHardBounces == '' || isNaN(parseInt(accountWrapper.maxHardBounces))) {accountWrapper.maxHardBounces = 0; }
	if (accountWrapper.maxSoftBounces == '' || isNaN(parseInt(accountWrapper.maxSoftBounces))) {accountWrapper.maxSoftBounces = 0; }
	
	$('editAccountError').update('');
	$('editAccountErrorContainer').hide();
	if(accountWrapper.name.blank()){
		$('editAccountError').update('Company Name is required.');
		$('editAccountErrorContainer').show();
		return;
	}
	if(accountWrapper.bounceEmail.blank()){
		$('editAccountError').update('Bounce Email is required.');
		$('editAccountErrorContainer').show();
		return;
	}
	if(accountWrapper.bouncePOPServer.blank()){
		$('editAccountError').update('Bounce POP3 Server is required.');
		$('editAccountErrorContainer').show();
		return;
	}
	if(accountWrapper.bouncePOPUser.blank()){
		$('editAccountError').update('Bounce POP3 Username is required.');
		$('editAccountErrorContainer').show();
		return;
	}
	if(accountWrapper.bouncePOPPassword.blank()){
		$('editAccountError').update('Bounce POP3 Password is required.');
		$('editAccountErrorContainer').show();
		return;
	}
	if(accountWrapper.smtpServer.blank()){
		$('editAccountError').update('SMTP Server is required.');
		$('editAccountErrorContainer').show();
		return;
	}
	if(accountWrapper.id==0){
		if(accountWrapper.userLoginEmail.blank()){
			$('editAccountError').update('User Login (Email) is required.');
			$('editAccountErrorContainer').show();
			return;
		}
		if(accountWrapper.userPassword.blank()){
			$('editAccountError').update('User Password is required.');
			$('editAccountErrorContainer').show();
			return;
		}
	}
	var editBox = {
		Container:container,
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'editAccountError'
	}
	Accounts.UpdateAccount(Sys.Serialization.JavaScriptSerializer.serialize(accountWrapper),
							OnSucceeded, OnError, editBox);
}
function URebrandingAccount(account, container){	
	$('rebrandingName').value=account.name;
	$('rebrandingPagesTitle').value=account.pagesTitle;
	$('rebrandingPagesHeader').value=account.pagesHeader;
	$('rebrandingPagesFooter').value=account.pagesFooter;
	$('rebrandingId').value=account.id;	
	$('rebrandingAccountErrorContainer').hide();
	container.show();
}
function UpdateRebrandingAccount(updateButtonId, container){
		var accountWrapper = {
			id:$('rebrandingId').value,
			pagesTitle:$('rebrandingPagesTitle').value,
			pagesHeader:$('rebrandingPagesHeader').value,
			pagesFooter:$('rebrandingPagesFooter').value
		}
		var editBox = {
		Container:container,
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'rebrandingAccountError'
	}
	Accounts.UpdateRebrandingAccount(Sys.Serialization.JavaScriptSerializer.serialize(accountWrapper),
							OnSucceeded, OnError, editBox);
}


function DAccount(objectWrapper,container){
	$('dCaption').update(objectWrapper.caption);
	$('dId').value=objectWrapper.id;
	$('deleteErrorContainer').hide();
	container.show();
}

function DeleteAccount(updateButtonId,container){
	var accountId=$('dId').value;
	if(accountId && accountId!=0){
			var editBox = {
				Container:container,
				UpdateButtonId:updateButtonId,
				ErrorBoxId:'deleteError'
			}				
			Accounts.DeleteAccount(accountId,OnSucceeded, OnError, editBox);		
	}	
}
//################################################################# Users FUNCTIONS
function UUser(user, container){	
	$('eLogin').value=user.login;
	$('ePassword').value=user.password;	
	for (var j=0; j < $('eRole').options.length; j++)
	{
		if ($('eRole').options[j].value==user.role){
			$('eRole').options[j].selected=true;
			break;
		}
  	}	
	$('eId').value=user.id;
	$('eAccountId').value=user.accountId;
	$('editUserErrorContainer').hide();
	container.show();
}
function UpdateUser(updateButtonId,container){				
	var userWrapper = {
		Id:$('eId').value,
		AccountId:$('eAccountId').value,
		Login:$('eLogin').value,
		Password:$('ePassword').value,
		UserRole:parseInt($('eRole').value)
	}
	var editBox = {
		Container:container,
		UpdateButtonId:updateButtonId,
		ErrorBoxId:'editUserError'
	}
	Accounts.UpdateUser(Sys.Serialization.JavaScriptSerializer.serialize(userWrapper),
							OnSucceeded, OnError, editBox);
}
function DUser(objectWrapper,container){
	$('dCaption').update(objectWrapper.caption);
	$('dId').value=objectWrapper.id;
	$('deleteErrorContainer').hide();
	container.show();
}
function DeleteUser(updateButtonId,container){
	var userId=$('dId').value;
	if(userId && userId!=0){
			var editBox = {
				Container:container,
				UpdateButtonId:updateButtonId,
				ErrorBoxId:'deleteError'
			}				
			Accounts.DeleteUser(userId,OnSucceeded, OnError, editBox);		
	}	
}
//################################################################# COMMON FUNCTIONS
//common function to clear whatermarks
function ClearWatermarks(ids){
	var TBWEAddNameTextBoxes = ids.split(',');
		for(i=0;i<TBWEAddNameTextBoxes.length;i++){
			var waterMark = $find(TBWEAddNameTextBoxes[i]);				
			if(waterMark) { waterMark._onSubmit(); }; 
	}	
}
//common function to restore whatermarks
function RestoreWatermarks(ids){
	var TBWEAddNameTextBoxes = ids.split(',');
	for(i=0;i<TBWEAddNameTextBoxes.length;i++){
			var waterMark = $find(TBWEAddNameTextBoxes[i]);				
			if(waterMark) { waterMark._applyWatermark; };
	}
}
//#################################################################################################################
//################################################################# FRONT END
//#################################################################################################################
function TestBounced(container,bouncedEmail, bouncedPop3Server, bouncedPop3User, bouncedPop3Password, SmtpServer, SmtpPort, SmtpUser, SmtpPassword){
$('sendTestLoading').show();
$('sendTestDoneContainer').hide();
$('sendTestErrorContainer').hide();
	var data = {
		BouncedEmail: $(bouncedEmail).value,
		BouncedPop3Server: $(bouncedPop3Server).value,
		BouncedPop3User: $(bouncedPop3User).value,
		BouncedPop3Password: $(bouncedPop3Password).value,
		SmtpServer: $(SmtpServer).value,
		SmtpPort: $(SmtpPort).value,
		SmtpUser: $(SmtpUser).value,
		SmtpPassword: $(SmtpPassword).value	
	}
	var editBox = {
			Container:container,			
			ErrorBoxId:'sendTestError',
			LoadingImg:'sendTestLoading'
		}		
	PublicService.Test(Sys.Serialization.JavaScriptSerializer.serialize(data), OnSendTestSucceeded, OnError, editBox);			
}


//###############################################################Global Page Refresher
function UpdateTime(){
	new Ajax.Updater('refresher', context_path+'ServerTime.ashx');
}

//###############################################################Translations
function Translate(trnslon, key){
    if (!Object.isUndefined(trnslon)) {
        var value = $H(trnslon).get(key);
        if (!Object.isUndefined(value)) {
            return value;
        }       
    }else{
       return '[translation not found '+key+']';
    }
}

//###############################################################COmmon callback functions 
function OnSendTestSucceeded(result, editBox)
{	
	if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}	  
  $('sendTestDoneContainer').show();
  $('sendTestDone').update(result);
}
/**
 * the callback function that processes the Web Service return value and hides the box.
 * also makes a postback to update the list
 * @param {Object} result server output
 * @param {Object} editBox a json object contains modal container and UpdateButton
 */
function OnSucceeded(result, editBox)
{			
	if(editBox.Container){	editBox.Container.hide();	}
	if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}
	if(editBox.UpdateButtonId){__doPostBack($(editBox.UpdateButtonId).id,'');}
}

/**
 * Shows error row and populates it with server output
 * @param {Object} result
 */
function OnError(result, editBox)
{		
	$(editBox.ErrorBoxId).update(result._message);
	$(editBox.ErrorBoxId+'Container').show();					
	if(editBox.LoadingImg){$(editBox.LoadingImg).hide();}
}

