//------------------------------------------------------------------------------
// partner_script.js
// Functions for enable/disable form fields
// Magnus Nilsson, 2/24/2005 13:38
// (C) Axis Communications
//------------------------------------------------------------------------------

//==============================================================================
//--C-o-n-s-t-a-n-t-s-----------------------------------------------------------
//------------------------------------------------------------------------------
var formName = 'PartnerRequest';
var select_fieldsToUpdate = [formName+'.dist1']
var select_fullListOptionText   = ' - Select - ';
var select_fullListOptionValue  = '-';
var select_emptyListOptionText  = ' - Empty - ';
var select_emptyListOptionValue = '*';
//==============================================================================
//--G-l-o-b-a-l--V-a-r-i-a-b-l-e-s----------------------------------------------
//------------------------------------------------------------------------------
var fieldName = new Array();
var alerts = new Array();
var list_empty_fields = new Array();
//==============================================================================
//--F-u-n-c-t-i-o-n-s-----------------------------------------------------------
//------------------------------------------------------------------------------
function refreshDistributor() {
	populateSelectList(document.PartnerRequest.country.value);
}
//------------------------------------------------------------------------------
function QualityCheck(){
	// COMPANY & PERSONAL INFORMATION ------------------------
	_check_if_empty2('pi_fname');
	_check_if_empty2('pi_sname');
	_check_if_empty2('pi_title');
	_check_if_empty2('pi_email');
	_check_if_empty2('company');
	_check_if_empty2('street');
	_check_if_empty2('state');
	_check_if_empty2('pi_phone');
	_check_if_empty2('zipcode');
	//_check_if_empty('fax', 'Fax'); //not mandatory
	_check_if_empty2('city');
	//_check_if_empty('url', 'Web site'); //not mandatory
	_check_if_empty2('country');
	
	// AXIS PRODUCT MANAGER ----------------------------------
	if (eval('window.document.'+formName+'.sc_same.value')) {
		_check_if_empty2('sc_fname');
		_check_if_empty2('sc_sname');
		_check_if_empty2('sc_phone');
		_check_if_empty2('sc_title');
		_check_if_empty2('sc_email');
	}
	
	// MARKETING CONTACT -------------------------------------
	if (eval('window.document.'+formName+'.tc_same.value')) {
		_check_if_empty2('tc_fname');
		_check_if_empty2('tc_sname');
		_check_if_empty2('tc_phone');
		_check_if_empty2('tc_title');
		_check_if_empty2('tc_email');
	}
	
	//DISTRIBUTORS ------------------------------------------------
	if (eval('window.document.'+formName+'.dist1.options[0].value')!=select_emptyListOptionValue) {
		var chosen_distributor = 0;
		if (eval('window.document.'+formName+'.dist1.value')!=select_fullListOptionValue) {
			chosen_distributor = 1;
			_check_if_empty2('acc1');
		}
		/*if (eval('window.document.'+formName+'.dist2.value')!=select_fullListOptionValue) {
			chosen_distributor = 1;
			_check_if_empty2('acc2');
		}
		if (eval('window.document.'+formName+'.dist3.value')!=select_fullListOptionValue) {
			chosen_distributor = 1;
			_check_if_empty2('acc3');
		}*/
		if (!chosen_distributor) {
			list_empty_fields[list_empty_fields.length] = fieldName['dist'];
		}
	}
	// COMPANY DATA ----------------------------------------------
	_check_if_empty2('company_size');
	//_check_if_empty2('franchises');
	//_check_if_empty2('customers');
	_check_if_empty2('locations');
	_check_if_empty2('itype');
	_check_if_empty2('vertical');
	//Axis distributors you buy from
	//_check_if_empty2('axisturnover');
	_check_if_empty2('turnover');
	//Desired partner status
	//Interest for Axis products
	
	//alert(list_empty_fields.length);
	//return false;

	if (list_empty_fields.length > 0) {
		if (list_empty_fields.length > 6) {
			alert(alerts['fill_all']);
			list_empty_fields  = new Array();
			return false;
		} else {
			var fields = list_empty_fields.join(', ');
			alert(alerts['fill_one']+":\n" + fields);
			list_empty_fields  = new Array();
			return false;
		}
	}
	return true;
}
//------------------------------------------------------------------------------
function switchDisabled(contact_type) {
	_doSwitchDisabled(contact_type,'fname');
	_doSwitchDisabled(contact_type,'sname');
	_doSwitchDisabled(contact_type,'phone');
	_doSwitchDisabled(contact_type,'title');
	_doSwitchDisabled(contact_type,'email');
}
//==============================================================================
//--L-o-c-a-l--F-u-n-c-t-i-o-n-s------------------------------------------------
//------------------------------------------------------------------------------
function _doSwitchDisabled(cType,cField) {
	var theField = eval('window.document.'+formName+'.'+cType+'_'+cField);
	theField.disabled = ! theField.disabled;
	if (theField.disabled) {
		//if (cField=='name') {
			//var fnameVal = eval('window.document.'+formName+'.pi_fname.value');
			//var snameVal = eval('window.document.'+formName+'.pi_sname.value');
			//theField.value = fnameVal+' '+snameVal;
		//} else {
			theField.value = eval('window.document.'+formName+'.pi_'+cField+'.value');
		//}
	} else {
		theField.value = '';
	}
}
//--email validation------------------------------------------------------------
function isValidEmail(email, required) {
	if (required==undefined) {   // if not specified, assume it's required
		required=true;
	}
	if (email==null) {
		if (required) {
			return false;
		}
		return true;
	}
	if (email.length==0) {  
		if (required) {
			return false;
		}
		return true;
	}
	if (!allValidChars(email)) {  // check to make sure all characters are valid
		return false;
	}
	if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
		return false;
	} else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
		return false;
	} else if (email.indexOf("@") == email.length) {  // @ must not be the last character
		return false;
	}
	return true;
}

function allValidChars(email) {
	var parsed = true;
	var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-";
	for (var i=0; i < email.length; i++) {
		var letter = email.charAt(i).toLowerCase();
		if (validchars.indexOf(letter) != -1)
			continue;
		parsed = false;
		break;
	}
	return parsed;
}
//--end email validation------------------------------------------------------------

function _check_if_empty(field, label) {
	var fieldname = eval('window.document.'+formName+'.'+field+'.value');
	if (field == 'pi_email') {
		if(!isValidEmail(fieldname)) {
			list_empty_fields[list_empty_fields.length] = label;
		}
	} else {
		if (fieldname == '')  {
			list_empty_fields[list_empty_fields.length] = label;
		}
	}
}

function _check_if_empty2(field) {
	var fieldname = eval('window.document.'+formName+'.'+field+'.value');
	if (field == 'pi_email') {
		if(!isValidEmail(fieldname)) {
			list_empty_fields[list_empty_fields.length] = fieldName[field];
		}
	} else {
		if (fieldname == '')  {
			list_empty_fields[list_empty_fields.length] = fieldName[field];
		}
	}
}