/*-----------\\\\\\\\\\\\\\\\///////////////////////------------#
#------------\\\\\\\\\\\\\\\\///////////////////////------------#
#								#
#	Original Package Name: asdgjavascript.js		#
#	Current  Package Name: asdgjavascript.js		#
#---------------------------------------------------------------#
#	THIS SCRIPT LICENSED TO: ASDG ONLY, INTERNAL		#
#***************************************************************#
#			COPYRIGHT NOTICE			#
#***************************************************************#
#	Not copywritten, this javascript is in the public domain#
#	and anyone is free to copy it outright.  Enjoy.		#
#---------------------------------------------------------------#
#	 [SCRIPT AUTHOR/DESIGNER: Robert McAdams, ASDG]		#
#	 [CONTACT:                http://www.asayogure.com]	#
#	------------------------------------------------	#
#	Last Programmer to update this script:			#
#	Robert McAdams - Script Designer			#
#---------------------------------------------------------------#
#	PURPOSE OF THIS PACKAGE:				#
#	Javascript for site AJAX				#
#---------------------------------------------------------------#
#	Version: Live						#
#	Last Date Updated: 2009-04-04				#
#---------------------------------------------------------------#
#	Version codes list: http://asayogure.com/vcodes.txt	#
#---------------------------------------------------------------#
#	CURRENT KNOWN BUG LIST!					#
#	--Major Bugs:	None.					#
#	--Small Bugs:	None.					#
#	--Tiny  Bugs:	None.					#
#------------\\\\\\\\\\\\\\\\///////////////////////------------#
#------------\\\\\\\\\\\\\\\\///////////////////////-----------*/

