/*
(C) AEwebworks Software Development Ltd., 2002-2003
IMPORTANT: This is a commercial software product and any kind of using it must agree
to the AEwebworks Software Development Ltd. license agreement. It can be found at
http://www.aewebworks.com/license.htm
This notice may not be removed from the source code.
*/

/**
 * Checks/unchecks all tables
 *
 * @param   string   the form name
 * @param   boolean  whether to check or to uncheck the element
 *
 * @return  boolean  always true
 */
function setCheckboxes(the_form, do_check)
{
    var elts      = document.forms[the_form].elements;
    var elts_cnt  = elts.length;
	
    for (var i = 0; i < elts_cnt; i++) {
        elts[i].checked = do_check;
		if (the_form + "_submit" == elts[i].name) {
			elts[i].disabled = !do_check;
		}
    } // end for

    return true;
} // end of the 'setCheckboxes()' function

function setCheckbox(the_form)
{
    var elts      = document.forms[the_form].elements;
    var elts_cnt  = elts.length;
    
    var allUnchecked = true;
	
    for (var i = 0; i < elts_cnt; i++)
    {
        if(elts[i].checked) allUnchecked = false;
    }
    
    for (var i = 0; i < elts_cnt; i++)
    {
        if(elts[i].name == (the_form + "_submit")) elts[i].disabled = allUnchecked;
    }

    return true;
}


var win = "width=400,height=500,left=100,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes";
function get_gallery(id_prof)
{
   window.open("photos_gallery.php?ID="+id_prof,'gallery',win);
}

function launchTellFriend ()
{   
    var win = "width=250,height=260,left=200,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=no";
    window.open("tellfriend.php",'tellfriend',win);
    return false;
}

function launchTellFriendProfile ( sID )
{
    var win = "width=300,height=300,left=0,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes";
    window.open("tellfriend.php?ID="+sID,'tellfriendprofile',win);
    return false;
}

function ShowShowHide ( show_name, show_name2, hide_name )
{
    if (hide_name) hide_name.style.display = 'none';
    if (show_name) show_name.style.display = 'inline';
    if (show_name2) show_name2.style.display = 'inline';
}

function ShowHideHide ( show_name, hide_name, hide_name2 )
{
    if (hide_name) hide_name.style.display = 'none';
    if (hide_name2) hide_name2.style.display = 'none';
    if (show_name) show_name.style.display = 'inline';
}


/**
 * change images onHover mouse action
 */
function show(FileName,jpg1Name)
{
	document.images[FileName].src = jpg1Name;
}

/**
 * set status of the browser window to 's'
 */
function ss(s) 
{
	window.status = s;
	return true;
}

/**
 * set status of the browser window to empty
 */
function ce()
{
	window.status='';
}


/**
 * insert emotion item
 */
function emoticon( txtarea, text ) {

	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		txtarea.focus();
	} else {
		txtarea.value  += text;
		txtarea.focus();
	}
}

function launchAddToIM (id)
{
    var win = "width=600,height=160,left=100,top=100,copyhistory=no,directories=no,menubar=no,location=no,resizable=no,scrollbars=yes";
    window.open("explanation.php?explain=imadd&ID="+id,'add_to_im',win);
    return false;
}

function docOpen(text)
{
	newWindow=window.open('','','toolbar=no,resizable=yes,scrollbars=yes,width=400,height=300');
	newWindow.document.open("text/html");
	newWindow.document.write(unescape(text));
	newWindow.document.close();
}


// declare a global  XMLHTTP Request object
var XmlHttpObj;
var fl_country;
var fl_region;
var fl_city;
// create an instance of XMLHTTPRequest Object, varies with browser type, try for IE first then Mozilla
function CreateXmlHttpObj()
{
	// try creating for IE (note: we don't know the user's browser type here, just attempting IE first.)
	try
	{
		XmlHttpObj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttpObj = new ActiveXObject("Microsoft.XMLHTTP");
		} 
		catch(oc)
		{
			XmlHttpObj = null;
		}
	}
	// if unable to create using IE specific code then try creating for Mozilla (FireFox) 
	if(!XmlHttpObj && typeof XMLHttpRequest != "undefined") 
	{
		XmlHttpObj = new XMLHttpRequest();
	}
}

