
var i=0;
var HiddenId='';

function drop(divId,eventKey){
    var keyCode = FindKeyCode(eventKey);
    if(keyCode==9 || keyCode==27){
        document.getElementById(divId).style.display="none";
    }
    else{

        document.getElementById(divId).style.display="block";

    }
}

function getCookieNameValue(name) {
	

    str=unescape(document.cookie);

    if(str!=null){
        var s=str.split('; ');					
        var i;

        for(i=0;i<s.length;i++)
        {
            var s1=s[i].split("=");				

            if(s1[0]==name)			
            {


                return(s1[1]);
			
            }

        }

    }
    return("");
}


function setCookieNameValue(name,value){
	
    var exp = new Date(); //set new date object
    exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30 * 12 * 20));
	
    document.cookie=name+"="+value+"; expires="+ exp.toGMTString();
	
		
}






function backHittting(){

    document.getElementById("tocity").value="";
    
}
function suggest(ulId,textId,divId,hiddenId,eventKey){
    //alert(hiddenId);

    try{

        document.getElementById('frame2').style.display='none';
        document.getElementById('frame1').style.display='none';

    }catch(e){}

    document.getElementById(divId).style.margin="0";
    document.getElementById(ulId).className="listClass";
    var KeyID=0;
    var extraString =String.fromCharCode(eventKey.charCode ? eventKey.charCode : eventKey.keyCode);
    if(hiddenId == ''){
        hiddenId  = HiddenId;
    }
    document.getElementById(textId).onsubmit='return giveFalse()';
    var done="no";
    if (window.event) {
        var KeyID = FindKeyCode(eventKey);
        //alert(KeyID);
        if(KeyID==9){
            try{
                pressEnter(ulId,textId,divId);

                return true;
                clearTextField()
                }catch(e){
            }
        }



        if(KeyID==13){
            try{
                pressEnter(ulId,textId,divId);

                return false;
            }catch(e){
                clearTextField();return false;
            }
        }

        if(KeyID==40){
            try{

                document.getElementById('frame2').style.display='block';
                document.getElementById('frame1').style.display='block';

            }catch(e){}
            try{
                selectNext(ulId,textId);
                done ="yes";
            }
            catch(e){


            }

        }


        if(KeyID==38){
            try{

                document.getElementById('frame2').style.display='block';
                document.getElementById('frame1').style.display='block';

            }catch(e){}

            selectPrevious(ulId,textId)
            done="yes";
        }



    }
    else{

        try{
            var vEventKeyCode = FindKeyCode(eventKey);
            KeyID = vEventKeyCode;
		
		
            if(vEventKeyCode==9){
                try{
                    pressEnter(ulId,textId,divId);

                    return true;
                }catch(e){
                    clearTextField();
                }
            }

		
		
            if(vEventKeyCode==38){

                selectPrevious(ulId,textId)
                done="yes";
            }
            if(vEventKeyCode==13){
                try{
                    pressEnter(ulId,textId,divId);
                }catch(e){
                    clearTextField();
                }
                return false;

            }
	
            if(vEventKeyCode==40){


                selectNext(ulId,textId);
                done ="yes";
            }
        }
        catch(e){}
    }



    if(done=="no"){
        if(KeyID == 8){

            var val = document.getElementById(textId).value;
            var lval = val.length;
            if(length==0){
                val ='';

            }
            val =val.substring(0,lval-1);
        //alert(val);
        }else{
            var val = document.getElementById(textId).value +extraString;
        //alert("dsasd");
        }
        var qty ='';

        try 
        {
            qty=document.getElementById(hiddenId).value;
        }
        catch(e){
        	
           if(document.getElementById('tocity').value!=''){
        	  // alert('ddddd');
        	 document.getElementById('fromcity').value='';
           }
        }      
       
        var tempQty = new Array();
        tempQty = qty.split(",");
        //alert(qty);
        var i=1;
        var hideFrame=0;
        var expandInnerHtml= "";
        var judgeflag="true";
        while (tempQty[i] != null) {

            var values =tempQty[i];
            //alert(i);
            var length = val.length;
			
            var match = values.substring(0,length).toLowerCase();
            val = val.toLowerCase();


            if(val == match ){
                expandInnerHtml= expandInnerHtml+"<li id='"+values+"' onclick=selectIt(this,'"+divId+"','"+textId+"'); onmouseover=setSelected(this,'"+ulId+"')>"+values+"</li>";
                judgeflag = "false";
                hideFrame++;

                try{
                    if(hideFrame>6){
                        if(divId=='toExpand'){
                            document.getElementById('frame2').style.display='block';
                            document.getElementById('frame1').style.display='none';

                        }
                        if(divId=='expand'){
                            document.getElementById('frame1').style.display='block';
                            document.getElementById('frame2').style.display='none';
                        }

                    }
                }catch(e){}
            }

            if(length==0 ){
                document.getElementById(divId).style.display="none";
            }




            i++;
        }

        if(judgeflag=="true"){
            document.getElementById(divId).style.display="none";

        }


        document.getElementById(ulId).innerHTML=expandInnerHtml;
        disablePrevious(ulId);



        var parent1 = document.getElementById(ulId);


        thisChild = parent1.firstChild;
	
        if(val.length==0){
            parent1.firstChild.style.backgroundColor="LightGoldenRodYellow";
        }
        if(parent1.firstChild == parent1.lastChild){
            try{
                var textSearch  = ""+ thisChild.innerHTML;
                var length = val.length;
				
                var match = textSearch.substring(0,length).toLowerCase();
				
				
                var val =val.toLowerCase();
                if(val == match){
				
                    thisChild.style.backgroundColor="LightGoldenRodYellow";
                }
            }catch(e){}
        }
	
	
        while ( thisChild != parent1.lastChild )
        {
            if ( thisChild.nodeType == 1)
            {	
                var textSearch  = ""+ thisChild.innerHTML;
                var length = val.length;
			
                var match = textSearch.substring(0,length).toLowerCase();
			
                var val =val.toLowerCase();
                if(val == match && length !=0 ){
					
                    thisChild.style.backgroundColor="LightGoldenRodYellow";
                    break;
			
                }
			
			
			
					

            }
		
            thisChild = thisChild.nextSibling;
		
        }

    }

}


