/*
 *	NOTE:
 * 
 *  object urlGen is being created directly in the page,
 *	along with other members and values from EFA during XSL transformation.
 */

urlGen.validateInput = function() {
	
	if (this.isVerified === 'true') {
		location.href = this.efalink;	
		return true;	
	}
	
	if (this.odvReady() === true || this.odvReady() === 'allEmpty') {
		this.appendHiddenField('itdLPxx_isVerified', 'true');
	}
	this.f.submit();
};

urlGen.odvReady = function() {
	
	// odv: nothing entered
	if (this.placeOrig.value === '' && 
		this.nameOrig.value === '' && 
		this.placeDest.value === '' && 
		this.nameDest.value === '')  {
		
		return 'allEmpty';
		
	}
	
	
	// odv: one field is verified, the other empty 
	 
	else if ((this.stateOrig.value === 'identified' && 
			this.placeDest.value === '' && 
			this.nameDest.value === '') || 
			(this.stateDest.value === 'identified' && 
			this.placeOrig.value === '' && 
			this.nameOrig.value === '')) {
		
		return true;
	}
	
	// odv: both fields are verified 
	 
	else if (this.stateOrig.value === 'identified' && 
			this.stateDest.value === 'identified')	{
	
		return true;

	} else {
		return false;
	}
}; 

urlGen.appendHiddenField = function(name, value) {
	
	var el = document.createElement('input');
	el.setAttribute('type', 'hidden');
	el.setAttribute('name', name);
	el.setAttribute('value', value);
	this.f.appendChild(el);
	return true;
};
	
urlGen.prepareUrlGen = function() {
	
	this.f = $name('jp')[0];
	this.stateOrig = $id('nameState_origin');
	this.placeOrig = $name('place_origin')[0];
	this.nameOrig = $id('name_origin');
	this.stateDest = $id('nameState_destination');
	this.placeDest = $name('place_destination')[0];
	this.nameDest = $id('name_destination');
	
	// if result screen, generate URL...
	if (!$id('itdLPxx_optionExecInst')) {
		
		this.efalink = this.baseUrl;
		
		// general options
		this.efalink += 'language=' + this.f.language.value;
		this.efalink += '&sessionID=0';
		this.efalink += '&useRealtime=1';
        this.efalink += '&itdLPxx_transpCompany=' + this.f.itdLPxx_transpCompany.value;
		if (this.f.itdLPxx_hideNavigationBar && this.f.itdLPxx_hideNavigationBar !== '') {
			this.efalink += '&itdLPxx_hideNavigationBar=' + this.f.itdLPxx_hideNavigationBar.value;	
		}
		
		// execInst
		if (this.optionExecInst === '' && 
			((this.f.nameState_origin && this.f.nameState_origin.value === 'identified') || 
			(this.f.nameState_destination && this.f.nameState_destination.value === 'identified'))) {
			this.efalink += '&execInst=readOnly';	
		}
		
		//line restriction
		if (this.optionLineRestriction !== '') {
			this.efalink += '&lineRestriction=' + this.optionLineRestriction;
			this.efalink += '&ptOptionsActive=1';
		}
		
		//odv
		if(this.f.nameState_origin && this.f.nameState_origin.value === 'identified') {
			this.efalink += '&place_origin=' + this.odvOrigPlace;
			this.efalink += '&name_origin=' + this.odvOrigName;
			this.efalink += '&type_origin=' + this.f.type_origin.value;
		}
		
		if(this.f.nameState_destination && this.f.nameState_destination.value === 'identified') {
			this.efalink += '&place_destination=' + this.odvDestPlace;
			this.efalink += '&name_destination=' + this.odvDestName;
			this.efalink += '&type_destination=' + this.f.type_destination.value;
		}
		
		// dateTime
		if (this.optionDateTime === '1') {
			this.efalink += '&itdDate=' + this.timeDate;
			this.efalink += '&itdTime=' + this.timeTime;
			this.efalink += '&itdTripDateTimeDepArr=' + this.timeDepArr;
		}
		
		// replace spaces
		this.efalink = this.efalink.replace(/ /g, '%20');
 		
 		// create wrap string, keep efalink
 		var wArr = this.efalink.split('&');
		var wrapped = '';
		for(var i=0; i<wArr.length; i++) {
			wrapped += wArr[i] + '&<br>';
		}
 		
 		
 		$id('urlGen_efaLink').innerHTML = wrapped;//this.efalink;
	}
	
	// ...otherwise update options,
	// if the user already entered any
	else {
	
		if (this.odvReady() === true) {
			this.f.submitButton.value = this.textCreate;
		}
		
		if(this.optionExecInst && this.optionExecInst !== '') {
			$id('itdLPxx_optionExecInst').checked = 'checked';
		}
		if(this.optionDateTime && this.optionDateTime !== '') {
			$id('itdLPxx_optionDateTime').checked = 'checked';
		}
		if(this.optionLineRestriction && this.optionLineRestriction !== '') {
			for(var i=0; i<$name('itdLPxx_optionLineRestriction').length; i++) {
				if ($name('itdLPxx_optionLineRestriction')[i].value === this.optionLineRestriction) {
					$name('itdLPxx_optionLineRestriction')[i].checked = 'checked';
					break;
				}
			}
		}
	}
};

// --------------------- HELPER ------------------------------------

function $id(id) {
		 
	return document.getElementById(id);	
}

function $name(name) {
	return document.getElementsByName(name);
}

if (typeof Function.prototype.bind !== 'function') {
 	Function.prototype.bind = function(obj) {
	  var method = this, temp = function() {
	    return method.apply(obj, arguments);
	   };
	  return temp;
	 }; 
}

function checkReturnKey(event) {
	if(!event) event = window.event;
		var keycode = event.which || event.keyCode;
	
		if (event && event.keyCode==13) {
			urlGen.validateInput();
		}
}

function attachEventListener(target, eventType, functionRef, capture) {
  if (typeof target.addEventListener != "undefined") {
	target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined") {
	target.attachEvent("on" + eventType, functionRef);
  }
  else {
	eventType = "on" + eventType;

	if (typeof target[eventType] == "function") {
	  var oldListener = target[eventType];

	  target[eventType] = function() {
		oldListener();

		return  functionRef();
	  }
	}
	else {
	  target[eventType] = functionRef;
	}
  }

  return true; 
}



attachEventListener(window, 'load', urlGen.prepareUrlGen.bind(urlGen), false);
attachEventListener(document, 'keypress', checkReturnKey, false);



