
/*
 //This is for IE Mac:
  if(typeof(Array.prototype.push == "undefined")){
	  Array.prototype.push=function(){
		var i=0;
		b=this.length,a=arguments;
		for(i;i<a.length;i++)this[b+i]=a[i];
		return this.length
	  }
  }
 */
  
//***********************TAKEN FROM QUIRKSMODE.ORG***  *****BROWSER, OS, VER DETECTION*******
var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

if (checkIt('konqueror'))
{
	browser = "Konqueror";
	OS = "Linux";
}
else if (checkIt('firefox')) browser = "Firefox"
else if (checkIt('safari')) browser = "Safari"
else if (checkIt('omniweb')) browser = "OmniWeb"
else if (checkIt('opera')) browser = "Opera"
else if (checkIt('webtv')) browser = "WebTV";
else if (checkIt('icab')) browser = "iCab"
else if (checkIt('msie')) browser = "Internet Explorer"
else if (!checkIt('compatible'))
{
	browser = "Netscape Navigator"
	version = detect.charAt(8);
}
else browser = "An unknown browser";

if (!version) version = detect.charAt(place + thestring.length);

if (!OS)
{
	if (checkIt('linux')) OS = "Linux";
	else if (checkIt('x11')) OS = "Unix";
	else if (checkIt('mac')) OS = "Mac"
	else if (checkIt('win')) OS = "Windows"
	else OS = "an unknown operating system";
}