function setSelected(object,ulId){
    disablePrevious(ulId);
    object.style.backgroundColor="LightGoldenRodYellow";


}

function disablePrevious(id){

    var parent1 = document.getElementById(id);

    var thisChild = parent1.firstChild;

    while ( thisChild != parent1.lastChild )
    {
        if ( thisChild.nodeType == 1 )
        {
            thisChild.style.backgroundColor="white";
        }
        thisChild = thisChild.nextSibling;
        thisChild.style.backgroundColor="white";
    }

}

function selectIt(object,expand,text){
    //alert(object.innerHTML);

    document.getElementById(''+text).value = ""+object.innerHTML;
    document.getElementById(''+expand).style.display="none";
    var idCompare = document.getElementById(''+text).value;
    if(text=='fromcity'){
        prepareTo('tocity','hidden'+object.id)
        returnfromCityId(idCompare);
    }else{
        returntoCityId(fromcityid,idCompare);
    }
}

function selectIt2(val,expand,text){

    //document.getElementById(''+text).value = ""+val;
    //document.getElementById(''+expand).style.display="none";
    var idCompare = document.getElementById(''+text).value;
    //if(text=='fromcity'){
    prepareTo('tocity','hidden'+val)
    //returnfromCityId(idCompare);
    //}else{
    //returntoCityId(fromcityid,idCompare);
    //}
    //alert('done');
}

function hide(divId){
    document.getElementById(divId).style.display="none";
}


function selectNext(id,text){
    var i=0;
    var child='';
    //alert("sss");
    var parent1 = document.getElementById(id);
    thisChild = parent1.firstChild;
    while ( thisChild != parent1.lastChild )
    {
        i++;
        //alert(i);
			
	
        if ( thisChild.nodeType == 1 )
        {	
            //alert(thisChild.style.backgroundColor);
            if(thisChild.style.backgroundColor.toLowerCase() == "LightGoldenRodYellow".toLowerCase() ){
                //alert(thisChild);
                disablePrevious(id);
                try{
                    //child =thisChild;
                    thisChild = thisChild.nextSibling;
                    thisChild.style.backgroundColor = "LightGoldenRodYellow";
                    if(i>=0){

                }
	
                }
                catch(e){}
                break;
			
			
            }
			
			
					

        }
        //thisChild=child ;
        thisChild = thisChild.nextSibling;
		
    }


}




