var goFlag = true;
var msg = "";
var courseQuantityError = 0;
var courseSizeError = 0;

function tagError(id,text){
	tmpPar = $(id).parentNode;
	tmpPar.className = tmpPar.className + " error";
	tmpPar.setAttribute("title","This field is required");
	goFlag = false;
	msg = text;
}

function val_ecas(){
	var theTable = $("globalLogin");
	var tdList = theTable.getElementsByTagName("td");
	clearForm(tdList);
	$("userName").value = regXtrim($("userName").value);
	if($("userName").value.length < 1){
		tagError("userName","Enter your username");
	}
	$("password").value = regXtrim($("password").value);
	if($("password").value.length < 1){
		tagError("password","Enter your password");
	}
	// return result
	if(goFlag){
		return true;    
	}else{
		$("errFlag").innerHTML = msg;
		$("errFlag").style.display = "block";
		return false;
	}
}

function val_courseCont(){
	msg = "";
	var theTable = $("contact");
	var tdList = theTable.getElementsByTagName("td");
	//reset form
	clearForm(tdList);

	if($("userName")){
		$("userName").value = regXtrim($("userName").value);
		if($("userName").value.length < 1){
			tagError("userName","Enter your name");
		}
	}
	if($("myEmail")){
		if(!checkMailSyntax($("myEmail").value)){
			tagError("myEmail","The Email address appears to be invalid.");
		}
	}
	if(regXtrim($("company").value)< 1){
		tagError("company","Please enter your company name.");
	}
	if($("country").options[$("country").selectedIndex].value == "United States" && $("state").options[$("state").selectedIndex].value<1){
		tagError("state","Select a state or province");
	}
	if($("country").options[$("country").selectedIndex].value == "Canada" && $("state").options[$("state").selectedIndex].value<1){
		tagError("state","Select a state or province");
	}
	if($("country").options[$("country").selectedIndex].value<1)
	{
		tagError("country","Select your country"); 
	}
	if(regXtrim($("theMessage").value)< 1){
		tagError("theMessage","Please enter your message.");
	}

	// return result
	if(goFlag){
		return true;
	}else{
		$("errFlag").innerHTML = msg;
		$("errFlag").style.display = "block";
		return false;
	}
	return false
}

function val_contactUs(){
	msg = "";
	goFlag = true;
	var theTable = $("contact");
	var tdList = theTable.getElementsByTagName("td");
	//reset form
	clearForm(tdList);
	//strip spaces
	$("userName").value = regXtrim($("userName").value);
	if($("userName").value.length < 1){
		tagError("userName","Enter your name");
	}
	if(!checkMailSyntax($("email").value)){
		tagError("email","The Email address appears to be invalid.");
	}

	$("ContatCompany").value = regXtrim($("ContatCompany").value);
	if($("ContatCompany").value.length < 1){
		tagError("ContatCompany","Please enter your company name.");
	}
		
	if($("country").options[$("country").selectedIndex].value == "United States" && $("state").options[$("state").selectedIndex].value<1)
	{
		tagError("state","Select a state or province");
	}
	if($("country").options[$("country").selectedIndex].value == "Canada" && $("state").options[$("state").selectedIndex].value<1)
	{
		tagError("state","Select a state or province");
	}
	if($("country").options[$("country").selectedIndex].value<1)
	{
		tagError("country","Select your country"); 
	}
	if(regXtrim($("theMessage").value)< 1){
		tagError("theMessage","Please enter your message.");
	}

	// return result
	if (goFlag){
		document.getElementById('contactusform').submit()
		//return true;
	}else{
		$("errFlag").innerHTML = msg;
		$("errFlag").style.display = "block";
		return false;
	}
}