//---------------------------------------------------------------
//GLOBAL VARIABLES
var xmlHttp  = createXmlHttp();
var uxmlHttp = createXmlHttp();
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function checkOKSendDeliverMyFiles(){
  var has_email = false;
  var has_file  = false;
  for (var i=0; i < document.forms['main'].elements.length; i++){
    //alert("processing field: "+document.forms['main'].elements[i].name+" of type: "+document.forms['main'].elements[i].type);
    var val = document.forms['main'].elements[i].value;
    if(document.forms['main'].elements[i].type == "checkbox"){
      if(document.forms['main'].elements[i].checked){
        has_email = true;
      }
    }
    else if(document.forms['main'].elements[i].type == "text" && document.forms['main'].elements[i].name == "extra_to"){
      if (val.length>0){
        has_email = true;
      }
    }
    //else if(document.forms['main'].elements[i].type == "file"){
    //  if (val.length>0){
    //    has_file  = true;
    //  }
    //}
  }
  //var _file = parent.document.getElementById("fileName");
  //if (_file != null){
  //  if (_file.value != null && _file.value != ""){
  //    has_file  = true;
  //  }
  //}
  //else{
  //  alert("fileName has the value: "+document.getElementById('fileName'));
  //}
  if (has_email && fileCount > 0){
    $('#file_upload').uploadifyUpload();
    return true;
  }
  else{
    alert("ERROR, either missing an e-mail to send to, or a file to send!\n[has_email value: "+has_email+" file count: "+fileCount+"]");
    return false;
  }
}
function clear_extra_addresses(){
  var extra_to = document.getElementById("extra_to");
  var extra_names = document.getElementById("extra_names");
  extra_to.value="";
  extra_names.value="";
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function createXmlHttp(){
  var _xmlHttp;
  try{
    // Firefox, Opera 8.0+, Safari
    _xmlHttp=new XMLHttpRequest();
  }
  catch (e){
    // Internet Explorer
    try{
      _xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e){
      try{
        _xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e){
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return _xmlHttp;
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function getRequestBody(oForm){
  var aParams = new Array();
  for (var i=0; i < oForm.elements.length; i++){
    var sParam = encodeURIComponent(oForm.elements[i].name);
    //alert("name:"+oForm.elements[i].name.toString()+",type:"+oForm.elements[i].type.toString()+",value:"+oForm.elements[i].value.toString());
    //alert("name:"+oForm.elements[i].name.toString()+",type:"+oForm.elements[i].type.toString());
    if(oForm.elements[i].type == "checkbox"){
      //alert("checkbox!");
      if(oForm.elements[i].checked){
        //alert("box was checked");
        sParam += "=";
        sParam += encodeURIComponent(oForm.elements[i].value);
        aParams.push(sParam);
      }
    }
    else if(oForm.elements[i].type == "file" || 
            oForm.elements[i].type == "application/x-shockwave-flash" || 
            oForm.elements[i].type == "button"){
      //
    }
    else{
      //alert("not a checkbox");
      sParam += "=";
      sParam += encodeURIComponent(oForm.elements[i].value);
      aParams.push(sParam);
    }
    //alert("sParam:"+sParam);
    //alert("aParams:"+aParams);
  }
  //alert("aParams final:"+aParams.join("&"));
  return aParams.join("&");
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function deleteFile(formName){
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  var divStatus = document.getElementById("divStatus");
  divStatus.innerHTML = "";
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  var oForm = document.forms[formName];
  var sBody = getRequestBody(oForm);
  var xmlHttp = createXmlHttp();
  xmlHttp.open("post",oForm.action, true);
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.onreadystatechange = function(){
    if (xmlHttp.readyState == 4){
      if (xmlHttp.status == 200){
        saveResult(xmlHttp.responseText);
      }
      else{
        alert("An error occurred: " + xmlHttp.statusText);
      }
    }
  };
  xmlHttp.send(sBody);
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function sendRequest(formName){
  //alert('got form: ' + formName);
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  var divStatus = document.getElementById("divStatus");
  divStatus.innerHTML = "";
  //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  var oForm = document.forms[formName];
  var sBody = getRequestBody(oForm);
  var xmlHttp = createXmlHttp();
  xmlHttp.open("post",oForm.action, true);
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.onreadystatechange = function(){
    if (xmlHttp.readyState == 4){
      if (xmlHttp.status == 200){
        if (formName == 'main'){
          saveResult(xmlHttp.responseText);
        }
        else if(formName == 'editaboutmeform'){
          showStatus(xmlHttp.responseText,'editAboutMeStatus');
          updateAboutMeView();
          toggle('editaboutme');
          toggle('showaboutme');
        }
        else if(formName == 'editaccountform'){
          toggle('edityouraccount');
          showStatus(xmlHttp.responseText,'editAccountStatus');
        }
        else if(formName == 'findfriendsform'){
          var showWorking = document.getElementById("friendSearchWorking");
          showWorking.style.display = "";
          displayFriends();
        }
        else{
          alert("Received instruction, " + formName + ", but don't know what to do with it.");
        }
      }
      else{
        alert("An error occurred: " + xmlHttp.statusText);
      }
    }
  };
  xmlHttp.send(sBody);
}
function saveResult(sMessage) {
  var divStatus = document.getElementById("divStatus");
  divStatus.innerHTML = sMessage;            
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function showBusy(){
  //turn "working" icon on
  var showWorking = document.getElementById("friendSearchWorking");
  showWorking.style.display = "";
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function showFree(){
  //turn "working" icon off
  var showWorking = document.getElementById("friendSearchWorking");
  showWorking.style.display = "none";
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function hideMOTD(uid){
  showBusy();
  var url="http://friendspace.cowanrfamily.com/cgi-bin/hidemotd.cgi?uid="+uid;
  uxmlHttp.onreadystatechange=doNothing;
  uxmlHttp.open("GET",url,true);
  uxmlHttp.send(null);
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function doNothing(){
  if (uxmlHttp.readyState==4 && uxmlHttp.status == 200){ 
    if (uxmlHttp.status != 200){alert("status: "+ uxmlHttp.status)}
    if (uxmlHttp.responseXML.documentElement == null){
      alert("ERROR: status: "+ uxmlHttp.status + " status text:" + uxmlHttp.statusText);
    }
    showFree();
  }
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function showFriend(uid){
  showBusy();
  var url="http://friendspace.cowanrfamily.com/cgi-bin/showfriend.cgi?uid="+uid;
  uxmlHttp.onreadystatechange=showFriendPreview;
  uxmlHttp.open("GET",url,true);
  uxmlHttp.send(null);
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function showFriendPreview(){
  if (uxmlHttp.readyState==4 && uxmlHttp.status == 200){ 
    if (uxmlHttp.status != 200){alert("status: "+ uxmlHttp.status)}
    if (uxmlHttp.responseXML.documentElement == null){
      alert("ERROR: status: "+ uxmlHttp.status + " status text:" + uxmlHttp.statusText);
    }
    var xmlDoc=uxmlHttp.responseXML.documentElement;
    var wereokay = handleError(xmlDoc);if (!wereokay){return false;}
    functionUpdateDomElement(xmlDoc,"displaywindow_message",true);
    functionUpdateDomElement(xmlDoc,"displaywindow_name",true);
    functionUpdateDomElement(xmlDoc,"displaywindow_picture",true);
    functionUpdateDomElement(xmlDoc,"displaywindow_name2",true);
    functionUpdateDomElement(xmlDoc,"displaywindow_mutual",true);
    var friends = parseInt(xmlDoc.getElementsByTagName("xmldisplaywindow_mutual")[0].childNodes[0].nodeValue);
    if (friends > 4){friends = 4;}
    if (friends > 0){
      for (var i=0;i<friends;i++){
        var tmpName = "displaywindow_f"+(i+1);
        functionUpdateDomElement(xmlDoc,tmpName,true);
      }
    }
    openWindow('displayFriend');
    showFree();
  }
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function handleError(xmlDoc){
  var chkError;
  try{
    chkError = xmlDoc.getElementsByTagName("xmlerror")[0].childNodes[0].nodeValue;
  }
  catch (e){
    return true;
  }
  if (chkError.length > 0){alert(chkError); showFree(); return false;}
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function displayFriends(){
  var searchFriendCriteria = document.getElementById("searchFriendCriteria");
  var searchFriendValue = document.getElementById("searchFriendValue");
  var p1 = searchFriendCriteria[searchFriendCriteria.selectedIndex].value;
  var p2 = searchFriendValue.value;
  var url="http://friendspace.cowanrfamily.com/cgi-bin/searchfriend.cgi?searchFriendCriteria=" + p1 + "&searchFriendValue=" + p2;
  uxmlHttp.onreadystatechange=showFriendSearchResults;
  uxmlHttp.open("GET",url,true);
  uxmlHttp.send(null);
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function showFriendSearchResults(){
  if (uxmlHttp.readyState==4 && uxmlHttp.status == 200){ 
    if (uxmlHttp.status != 200){alert("status: "+ uxmlHttp.status)}
    if (uxmlHttp.responseXML.documentElement == null){
      alert("ERROR: status: "+ uxmlHttp.status + " status text:" + uxmlHttp.statusText);
    }
    var xmlDoc=uxmlHttp.responseXML.documentElement;
    var wereokay = handleError(xmlDoc);if (!wereokay){return false;}
    var matches = parseInt(xmlDoc.getElementsByTagName("xmlcount")[0].childNodes[0].nodeValue);
    if (matches > 10){matches = 10;}//only have room to display 10
    functionUpdateDomElement(xmlDoc,"count",true);
    var loopcount = 0
    for (loopcount=0;loopcount<matches;loopcount++){
      var tmpName = "match"+loopcount;
      functionUpdateDomElement(xmlDoc,tmpName,true);
    }
    var showWorking = document.getElementById("friendSearchWorking");
    showWorking.style.display = "none";
    openWindow('displaySearch');
  }
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function updateAboutMeView(){ 
  var url="http://friendspace.cowanrfamily.com/cgi-bin/showaboutme.cgi";
  uxmlHttp.onreadystatechange=showAboutMeUpdates;
  uxmlHttp.open("GET",url,true);
  uxmlHttp.send(null);
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function showAboutMeUpdates(){ 
  if (uxmlHttp.readyState==4 && uxmlHttp.status == 200){ 
    if (uxmlHttp.status != 200){alert("status: "+ uxmlHttp.status)}
    if (uxmlHttp.responseXML.documentElement == null){
      alert("ERROR: status: "+ uxmlHttp.status + " status text:" + uxmlHttp.statusText);
    }
    var xmlDoc=uxmlHttp.responseXML.documentElement;
    functionUpdateDomElement(xmlDoc,"email",false);
    functionUpdateDomElement(xmlDoc,"about_me",false);
    functionUpdateDomElement(xmlDoc,"aim",false);
    functionUpdateDomElement(xmlDoc,"icq",false);
    functionUpdateDomElement(xmlDoc,"imvu",false);
    functionUpdateDomElement(xmlDoc,"jabber",false);
    functionUpdateDomElement(xmlDoc,"msnm",false);
    functionUpdateDomElement(xmlDoc,"skype",false);
    functionUpdateDomElement(xmlDoc,"yahoo",false);
    functionUpdateDomElement(xmlDoc,"town_home",false);
    functionUpdateDomElement(xmlDoc,"town_now",false);
    functionUpdateDomElement(xmlDoc,"website",false);
    functionUpdateDomElement(xmlDoc,"dating",false);
    functionUpdateDomElement(xmlDoc,"sex_pref",false);
    functionUpdateDomElement(xmlDoc,"desire",false);
    functionUpdateDomElement(xmlDoc,"political",false);
    functionUpdateDomElement(xmlDoc,"religious",false);
    functionUpdateDomElement(xmlDoc,"zodiac",false);
    functionUpdateDomElement(xmlDoc,"children",false);
  }
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function functionUpdateDomElement(xmlDoc,domName,noToggleNew){
  var shwName = "show"+domName;
  var xmlName = "xml" +domName;
  var topName = "top_"+domName;
  try{
    document.getElementById(shwName).innerHTML=unescape(xmlDoc.getElementsByTagName(""+xmlName+"")[0].childNodes[0].nodeValue);
    var str = xmlDoc.getElementsByTagName(""+xmlName+"")[0].childNodes[0].nodeValue;
  }
  catch (e){
    return true;
  }
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function showStatus(sMessage,domElement) {
  var divStatus = document.getElementById(domElement);
  divStatus.innerHTML = sMessage;            
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function toggle(targetId){
  if (document.getElementById){
    target = document.getElementById(targetId);
    if (target.style.display == "none"){target.style.display = "";}
    else                               {target.style.display = "none";}
  }
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function closeWindow(targetId){
  if (document.getElementById){
    target = document.getElementById(targetId);
    target.style.display = "none";
  }
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function openWindow(targetId){
  if (document.getElementById){
    target = document.getElementById(targetId);
    target.style.display = "";
  }
}
//#---------------------------------------------------------------
//#FUNCTION: 	xxxxx
//#PURPOSE:	xxxxx
//#PARAMS:   	xxxxx
//#FIELDS:   	xxxxx
//#TO DO:    	none
//#---------------------------------------------------------------
function toggleNew(len,topID){
  if (document.getElementById){
    target = document.getElementById(topID);
    if (target == null){alert("target " + topID + " is null");return;}
    if (len > 1){
      target.style.display = "";
    }
    else{
      target.style.display = "none";
    }
  }
}
//#---------------------------------------------------------------
//#FUNCTION: 	autoTab
//#PURPOSE:	Moves the cursor from phone field to phone field
//#PARAMS:   	input,len, e
//#FIELDS:   	phone fields call this
//#TO DO:    	none
//#---------------------------------------------------------------
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }
  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }
  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}
//#---------------------------------------------------------------
//#FUNCTION: 	checkForm
//#PURPOSE:	checks form to verify required fields have
//#		been filled out.  The perl script will catch
//#		it if javascript is disabled, but this feedback
//#		method is more immediate for the user
//#PARAMS:   	none
//#FIELDS:   	required fields, marked as bold in form
//#TO DO:    	none
//#---------------------------------------------------------------
function checkform(form){
  if (form.fname.value == ""){
    alert( "Please enter your first name." );
    form.main.fname.focus();
    return false;
  }
  if (form.lname.value == ""){
    alert( "Please enter your last name." );
    form.lname.focus();
    return false;
  }
  if (form.nickname.value == ""){
    alert( "Please choose a nickname." );
    form.nickname.focus();
    return false;
  }
  if (form.email.value == ""){
    alert( "Please enter your e-mail address." );
    form.email.focus();
    return false;
  }
  if (form.username.value == ""){
    alert( "Please pick a username." );
    form.username.focus();
    return false;
  }
  if (form.password.value == ""){
    alert( "Please pick a password." );
    form.password.focus();
    return false;
  }
  if (form.username.value == form.password.value){
    alert( "Your password cannot equal your username." );
    form.password.focus();
    return false;
  }
  if (form.username.value.length < 5){
    alert( "Your username must be at least 5 characters long." );
    form.password.focus();
    return false;
  }
  if (form.password.value.length < 5){
    alert( "Your password must be at least 5 characters long." );
    form.password.focus();
    return false;
  }
  var pass = form.password.value;
  var testalpha = new RegExp("[a-zA-Z]","gi");
  var testnumbr = new RegExp("[0-9]","gi");
  if(!pass.match(testalpha)){
    alert( "Your password must contain at least one alphabet character (a-z,A-Z)." );
    form.password.focus();
    return false;
  }
  if(!pass.match(testnumbr)){
    alert( "Your password must contain at least one numeric character (0-9)." );
    form.password.focus();
    return false;
  }
  if (form.answer.value == ""){
    alert( "Please enter your answer to the human test." );
    form.answer.focus();
    return false;
  }
  return true ;
}
//#---------------------------------------------------------------
//#FUNCTION: 	trackFieldLength
//#PURPOSE:	Keeps a running track of the number of characters
//#		typed into a text field, and then updates and 
//#		displays that running total in a disabled input
//#		text field.
//#PARAMS:   	none
//#FIELDS:   	desc = the textarea field to be monitored
//#          	fieldlength = the disabled text input field
//#		that displays the running count.
//#          	for custom resolutions
//#TO DO: 	I want to add support for changing text colors
//#		when the limit is exceeded, and to hide the display
//#		completely, when the length is zero, and add support
//#		for onChange and onClick events, in case the text
//# 	 	is input by copy+paste via the mouse
//#---------------------------------------------------------------
function trackFieldLength(){
  var fieldlen = document.main.descr.value;
  document.main.fieldlength.value = fieldlen.length;
  if (fieldlen.length > 500 && hasWarned == false){
    alert("You have exceeded the 500 character maximum for this field!\n"+
          "Anything after the 500th character WILL NOT BE SAVED.\n"+
          "Please edit the text you have entered, to a length of 500 characters or less.");
  }
  if (hasWarned == true && fieldlen.length <= 500){
    hasWarned = false;
  }
}



//http://www.java2s.com/Code/JavaScript/Event/MouseDragandDrop.htm
function draggable(){
  //Stop Opera selecting anything whilst dragging.
  if (window.opera){
    document.write("<input type='hidden' id='Q' value=' '>");
  }
  var n = 500;
  var dragok = false;
  var y,x,d,dy,dx;

  function move(e){
  if (!e) e = window.event;
   if (dragok){
    d.style.left = dx + e.clientX - x + "px";
    d.style.top  = dy + e.clientY - y + "px";
    return false;
   }
  }

  function down(e){
    if (!e) e = window.event;
    var temp = (typeof e.target != "undefined")?e.target:e.srcElement;
    if (temp.tagName != "HTML"|"BODY" && temp.className != "draggable"){
      temp = (typeof temp.parentNode != "undefined")?temp.parentNode:temp.parentElement;
    }
    if (temp.className == "draggable"){
      if (window.opera){
        document.getElementById("Q").focus();
      }
      dragok = true;
      temp.style.zIndex = n++;
      d = temp;
      dx = parseInt(temp.style.left+0);
      dy = parseInt(temp.style.top+0);
      x = e.clientX;
      y = e.clientY;
      document.onmousemove = move;
      return false;
    }
  }
  function up(){
    dragok = false;
    document.onmousemove = null;
  }
  document.onmousedown = down;
  document.onmouseup = up;
}


  function beginDrag(elementToDrag, event){
    var deltaX = event.clientX - parseInt(elementToDrag.style.left);
    var deltaY = event.clientY - parseInt(elementToDrag.style.top);
    if (document.addEventListener){
      document.addEventListener("mousemove", moveHandler, true);
      document.addEventListener("mouseup", upHandler, true);
    }
    else if (document.attachEvent){
      document.attachEvent("onmousemove", moveHandler);
      document.attachEvent("onmouseup", upHandler);
    }
    else{
      var oldmovehandler = document.onmousemove;
      var olduphandler = document.onmouseup;
      document.onmousemove = moveHandler;
      document.onmouseup = upHandler;
    }
    if (event.stopPropagation) event.stopPropagation();
    else event.cancelBubble = true;
    if (event.preventDefault) event.preventDefault();
    else event.returnValue = false;
    function moveHandler(e){
      if (!e) e = window.event;
      document.write("setting left (" + elementToDrag.style.left + ") to: " + "(" + e.clientX + "-" + deltaX +")" + "px");
      elementToDrag.style.left = (e.clientX - deltaX) + "px";
      elementToDrag.style.top = (e.clientY - deltaY) + "px";
      if (e.stopPropagation) e.stopPropagation();
      else e.cancelBubble = true;
    }
    function upHandler(e){
      if (!e) e = window.event;
      if (document.removeEventListener){
        document.removeEventListener("mouseup", upHandler, true);
        document.removeEventListener("mousemove", moveHandler, true);
      }
      else if (document.detachEvent){
        document.detachEvent("onmouseup", upHandler);
        document.detachEvent("onmousemove", moveHandler);
      }
      else {
        document.onmouseup = olduphandler;
        document.onmousemove = oldmovehandler;
      }
      if (e.stopPropagation) e.stopPropagation();
      else e.cancelBubble = true;
    }
  }

