// JavaScript Document

// Inicializacije 
var trBrowser;
var trChildAge;
var trTravelerList;
var trHotelStar;
var Cities;

// Definicije
var imgStars = new Array("img1", "img2", "img3", "img4", "img5");

function Profile(){};function OpenSection(){};function URLImpl(){}

OpenSection._sections = new Array;
OpenSection._sections[0] = "Osnovni";
OpenSection._sections[1] = "Potniki";
OpenSection._sections[2] = "Kartice";

OpenSection._items = new Array;
OpenSection._isValid = true;

OpenSection.setElements = function()
{
	var sects = OpenSection._sections;
	var items = OpenSection._items;

	for( i=0; i < sects.length; i++ )
	{
		var obj = document.getElementById( sects[i] );
		
		if( obj )
		{
			items[i] = obj;
		}
		else
			OpenSection._isValid = false;
	}
}

OpenSection.init = function()
{
	if( OpenSection._items.length == 0 )
	{
		OpenSection.setElements();
	}
}

OpenSection.openElement = function( p_value )
{
	OpenSection.init() // Static, zato treba inicializirati stvari

	if( !OpenSection._isValid )
		return;
		
	var sects = OpenSection._sections;
	var items = OpenSection._items;
	
	for( i=0; i < sects.length; i++ )
	{
		if( sects[i] == p_value )
		{
			items[i].className = 'ProfileShown';
			items[i].parentNode.parentNode.className = 'ProfileColor';
		}
		else
		{
			items[i].className = 'ProfileHidden';
			items[i].parentNode.parentNode.className = "";
		}
	}
}

function trBrowserImpl()
{
	this.browser = navigator.appName;
	this.ie = document.all ? true : false;
}

trBrowserImpl.prototype.sourceEvent = function( e )
{
	return this.ie ? event.srcElement : e.target;
}

trBrowserImpl.prototype.toString = function()
{
	return "trBrowserImpl";
}


URLImpl.prototype.toString = function()
{
	return "URLImpl";
}

URLImpl.prototype.hopTo = function( p_url )
{
	location.href = p_url;
}

var Url = new URLImpl;

function trHotelStar( p_img_id_array, p_callback_func, p_default_index )
{
	this.imgArray = new Array();
	this.imgStarOn = "/images/zvezda-sifrant.gif";
	this.imgStarOff = "/images/zvezda-sifrant-nic.gif";
	this.selectedIndex = 1;
	this.callback = null;
	
	var imgList;
	
	if( p_callback_func )
		this.callback = p_callback_func;
		
	if( p_default_index )
		this.selectedIndex = p_default_index;
	
	if( typeof( p_img_id_array[0] ) == "string" )
	{
		for( i=0; i < p_img_id_array.length; i++ )
		{
			// Nastavljanje Poslusalcev.
			if(	imgList = document.getElementById( p_img_id_array[i] ) )
			{
				imgList.onmouseover = this.onmouseover;
				imgList.onmouseout = this.onmouseout;
				imgList.onclick = this.onclick;
				this.imgArray.push( imgList );
			
				// Inicializacija zvezdic
				if( i <= this.selectedIndex - 1 )
				{
					imgList.src = this.imgStarOn;
				}
			}
		}
	}
}

trHotelStar.prototype.getArrayIndex = function( p_name )
{
	imgs = this.imgArray;
	
	for( i=0; i<imgs.length; i++ )
	{
		if( imgs[i].id == p_name )
			return i;
	}
	return -1;	
}

trHotelStar.prototype.onmouseover = function( e )
{
	var input = AbBrowser.sourceEvent( e );
	var getId = trHotelStar.getArrayIndex(input.id);

	var Star = trHotelStar.imgArray;
	
	if( getId != -1 )
	{
		for( i=0; i<=trHotelStar.selectedIndex-1; i++ )
		{
			Star[i].src = trHotelStar.imgStarOff;
		}
		for( i=0; i<=getId; i++ )
		{
			Star[i].src = trHotelStar.imgStarOn;
		}
	}
}

trHotelStar.prototype.onmouseout = function( e )
{
	var Star = trHotelStar.imgArray;
	
	for( i=0; i<=4; i++ )
	{
		Star[i].src = trHotelStar.imgStarOff;
	}
	
	for( i=0; i<=trHotelStar.selectedIndex-1; i++ )
	{
		Star[i].src = trHotelStar.imgStarOn;
	}
}

