
var initPage = function(){
	jQuery('#sbSupportLink').removeClass('sidebarNav').addClass('sidebarNavCur');
	jQuery('#supportForm #subject').change(switchSubjectType);
	jQuery('#supportForm #subject').trigger('change');

	var manufacturer = jQuery('#supportForm #manufacturer');
	jQuery(manufacturer).val('e.g. Acer');
	jQuery(manufacturer).focus(function(){
		if (this.value == 'e.g. Acer') {
			this.value = '';
		}
	});

	var model = jQuery('#supportForm #model');
	jQuery(model).val('e.g. Aspire 8920');
	jQuery(model).focus(function(){
		if (this.value == 'e.g. Aspire 8920') {
			this.value = '';
		}
	});

	jQuery('#supportForm').submit(send);
	jQuery('#supportForm #send').click(send);
	jQuery('#supportForm #name').focus();

	loadCaptcha();
};

jQuery(document).ready(function(){
	initPage();
});

var switchSubjectType = function(){
	var selectedVal = jQuery(this).val();
	if (selectedVal != 'Others') {
		jQuery('#supportForm #extraSubject').empty();
	} else {
		var otherSubject = document.createElement("input");
		otherSubject.type = 'text';
		otherSubject.id = 'otherSubject';
		otherSubject.name = 'otherSubject';
		otherSubject.value = '';
		otherSubject.className = 'textField longField';
		jQuery('#extraSubject').append('type: ');
		jQuery('#extraSubject').append(otherSubject);
		jQuery(otherSubject).focus();
	}
};

var send = function(){
	var validName = jQuery('#supportForm #name').val();
	if (jQuery.trim(validName) == '') {
		jQuery('#supportForm #name').focus();
		alert('Please enter your first name.');
		return false;
	}

	var validEmail = validateFormat(emailFormat, jQuery('#supportForm #email').val());
	if (!validEmail) {
		jQuery('#supportForm #email').focus();
		alert('Please enter a valid E-mail address.');
		return false;
	}

	var account = jQuery('#supportForm #account').val();
	if (jQuery.trim(account) != '') {
		var validAccount = validateFormat(accountFormat, account);
		if (!validAccount) {
			jQuery('#supportForm #account').focus();
			alert('Account ID format error, please enter a valid Account ID.');
			return false;
		}
	}

	var manufacturer = jQuery('#supportForm #manufacturer');
	if (jQuery(manufacturer).val() == 'e.g. Acer') {
		jQuery(manufacturer).val('');
	}

	var validManufacturer = jQuery('#supportForm #manufacturer').val();
	if (jQuery.trim(validManufacturer) == '') {
		jQuery('#supportForm #manufacturer').focus();
		alert('Please enter your computer manufacturer.');
		return false;
	}

	var model = jQuery('#supportForm #model');
	if (jQuery(model).val() == 'e.g. Aspire 8920') {
		jQuery(model).val('');
	}

	var validModel = jQuery('#supportForm #model').val();
	if (jQuery.trim(validModel) == '') {
		jQuery('#supportForm #model').focus();
		alert('Please enter your machine model.');
		return false;
	}

	var validOs = jQuery('#supportForm #os').val();
	if (jQuery.trim(validOs) == '') {
		jQuery('#supportForm #os').focus();
		alert('Please select your computer operation system.');
		return false;
	}

	var validVersion = jQuery('#supportForm #esobiVersion').val();
	if (jQuery.trim(validVersion) == '') {
		jQuery('#supportForm #esobiVersion').focus();
		alert('Please select your eSobi version.');
		return false;
	}

	var versionNumber = jQuery('#supportForm #versionNumber');
	if (jQuery(versionNumber).val() != '') {
		var validNumber = validateFormat(versionFormat, jQuery('#supportForm #versionNumber').val());
		if (!validNumber) {
			jQuery('#supportForm #versionNumber').focus();
			alert('Please enter a valid eSobi Version number,\ne.g. 2.5.3.0203.');
			return false;
		}
	}

	var validSubject = jQuery('#supportForm #subject').val();
	if (jQuery.trim(validSubject) == '') {
		jQuery('#supportForm #subject').focus();
		alert('Please select the inquiry type that best describes your question.');
		return false;
	}

	var subject = jQuery('#supportForm #subject').val();
	if (subject == 'Others') {
		var validOtherSubject = jQuery('#supportForm #otherSubject').val();
		if (jQuery.trim(validOtherSubject) == '') {
			jQuery('#supportForm #otherSubject').focus();
			alert('Please describe your inquiry type with one short sentence.');
			return false;
		}
	}

	var validQuestion = jQuery('#supportForm #question').val();
	if (jQuery.trim(validQuestion) == '') {
		jQuery('#supportForm #question').focus();
		alert('Please describe your question(s) in detail.');
		return false;
	}

	var validCaptcha = validateFormat(captchaFormat, jQuery('#supportForm #captchaId').val());
	if (!validCaptcha) {
		jQuery('#supportForm #captchaId').focus();
		alert('Please enter the correct Secure ID you see in the image.');
		return false;
	}

	var param = jQuery('#supportForm').serialize();
	Common.Utils.log("supportForm form: \n" + Common.Utils.tostr(param, 2));
	ajaxPostJson("/cs/SendIssueCall.call", param, 'json', afterSend);

	// force browser ignore the another event at one time because this method binds the event 'onsubmit' and 'onclick'
	return false;
};

var afterSend = function(data, textStatus){
	Common.Utils.log(Common.Utils.tostr(data, 2));

	if (DWRUtil._isObject(data)) {
		if (data.type == 'INFO') {
			var ignoredNames = new Array();
			ignoredNames.push('os');
			ignoredNames.push('esobiVersion');
			ignoredNames.push('subject');
			jQuery('#supportForm').clearForm(ignoredNames);
			alert(data.message);
		} else {
			// occur exception
			alert(data.message);
		}
	}
};
