var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
var IE6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4);
var addEvent;
if (document.addEventListener) {
  addEvent = function(element, type, handler) {
      element.addEventListener( type, handler, null);
  }
}
else if (document.attachEvent) {
  addEvent = function(element, type, handler) {
  element.attachEvent("on" + type, handler);
  }
}
else {
  addEvent = new Function; // not supported
}

function loadNextEvents(){				
   loading_data();	  
    var sd = $("#ndate").val();
    var pd = $("#pdate").val();
    var startval = $("#nstart").val();   
    var prevval = $("#pstart").val();  
    var limitval = $("#limit").val();
    var drval = $("#drange").val();
    var locval = $("#location").val();
    var plocval = $("#prevloc").val();
    //alert( drval );
    var action = "next";    
    $.ajax({
	    url: "../event/dyn_pubevents.php",
	    cache: false,
	    type:"GET",
	    dataType: "html",
	    data: ({ ndate:sd, pdate:pd, nstart:startval, pstart: prevval, limit:limitval, act: action, drange:drval, location: locval, prevloc: plocval}),
	    success: function( html ) {				
		$("#npubevents_data").empty().append( html ); 				
		unloading_data();
	    }
    });
    return false;
}

function loadPreviousEvents(){				
    loading_data();	  
    var sd = $("#ndate").val();
    var pd = $("#pdate").val();
    var startval = $("#nstart").val();   
    var prevval = $("#pstart").val();  
    var limitval = $("#limit").val();
    var drval = $("#drange").val();
    var locval = $("#location").val();
    var plocval = $("#prevloc").val();
    var action = "prev";   
    $.ajax({
	    url: "../event/dyn_pubevents.php",
	    cache: false,
	    type:"GET",
	    dataType: "html",
	    data: ({ ndate:sd, pdate:pd, nstart:startval, pstart: prevval, limit:limitval, act: action, drange:drval, location: locval, prevloc: plocval}),
	    success: function( html ) {				
		$("#npubevents_data").empty().append( html ); 				
		unloading_data();
	    }
    });
    return false;
}
	

function loading_data() {
    document.getElementById('loading-data' ).style.display = 'inline';	
}
function unloading_data() {
	document.getElementById('loading-data' ).style.display = 'none';	
}

function confirmDelete( a_url ) {
	var answer = confirm ("Do you really want to delete your fun post?")
	if (answer) {
    	    window.location=a_url;
	    return true;
	}
	return false;
}

function confirmFriendsDelete( u_id ) {
	var answer = confirm ("Do you really want to remove your friend?");
	if (answer) {
	    l_element = document.getElementById( 'rmfrnd-' + u_id );
	    window.location=l_element.getAttribute("value");
	    return true;
	}
	return false;
}

function confirmFunpipersDelete( u_id ) {
	var answer = confirm ("Do you really want to remove your funpiper?")
	if (answer) {
	    l_element = document.getElementById( 'rmfnppr-' + u_id );
	    window.location=l_element.getAttribute("value");
	    return true;
	}
	return false;
}
function Trim(a_value)
{
    if(a_value.length < 1)
    {
        return"";
    }
    a_value = RTrim(a_value);
    a_value = LTrim(a_value);
    if(a_value == "")
    {
        return "";
    }else
    {
        return a_value;
    }
} //End Function

function RTrim(a_value)
{
    var w_space = String.fromCharCode(32);
    var v_length = a_value.length;
    var strTemp = "";
    if(v_length < 0)
    {
        return"";
    }
    var iTemp = v_length -1;

    while(iTemp > -1)
    {
        if(a_value.charAt(iTemp) == w_space)
        {
        }else
        {
            strTemp = a_value.substring(0,iTemp +1);
            break;
        }
        iTemp = iTemp-1;
    } //End While
    return strTemp;
} //End Function

function LTrim(a_value)
{
    var w_space = String.fromCharCode(32);
    if(v_length < 1)
    {
         return"";
    }
    var v_length = a_value.length;
    var strTemp = "";
    var iTemp = 0;

    while(iTemp < v_length)
    {
        if(a_value.charAt(iTemp) == w_space)
        {
        }else
        {
            strTemp = a_value.substring(iTemp,v_length);
            break;
        }
        iTemp = iTemp + 1;
    } //End While
    return strTemp;
}