function val_StartNewVCCourse() {
	goFlag = true;
	msg = "";
	if (regXtrim($("RCode").value) == "Enter your 14-digit code" || regXtrim($("RCode").value) == "") {
		//tagError("classID","Enter a class ID");
		goFlag = false;
		msg = "<span class='error'>Enter a Registration Code</span>";
	}
	if (!goFlag) {
		document.getElementById('response').innerHTML = msg;
		document.getElementById('response').style.display = "";
		//$("errFlag").innerHTML = msg;
		//$("errFlag").style.display = "block";
		return false;
	}
	else {
		//document.getElementById('form_StartNewVCCourse').submit();

		jQuery.ajax({
			type: "POST",
			url: "/campus/catalog/registration/index.cfm",
			data: {RCode: jQuery("#RCode").val()},
			success:function(data) {
				if (data.toLowerCase().indexOf("success") > 0) {
					location.href='/gateway/index.cfm?fa=mytraining.myVCTraining&courseAdded=true';
				}
				else {
					document.getElementById('response').innerHTML = data;
					document.getElementById('response').style.display = "";
				}
			}
		});
		return false;
	}
}

function val_myTrainingContact(theForm){
	//strip spaces
	goFlag = true;
	theForm.Name.value = regXtrim(theForm.Name.value);
	if(theForm.Name.value.length < 1){
		tagError("Name","Enter your name");
	}
	if(!checkMailSyntax(theForm.Email.value)){
		tagError("Email","The Email address appears to be invalid.");
	}

	theForm.Company.value = regXtrim(theForm.Company.value);
	if(theForm.Company.value.length < 1){
		tagError("Company","Please enter your company name.");
	}
		
	if(theForm.country.options[theForm.country.selectedIndex].value == "United States" && theForm.state.options[theForm.state.selectedIndex].value<1)
	{
		tagError("state","Select a state or province");
	}
	if(theForm.country.options[theForm.country.selectedIndex].value == "Canada" && theForm.state.options[theForm.state.selectedIndex].value < 1)
	{
		tagError("state","Select a state or province");
	}
	if(theForm.country.options[theForm.country.selectedIndex].value < 1)
	{
		tagError("country","Select your country"); 
	}
	if (theForm.Subject.value.length < 1) {
		tagError("Subject", "Please enter Subject");
	}
	if(regXtrim(theForm.theMessage.value) < 1){
		tagError("theMessage","Please enter your message.");
	}

	// return result
	if (goFlag) {
		document.getElementById('myTrainingContact').submit();
		//return true;
	}else{
		$("errFlag").innerHTML = msg;
		$("errFlag").style.display = "block";
		return false;
	}
}

function val_bug1() {
	goFlag = true;
	var theTable = $("reportTable");
	var tdList = theTable.getElementsByTagName("td");
	//reset form
	clearForm(tdList);

	//strip spaces
	$("reportQ").value = regXtrim($("reportQ").value);
	if($("reportQ").value.length < 1){
		goFlag = false;
		tdList.item(0).className="error";
	}else{
		// update the query - notice the ID is different so it does not clash with the search form
		$("eQuery2").value = $("reportQ").value;
	}

	// return result
	if(goFlag) {
		return true;
	}else{
		return false;
	}	
}

function validate(){
	goFlag = true;
	var tdList = document.getElementsByTagName("td");
	clearForm(tdList);

	// check the summary textarea
	if(regXtrim($("Textarea1").value)< 1){
		goFlag = false;
		$("errFlag").style.display = "block";
		tdList.item(2).className="error";
	}

	// check the steps to repro textarea
	if(regXtrim($("Textarea2").value)< 1){
		goFlag = false;
		$("errFlag").style.display = "block";
		tdList.item(3).className="error";
	}

	// return result
	if(goFlag){
		return true;
	}else{
		return false;
	}	
}

function val_myILT(){
	goFlag = true;
	msg = "";
	if(regXtrim($("classID").value) == ""){
		//tagError("classID","Enter a class ID");
		goFlag = false;
		msg = "Enter a class ID";
	}
	if (isNaN($("classID").value)) {
		//tagError("classID", "Class ID should be numeric");
		goFlag = false;
		msg = "Class ID should be numeric";
	}
	if (!goFlag) {
		$("errFlag").innerHTML = msg;
		$("errFlag").style.display = "block";
		return false;
	}
	document.getElementById('formILTClassInfo').submit();
}

function val_subscriptLogin(){
	var theTable = $("subscriptionTable");
	var tdList = theTable.getElementsByTagName("td");
	clearForm(tdList);

	if(regXtrim($("sCode").value) == ""){
		tagError("sCode","Enter your master code to continue.");
	}
	if(goFlag){
		return true;
	}else{
		$("errFlag").innerHTML = msg;
		$("errFlag").style.display = "block";
		return false;
	}	
}

