/**
 * Status
 *
 * @author 
 * @version $Id: Publish.js,v 1.23 2006/08/22 12:14:09 smilke Exp $
 * @since 2005/05/18
 * @package SB
 * @subpackage Wizard
 * @copyright (c) 2004-2006, SWsoft
 */

function SB_Publish() {
	this.publishSettings = document.getElementById('publishSettings');
	this.publishScenario = '';
	this.validateSettings = false;
	this.authType = '';
}

SB_Publish.prototype.showSettings = function() {
	this.publishSettings.show();
}

SB_Publish.prototype.hideSettings = function() {
	this.publishSettings.hide();
}

SB_Publish.prototype.showField = function(fieldName) {
	showElement(document.getElementById(fieldName), true);
}

SB_Publish.prototype.hideField = function(fieldName) {
	showElement(document.getElementById(fieldName), false);
}

SB_Publish.prototype.showPublishFields = function() {				
	publishModeId = document.getElementById('publishModeId');
	
	// publish mode for FTP
	if (1 == publishModeId.value) {

		// show table
		this.showField('tableAuth');

		// show fields
		this.showField('rowDirectory');
		this.showField('rowHost');

		// hide fields
		this.hideField('rowGroup');
		this.hideField('rowVpsId');
		this.hideField('rowUser');

	// publish mode for VPS
	} else if (2 == publishModeId.value) {
		// show fields	
		this.showField('rowUser');
		this.showField('rowGroup');
		this.showField('rowDirectory');
		this.showField('rowVpsId');
		this.showField('rowHost');
		
		// hide fields
		this.hideField('tableAuth');
	// publish mode for XCopy
	} else if (3 == publishModeId.value) {
		// show fields
		this.showField('rowDirectory', true);
		
		// hide fields
		this.hideField('rowUser');
		this.hideField('rowGroup');
		this.hideField('rowHost');
		this.hideField('rowVpsId');
		this.hideField('tableAuth');
	}
	
	return false;
}

SB_Publish.prototype.validateEmail = function() {
	if (validateField('SB_Validator.notEmpty', 'email', 'EMPTY-EMAIL')
		&& validateField('SB_Validator.isEmail', 'email', 'INVALID-EMAIL')
	) {
		result = true;
	} else {
			result = false;
	}
	
	if (result) {
		top.sbApplyChangesObject.registerChange();
		top.sbApplyChangesObject.registerForcedSaving();
		sbNavigationObject.go();
		return true;
	} else {
		return false;
	}

}

SB_Publish.prototype.validatePublishSettings = function() {
	result = true;
	if(this.publishScenario == 1 && this.validateSettings == true) {
		publishModeId = document.getElementById('publishModeId');
		// publish mode for FTP
		if (1 == publishModeId.value) {
			if(this.authType != 'anonym') {
				if (validateField('SB_Validator.isHttpUrl', 'hostingUrl', 'INVALID-SITE-URL')
					&& validateField('SB_Validator.notEmpty', 'login', 'EMPTY-FTP-LOGIN')
					&& validateField('SB_Validator.notEmpty', 'password', 'EMPTY-FTP-PASSWORD')
					&& extendedValidateField("SB_Validator.isEqual(document.getElementById('password').value, document.getElementById('confirmPassword').value)", 'confirmPassword', 'NOT-EQUAL-FTP-PASSWORD')
					&& validateFieldIfNotEmpty('SB_Validator.isPath', 'directory', 'INVALID-FTP-DIRECTORY')
					&& validateField('SB_Validator.notEmpty', 'hostId', 'EMPTY-HOST')
					&& validateField('SB_Validator.isDigits', 'hostId', 'INVALID-HOST')
				) {
					result = true;
				} else {
					result = false;
				}
			} else {
				if (validateField('SB_Validator.isHttpUrl', 'hostingUrl', 'INVALID-SITE-URL')
					&& validateFieldIfNotEmpty('SB_Validator.isPath', 'directory', 'INVALID-FTP-DIRECTORY')
					&& validateField('SB_Validator.notEmpty', 'hostId', 'EMPTY-HOST')
					&& validateField('SB_Validator.isDigits', 'hostId', 'INVALID-HOST')
				) {
					result = true;
				} else {
					result = false;
				}
			}
			// publish mode for VPS
		} else if (2 == publishModeId.value) {
			if (validateField('SB_Validator.isHttpUrl', 'hostingUrl', 'INVALID-SITE-URL')
				&& validateField('SB_Validator.notEmpty', 'user', 'EMPTY-VPS-LOGIN')
				&& validateField('SB_Validator.notEmpty', 'group', 'EMPTY-VPS-GROUP')
				&& validateField('SB_Validator.notEmpty', 'directory', 'EMPTY-VPS-DIRECTORY')
				&& validateField('SB_Validator.isPath', 'directory', 'INVALID-VPS-DIRECTORY')
				&& validateField('SB_Validator.notEmpty', 'vpsId', 'EMPTY-VPS-VPS-ID')
				&& validateField('SB_Validator.isDigits', 'vpsId', 'INVALID-VPS-VPS-ID')
				&& validateField('SB_Validator.notEmpty', 'hostId', 'EMPTY-HOST')
				&& validateField('SB_Validator.isDigits', 'hostId', 'INVALID-HOST')
			) {
				result = true;
			} else {
				result = false;
			}
		// publish mode for XCopy
		} else if (3 == publishModeId.value) {
			if (validateField('SB_Validator.isHttpUrl', 'hostingUrl', 'INVALID-SITE-URL')
				&& validateField('SB_Validator.notEmpty', 'directory', 'EMPTY-XCOPY-DIRECTORY')
				&& validateField('SB_Validator.isPath', 'directory', 'INVALID-XCOPY-DIRECTORY')
			) {
				result = true;
			} else {
				result = false;
			}
		} else {
			if (validateField('SB_Validator.notEmpty', 'publishModeId', 'EMPTY-PUBLICATION-MODE')
			) {
				result = true;
			} else {
				result = false;
			}

		}
	} else if (this.publishScenario == 2) {
		// check known publish settings
		if(this.authType != 'anonym') {
			if (validateField('SB_Validator.notEmpty', 'knownUrl', 'EMPTY-SITE-URL')
				&& validateField('SB_Validator.isHttpUrl', 'knownUrl', 'INVALID-SITE-URL')
				&& validateField('SB_Validator.notEmpty', 'knownHost', 'INVALID-HOST')
				&& validateField('SB_Validator.isHostname', 'knownHost', 'INVALID-HOST')
				&& validateFieldIfNotEmpty('SB_Validator.isPath', 'knownDirectory', 'INVALID-FTP-DIRECTORY')
				&& validateField('SB_Validator.notEmpty', 'knownPassword', 'EMPTY-FTP-PASSWORD')
				&& extendedValidateField("SB_Validator.isEqual(document.getElementById('knownPassword').value, document.getElementById('confirmKnownPassword').value)", 'confirmKnownPassword', 'NOT-EQUAL-FTP-PASSWORD')
				&& validateField('SB_Validator.notEmpty', 'knownLogin', 'EMPTY-FTP-LOGIN')
			) {
				result = true;
			} else {
				result = false;
			}
		} else {
			if (validateField('SB_Validator.notEmpty', 'knownUrl', 'EMPTY-SITE-URL')
				&& validateField('SB_Validator.isHttpUrl', 'knownUrl', 'INVALID-SITE-URL')
				&& validateField('SB_Validator.notEmpty', 'knownHost', 'INVALID-HOST')
				&& validateField('SB_Validator.isHostname', 'knownHost', 'INVALID-HOST')
				&& validateFieldIfNotEmpty('SB_Validator.isPath', 'knownDirectory', 'INVALID-FTP-DIRECTORY')
			) {
				result = true;
			} else {
				result = false;
			}
		}
	}

	if (result) {
		top.sbApplyChangesObject.registerChange();
		top.sbApplyChangesObject.registerForcedSaving();
		sbNavigationObject.go();
		return true;
	} else {
		return false;
	}
}