function get_cities_home() {	
    rate_loading_text1();
    var select = document.shform.country;
    a_countryid = select.options[select.selectedIndex].value;    
    //alert( "country id: " + a_countryid );
    var ajax = new Ajax.Updater('city', '../ajax/ddrtrvr.php?country_id=' + a_countryid,{method:'get',asynchronous:true,onComplete: showCitiesHome });
}

function get_cities_home11() {	
    rate_loading_text11();
    var select = document.shform11.country11;
    a_countryid = select.options[select.selectedIndex].value;    
    var ajax = new Ajax.Updater('city11', '../ajax/ddrtrvr.php?country_id11=' + a_countryid,{method:'get',asynchronous:true,onComplete: showCitiesHome11 });
}

function get_cities() {	
    rate_loading_text();
    var select = document.profform.country;
    a_countryid = select.options[select.selectedIndex].value;    
    var ajax = new Ajax.Updater('city', '../ajax/ddrtrvr.php?country_id=' + a_countryid,{method:'get',asynchronous:true,onComplete: showCitiesProfile });
}
function rate_loading_text1() {
	document.getElementById('loading-cities' ).style.display = 'inline';	
}
function rate_loading_text() {
    document.getElementById('loading-cities' ).style.display = 'inline';	
}
function rate_unloading_text() {
	document.getElementById('loading-cities' ).style.display = 'none';	
}
function rate_unloading_text1() {
	document.getElementById('loading-cities' ).style.display = 'none';	
}
function rate_loading_text11() {
	document.getElementById('loading-cities11' ).style.display = 'inline';	
}
function rate_unloading_text11() {
	document.getElementById('loading-cities11' ).style.display = 'none';	
}
function showCitiesHome11( req ) {   
    var citystr =  req.responseText;
	var cities = citystr.split("," );
	l_ctyelem = document.getElementById( 'city11' );               
        l_parentelem = l_ctyelem.parentNode;
        
        soption = document.createElement('option');
        soption.appendChild(document.createTextNode( ' -- Select -- ' ));
	soption.setAttribute( "value", "-1" );
        soption.setAttribute( "class", "small" );
        l_ctyelem.appendChild( soption ); 
        
        for( i = 0; i < cities.length; i++ )
        {
		if( cities[i] == "" ) {
		    continue;
		}
		cityarr = cities[i].split( "__" );
		var l_name = cityarr[0];
		var l_value = cityarr[1];
		soption = document.createElement('option');
		soption.appendChild(document.createTextNode( l_name ));
		soption.setAttribute( "name", l_name );
		soption.setAttribute( "value", l_value );
		l_ctyelem.appendChild(soption);
        }
        
    rate_unloading_text11();
}
function showCitiesHome( req ) {   
    var identifier = "home";
    var citystr =  req.responseText;
	var cities = citystr.split("," );
	l_ctyelem = document.getElementById( 'city' );               
        l_parentelem = l_ctyelem.parentNode;
        
        soption = document.createElement('option');
        soption.appendChild(document.createTextNode( 'all the cities' ));
	soption.setAttribute( "value", "10000" );
        soption.setAttribute( "class", "small" );
        l_ctyelem.appendChild( soption ); 
        
        for( i = 0; i < cities.length; i++ )
        {
		if( cities[i] == "" ) {
		    continue;
		}
		cnameids = cities[i].split("__");
		soption = document.createElement('option');
		soption.appendChild(document.createTextNode( cnameids[0] ));
		var l_name = cnameids[0];
		var l_value = cnameids[1];
		soption.setAttribute( "name", l_name );
		soption.setAttribute( "value", l_value );
		l_ctyelem.appendChild(soption);
        }
        
    rate_unloading_text1();
}


