// General form validation class.
//
// formId is the ID of the form to be validated.
//
// fields is an array of FieldValidator objects.
function FormValidator(formId, fields) {
this._isValid = true;
this._fields = fields;
// Attach this validator to the form's onsubmit event.
var form = document.getElementById(formId);
if (form != null) {
formValidator = this;
form.onsubmit = function() {
return formValidator.onSubmit(form);
}
}
}
// Listens for the form's onsubmit event and passes it on to the
// FieldValidator objects. Returns true if all the fields are valid.
FormValidator.prototype.onSubmit = function(form) {
this._isValid = true;
for (var i = 0; i < this._fields.length; i++) {
if (!this._fields[i].onSubmit(form)) {
this._isValid = false;
}
}
if (!this._isValid) {
alert('Please fill in / correct the highlighted fields before submitting again.');
}
return this._isValid;
}
// Class to apply a validation function to a field or fields.
//
// fieldNames is an array of the names of the fields passed as
// arguments to checkValid.
//
// isRequired is set to true if the field can't be empty.
//
// validCheck is a function that returns true if the field is valid.
// It takes any number of fields as arguments. The first field is the
// one that will be highlighted if it is invalid.
//
// requiredCheck if a function that returns true if the field is required.
function FieldValidator(fieldNames, isRequired, validCheck, requiredCheck) {
// Status flags.
this._isEmpty = false;
this._isValid = true;
this._fieldNames = fieldNames;
this._isRequired = isRequired;
this._validCheck = validCheck;
this._requiredCheck = requiredCheck;
}
// Listens (via FormValidator.onSubmit()) for the form's onsubmit
// event and highlights the field it is validating if it's not valid.
// Returns true if the field is valid.
FieldValidator.prototype.onSubmit = function(form) {
var fields = [];
for(i = 0; i < this._fieldNames.length; i++) {
fields[i] = form[this._fieldNames[i]];
}
if (this._isEmpty = this._emptyCheck(fields[0])) {
if (!this._requiredCheck) {
this._isValid = !this._isRequired;
} else {
this._isValid = !this._requiredCheck.apply(this, fields);
}
} else {
// Slightly obscure javascript here:
// this._validCheck is called as a method of this, and the fields array is expanded as it's argument list.
this._isValid = this._validCheck.apply(this, fields);
}
// Set or clear the 'invalid' class.
var className = fields[0].className;
if (this._isValid) {
className = className.replace(/invalid/, '');
fields[0].style.backgroundColor="#F8F8F8";
} else {
if (className.search(/invalid/) == -1) {
//className += ' invalid';
fields[0].style.backgroundColor="#F8F8A8";
}
}
fields[0].className = className;
return this._isValid;
}
FieldValidator.prototype._emptyCheck = function(field) {
return (field.value.search(/\S/) == -1);
}
// Genrate Transition URL
var changeURL ="";
var stateType;
var transitionURL;
var setCMD;
var setDocument_State_ID,setSite_ID, setSite_Name,setDocType_TableName,setDocument_ID,setDocType_ID,setSiteArea_ID,setSiteArea_Name,setSiteAreaDocTypeID,setCounter;
function setURL(type,cmd,document_state_id,site_id,site_name,doctype_tablename,document_id,doctype_id,sitearea_id,sitearea_name,sitearea_doctype_id,counter_id)
{
transitionURL ="cmd="+cmd+"&document_state_id="+document_state_id+"&site_id="+site_id+"&site_name="+site_name+"&doctype_tablename="+doctype_tablename+"&document_id="+document_id+"&doctype_id="+doctype_id+"&sitearea_id="+sitearea_id+"&sitearea_name="+sitearea_name+"&sitearea_doctype_id="+sitearea_doctype_id;
setCMD =cmd;
setSite_ID = site_id;
setSite_Name = site_name;
setDocType_TableName = doctype_tablename;
setDocument_ID = document_id;
setDocType_ID = doctype_id;
setSiteArea_ID = sitearea_id;
setSiteArea_Name = sitearea_name;
setSiteAreaDocTypeID = sitearea_doctype_id;
stateType = type;
setCounter = counter_id;
if(type=="Hide")
{
setDocument_State_ID =2;
}
else
{
setDocument_State_ID =3;
}
//alert(transitionURL);
changeTransitionStatus(transitionURL);
}
// Get an XMLHttpRequest object in a portable way.
function newRequest()
{
xmlHttpReq = false;
// For Safari, Firefox, and other non-MS browsers
if (window.XMLHttpRequest) {
try {
xmlHttpReq = new XMLHttpRequest();
} catch (e) {
xmlHttpReq = false;
}
} else if (window.ActiveXObject) {
// For Internet Explorer on Windows
try {
xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xmlHttpReq = false;
}
}
}
}
// function that communucate to server
function changeTransitionStatus(URL)
{
//alert(cID);
newRequest();
if(xmlHttpReq)
{
xmlHttpReq.open("GET", "index.php?"+URL, true);
xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
var encoded = "";
xmlHttpReq.send(null);
xmlHttpReq.onreadystatechange = setValue;
}
else
{
alert("There was a communications error");
return false;
}
}
//check HTTP staus of Server
function setValue()
{
if(xmlHttpReq.readyState == 4)
{
// To make sure valid response is received from the server, 200 means response received is OK
if(xmlHttpReq.status == 200)
{
// alert(xmlHttpReq.responseXML);
// PopulateResult(xmlHttpReq.responseXML.documentElement);
PopulateResult(xmlHttpReq.responseText);
}
else
{
alert("problem retrieving data from the server, status code: " + xmlHttpReq.status);
}
}
}
function PopulateResult(results)
{
// alert(results);
var divID;
var divID1;
// alert(results);
if(results=="True")
{
// divID = document.getElementById("show");
// alert(stateType);
if(stateType=="Hide")
{
divID = document.getElementById("show"+setCounter);
divID1 = document.getElementById("hide"+setCounter);
stateType ="Show";
divID.innerHTML= 'Show';
divID.style.display="";
divID1.style.display="none";
}
else
{
divID = document.getElementById("hide"+setCounter);
divID1 = document.getElementById("show"+setCounter);
stateType ="Hide";
divID.innerHTML= 'Hide';
divID.style.display="";
divID1.style.display="none";
}
}
else
{
if(stateType=="Hide")
{
divID = document.getElementById("hide"+setCounter);
setDocument_State_ID =3;
divID.innerHTML= 'Show';
}
else
{
divID = document.getElementById("show"+setCounter);
setDocument_State_ID =2;
divID.innerHTML= 'Show';
}
}
}
function GetInnerText (node)
{
return (node.textContent || node.innerText || node.text) ;
}