function initiateSourceCities(){
	//alert(document.getElementById('hiddenTet').value);
    var qty=document.getElementById('From').value;

    var tempQty = new Array();
    tempQty = qty.split(",");
		        
    var d=document.getElementById('frombox');

    var i=d.options.length; 
    d.options.length=tempQty.length+d.options.length -1;                               
    
    var ind=1;
  /*  if(tempQty[1]!=null){
    	document.getElementById("fromcity").value=tempQty[1];	
    }*/
    while (tempQty[ind] != null) {
		if (tempQty[ind].length > 0) {
			d.options[i].text=tempQty[ind];
			d.options[i].value=tempQty[ind];
			i=i+1;  
		}                               	
        ind =ind+1;
    }
    selectedSourceCities();
    var	source=document.getElementById('fromcity').value;
    initiateDestCities(document.getElementById('hidden'+source).value);
}
function modifyPopulate(source,destination){
	
	var frombox = document.getElementById('frombox');
	var i=frombox.options.length; 
	i=i-1;
	while(i>0){
		if(frombox.options[i].value.toUpperCase()==source.toUpperCase()){
			frombox.options[i].selected=true;
			break
		}
		i=i-1;
	}
	
	document.getElementById('fromcity').value=frombox.value;
	document.getElementById('tocity').value='';
	selectedSourceCities();
	
	
	var tobox = document.getElementById('tobox');
	var i=tobox.options.length; 
	i=i-1;
	while(i>0){
		if(tobox.options[i].value.toUpperCase()==destination.toUpperCase()){
			tobox.options[i].selected=true;
			break
		}
		i=i-1;
	}
	
	selectedDestination();
	document.getElementById('tocity').value=tobox.value;
	
}

function selectedSourceCities(){

	
    var	source=document.getElementById('fromcity').value;
    //selectIt2(source);
    HiddenId='hidden'+source;
    var dest="";
    if(source!="" && source!=" "){
        dest=document.getElementById('hidden'+source).value;
    }
    initiateDestCities(dest);

    getfromCityId(source);
    
    selectedDestination();

}
function initiateDestCities(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-1;                               
 
        var i=0; 
        var index=1;
        if(tempQty[1]!=null){
        	document.getElementById("tocity").value=tempQty[1];	
        }
        while (tempQty[index] != null) {
            d.options[i].text=tempQty[index];
            d.options[i].value=tempQty[index];
            i=i+1;  
            index++;
        }
    }
    else{
	
        var d=document.getElementById('tobox');

        d.options.length=0;
	
    }
}
function getfromCityId(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++;
    }

}
function selectedDestination(){
	var toObj = document.getElementById("tobox");
	var dest=toObj.value;
	    document.getElementById('tocity').value=dest;

	    checkIfValidDestination();
	    var src='hidden'+document.getElementById('fromcity').value+document.getElementById('fromStopId').value;
	    gettoCityId(src,dest);

	}

function checkIfValidDestination(){ 
	
    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 gettoCityId(fromcity,value){

	var selectedfrom = document.getElementById(fromcity);
	var no='NaN';
	if(selectedfrom!=null){

		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;
	}
}