function val_courseAccess(){
	var theTable = $("accessTable");
	var tdList = theTable.getElementsByTagName("td");
	clearForm(tdList);

	if(regXtrim($("rCode").value) == ""){
		tagError("rCode","Enter your access code to continue.");
	}
	if(goFlag){
		return true;
	}else{
		$("errFlag").innerHTML = msg;
		$("errFlag").style.display = "block";
		return false;
	}	
}


function val_faqsubmit(){
	goFlag = true;
	msg = "";
	var theTable = $("contact");
	var tdList = theTable.getElementsByTagName("td");
	//reset form
	clearForm(tdList);

	if (document.reportmaker.userName.value.length == 0) {
		tagError("userName","Please fill in all required fields.");
	}

	if (document.reportmaker.myEmail.value.length == 0) {
		tagError("myEmail","Please fill in all required fields.");
	}

	if($("myEmail")){
		if(!checkMailSyntax($("myEmail").value)){
			tagError("myEmail","The Email address appears to be invalid.");
			
		}
	}

	mytrialsoftware = -1;
  	for (i=0; i<document.reportmaker.trialsoftware.length; i++) {
   		if (document.reportmaker.trialsoftware[i].checked) {
      		mytrialsoftware = 1;
    	}
  	}

	if (document.reportmaker.ExerciseName.value.length == 0) {
		tagError("ExerciseName","Please fill in all required fields.");
	}

	if (document.reportmaker.step.value == "--") {
		tagError("step","Please fill in all required fields.");
	}

	if (document.reportmaker.dataPath.value.length == 0) {
		tagError("dataPath","Please fill in all required fields.");
	}

	if (document.reportmaker.os.value == "--") {
		tagError("os","Please fill in all required fields.");
	}

	if (document.reportmaker.PID.value == "--") {
		tagError("PID","Please fill in all required fields.");
	}

	if (document.reportmaker.PVNAME.value == "--") {
		tagError("PVNAME","Please fill in all required fields.");
	}

	if (mytrialsoftware == -1) {
		tagError("trialsoftware","Please select Yes or No regarding trial software.");
  	}

	if (document.reportmaker.theMessage.value.length == 0) {
		tagError("theMessage","Please fill in all required fields.");
	}

	if (goFlag) {
		return true;
	} else {
		$("errFlag").innerHTML = msg;
		$("errFlag").style.display = "block";
		//goFlag = true;
		return false;
	}
}

function val_ltsReminder(){
	goFlag = true;
	var theTable = $("ltsTable");
	var tdList = theTable.getElementsByTagName("td");
	clearForm(tdList);
	if(!checkMailSyntax($("email").value)){
		tagError("email","The Email address appears to be invalid.");
	}
	if(!checkMailSyntax($("confirmEmail").value)){
		tagError("confirmEmail","The Email address appears to be invalid.");
	}
	if($("email").value != $("confirmEmail").value){
		tagError("email","The email addresses are different.");
	}
	if(goFlag) {
		return true;
	}else{
		$("errFlag").innerHTML = msg;
		$("errFlag").style.display = "block";
		return false;
	}	
}
	
function submitNext(answer){
	if(answer == "yes"){
		document.getElementById("command").value="next >";
	}else{
		document.getElementById("command").value="finish";
	}
}

function clearForm(collection){
	goFlag= true;
	for(var i= 0; i< collection.length; i++){
		if(collection.item(i).className == "error"){
			collection.item(i).className = "";
			collection.item(i).removeAttribute("title");
		}
	}
}

function regXtrim(theText){
	// delete leading, then trailing, white-space
	trimmed = theText.replace(/^\s+/,'');
	trimmed = trimmed.replace(/\s+$/,'');
	return trimmed;
}

// returns true or false
function checkMailSyntax(mail){
	if (window.RegExp) { // error check using regular expressions
		var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
		var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,20}|[0-9]{1,3})(\]?)$/; // valid email format
		if (!reg1.test(mail) && reg2.test(mail)) { // if syntax is valid do nothing
			return true;
		}else{
			return false;				
		}
	}
}

