Login   Register  
PHP Classes
elePHPant
Icontem

File: clevertext.js

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Gary Hockin  >  "Clevertext" AJAX text box  >  clevertext.js  >  Download  
File: clevertext.js
Role: Auxiliary data
Content type: text/plain
Description: Clevertext Javascript
Class: "Clevertext" AJAX text box
Generate editable text box that is saved with AJAX
Author: By
Last change:
Date: 2006-06-27 02:08
Size: 3,258 bytes
 

Contents

Class file image Download
// JavaScript Document
// Clevertext textbox javascript handlers.
// Gary Hockin .. gary --at-- garyhockin --dot-- co --dot-- uk
// June 2006

window.onload = attach;

function attach() {
	var objInput = document.getElementsByTagName('input');
	for (var iCounter=0; iCounter<objInput.length; iCounter++)
	objInput[iCounter].onkeydown = function(){return isreturn;} //attach the onchange to each input field
}

function isreturn() {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	alert("keycode: " + keycode);
}

function updatetext($field, $pk, $pkval, $table) {
	http = getHTTPObject();
	var url = 'clevertext.php';
	var $text = document.getElementById($field + '_edit').value;
	var $fullurl = url + '?field=' + $field + '&val=' + $text + '&pkval=' + $pkval + '&pk=';
	var $fullurl = $fullurl + $pk + '&table=' + $table;
	http.open("GET", $fullurl, true);
	http.send(null);
	http.onreadystatechange = handleStateChange;
}

function handleStateChange() {
	if (http.readyState == 4) {
		
	 	if (http.responseXML.getElementsByTagName('field')[0].getAttribute("error") != '') {
			var field = http.responseXML.getElementsByTagName('field')[0].getAttribute('name');
			document.getElementById(field + '_error').innerHTML = http.responseXML.getElementsByTagName('field')[0].getAttribute("error");
			s1 = document.getElementById(field + '_error').style;
			s1.display = "block";
			givefocus(field + '_edit');
		} else {
			node= http.responseXML.getElementsByTagName('field');		
			var field = node[0].getAttribute("name");
			var textvalue = node[0].getAttribute("value");
			
			document.getElementById(field).innerHTML = textvalue;
			document.getElementById(field + '_edit').value = textvalue;
			
			s1 = document.getElementById(field + '_error').style;
			s1.display = "none";
			
			toggle (field, field + '_edit');
		}
	}
}

function givefocus($field) {
	document.getElementById($field).focus();
}

function getHTTPObject() {
var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try 
		{
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
		xmlhttp = false;
		}
	}
	return xmlhttp;
}

function toggle(layeron, layeroff) {
	if (document.getElementById) {
		// this is the way the standards work
		var s1 = document.getElementById(layeron).style;
		s1.display = "block";
		
		var s2 = document.getElementById(layeroff).style;
		s2.display = "none";

	} else if (document.all) {
		// this is the way old msie versions work
		var s1 = document.getElementById(layeron).style;
		s1.display = "block";
		
		var s2 = document.getElementById(layeroff).style;
		s2.display = "none";

	} else if (document.layers)	{
		// this is the way nn4 works
		var s1 = document.getElementById(layeron).style;
		s1.display = "block";
		
		var s2 = document.getElementById(layeroff).style;
		s2.display = "none";
	}	
}