SB_Publish.prototype.changeScenario = function(scenario) {
	if(scenario=="2") {
		document.getElementById("panelPublishParameters").style.display="block";
		this.publishScenario = "2";
	}
	if(scenario=="1") {
		document.getElementById("panelPublishParameters").style.display="none";
		this.publishScenario = "1";
	}
}

SB_Publish.prototype.setDefaultScenario = function(scenario) {
	var scenaruios = document.getElementsByName("defaultScenario");
	if(scenario == '') {
		scenario = 1;
	}
	for(var b = 0, l = scenaruios.length; b < l; b++) { 
		if(scenaruios.item(b).value == scenario) {
			scenaruios.item(b).checked = true;
			this.publishScenario = scenario;
		}
	}
}

SB_Publish.prototype.setDefaultPublicationMode = function(mode) {
	var modes = document.getElementById("publishModeId");
	if(mode == '' ) {
		mode = 1;
	}
	for(var b = 0, l = modes.options.length; b < l; b++) { 
		if(modes.options[b].value == mode) {
			modes.options[b].selected = true;
		}
	}
}

SB_Publish.prototype.changeAuthType = function(scenarion, authType) {
	if(scenarion == 1) {
	    var login = document.getElementById('login');
    	var password = document.getElementById('password');
	    var confirmPassword = document.getElementById('confirmPassword');
	} else if(scenarion == 2) {
	    var login = document.getElementById('knownLogin');
    	var password = document.getElementById('knownPassword');
	    var confirmPassword = document.getElementById('confirmKnownPassword');
	}

    if(authType=="anonym") {
		this.authType = 'anonym';
		login.value = 'anonymous';
		password.value = '';
		confirmPassword.value = '';
		login.disabled = true;
		password.disabled = true;
		confirmPassword.disabled = true;
		login.style.backgroundColor = "lightgrey";
		password.style.backgroundColor = "lightgrey";
		confirmPassword.style.backgroundColor = "lightgrey";
    }
    else {
		this.authType = 'standart';
        login.disabled = false;
        password.disabled = false;
        confirmPassword.disabled = false;
        login.style.backgroundColor = "white";
        password.style.backgroundColor = "white";
        confirmPassword.style.backgroundColor = "white";
    }
}

SB_Publish.prototype.publishByProvider = function() {
	var scenaruios = document.getElementsByName("defaultScenario");
	for(var b = 0, l = scenaruios.length; b < l; b++) { 
		if(scenaruios.item(b).value == 1) {
			scenaruios.item(b).checked = true;
		}
	} 	
	top.sbApplyChangesObject.registerChange();
	top.sbApplyChangesObject.registerForcedSaving();
	sbNavigationObject.go();
	return true;
}