// Form validate scripts.


// Check all input type=text and textarea form controls for XML well-formed-ness
//
// Returns true if all form values are valid xml, false if they are not.

function formCheckValues()
{
    inputs = document.getElementsByTagName('input');
	
    error = true;

    for (i = 0; i < inputs.length; i++) { 
        if (inputs[i].type == 'text') {
            result = formCheckXmlValid(inputs[i]);	
          
            if (!result) { 
                error = false;
            }
        }
    }
	
    textareas = document.getElementsByTagName('textarea');

    for (i = 0; i < textareas.length; i++) { 
        result = formCheckXmlValid(textareas[i]);	

        if (!result) { 
            error = false;
        }
    }	

    return error;
}
	
// Checks specific form control for well-formed-ness	
function formCheckXmlValid(obj) 
{
    elements = obj.value.split('<');
    
    end   = /^\/([^>]+?)>(.*)/;
    start = /^([^>]+?)>(.*)/;
    
    stack = new Array();
    
    for (j = 0; j < elements.length; j++) { 
        ele = elements[j];
        
        // alert(ele);
        
        resulta = ele.match(end); 
        if (!resulta) { 
            // To save a bit of time only do the second
            // match if the first fails 
            resultb = ele.match(start);
        }
        
        if (resulta) { 
            // pop
            
            // alert('popping: ' + resulta[1]); 
            
            if (stack.length == 0) { 
                alert('Too many closed tags: </' + ele + '>');
                obj.className = 'xmlError';
                obj.focus();
                return false;
            }
            
            a = stack.pop();

            if (a != resulta[1]) { 
                alert('Tags do not match: <' + a + '> and </' + resulta[1] + '>');
                obj.className = 'xmlError';
                obj.focus();
                return false;
            }

        } else if (resultb) {
            // push
            
            // alert('pushing: ' + resultb[1]);
            
            stack.push(resultb[1]);
            
        }
    }
    
    if (stack.length) { 
	
        while (tag = stack.pop()) { 
            alert('You must close: <' + tag + '>');
        }
        
        obj.className = 'xmlError';
        obj.focus();

        return false;
    } else { 
        obj.className = '';

        return true;
    }
}




// Validation functions...

function getSelectedRadio(group) 
{
    if (group[0]) { // if the button group is an array (one button is not an array)
        for (var i=0; i < group.length; i++) {
            if (group[i].checked) {
                return i;
            }
        }
    } else if (group.checked) { 
        return 0; 
    }
    
    return -1;
}

function getElementValue(name)
{
    d = document.getElementsByName(name);
    if (!d) {
        alert('getElementValue ' + name + ' not found');
        return false;
    }
    
    type = d[0].type;
    
    if (type == 'text' || type == 'hidden' || type == 'password' || type == 'textarea') {
        return d[0].value;
    } else if (type == 'checkbox') {
        if (d[0].checked) { 
            return d[0].value;
        } else {
            return false;
        }
    } else if (type == 'select-one') {
        return d[0].options[d[0].selectedIndex].value;
    } else if (type == 'radio') {
        i = getSelectedRadio(d);
        if (i > -1) {
            return d[i].value;
        } else {
            return false;
        }
    }
    return false;
}

function not_empty(name) 
{
    var str = getElementValue(name);
    var re = /.+/;
    
    if (!str || !str.match(re)) {
        return false;
    } else {
        return true;
    }
}

function valid_date(name) 
{
    return true;
}

function number(name, min, max) 
{
    var str = getElementValue(name);
    var re = /^[-]?\d*\.?\d*$/;

    check = str.toString();
    if (check == '' || !check.match(re)) {
        return false;
    } else {
        if (min != undefined && str < min) {
            alert('number: ' + name + ', min=' + min);
            return false;
        } else if (max != undefined && str > max) {
            alert('number: ' + name + ', max=' + max);
            return false;
        }
    }
    return true;
}

