/******************* validation functions *******************/

function checkDefaults (value, labels) {
  if (value == "Enter your full name here" || value == "Enter email address here")
    {
		highlight("error",labels);
    return false
      }

		highlight("noerror",labels);

  return true;
}

function whiteSpaceOnly(sVal) {
  var sString = new String(sVal);
  var strRegEx = /^\s+$/; // matches strings of all whitespace chars

  if (sVal.match(strRegEx) || sVal == "") {
     return true;
  }
  else {
     return false;
  }
}


// If only numerals are allowed
function isNumOnly(sNum) {
  var sNum = new String(sNum)
  var re = new RegExp(/\D/);

  if (sNum.length > 0) {
  

     if (sNum.search(re)>=0) {
        return false;
     } 
  }
  return true;
}

// If only alphanumeric characters are allowed
function isAlphaNum(sNum) {
  var sString = new String(sNum)
  var re = new RegExp(/[^a-zA-Z0-9 \.\-]/);
  
  if (sString.length > 0) {
  
  	 if (sNum.search(re)>=0) {
        return false;
    } 
 return true;
}
}

function isValidEmail(str, labels) {
 if (str != "") { 
  // check that regular expressions are supported
  var bSupported = 0;
  if (window.RegExp) {
     var tempStr = "a";
     var tempReg = new RegExp(tempStr);
     if (tempReg.test(tempStr)) {
        bSupported = 1;
     }
  }
  if (!bSupported) {
  	alert("called 1");
    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
  }

  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
 	return (!r1.test(str) && r2.test(str));
  }
  else {
  	return false;
  }
	
}


// If only numerals are allowed
function checkNum(sNum) {
	var sNum = new String(sNum)
	var re = new RegExp(/\D/);

	if (sNum.length > 0) {
		if (sNum.search(re)>=0) {
			return false;
		}
	}
	return true;
}//checkNum

// Display error message if they didn't complete all the required fields
function swap(name, index) {
	var i,e;
	/* Loop through all elements that match name+i */
	for (i=1; e=document.getElementById(name+i); i++) {
		/* Check to see if this is the one we want */
		if (i == index) {
			/* If this is our index, make it display */
			e.style.display = "block"; 
		} 
		else {
      		/* If this is not our index, make it hidden */
      		e.style.display = "none";
    	}
  	}
  	return false;
}//swap


function ClearField(cf) {
     if (cf.value == cf.defaultValue) {
         cf.value = "";
     }
}

function isAlphaOnly(sString, labels) {
  var sString = new String(sString);
  var re = new RegExp(/[^a-zA-Z \-]/);
 if (sString != "") { 
  if (sString.length > 0) {
     if (sString.search(re)>=0) {
        return false;
     }
  }
  return true;
  }
  else {
  	return false;
  }
}

function ie_getElementsByTagName(str) {
 // Map to the all collections
  return document.body.all.tags(str)
}


function textAreaTest(value, labels) {
	if (value == "") {
		highlight("error",labels);
		return false;
	}
	else {
		highlight("noerror",labels);
		return true;
	}

}




function textFieldTest(data, labels, results) {
if (data.name.indexOf("email") != -1) { /* tests if value is an email address, email must be in the string name passed */
		results = isValidEmail(data.value, labels);
		
			if (!results) {
				highlight("error",labels);
				return results;
			}
			else {
				highlight("noerror",labels);
				return results;
			}
		}
		else if ( data.name.indexOf("numeric") != -1 ){ /* all numeric values */
		results = checkNum(data.value, labels);
		if (!results) {
				highlight("error",labels);
				return results;
			}
			else {
				highlight("noerror",labels);
				return results;
			}
		}
		
		else {
		results = isAlphaOnly(data.value, labels);
		if (!results) {
				highlight("error",labels);
				return results;
			}
			else {
				highlight("noerror",labels);
				return results;
			}
		}
}


function isSelected(value,labels) {
if (value == "") {
	highlight("error",labels);
	return false;
}
else {
	highlight("noerror",labels);
	return true;
}

}

function highlight(value,labels) {

	if( value == "error") {
		labels.style.color = "#ff0000";
		labels.className = "error";
	}
	else {
		labels.style.color = "#000000";
  		labels.className = "noerror";
	}

}

function arrayTester(theArray, total){
var i;
for (i = 0; i < total; i++) {
	if (theArray[i] == false) { 
		/* turns on the error div */
		var ed = document.getElementById("errorDiv");
		ed.style.display = "block";
		return false;
	}
}
return true;
}



function checkForm(data) {
var i;
var j = 0;
var out;
var a = new Array();
var resulted = new Array();

/* IE test due to lack of getElementsByTagName proper support */
if (document.all) {
	var labels = new Array();
	labels = ie_getElementsByTagName("label");
	
 }
else {
	var labels = document.getElementsByTagName("label");
}

/* Having to use the attribute "name" instead of "for" because IE won't 
allow getAttribute to identify label tag if attribute is "for" */


for ( i = 0; i < data.length; i++) {

if (typeof labels[j].getAttribute("name") != "undefined") {

	while ( data[i].id != labels[j].getAttribute("name")) {
		j++;
	}

	if (data[i].type == "text" || data[i].type == "select-one" || data[i].type == "textarea") {

	  if (checkDefaults (data[i].value, labels[j]) == false) {
	    a[i] = false;
	    continue;
	  }  
			switch(data[i].type) 
			{

			case "text":
				out = textFieldTest(data[i], labels[j], resulted[i]);
				if (!out) { a[i] = out; }
				break;

	
			case "select-one":
				out = isSelected(data[i].value, labels[j]);
				if (!out) { a[i] = out; }
				break;

	
			case "textarea":
				out = textAreaTest(data[i].value, labels[j]);
				if (!out) { a[i] = out; }
				break;

	
			default:
				break;
			}	
			

	}
	

}	
/* tests the array to see if there is a false value */
		if (j == labels.length - 1) {
			return arrayTester(a, labels.length);
		}

	}


}