trHotelStar.prototype.onclick = function( e )
{
	var input = AbBrowser.sourceEvent( e );
	
	var getId = trHotelStar.getArrayIndex(input.id);
	trHotelStar.selectedIndex = getId+1;
	
	if( trHotelStar.callback )
		trHotelStar.callback( getId+1 );
}

trHotelStar.prototype.toString = function()
{
	return "trHotelStar";
}

function test( p_index )
{
		alert( "index: " + p_index );
}

function XHConn()
{
  var xmlhttp;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
		if (sVars != "") {
			sVars = "?"+sVars;
		}
        xmlhttp.open(sMethod, sURL+sVars, true);
		sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
    if(fnDone) {
		xmlhttp.onreadystatechange = function() { 
			if (xmlhttp.readyState == 4) {
	        fnDone(xmlhttp); 
			}
		};
	};
    xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}


function ErrorReport( p_error )
{
	var error_element = document.getElementById("DocumentError");
	
	if( error_element )
	{
		error_element.innerHTML =  "<b>JavaScript Izjema:</b><br />";
		error_element.innerHTML += "Datoteka: <a href=\"" +p_error.fileName + "\" target=\"_blank\">" + p_error.fileName + "<br />";
		error_element.innerHTML += "Vrstica: " + p_error.lineNumber + "<br />";
		error_element.innerHTML += "Sporocilo: " + p_error.message;
		error_element.innerHTML += "<br /><a href=\"javascript: void(0);\" onclick=\"this.parentNode.style.display = 'none';this.parentNode.innerHTML='';\">Zapri</a>";
		error_element.style.display = "block";
	}
}

function TabMenu( p_name )
{
	TabMenu.selectedMenu = null;
	TabMenu.OverMenu = null;
	var MenuListElem, MenuList = new Array();
	
	MenuListElem = document.getElementById("TabMenu");
	
	if(!MenuListElem)
		return;
		
	var childNodeList = MenuListElem.childNodes;
	for( i=0; i < childNodeList.length; i++)
	{
		if( childNodeList.item(i).nodeType == 1 && !(/(Space|Last)/.test(childNodeList.item(i).className )) )
		{
			childNode = childNodeList.item( i );
			MenuList.push( childNode );
			childNode.onmouseover = this.onmouseover;
			childNode.onmouseout = this.onmouseout;
			childNode.onclick = this.onclick;
			
			if( /Select/.test( childNode.className ) )
			{
				TabMenu.selectedMenu = childNode;
			}
		}
	}	
}

TabMenu.prototype.onmouseover = function( e )
{
	var input = AbBrowser.sourceEvent( e );

	
	if( /Select/.test( input.className ) )
	{
	}
	else
	{
		TabMenu.OverMenu = input;
		input.className = "TypeTabHover";
	}
}

TabMenu.prototype.onmouseout = function( e )
{
	var input = AbBrowser.sourceEvent( e );
	
	if( /Select/.test( input.className ) )
	{
	}
	else
	{
		TabMenu.OverMenu.className = "TypeTab";
		//source.className = "TypeTab";
	}
}

TabMenu.prototype.onclick = function( e )
{
	var input = AbBrowser.sourceEvent( e );
	
	if( !( /Select/.test( input.className ) ) )
	{
		input.className = "TypeTabSelect";
		try
		{
			// Če ni noben izbran, potem javi error, expection nic ne naredi, ker je dovoljeno izbirati
			TabMenu.selectedMenu.className = "TypeTab";
			TabMenu.selectedMenu = input;
		}
		catch( e )
		{
		}
		
		try
		{
			Url.hopTo(input.getAttribute("url"));
		}
		catch( e )
		{
			throw( "URLException" );
		}
	}
}
// Starost otrok :)
function trChildAgeImpl( p_childs, p_callback )
{
	var child_dropdown = document.getElementById('lng_otrok');
	
	if(!child_dropdown)
		return;
	
	this.triggerElement = child_dropdown;
	
	this.childs = p_childs ? p_childs : 0;
	
	this.triggerElement.onchange = this.startOnChange;
	
	// Olajsamo si zadeve.
	var thisNode = this.triggerElement;
	
	// Gremo v TR Element
	while( thisNode.nodeName != 'TR' )
	{
		thisNode = thisNode.parentNode;
	}
	// Dodamo metodo za dostopanje iste vrstice.
	this._ThisNode = thisNode;
	
	// En TR element naprej "POD"
	do
	{
		thisNode = thisNode.nextSibling;
	} 
	while( thisNode.nodeType != 1 );
	
	// Naslednji TR element zapisan v strukturo za lahki dostop.
	this._AddNode = thisNode;
	
	if( p_callback )
		this._callback = p_callback;
	
	// Gremo naprej do 2 Elementa v thisNode TD, kjer bomo zapisali notri SELECT.

	thisNode = thisNode.childNodes;

	var tdElem = new Array;
	
	for( i=0; i < thisNode.length; i++ )
	{
		switch( thisNode.item( i ).nodeType )
		{
			case 1:
				tdElem.push( thisNode.item( i ) );
				break;
			default:
				break;
		}
	}
	
	if(tdElem[1])
	{
		thisNode = tdElem[1];
		this._AddSNode = thisNode ? thisNode : null;
	}
}
// KONSTANTE
trChildAgeImpl.MAX_AGE = 18;
trChildAgeImpl.MIN_AGE = 2;
trChildAgeImpl.MAX_CHILD = 3;