function showCitiesProfile( req ) {   
    var identifier = "profile";
    return showCities( req, identifier );
}
function showCities( req, identifier ) {
    var citystr =  req.responseText;
    var cities = citystr.split("," );
    l_ctyelem = document.getElementById( 'city' );
    
    l_parentelem = l_ctyelem.parentNode;
    var selectf = "";
    citytype = document.getElementById( 'city_type' );
    if( citytype.value == "select" ) {
        selectf = l_ctyelem;
    } else {
        selectf = document.createElement('select');
        selectf.setAttribute( 'name', 'city' );
        selectf.setAttribute( 'id', 'city' );
        selectf.onchange = processCity;
    }
    
    soption = document.createElement('option');
    soption.appendChild(document.createTextNode( 'Select' ));
    selectf.appendChild( soption );    
    for( i = 0; i < cities.length; i++ )
    {
	if( cities[i] == "" ) {
	    continue;
	}
	cnameids = cities[i].split("__");
	soption = document.createElement('option');
	soption.appendChild(document.createTextNode( cnameids[0] ));
	var l_name = cnameids[0];
	var l_value = cnameids[1];
	soption.setAttribute( "name", l_name );
	soption.setAttribute( "value", l_value );
	selectf.appendChild(soption);
    }
    soption = document.createElement('option');
    soption.appendChild(document.createTextNode( '-- Add your city --' ));
    soption.setAttribute( "value", "add_your_city" );
    soption.setAttribute( "class", "small" );
    selectf.appendChild( soption );    
    
    //if( citytype.value == "int" ) {
        l_parentelem.replaceChild( selectf, l_ctyelem ); 
    //}  
    	   
    citytype = document.getElementById( 'city_type' );        
    citytype.setAttribute( 'value', 'select' );
    rate_unloading_text();
}

function processCity() {
    
    var select = document.getElementById( 'city' );    
    a_cityid = select.options[select.selectedIndex].value;    
    if( a_cityid != 'add_your_city' ) {
    	return true;
    }    
    l_parentelem = document.getElementById('select-city' );
    
    var textf = document.createElement( 'input' );
    textf.setAttribute( 'type', 'text' );
    textf.setAttribute( 'id', 'city' );
    textf.setAttribute( 'name', 'city' );
    textf.setAttribute( 'value', '' );
    textf.setAttribute( 'class', 'mediumlarge1' );
    textf.setAttribute( 'maxlength', '100' );
    l_elem = l_parentelem.replaceChild( textf, select );
    
    citytype = document.getElementById( 'city_type' );        
    citytype.setAttribute( 'value', 'int' );
}
function newwindow( url, title, a_width, a_height ) 
{ 
    attr = 'width='+ a_width + ',height=' + a_height + ',resizable=yes,scrollbars=yes';
    window.open( url,title,attr); 
}
function setBodyStyle() {
   var width = screen.width;
   var height = screen.height;
   var bodystyle = document.getElementById("mybdyid"); 
   if( width <= 800 ) {
       bodystyle.style.background='url(image/bubbles.jpg) top left repeat-y';
   } else if( width > 800 && width <= 1024 ) {
       bodystyle.style.background='url(image/bubbles.jpg) 4% 30% repeat-y';
   } else if( width > 1024 && width < 1680 ) {
       bodystyle.style.background='url(image/bubbles.jpg) 14% 30% repeat-y';
   } else if( width >= 1680  ) {
       bodystyle.style.background='url(image/bubbles.jpg) 24% 30% repeat-y';
   }
}
function loadHome( userid, useremail ) {
	loading_home_data();	
	var identifier = "home_reviews";
	$.ajax({
	       	    url: "../ajax/ddrtrvr.php",
	       	    cache: false,
	       	    type:"GET",
	       	    dataType: "html",
	       	    data:({ data:identifier, user_id: userid, email: useremail}),
	       	    success: function( html ) {				
	       		showHomePageData( html );				   		       		
	       	    }
    	});	
	var identifier = "home_plans";
	$.ajax({
	       	    url: "../ajax/ddrtrvr.php",
	       	    cache: false,
	       	    type:"GET",
	       	    dataType: "html",
	       	    data:({ data:identifier, user_id: userid, email: useremail}),
	       	    success: function( html ) {				
	       		showHomePageData( html );				   		       		
	       	    }
    	});
	var identifier = "home_friends";
	$.ajax({
	       	    url: "../ajax/ddrtrvr.php",
	       	    cache: false,
	       	    type:"GET",
	       	    dataType: "html",
	       	    data:({ data:identifier, user_id: userid, email: useremail}),
	       	    success: function( html ) {				
	       		showHomePageData( html );				   		       		
	       	    }
    	});
}
function showHomePageData( html ){
	var retdata = html;
	var data = retdata.split( ":" );
	var id = data[0];
	elem = document.getElementById( id );
	elem.innerHTML = "<b>" + data[1] + "</b>";
	
	var loadid = "loading-" + id;
	unloading_home_data( loadid );
}
function loading_home_data(){
	if( document.getElementById('loading-reviews' ) != null ) {
		document.getElementById('loading-reviews' ).style.display = 'inline';
	}
	if( document.getElementById('loading-plans' ) != null ) {
		document.getElementById('loading-plans' ).style.display = 'inline';
	}
	if( document.getElementById('loading-friends' ) != null ) {
		document.getElementById('loading-friends' ).style.display = 'inline';
	}
}
function unloading_home_data( id ){
	document.getElementById( id ).style.display = 'none';	
}
function selectVenue() {	
    var select = document.shform11.itnr_type;
    l_itnrtype = select.options[select.selectedIndex].value;    
	rate_loading_text11();
    var ajax = new Ajax.Updater('', '../ajax/ddrtrvr.php?itnr_type=' +l_itnrtype,{method:'get',asynchronous:true,onComplete: setVenueForItnrType });
}
function setVenueForItnrType( res )
{
	var vtype =  res.responseText;
	var is_moz = (!document.all && document.getElementById);
	var is_opera = (navigator.userAgent.indexOf("Opera") > -1);
	if( vtype != -1 ) {
		var select = document.shform11.wfs_type;
		for( i = 0; i < select.options.length; i++ ) {
			var wfstype = select.options[ i ].value;
			if( wfstype == vtype ) {
				if( is_moz || is_opera ) {
					select.options[ i ].selected = 'true';
				} else {
					select.options[ i ].setAttribute( 'selected', 'selected' );
				}
			}			
		}
	} 
	rate_unloading_text11();
	return false;
}