// called from onChange or onClick event of the continent dropdown list
function CountryListOnChange(pfl_country,pfl_region,pfl_city) 
{
    fl_country = pfl_country;
    fl_region  = pfl_region;
    fl_city    = pfl_city;
    window.status = "Loading Region .....";	
    //var Geo_Country = document.getElementById("Geo_Country");
    var Geo_Country = document.getElementById(fl_country);
    // get selected continent from dropdown list
    var selectedCountry = Geo_Country.options[Geo_Country.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    
    // use the following line if using php
    requestUrl = "modmysite/worldcities/xml_data_provider.php" + "?action=r&country=" + encodeURIComponent(selectedCountry);
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = RegionStateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
}


// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function RegionStateChangeHandler()
{
	//alert(fl_country + fl_region + fl_city);
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateRegionList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the Region dropdown list
function PopulateRegionList(RegionNode)
{
	
    	//var RegionList = document.getElementById("Geo_Region");
    	var RegionList = document.getElementById(fl_region);
	// clear the Region list 	
	for (var count = RegionList.options.length-1; count >-1; count--)
	{
		RegionList.options[count] = null;
	}
		
	var CityList = document.getElementById(fl_city);
	
	// clear the city list 	
	for (var count = CityList.options.length-1; count >-1; count--)
		{
		CityList.options[count] = null;
		}
	
	var RegionNodes = RegionNode.getElementsByTagName('region');
	
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	
	for (var count = 0; count < RegionNodes.length; count++)
	{
   		textValue = GetInnerText(RegionNodes[count]);
		idValue = RegionNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		RegionList.options[RegionList.length] = optionItem;
	}
	window.status = "";
}


function RegionListOnChange(pfl_country,pfl_region,pfl_city) 
{
    fl_country = pfl_country;
    fl_region  = pfl_region;
    fl_city    = pfl_city;
	
    window.status = "Loading Cities .....";	
    var Geo_Country = document.getElementById(fl_country);    

    var selectedCountry = Geo_Country.options[Geo_Country.selectedIndex].value;
    
    var Geo_Region = document.getElementById(fl_region);
    
    // get selected continent from dropdown list
    var selectedRegion = Geo_Region.options[Geo_Region.selectedIndex].value;
    
    // url of page that will send xml data back to client browser
    var requestUrl;
    
    // use the following line if using php
    s = current_date_txt();
    
    requestUrl = "modmysite/worldcities/xml_data_provider.php" + "?action=c&region=" + encodeURIComponent(selectedRegion) + "&country=" + encodeURIComponent(selectedCountry) + '&s=' + s;
    
	CreateXmlHttpObj();
	
	// verify XmlHttpObj variable was successfully initialized
	if(XmlHttpObj)
	{
        // assign the StateChangeHandler function ( defined below in this file)
        // to be called when the state of the XmlHttpObj changes
        // receiving data back from the server is one such change
		XmlHttpObj.onreadystatechange = CityStateChangeHandler;
		
		// define the iteraction with the server -- true for as asynchronous.
		XmlHttpObj.open("GET", requestUrl,  true);
		
		// send request to server, null arg  when using "GET"
		XmlHttpObj.send(null);		
	}
	
}


// this function called when state of  XmlHttpObj changes
// we're interested in the state that indicates data has been
// received from the server
function CityStateChangeHandler()
{
	// state ==4 indicates receiving response data from server is completed
	if(XmlHttpObj.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttpObj.status == 200)
		{			
			PopulateCityList(XmlHttpObj.responseXML.documentElement);
		}
		else
		{
			alert("problem retrieving data from the server, status code: "  + XmlHttpObj.status);
		}
	}
}

// populate the contents of the Region dropdown list
function PopulateCityList(CityNode)
{
    var CityList = document.getElementById(fl_city);
	// clear the Region list 
	
	
	for (var count = CityList.options.length-1; count >-1; count--)
	{
		CityList.options[count] = null;
	}

	var CityNodes = CityNode.getElementsByTagName('city');
	
	var idValue;
	var textValue; 
	var optionItem;
	// populate the dropdown list with data from the xml doc
	//alert(CityNodes.length);
	for (var count = 0; count < CityNodes.length; count++)
	{
   		textValue = GetInnerText(CityNodes[count]);
		idValue = CityNodes[count].getAttribute("id");
		optionItem = new Option( textValue, idValue,  false, false);
		CityList.options[CityList.length] = optionItem;
	}
	window.status = "";
	
}


// returns the node text value 
function GetInnerText (node)
{
	 return (node.textContent || node.innerText || node.text) ;
}

function yesno(msg)
{
var truthBeTold = confirm(msg);
if (!truthBeTold)
   {
   alert("CANCELLED!");
   return false;
   }
return true;
}


function current_date_txt()
{
	 var d,s;
	 d = new Date();
	 s = (d.getMonth() + 1)  ;
  	 s += d.getDate()  ;
  	 s += d.getFullYear();
	 s += d.getHours();
  	 s += d.getMinutes();
  	 s += d.getSeconds();
  	 return s;
}	