function $() {
	var elements = new Array();

	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);

		if (arguments.length == 1)
			return element;

		elements.push(element);
	}
	return elements;
}

//	This function allows for the concatenation of the quantity of a course with the course ID information.
//	This is performed by creating a new option to each select element with the following values
//	Text = the quantity selected
//	Value = concatenated quanity and course ID information

function formatTextBoxes(temp) {
	goFlag = true;
	error = false;
	qError = false;
	var atleastonevalue = 0;
	//if no error process the form
	if(courseQuantityError > 0){
		//alert("There are too many errors: "+courseQuantityError)
		return false;
	}
	else{
		var selectedCourses = "";
		for(i=0; i<temp.elements.length; i++){
			//if it's a select element
			if(temp.elements[i].name == "myselectedCourses"){
				if (temp.elements[i].value!=0){
					//quantity they wish to sign up for
					tempValue = temp.elements[i].value;
					if (tempValue > 0) {
						atleastonevalue = 1
					}
					//course id information
					tempId = temp.elements[i].id;

					//new option value
					//temp.elements[i].value = tempValue+tempId;

					temp.selectedCourses.value = temp.selectedCourses.value + "|" + tempValue+tempId;
					//error checking
					//alert(temp.elements[i].value); 
				}
			}
		}
		document.getElementById('errFlag').style.display = "none";
		if (atleastonevalue ==1){
			temp.submit();
		}
		else{
			alert("You must specify the number of seats for the class you wish to register.");
			return false;
		}
	}
}


//Validates the quantities listed for attendance when signing up for classes through the course catalogue.
function verifySingleInput(temp) {
	goFlag = true;
	error = false;
	sizeError = false;

	//if it's a text input element
	if(temp.name == "myselectedCourses"){

		//trim spaces
		temp.value=temp.value.replace(/^\s+/g, '').replace(/\s+$/g, '');

		//trim any leading zero's
		while (temp.value.substr(0,1) == '0' && temp.value.length>1){
			temp.value = temp.value.substr(1,9999);
		}

		//if its not a number
		if(isNaN(temp.value)){
			//show the error flags
			showInputError(temp);
		}
		//if the value is blank
		else if (temp.value=="" || temp.value==null){
			//show the error flags
			showInputError(temp);
		}
		//if its a number greater than 99
		else if(temp.value > 20){
			//show the error flags
			showInputWarning(temp);
		}
		//if its acceptable
		else{
			//hide any error flags
			if(document.getElementById(temp.id).parentNode.className == "errFlag"){
				courseQuantityError--;
			}
			if(document.getElementById(temp.id).parentNode.className == "warningFlag"){
				if(courseSizeError >0){
					courseSizeError--;
				}
			}
			hideInputError(temp);
		}
	}
	if(courseQuantityError == 0 && courseSizeError == 0){
		document.getElementById('errorText').style.display = "none";
		document.getElementById('errFlag').style.display = "none";
	}
	return error;
}

function showInputError(inputField){
	if(document.getElementById(inputField.id).parentNode.className == "inputField"){
		//set the error
		courseQuantityError++;
	}
	if(document.getElementById(inputField.id).parentNode.className == "warningFlag"){
		courseSizeError--;
		courseQuantityError++;
	}
	document.getElementById(inputField.id).parentNode.className = "errFlag";
	document.getElementById(inputField.id).parentNode.title = "Please enter a number (0-20) for the quantity.";
	document.getElementById('errFlag').style.display = "block";
	document.getElementById('errorText').style.display = "inline";
}

function showInputWarning(inputField){
	if(document.getElementById(inputField.id).parentNode.className == "inputField"){
		//set the error
		courseSizeError++;
	}
	if(document.getElementById(inputField.id).parentNode.className == "errFlag"){
		courseQuantityError--;
		courseSizeError++;
	}
	document.getElementById(inputField.id).parentNode.className = "warningFlag";
	document.getElementById(inputField.id).parentNode.title = "Please enter a number (0-20) for the quantity.";
	document.getElementById('errFlag').style.display = "block";
	document.getElementById('errorText').style.display = "inline";
}

function hideInputError(inputField){
	document.getElementById(inputField.id).parentNode.className = "inputField";
	document.getElementById(inputField.id).parentNode.title = "";
}