// number_str
//
// Params- 
//
//   str - string to validate
//   min - min valid value (optional - NULL for no)
//   max - max valid value (optional - NULL for no)
//
// Returns
//
//   false - valid
//   (otherwise error string)


function number_str(str, min, max) 
{
    var re = /^[-]?\d*\.?\d*$/;

    check = str.toString();
    if (check == '' || !check.match(re)) {
        return 'not a number';
    } else {
        if (min != null && str < min) {
            return 'must be greater than ' + (min - 1);
        } else if (max != null && str > max) {
            return 'must be less than ' + (max - 1);
        }
    }
   
    return false;
}

function trim(str)
{
    if (str.length < 1) {
        return '';
    }
    str = rtrim(str);
    str = ltrim(str);

    return str;
} 

function rtrim(str){
    var w_space  = String.fromCharCode(32);
    var v_length = str.length;
    var strTemp  = '';
    
    if(v_length < 0) {
        return '';
    }
    
    var iTemp = v_length - 1;

    while (iTemp > -1) {
        if (str.charAt(iTemp) == w_space) {
            // do nothing
        } else {
            strTemp = str.substring(0,iTemp +1);
            break;
        }
        
        iTemp = iTemp-1;

    } 
    
    return strTemp;
} 

function ltrim(str)
{
    var w_space = String.fromCharCode(32);
    if (v_length < 1) {
        return '';
    }
    var v_length = str.length;
    var strTemp = '';

    var iTemp = 0;

    while (iTemp < v_length) {
        if (str.charAt(iTemp) == w_space) {
            // do nothing
        } else {
            strTemp = str.substring(iTemp,v_length);
            break;
        }
        
        iTemp = iTemp + 1;
    }
    
    return strTemp;
}

function empty_string(str) 
{
  var tmp = str;
  tmp 

  if(tmp.length > 0){
    return false;
  }
  return true;
}

function valid_emailaddr(str) 
{
  var objRegExp = /^([_~a-z0-9-]+[._a-z0-9-]*)@(([a-z0-9-]+\.)*([a-z0-9-]+)(\.[a-z]{2,4}))$/i;

  return objRegExp.test(str);
}

function getCheckedRadioButton(name)
{
  radios = document.getElementsByName(name);
  radio = false;
  for (i = 0; i < radios.length; i++) { 
    if (radios[i].checked) {
      radio = radios[i];
    }
  }
  
  return radio;
}

function getSelectBoxValue(id)
{
  box = document.getElementById(id);
  if (!box) {
    alert(id + ' not found');
  } else {
    if (box.options) {
      value = box.options[box.selectedIndex].value;
      return value;
    }
  }

  return false;
}

// Helper functions

function getLabelById(id) 
{
  labels = document.getElementsByTagName('label');
  label = false;

  for (i = 0; i < labels.length; i++) { 
    if (labels[i].htmlFor == id) { 
      label = labels[i];
      break;
    }
  }
  
  if (!label) {
    //    alert('label for: ' + id + ' not found');
  }
    
  return label;
}

function reportError(id) 
{
    ele = document.getElementById(id);
    if (!ele) { 
        alert('reportError: form element ' + id + ' not found');
        return;
    }

    ele.className = 'validationError';
}

function unReportError(id) 
{
    ele = document.getElementById(id);
    if (!ele) { 
        alert('reportError: form element ' + id + ' not found');
        return;
    }

    ele.className = '';
}

function reportErrorModif(id) 
{
    ele = document.getElementById(id);
    if (!ele) { 
        alert('reportError: form element ' + id + ' not found');
        return;
    }
	
	if (ele.className.search("validationError") == -1) {
		var oldClass = ele.className + " ";
		ele.className = oldClass + 'validationError';
	}
}

function unReportErrorModif(id) 
{
    ele = document.getElementById(id);
    if (!ele) { 
        alert('reportError: form element ' + id + ' not found');
        return;
    }
	
	var newClass = ele.className.replace(" validationError", "");
    ele.className = newClass;
}