function acceptAsFriend( a_fnpid )
{
    var ajax = new Ajax.Updater('', '../ajax/ddrtrvr.php?acptfnpid=' + a_fnpid,{method:'get',asynchronous:true,onComplete: postFriendAcceptance });
}
function postFriendAcceptance( req )
{
    fnp_id = req.responseText;
    l_elem = document.getElementById( "frndstatsid-" + fnp_id );
    l_elem.innerHTML = "";
    l_elem = document.getElementById( "href-" + fnp_id );
    l_elem.innerHTML = "";
    alert( "Friends accepted!" );
}

function startAllOver(){
    elem = document.getElementById( "bccblock" );
    elem.style.display = "block";
    elem = document.getElementById( "startagain" );
    elem.style.display = "none";
    elem = document.getElementById( "addvenue" );
    elem.style.display = "none";
    elem = document.getElementById( "advnublock" );
    elem.style.display = "none";
    elem = document.getElementById( "spacer_bcc" );
    elem.style.display = "block";
}

function addVenue(){
    elem = document.getElementById( "advnublock" );
    if( elem.style.display == "block" ) {
        elem.style.display = "none";
    } else {
        elem.style.display = "block";
        document.getElementById( "wfs_name_id" ).value = "";
        document.getElementById( "wfs_address_id" ).value = "";
        document.getElementById( "wfs_descr_id" ).value = "";
        document.getElementById( "wfs_phone_id" ).value = "";
        document.getElementById( "email_id" ).value = "";
    }
    elem = document.getElementById( "advnuresult" );
    elem.style.display="none";
}

// Start the first rotation.
function addVenueAction() {
    l_name = Trim( document.getElementById( "wfs_name_id" ).value );
    l_addr = Trim( document.getElementById( "wfs_address_id" ).value );
    if( l_name.length == 0 ) {
    	alert( "Venue name is a mandatory field. Please try again!" );
    	return false;
    }
    if( l_addr.length == 0 ) {
        alert( "Venue address is a mandatory field. Please try again!" );
        return false;
    }  
    rate_loading_objects();
    l_phone = document.getElementById( "wfs_phone_id" ).value;
    l_descr = document.getElementById( "wfs_descr_id" ).value;
    l_email = document.getElementById( "email_id" ).value;
    l_ctyid = document.getElementById( "wfs_cityid_id" ).value;
    l_cntryid = document.getElementById( "wfs_countryid_id" ).value;
    l_query = 'wfs_name_id=' + l_name +
    		'&wfs_address_id=' + l_addr +
    		'&wfs_phone_id=' + l_phone +
    		'&wfs_descr_id=' + l_descr +
    		'&email_id=' + l_email +
    		'&wfs_cityid_id=' + l_ctyid +
    		'&wfs_countryid_id=' + l_cntryid ;
    var ajax = new Ajax.Updater('', '../ajax/ddrtrvr.php?' + l_query,
    				{method:'get',asynchronous:true,onComplete: addVenueCompleted });
}