trChildAgeImpl.prototype._callback = null;

trChildAgeImpl.prototype.createOption = function( p_value )
{
	return new Option( p_value, p_value );
}

trChildAgeImpl.prototype.createSelect = function( p_name )
{
	var t_select = document.createElement('select');
	t_select.name = p_name;
	t_select.id = p_name;
	return t_select;
}

trChildAgeImpl.prototype.render = function( p_select_array )
{
	var trOutput = "";
	if( p_select_array.length > 0 )
		trOutput = 'Starost otrok<br />';
	
	trChildAge._AddSNode.innerHTML = trOutput;
	
	for( i=0; i < p_select_array.length; i++ )
	{
		trChildAge._AddSNode.appendChild( p_select_array[i] );
		//trChildAge._AddSNode.appendChild( document.createElement('br') );
	}
}

// ITEM ON CHANGE
trChildAgeImpl.prototype.startOnChange = function( e )
{
	var selectedItem = AbBrowser.sourceEvent( e );
	
	// Preverimo, če je 1-3 potem ni NIČ :)
	var selectNum = ( 0 < parseInt(selectedItem.value, 10) <= trChildAgeImpl.MAX_CHILD ) ? parseInt(selectedItem.value, 10) : 0;
	
	// Array v katerega bomo zapisali vrstice
	var selectArray = new Array;
	
	for( i=0; i < selectNum; i++ )
	{
		selectArray[ i ] = trChildAge.createSelect('lng_childage');
		
		for( j=trChildAgeImpl.MIN_AGE; j <= trChildAgeImpl.MAX_AGE; j++ )
		{
			selectArray[ i ].options[ selectArray[ i ].options.length ] = trChildAge.createOption( j );
		}
	}
	
	trChildAge.render( selectArray );
	
	if( this._callback )
		this._callback(); // Zaenkrat sprozimo trigger...
}

function Traveler( p_name, p_surname, p_birthday, p_pkeyi )
{
	this._id = Traveler._Sequence++;
	this._pk = p_pkeyi;
	this._name = p_name;
	this._surname = p_surname;
	this._birthday = p_birthday;
}

Traveler._Sequence = 0;
Traveler.prototype._id = null;
Traveler.prototype._name = null;
Traveler.prototype._surname = null;
Traveler.prototype._birthday = null;
Traveler.prototype._pk = null;

Traveler.prototype.toString = function()
{
	return this._id;
}