function selectPrevious(id,text){
    var i=0;
    var parent1 = document.getElementById(id);
    thisChild = parent1.lastChild;
	
    while ( thisChild != parent1.firstChild )
    { 
        i++;
	
	
        if ( thisChild.nodeType == 1 )
        {	
            //alert(thisChild.style.backgroundColor);
			
            if(''+thisChild.style.backgroundColor.toLowerCase() == "LightGoldenRodYellow".toLowerCase()){
			
                disablePrevious(id);
                try{
                    thisChild = thisChild.previousSibling;
                    thisChild.style.backgroundColor = "LightGoldenRodYellow";
                    if(i>=0){

                }
	
                }
                catch(e){}
                break;
			
			
			
            }
			
			
					

        }
		
        thisChild = thisChild.previousSibling;
		
    }


}










function prepareTo(textId,val)

{ //alert(textId);
    //var object =document.getElementById(textId);
    //var suggestedTo = "<input id='tocity' name='toStopId' class='text' onclick= drop('toExpand',event);suggest('toDrop','tocity','toExpand','"+val+"',event) onkeyup = checkValue('toDrop','tocity','toExpand','"+val+"',event);  onkeydown=drop('toExpand',event);suggest('toDrop','tocity','toExpand','"+val+"',event) type='text'/><br/><div id='toExpand'    class='Expand_div'><ul  id='toDrop' class='listClass'></ul></div>";
    //alert(x);

    //document.getElementById('ToDiv').innerHTML=
    document.getElementById(textId).value='';
    HiddenId =val;
    var qty ='';

    try 
    {
        qty=document.getElementById(val).value;
    }
    catch(e){}      
       
    var tempQty = new Array();
    tempQty = qty.split(",");
//alert(tempQty[1]);
//document.getElementById(textId).value= tempQty[1];

//alert(object.onclick);

}






function pressEnter(id,text,divId){
    HiddenId='';
    var control ="false";
    var parent1 = document.getElementById(id);
    thisChild = parent1.firstChild;
    if(thisChild==null){
    	//alert("press enter");
        document.getElementById('fromcity').value='';
	
    }
    if(''+parent1.lastChild.style.backgroundColor.toLowerCase() == "LightGoldenRodYellow".toLowerCase() )
    {
        document.getElementById(text).value= ""+parent1.lastChild.innerHTML;
        var idCompare = document.getElementById(''+text).value;	
        //selectIt(parent1.lastChild,divId,text);

        if(text=='fromcity'){
            prepareTo('tocity','hidden'+thisChild.id);
            returnfromCityId(idCompare);
        }else{
            returntoCityId(fromcityid,idCompare);
        }
			
        control ="true";
    }
			
    if(control =="false"){	
        //alert(control)
        while ( thisChild != parent1.lastChild )
        {	
            if ( thisChild.nodeType == 1 )

            {	
			
                if(''+thisChild.style.backgroundColor.toLowerCase() == "LightGoldenRodYellow".toLowerCase()){
                    document.getElementById(text).value= ""+thisChild.innerHTML;
                    var idCompare = document.getElementById(''+text).value;
			
			
                    if(text=='fromcity'){
                        prepareTo('tocity','hidden'+thisChild.id);
                        returnfromCityId(idCompare);
                    }else{
                        returntoCityId(fromcityid,idCompare);
                    }
			
			
                    break;
			
                }
			
			
					

            }
		
            thisChild = thisChild.nextSibling;
		
        }
    }

    document.getElementById(divId).style.display="none";
}
function clear(){

    document.getElementById('tab1_form').reset();

}
function FindKeyCode(e)
{
    if(e.which)
    {
        keycode=e.which;  //Netscape
    }
    else
    {
        keycode=e.keyCode; //Internet Explorer
    //	alert(e.srcElement);
    }

    //alert("FindKeyCode"+ keycode);
    return keycode;
}

