// NEW WINDOW
function goWindow(windowname,page,width,height,resize,scroll){
var url = "" + page;
var hWnd = window.open(url,windowname,"width=" + width +",height=" + height +",resizable=" + resize + ",scrollbars=" + scroll + ",top=0,left=" + (screen.width-(width+10)));
if ((document.window != null) && (!hWnd.opener))
hWnd.opener = document.window;
}

// Toggle select/unselect for a list of checkboxes
function checkAll(checkname, exby) {
  for (i = 0; i < checkname.length; i++)
  checkname[i].checked = exby.checked? true:false
}

// Set main_action value in a form
function main_action_set(form_name,action_value){
    var form = eval("document." + form_name);
    if (form != null){
     form.main_action.value = action_value;
    }
}

// DISPLAY TOGGLE STUFF
// div id show start with a small d as in id = "dsearch"
// to toggle the div use showD('search')
function showD(id){
	var showIt;
	if (document.getElementById("d"+id).style.display == "") showIt = "none";
	else showIt = "";
	document.getElementById("d"+id).style.display = showIt;
}

// Toggle modal window
function toggle_modal_window(){
showD('modal');
showD('modal2');
showD('pagebody');
}

// Hide all debug div tags
function hideallDs(){
	var list=new Array("session","user","view");

	for(var item in list)
		document.getElementById("d" + list[item]).style.display = "none";
}


function show(arg){
  var showIt = "";
  document.getElementById("d"+id).style.display = showIt;
}

function hide(id){
  var showIt = "none";
	document.getElementById("d"+id).style.display = showIt;

}


// COOKIES
function getCookie(NameOfCookie)
{  if (document.cookie.length > 0)
{     begin = document.cookie.indexOf(NameOfCookie+"=");
    if (begin != -1)
     begin += NameOfCookie.length+1;
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
      return unescape(document.cookie.substring(begin, end));
  }
return null;
}

function setCookie(NameOfCookie, value)
{  var ExpireDate = new Date ();
  ExpireDate.setTime(ExpireDate.getTime());
  document.cookie = NameOfCookie + "=" +
  escape(value) +
  ";path=/";
}

function delCookie (NameOfCookie)
{  if (getCookie(NameOfCookie)) {
  document.cookie = NameOfCookie + "=" +
   "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
}

// LINKS

// Create a link in a text field
function create_link(objField){
var destination = "";
var phrase;
destination = prompt( "Enter your link target (e.g. http://www.your.link)", "http://" );
if (destination != null && destination != "http://") {
		  	phrase = prompt( "Enter your link text (e.g. click here)", "" );
		    if ( phrase != null ) {
			   	objField.value += '<a href=\"';
				  objField.value += destination + '\">' + phrase + "</a>";
				objField.focus();
			  }
		  }
}

// Create an image in a text filed
function create_image(objField){
var destination = "";
destination = prompt( "Enter the absolute path to your image (e.g. http://www.path.to/your/image)", "" );
		    if ( destination != null && destination !="") {
			   	objField.value += '<img src=\"';
				  objField.value += destination + "\" alt=\"[]\">";
				objField.focus();
			  }
}

// INNER HTML

function changespancontent(input,spantarget){
	loadFragmentInToElement(input,spantarget);
}

function loadFragmentInToElement(text, element_id) {
    var element = document.getElementById(element_id);
    element.innerHTML = text;
		var onreadystatechange = function() {
        element.innerHTML = xmlhttp.responseText;
		}
return true;
}


function submitForm(arg){
//alert(view_id);
eval("document. " + arg + ".submit()");
}

// text area focus functions

function setSelectedTextRange(elm, selectionStart, selectionEnd) {
if (elm.setSelectionRange) {
elm.focus();
elm.setSelectionRange(selectionStart, selectionEnd);
}
else if (elm.createTextRange) {
var range = elm.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
}
function setCaretToEnd (elm) {
setSelectedTextRange(elm, elm.value.length, elm.value.length);
}
function setCaretToStart (elm) {
setSelectedTextRange(elm, 0, 0);
}
function setCaretToPos (elm, pos) {
setSelectedTextRange(elm, pos, pos);
}

// AJAX - main functions

function loadXMLDoc(url){
   // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange(){
    //alert (reg.readyState);
   // only if req shows "complete"
   if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
             response  = req.responseXML.documentElement;
            method    = response.getElementsByTagName('method')[0].firstChild.data;
           result    = response.getElementsByTagName('result')[0].firstChild.data;
          //alert (result);
          eval(method + '(\'\', result)');
		//var fragment_url = result;
		 //document.getElementById("test").innerHTML="new content";
       } else {
       	showAlertMonitorError();
       }
    }
}

function showAlertMonitorError(){
var answer = confirm (APPLICATION_NAME + "'s Real Time Monitoring (RTM) failed to retrieve your :\n\n - new messages\n - online buddies\n\nIf this occus frequently you should probably adjust your RTM setting.\nLow bandwidth users should turn off RTM when downloading large files.\n\n\n\nOK [enter key] to continue\nClick Cancel to adjust your settings")
	if(!answer){
		window.location= APPLICATION_RESPONSE_PATH + "/profile_edit/";
	}
}

// AJAX - specific functions

   function checkusermessages(input,response){
	//alert(checkName);
      if (response!=''){
		//alert(response);
         if (response > 0){
			   loadFragmentInToElement('&nbsp;<img src=\"' + APPLICATION_IMAGES_PATH + '/mail_mail.gif\" border=\"0\"> <a href=\"' + APPLICATION_RESPONSE_PATH + '/messages/\" class=\"menu\">' + response + ' Unread Message(s)</a>', 'user_messages');
          }else{
			   loadFragmentInToElement('No New Messages: <a href="' + APPLICATION_RESPONSE_PATH + '/messages/" class="menu">In Box</a>', 'user_messages');
          }
        }else{
          // Input mode
          var url  = APPLICATION_RESPONSE_PATH + '/checkusermessages/?q=' + input;
					//alert(input);
           loadXMLDoc(url);
        }
   }

   function checkuserbuddiesonline(input,response){
	//alert(checkName);
      if (response!=''){
		//alert(response);
		   loadFragmentInToElement(response, 'user_buddies_online');

        }else{
          // Input mode
          url  = APPLICATION_RESPONSE_PATH + '/checkuserbuddiesonline/?q=' + input;
           loadXMLDoc(url);
        }
   }

   function checkuserbuddiestotal(input,response){
	//alert(checkName);
      if (response!=''){
		//alert(response);
		   loadFragmentInToElement(response, 'user_buddies_total');
        }else{
          // Input mode
          url  = APPLICATION_RESPONSE_PATH + '/checkuserbuddiestotal/?q=' + input;
           loadXMLDoc(url);
        }
   }