Traveler.prototype.toPK = function()
{
	return this._pk;
}
/*
function TravelerList()
{
	if( !TravelerList._Init )
	{
		// Dodamo reference do DOM elemetov HTMLja
		TravelerList.FIELD_PKEYI = document.getElementById('lng_user_traveler_pkeyi');
		TravelerList.FIELD_NAME = document.getElementById('txt_potnik_ime');
		TravelerList.FIELD_SURNAME = document.getElementById('txt_potnik_priimek');
		TravelerList.FIELD_DAY = document.getElementById('lng_day');
		TravelerList.FIELD_MONTH = document.getElementById('lng_month');
		TravelerList.FIELD_YEAR = document.getElementById('lng_year');
		TravelerList.BUTTON_ADD = document.getElementById('traveler_add');
		// Dodamo trigger za dodajanje.
		if( TravelerList.BUTTON_ADD )
		{
			TravelerList.BUTTON_ADD.onclick = TravelerList._opAdd;
		}
		TravelerList._Init = true;
	}
}

// Dodamo najvec 10 potnikov
TravelerList.MAX_TRAVELERS = 10;
// Sistemska polja
TravelerList._Init = false;
TravelerList.FIELD_NAME = null;
TravelerList.FIELD_SURNAME = null;
TravelerList.FIELD_DAY = null;
TravelerList.FIELD_MONTH = null;
TravelerList.FIELD_YEAR = null;
TravelerList.FIELD_PKEYI = null;
TravelerList.BUTTON_ADD = null;

TravelerList._List = new Array;

TravelerList.addTraveler = function( p_traveler )
{
	TravelerList._List.push( p_traveler );
}

TravelerList.removeTraveler = function ( p_traveler )
{
	var localList = this._List;
	for( i=0; i < localList.length; i++ )
	{
		if( localList[i] == p_traveler )
		{
			this._List.splice(i, 1);
		}
	}
}

TravelerList._opAdd = function( e )
{
	
	if( TravelerList._List.length >= TravelerList.MAX_TRAVELERS )
	{
		alert(" Ni možno dodati več kot " + TravelerList.MAX_TRAVELERS + " potnikov ");
		return;
	}
		
	var ev = TravelerList.BUTTON_ADD;
	var row = ev;
	
	while( row.nodeName != 'TR' )
	{
		row = row.parentNode;
	}

	var _traveler;
	if( e )
	{
		_traveler = new Traveler( TravelerList.FIELD_NAME.value, TravelerList.FIELD_SURNAME.value, new Date(TravelerList.FIELD_YEAR.value, parseInt(TravelerList.FIELD_MONTH.value-1, 10), TravelerList.FIELD_DAY.value), TravelerList.FIELD_PKEYI.value);
		
		TravelerList.FIELD_PKEYI.value = "";
		TravelerList.FIELD_NAME.value = "";
		TravelerList.FIELD_SURNAME.value = "";
		TravelerList.FIELD_NAME.focus();
	}
	else
	{
		if( TravelerList._ajaxList.length > 0 )
		{
			_traveler = TravelerList._ajaxList.pop();
		}		
	}
		
	TravelerList.addTraveler( _traveler ); // Fizicno dodamo v Array potnika :)
	
	var newRow = document.createElement('tr');
	
	td1 = document.createElement('td');
	td1.appendChild( document.createTextNode( _traveler._name ) ); // Simple izpis
	td2 = document.createElement('td');
	td2.appendChild( document.createTextNode( _traveler._surname ) ); // Simple izpis
	td3 = document.createElement('td');
	td3.appendChild( document.createTextNode( _traveler._birthday ) ); // Simple izpis
	td4 = document.createElement('td');
	
	var input = document.createElement('input');
	input.type = 'button';
	input.onclick = TravelerList._opRemove;
	input.value = 'Odstrani';
	input.data = _traveler;
	input.style.width = '50px';
	
	td4.appendChild( input );
	
	newRow.align = 'Left';
	newRow.appendChild( td1 );
	newRow.appendChild( td2 );
	newRow.appendChild( td3 );
	newRow.appendChild( td4 );
	
	row.parentNode.appendChild( newRow );
}

TravelerList._opRemove = function( e )
{
	var ev = trBrowser.sourceEvent( e );
	var row = ev;
	var data = ev.data ;
	
	while( row.nodeName != 'TR' )
	{
		row = row.parentNode;
	}
	TravelerList.removeTraveler( data );
	row.parentNode.removeChild( row );
}

TravelerList._opProfileProc = function( p_element )
{
	var root = p_element.childNodes;
	
	for( i=0; i < root.length; i++ )
	{
		if( root[i].nodeType == 1 )
		{
			var proc_element = root[i];
			var parent = proc_element.childNodes;
			
			var name, surname, birthday, pk;
			
			pk = root[i].getAttribute('pk');
			
			for( j=0; j < parent.length; j++ )
			{
				if( parent[j].nodeType == 1 )
				{
					var node = parent[j];
					switch( node.nodeName )
					{
						case 'name':
							name = node.firstChild.nodeValue;
							break;
						case 'surname':
							surname = node.firstChild.nodeValue;
							break;
						case 'birthday':
							birthday = node.firstChild.nodeValue;
							break;
						default:
							break;
					}
				}
			}
			
			var dates = birthday.split(".");
					
			var newTraveler = new Traveler( name, surname, new Date(parseInt(dates[2], 10), (parseInt(dates[1], 10)-1), parseInt(dates[0],10) ), pk );
			TravelerList._ajaxList.push( newTraveler );
			
			TravelerList._opAdd(); // Končno dodamo potnika
		}
	}
}

TravelerList.toString = function()
{
	var newArray = TravelerList._List;
	return new String(newArray);
}

TravelerList.getNumberOfTravelers = function()
{
	return TravelerList._List.length;
}

// JavaScript Document

TravelerList._ajaxList = new Array; // Shranimo tukaj informacije, in nato poklicemo _opAdd

TravelerList._submitForm = function( ev )
{
	
	var name_array = new Array;
	var last_array = new Array;
	var birthday_array = new Array;
	var pk_array = new Array;
	
	var sEvent = ev ? ev : window.event;
	var source = sEvent.srcElement ? sEvent.srcElement : sEvent.target;
	
	var city = source['lkp_city_fkeyi_ref'].value;
	if( city.length == 0 )
	{
		alert("Manjka Mesto");
		return false;
	}
	
	for( aa = 0; aa < TravelerList._List.length; aa++ )
	{
		name_array.push( TravelerList._List[aa]._name );
		last_array.push( TravelerList._List[aa]._surname );
		birthday_array.push( new String( TravelerList._List[aa]._birthday ) );
		pk_array.push( TravelerList._List[aa].toPK() );
	}
	
	var field = { name: name_array, lastname: last_array, birthday: birthday_array, pk: pk_array };
	
	for ( key in field )
	{
		var inputfield = document.createElement('input');
		inputfield.value = AbStringUtil( field[ key ] );
		inputfield.type = 'hidden';
		inputfield.name = AbStringUtil("passenger_", new String(key));
		source.appendChild( inputfield );
	}
	
	return true;
}
*/