function hideAll(event,fromcity,tocity){

    if(event.target){
        var element = event.target;
        if(element.id!='fromcity' && element.id!='tocity'&& element.id !='arrow_drop' && element.id !='arrow_drop2'){


            try{
                document.getElementById('frame1').style.display='none';
                document.getElementById('frame2').style.display='none';
            }catch(e){}
            if( document.getElementById('expand')!=null){
            document.getElementById('expand').style.display='none';
            }
            try{
                document.getElementById('toExpand').style.display='none';
            }catch(e){}

        }

        if(element.id=='tocity'){
            document.getElementById('expand').style.display='none';
        }
        if(element.id=='fromcity'){
            try{
                document.getElementById('toExpand').style.display='none';
            }catch(e){}
        }

    }
    else{

        var element = event.srcElement;
        if(element.id!='fromcity' && element.id!='tocity'&& element.id !='arrow_drop' && element.id !='arrow_drop2'){
            document.getElementById('expand').style.display='none';

            try{
                document.getElementById('frame1').style.display='none';
                document.getElementById('frame2').style.display='none';
            }catch(e){}

            try{
                document.getElementById('toExpand').style.display='none';
            }catch(e){}
        }

        if(element.id=='tocity'){
            document.getElementById('expand').style.display='none';
        }
        if(element.id=='fromcity'){
            try{
                document.getElementById('toExpand').style.display='none';
            }catch(e){}
        }




    }

}


function changeDisp(expand){

    document.getElementById(expand).style.display='none';
}

function checkValue(drop,fromcity,expand,From,event)
{
    if(document.getElementById(fromcity).value.length==1){

        //alert(drop);
        document.getElementById(expand).style.display='block';
        suggest(drop,fromcity,expand,From,event);
    }
    else{
        return false;
    }

}

function giveFalse(){
    return false;
}



function returnfromCityId(value){

    var y=0;
    while(document.getElementsByTagName('input')[y]!=null){

        if(document.getElementsByTagName('input')[y].type=='hidden')

            if((''+document.getElementsByTagName('input')[y].id).indexOf('hidden'+value)!=-1){

                var length =('hidden'+value).length;
                var string =document.getElementsByTagName('input')[y].id;

                var IdforfromCity = parseInt(string.substring(length));
                if(IdforfromCity){
                    fromcityid = string;
                    document.getElementById('fromStopId').value=IdforfromCity;
                //alert(IdforfromCity);

                }

            }
        y++;
    }
//alert(counter)

}


function returntoCityId(fromcity,value){


    var selectedfrom = document.getElementById(fromcity);
    var no='NaN';

    var index =(selectedfrom.value).indexOf(value);
    var idstring = (selectedfrom.value).substring(index);


    while(no.indexOf('NaN')!=-1){
        //alert(idstring);
        var temp='';
        var commaindex = idstring.indexOf(',');

        if(commaindex == -1){
            idstring = idstring.substring(value.length);
        }
        else{
            temp=idstring;
            idstring = idstring.substring(0,commaindex);
            idstring = idstring.substring(value.length);
        }
        no = ''+parseInt(idstring);
        //alert(no);
        if(no.indexOf('NaN')!=-1){
            idstring = temp.substring(temp.indexOf(',')+1);
        //alert(idstring);
        }

    }





    document.getElementById('toStopId').value=idstring;

}

function combo(ulId,textId,divId,hiddenId,eventKey){

    document.getElementById(ulId).className="listClass";

    if(hiddenId == ''){
        hiddenId  = HiddenId;

    }


    if(document.getElementById(hiddenId)==null){
    	//alert("hidden id prob...");
        document.getElementById('fromcity').value='';

        return false;
    }
    var qty=document.getElementById(hiddenId).value;
    var tempQty = new Array();
    tempQty = qty.split(",");
    //alert("");
    var i=1;
    var expandInnerHtml= "";
    while (tempQty[i] != null) {

        var values =tempQty[i];
        expandInnerHtml= expandInnerHtml+"<li id='"+values+"' onclick=selectIt(this,'"+divId+"','"+textId+"'); onmouseover=setSelected(this,'"+ulId+"')>"+values+"</li>";
        i++;

        try{
            if(i>6){
                if(divId=='toExpand'){
                    document.getElementById('frame2').style.display='block';
                    document.getElementById('frame1').style.display='none';

                }
                if(divId=='expand'){

                    document.getElementById('frame1').style.display='block';
                    document.getElementById('frame2').style.display='none';

                }
            }
        }catch(e){}
    }

    if(i>6){
        document.getElementById(ulId).className="listClass1";
    }
    document.getElementById(ulId).innerHTML=expandInnerHtml;
    disablePrevious(ulId);
    var parent1 = document.getElementById(ulId);
    thisChild = parent1.firstChild;
	
    while ( thisChild != parent1.lastChild )
    {
        if ( thisChild.nodeType == 1)
        {	
            thisChild.style.backgroundColor="LightGoldenRodYellow";
            break;
        }
        thisChild = thisChild.nextSibling;
    }

    document.getElementById(divId).style.display="block";
    document.getElementById(divId).style.margin="-20px 0 0 0";

    hiddenId='';
//drop(divId);
//alert("658");
}