function addVenueCompleted( req ) {
    elem = document.getElementById( "advnublock" );
    elem.style.display = "none";
    elem = document.getElementById( "advnuresult" );
    l_text = "";
    if( req.responseText == 1 ) {
        l_text = "Thank you! Venue is successfully added. We will review it and add it to our database.";
    } else {
        l_text = "Thank you! There has been some unknown error. Please try again later.";
    }
    elem.innerHTML = l_text;
    elem.style.display="block";
    rate_unloading_objects();
}
function rate_loading_objects() {
    document.getElementById('loading-objects' ).style.display = 'inline';	
}
function rate_unloading_objects() {
	document.getElementById('loading-objects' ).style.display = 'none';	
}
function processVenueType( type ){
	wfstype = "wfs_type_" + type;
	addrelem = document.getElementById( wfstype );
	if( addrelem.value == 0 ) 
	{
	    uielem1 = document.getElementById( "find_a_venue" );
	    uielem1.style.display = "block";
	    spaceelem = document.getElementById( "spacer_bcc" );
	    if( spaceelem != null ) {
	        spaceelem.style.display = "block";
	    }
	}
	if( addrelem.value == 1 ) 
	{
	    uielem1 = document.getElementById( "find_a_venue" );
	    uielem1.style.display = "none";	
	    spaceelem = document.getElementById( "spacer_bcc" );
	    if( spaceelem != null ) {
	        spaceelem.style.display = "none";
	    }
	}
	 
}

function processQuickPersonalization( id ){
    elem = document.getElementById( id );
    if( elem.style.display == "block" ) {
    	elem.style.display = "none";
    } else {
    	elem.style.display = "block";
    }
}

function confirmPlanDelete( a_id ) {
	var answer = confirm ("Do you really want to delete this activity?")
	if (answer) {
	    var l_parent = document.getElementById( "venues" );
	    var childCount = 0;
	    var i = 0;
	    while( l_parent.hasChildNodes() )
	    {
		var l_elem = l_parent.childNodes[ i ];		
		if( l_elem == null ) {
		    break;
		}
		l_idstr = l_elem.id;				
		if( l_idstr == null ) {
		    i++;		    
		    continue;
		}
		var l_index = l_idstr.indexOf( "wfs_id" );
		if( l_index > -1 ) {
		    childCount++;
		    if( childCount > 1 ) {
		        break;
		    }
		}
		i++;
	    }	    
	    if( childCount == 1 ) {
	    	alert( "Sorry, you can't delete as your current plan is left with just one venue!" );
	    	return false;
	    }
    	    var l_child = document.getElementById( a_id );    	    
    	    l_parent.removeChild( l_child );
    	    return true;
	}
	return false;
}
function approve( a_gid, a_uid, a_sid, a_divid ) 
{
    var ids = a_gid + "_" + a_uid + "_" + a_sid + "_" + a_divid;        
    $.ajax({
    	    url: "../ajax/ddrtrvr.php",
    	    cache: false,
    	    type:"GET",
    	    dataType: "html",
    	    data:({ apprvids:ids}),
    	    success: function( html ) {				
    	        processApproval( html );				   		       		
    	    }
    });
}
function processApproval( html ) {
    
    divid = "div_" + html;
    elem = document.getElementById( divid );
    elem.style.display = 'none';
}
function getItnrType(){
   rate_loading_objects()
    var option = $("input:radio[name=itnr_etype]:checked").val();
    var atype= "getitnrtype";  
    
    $.ajax({
    	    url: "../ajax/ddrtrvr.php",
    	    cache: false,
    	    type:"GET",
    	    dataType: "html",
    	    data:({ type:atype,itnr_etype:option}),
    	    success: function( html ) {				
    	        $("#itnr_type_id").empty().append( html );  			   		       		
    	        rate_unloading_objects()
    	    }
    });
}