Profile._opAjaxProc = function( xml )
{
	var root = xml.responseXML.firstChild;
	
	var next = root.firstChild;
	
	if( next.nodeName == 'nosession' )
		return;
		
	do
	{
		switch( next.nodeType )
		{
			case 1:
				{
					switch( next.nodeName )
					{
						case 'basic':
							break;
						case 'travelers':
							TravelerList._opProfileProc( next );
							break;
						case 'creditcards':
							break;
						default:
							break;
					}
				}
				break;
			default:
				break;
		}
		
		if( next.nextSibling )
			next = next.nextSibling;
		else
			next = false;
	} while( next )
}

Profile.getRemoteData = function()
{
	var xmlData = new XHConn();
	var seed = new Date();
	var post = "seed=" + seed.getTime();
	xmlData.connect("/ajax/profile_all.cfm", "GET", post, Profile._opAjaxProc);
	
	return false;
}

Profile.MENU = new Array;
Profile.ELEMENT = new Array;
Profile.MENU[0] = "SearchHistory";
Profile.MENU[1] = "Messages";
Profile.MENU[2] = "MyProfile";
Profile.MENU[3] = "Shopping";

Profile._Init = function()
{
	for( i=0; i < Profile.MENU.length; i++ )
	{
		Profile.ELEMENT[i] = document.getElementById( Profile.MENU[i] );
	}
}

Profile._showMenu = function( p_num )
{
	for( i=0; i < Profile.ELEMENT.length; i++ )
	{
		if( i == p_num )
		{
			if( Profile.ELEMENT[i] )
				Profile.ELEMENT[i].style.display = 'inline';
		}
		else
		{
			if( Profile.ELEMENT[i] )
				Profile.ELEMENT[i].style.display = 'none';
		}
	}
}

function debug()
{
	if( AbDebug && AbDebug.ENABLE )
	{
		AbDebug.DOC_LOADED = true;
		AbDebug.invoke('Init Successful...', AbDebug.LOG_INFO, AbDebug.INFO_APPLICATION);
	}
}


function OpenAllOffersTest()
{
	var divs = document.getElementsByTagName('div');
	
	for( x in divs )
	{
		if( divs[x].id == "txt_fdsdfs" )
		{
			if( divs[x].style )
			{
				divs[x].style.display = "block";
				divs[x].parentNode.parentNode.className = "BottomBorderWhite";
			}
		}
	}
}

function editsection( section )
{
	var table_type;

	if( AbBrowser.isIE() )
		table_type = 'block';
	else
		table_type = 'table-row';
		
	document.getElementById("display" + section).style.display = 'none';
	//TODO: tukaj treba še enkstra za združljivost z IE;		
	document.getElementById("edit" + section).style.display = table_type;
}

function editsection_close( section )
{
	var table_type;
	
	if( AbBrowser.isIE() )
		table_type = 'block';
	else
		table_type = 'table-row';
	document.getElementById("edit" + section).style.display = 'none';
	document.getElementById("display" + section).style.display = table_type;
}

function CCCheck()
{
	var element = document.getElementById("tblCreditCardPayment");
	
	element.style.display = (element.style.display == "none") ? (AbBrowser.isIE() ? "block" : "table-row") : "none";
}