function clearTextField(){
	
    alert("Currently we do not offer trips starting from this location.");
    document.getElementById('fromcity').value='';
}




function selectedDest(dest){


    document.getElementById('tocity').value=dest;

    checkIfValidDest();
    var src='hidden'+document.getElementById('fromcity').value+document.getElementById('fromStopId').value;

    returntoCityId(src,dest);

}

function initiateSource(){
	
	
    var qty=document.getElementById('From').value;

    var tempQty = new Array();
    tempQty = qty.split(",");
		        
    var d=document.getElementById('frombox');

    d.options.length=0;

    d.options.length=tempQty.length;                               
		     
    var i=0; 

    while (tempQty[i] != null) {
		                               	
        d.options[i].text=tempQty[i];
        d.options[i].value=tempQty[i];
        i=i+1;     
    }
	
}

function searchSource(regex){
	
	
    var qty=document.getElementById('From').value;

    var tempQty = new Array();
    tempQty = qty.split(",");

    var d=document.getElementById('frombox');

    d.options.length=0;

    d.options.length=tempQty.length;                               
		     
    var i=0; 

    while (tempQty[i] != null) {
		                               	
        d.options[i].text=tempQty[i];
        d.options[i].value=tempQty[i];
        i=i+1;     
    }
	
}

function initiateDest(str){
    document.getElementById('tocity').value="";
    if(str!=null && str!=''){
        var tempQty = new Array();
        tempQty = str.split(",");
    
        var d=document.getElementById('tobox');

        d.options.length=0;

        d.options.length=tempQty.length;                               
 
        var i=0; 

        while (tempQty[i] != null) {
                           	
            d.options[i].text=tempQty[i];
            d.options[i].value=tempQty[i];
            i=i+1;     
        }
    }
    else{
	
        var d=document.getElementById('tobox');

        d.options.length=0;
	
    }
}


function checkIfValidSource(){   // earlier called checkfromentry

    var x= document.getElementById('From').value;

    var y= document.getElementById('fromcity').value;
	

    //y=','+y+',';

	if(y!=""){
    if(x.indexOf(y)==-1){

        // earlier contained in clearTextField function
        alert("Currently we do not offer trips starting from this location.");
        document.getElementById('fromcity').value='';
		
        var d=document.getElementById('tobox');
        d.options.length=0;

    }

	}
}
	
	
function checkIfValidDest(){ 
		
    var str= document.getElementById('fromcity').value;

    var x= document.getElementById('hidden'+str).value;

    var y= document.getElementById('tocity').value;

    //y=','+y+',';
if(y!=""){
    if(x.indexOf(y)==-1){

        document.getElementById('tocity').value="";
    }

}
		
	
	
}
		
function selectedSource(){

		
    var	source=document.getElementById('fromcity').value;
    //selectIt2(source);
    HiddenId='hidden'+source;
    var dest="";
    if(source!="" && source!=" "){
        dest=document.getElementById('hidden'+source).value;
    }
    initiateDest(dest);

    returnfromCityId(source);
    
    


}


function checkfromentry(){
    var x= document.getElementById('From').value;
    //alert(x)
    var y= document.getElementById('fromcity').value;
    if(y!=""){
    if(x.indexOf(y)==-1){

        clearTextField();
    }
    }
}