function checkIt(string)
{
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

//************************************************************************

function test(){
	alert("this is a test");
}
//note: this stuff will NOT work on safari or IE for the mac
function is_ok_browser(){
	if(OS=="MAC"){
		if(browser=="Internet Explorer" || browser=="Safari"){
			return false;
		}
	}
	return true;
}

//***********END BROWSER DETECTION STUFF**********************************
function formatCurrency(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
			num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
			cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
			num = num.substring(0,num.length-(4*i+3))+','+
			num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	}

function open_me(target){
	if(is_ok_browser){
		
		if(target.name=="countries_travelled_in[]"){
			restore_my_values(target,"countries_travelled_in_array");
		}
		if(target.name=="countries_lived_in[]"){
			restore_my_values(target,"countries_lived_in_array");
		}
		target.size=30;
	}
}
function close_me(target){
	if(is_ok_browser){
		if(target.name=="countries_travelled_in[]"){
			record_my_values(target,"countries_travelled_in_array");
		}
		if(target.name=="countries_lived_in[]"){
			record_my_values(target,"countries_lived_in_array");
		}
		target.size=1;
	}
}
function record_my_values(target,array_name){
	if(is_ok_browser){
		for (i=1; i<target.length; i++){
			 if (target.options[i].selected){
				eval(array_name)[i] = 1;
			}else{
				eval(array_name)[i] = 0;
			}
		}
	}
}
function restore_my_values(target,array_name){
	if(is_ok_browser){
		for (i=1; i<target.length; i++){
			 if (eval(array_name)[i] == 1){
				target.options[i].selected=true;
			}else{
				target.options[i].selected=false;
			}
		}
	}
}
function make_me_blank(target,original_value_array,position,dummy_value){
	if(target.value == dummy_value){
		original_value_array[position]=target.value;
		target.value="";
	}
}
function restore_my_value(target,original_value){
	if(trim(target.value) ==""){
		target.value=original_value;
	}
}
countries_lived_in_array=Array();
countries_travelled_in_array=Array();

function confirm_bookmark(destination){
	if(confirm("Are you sure you want to bookmark this travel mate?")){
		window.location=destination;
		//note: next line is to help with mac issues...
		//see: http://www.snook.ca/archives/000165.php 
		return false;
	}
}
function doublecheck_delete(){
	var answer = confirm ("Are you sure you want to delete this?")
	if (answer){
		
	}else{
		return false;
	}

}
function confirm_refresh(id){
	var answer = confirm ("Are you sure you want to refresh this listing?")
	if (answer){
		window.location="index.php?action=refresh&listing_id="+id;
		//note: next line is to help with mac issues...
		//see: http://www.snook.ca/archives/000165.php 
		return false;
	}else{
		return false;
	}
}
//*******************************************form validation**************************************
function set_all_selected(target_select){
	for(i=target_select.length-1; i>=0; i--){
		target_select.options[i].selected=true;
	}
}
function inForm(objToTest) {
	//note: there's currently a problem with this script with IE Mac
	if (objToTest == null || objToTest == undefined) {
		return false;
	}
	return true;
}
function removeCommas(str) {
	//taken from http://www.irt.org/feedback/205.htm
	// Strip any commas that may be in string.
	// Must be recognized as a type string or indexOf function won't work
	str = String(str);
	while (str.indexOf(',') >= 0) {
	var temp = '';
	
	// if the comma is the first character, create a substring of everything after
	if (str.indexOf(',') == 0 && str.length > 1) {
	temp = str.substring(1, str.length);
	// if teh comma is not first or last character, join all before and all after comma
	} else if (str.indexOf(',') > 0 && str.indexOf(',') < str.length-1) {
	temp = str.substring(0, str.indexOf(',')) + str.substring(str.indexOf(',')+1, str.length);
	// if the comma is the last character, create a substring of everything before
	} else if (str.indexOf(',') > 0 && str.indexOf(',') == str.length-1) {
	temp = str.substring(0, str.length - 1);
	}
	// save changes
	str = temp;
	}
	return str;
}
function trim(inputString) {
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
	  retValue = retValue.substring(1, retValue.length);
	  ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
	  retValue = retValue.substring(0, retValue.length-1);
	  ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
	  retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function check_form(form){
	
	if(inForm(form.username) && (trim(form.username.value) == "" || trim(form.username.value)=="username")){
	alert('please enter a username.'); return false;}
	if(inForm(form.password) && (form.password.value.length<4 ||(trim(form.password.value)=="password" && trim(form.newuser.value)!=""))){
		alert('the password must be at least 4 characters long.'); 
		return false;
		}
	if(inForm(form.first_name) && (trim(form.first_name.value) == "" || trim(form.first_name.value) == "First Name")){
		alert('please enter your first name.'); return false;}
	if(inForm(form.last_name) && (trim(form.last_name.value) == "" || trim(form.last_name.value) == "Last Name")){
	alert('please enter your last name.'); return false;}
	if(inForm(form.email) && !(/[A-Za-z0-9_\.\-]+@+[A-Za-z0-9_\-]+\.[A-Za-z]+/.test(form.email.value))){
		alert('please enter a valid email address');
		return false;}

	if(inForm(form.listing_title)&& (trim(form.listing_title.value)) == ""){
		alert('Please enter a title for this listing.'); return false;}
	
	if(inForm(form.listing_category) && form.listing_category.value == ""){
		alert('please select a category for this listing.'); return false;}
	
	/*	
	if(inForm(form.agree) && !(form.agree.checked)){alert('you must agree to the terms and conditions.'); return false;}
	
	var last_dot=form.author_picture.value.lastIndexOf(".");
	var file_extension=form.author_picture.substr(last_dot);
	file_extension=file_extension.toLowerCase();
	var acceptable_extensions=".gif.jpg.png";
	var is_acceptable;
	is_acceptable=acceptable_extensions.indexOf(file_extension);
	if(is_acceptable>-1){
	
	}else{
		alert('please choose a picture file of yourself which ends in a .gif, .jpg or .png.'); 
		return false;
	}*/
	/*
	
	if(inForm(form.phone) && trim(form.phone.value) == ""){alert('please enter your phone number.'); return false;}
	if(inForm(form.street_address) && trim(form.street_address.value) == ""){alert('please enter your street address, test.'+form.street_address.value); return false;}
	if(inForm(form.city) && trim(form.city.value) == ""){alert('please enter your street address.'); return false;}
	if(inForm(form.state) && trim(form.state.value) == ""){alert('please enter your state/province.'); return false;}
	if(inForm(form.client_password) && form.client_password.value.length<4){
		alert('the password must be at least 4 characters long.'); 
		return false;
		}
	if(inForm(form.admin_password) && form.admin_password.value.length<4){
		alert('the password must be at least 4 characters long.'); 
		return false;
		}
	*/
	//make all selects
	//set_all_selected(document.getElementById("nationality"));
	if(document.getElementById("countries_travelled_in")){
		set_all_selected(document.getElementById("countries_travelled_in"));
	}
	if(document.getElementById("countries_lived_in")){
		set_all_selected(document.getElementById("countries_lived_in"));
	}
	if(document.getElementById("activities")){
		set_all_selected(document.getElementById("activities"));
	}
	if(document.getElementById("previous_travels")){
		set_all_selected(document.getElementById("previous_travels"));
	}
	
	return true;
}
	
//********************TAKEN FROM http://www.mredkj.com/tutorials/tutorial_mixed2b.html ************************

var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue)
{
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function deleteOption(theSel, theIndex)
{ 
  var selLength = theSel.length;
  if(selLength>0)
  {
    theSel.options[theIndex] = null;
  }
}

function moveOptions(theSelFrom, theSelTo)
{
  
  var selLength = theSelFrom.length;
  var selectedText = new Array();
  var selectedValues = new Array();
  var selectedCount = 0;
  
  var i;
  
  // Find the selected Options in reverse order
  // and delete them from the 'from' Select.
  for(i=selLength-1; i>=0; i--)
  {
    if(theSelFrom.options[i].selected)
    {
      selectedText[selectedCount] = theSelFrom.options[i].text;
      selectedValues[selectedCount] = theSelFrom.options[i].value;
      deleteOption(theSelFrom, i);
      selectedCount++;
    }
  }
  
  // Add the selected text/values in reverse order.
  // This will add the Options to the 'to' Select
  // in the same order as they were in the 'from' Select.
  for(i=selectedCount-1; i>=0; i--)
  {
    addOption(theSelTo, selectedText[i], selectedValues[i]);
  }
  
 // if(NS4) history.go(0);
}
//*********************CUSTOM FOR CPB************************

function delete_previous_travel(theSelFrom){
	var selLength = theSelFrom.length;
	  var i;

	  for(i=selLength-1; i>=0; i--)
	  {
		if(theSelFrom.options[i].selected)
		{
		  deleteOption(theSelFrom, i);
		}
	  }
}
function get_value_of(target_select){
	var selLength = target_select.length;
	var selectedText = new Array();
  
	var i;
	for(i=selLength-1; i>=0; i--)
	  {
		if(target_select.options[i].selected)
		{
		  return_text = target_select.options[i].text;
		}
	  }
	 return return_text;
}
function make_previous_travel(trip_type, to_country, target_select){
	new_text=get_value_of(to_country) +" - "+ get_value_of(trip_type);
	new_value=new_text;
	addOption(target_select,new_text,new_value);
}

function show_dollar_value_tr(){
	//the listing_dollar_amount gets set initially via .php in the page. This function accesses that variable
	document.listing_edit_form.listing_dollar_amount.removeAttribute("readonly");
	if(dollar_input_visible==false){
		document.listing_edit_form.listing_dollar_amount.value=listing_dollar_amount;
	}
	dollar_input_visible=true;
}
function hide_dollar_value_tr(){
	if(dollar_input_visible==true){
		listing_dollar_amount=document.listing_edit_form.listing_dollar_amount.value;
	}
	dollar_input_visible=false;
	document.listing_edit_form.listing_dollar_amount.value="";
	document.listing_edit_form.listing_dollar_amount.setAttribute("readonly","readonly");
}
//from http://javascript.internet.com/forms/form-focus.html 
function placeFocus() {
	if (document.forms.length > 0) {
		var field = document.forms[0];
		for (i = 0; i < field.length; i++) {
			if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
				document.forms[0].elements[i].focus();
				break;
			}
    	}
	}
}
//**************************************************************
Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
    var i;
    for (i=0; i < this.length; i++) {
        // Matches identical (===), not just similar (==).
        if (this[i] === value) {
            return true;
        }
    }
    return false;
};
Array.prototype.array_search = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
    var i;
    for (i=0; i < this.length; i++) {
        // Matches identical (===), not just similar (==).
        if (this[i] === value) {
            return i;
        }
    }
    return false;
};
//**************************************************************
function on_load_functions(){
	placeFocus();
}