var submit2flag;
var submit3flag;
var submit4flag;
var submit5flag;

var VALIDATEPROGRAMERROR; // determines whether or not to validate the program error value

function highlighterrobj(item)
{
	item.setAttribute("class","inputerror");
	item.className='inputerror';
	var errhandler = 'err' + item.name;

	if ( document.getElementById(errhandler) ) {
	  document.getElementById(errhandler).style.display = 'block';
	}
}

function unhighlighterrobj(item)
{
	if (item) {

		item.setAttribute("class","formtext")
		item.className='formtext';
		var errhandler = 'err' + item.name;

		if ( document.getElementById(errhandler) ) {
		  document.getElementById(errhandler).style.display = 'none';
		}

	}
	else {
		writeLayer("errmsg", '');
	}
}

/**********************************************************************************************

function name: toggleotherrequest(item)
	checks whether the other radio option is selected, if it is it dynamically shows the other
	textbox, if the other radio option is not selected it ensures that the other dialog is
	not present

**********************************************************************************************/
function toggleotherrequest(item)
{
	if ( item.value == '4' ) {
		showotherrequest();
	} else {
		hideotherrequest();
	}
}

/**********************************************************************************************

function name: hideotherrequest()
	Hides other request text box

**********************************************************************************************/
function hideotherrequest()
{
	if ( document.getElementById('otherblock') ) {
	  document.getElementById('otherblock').style.display = 'none';
	}
}

/**********************************************************************************************

function name: showotherrequest()
	Shows other request text box

**********************************************************************************************/
function showotherrequest()
{
	if ( document.getElementById('otherblock') ) {
  	document.getElementById('otherblock').style.display = 'block';
	}
}
/**********************************************************************************************

function name: othersupportreq(item)
	checks whether the other radio option is selected, if it is it dynamically shows the other
	textbox, if it is Nothingthe other radio option select it ensures that the other dialog is
	not present

**********************************************************************************************/
function othersupportreq(item)
{
	if ( item.value == "4" ) {
		//writeLayer('otherblock', '<span class="supportreqinput">Please specify a catagory:<br /><input type="text" name="requestother"></span>')
		return true;
	} else {
		writeLayer('otherblock', '');
		return true;
	}
}

/**********************************************************************************************

function name: othermodule(item)
	same actions as othersupportreq

**********************************************************************************************/
function othermodule(item)
{
	if ( item.checked == true ) {
		document.getElementById('otherblock').style.display = 'block';
		//writeLayer('otherblock', '<span class="supportreqinput">Please specify which module(s):<br /><input type="text" name="affectedmoduleother"></span>')
		return true;
	} else {
		document.getElementById('otherblock').style.display = 'none';
		//writeLayer('otherblock', '');
		return true;
	}
}

/**********************************************************************************************

function name: showprogramblock()
	same actions as othersupportreq, except it only shows the programerror box

**********************************************************************************************/
function showprogramblock() {
	document.getElementById('programerrorblock').style.display = 'block';
	VALIDATEPROGRAMERROR = true;
	//writeLayer('programerrorblock', 'Please document the error message displayed<br /><textarea name="programerror" class="formtextarea" rows="5" cols="50"></textarea>');
}

/**********************************************************************************************

function name: hideprogramblock(item)
	hides the program error block if another option is selected

**********************************************************************************************/
function hideprogramblock() {
	document.getElementById('programerrorblock').style.display = 'none';

	VALIDATEPROGRAMERROR = false;
	//writeLayer('programerrorblock', '');
}

/**********************************************************************************************

function name: valerrorreturned(item)
	checks if form has been submitted, and checks if it should validate the error returned
	textarea

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valerrorreturned( item )
{
	if ( submit2flag == true && VALIDATEPROGRAMERROR == true ) {

		interror = false;

		if (!valnull(item)) {
			error('Please enter the error message returned as a result of this issue.', item);
    	interror = true;
		}

 		if ( interror == true ) {
 			highlighterrobj(item);
			errorflag = true;
			return false;
		} else {
			unhighlighterrobj(item);
			return true;
		}
	} else {
		unhighlighterrobj(item);
		return true;
	}
}

/**********************************************************************************************

function name: valfullname(item)
	checks that full name is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valfullname( item )
{
	interror = false;

	if (!valnull(item)) {
		error('Your name (First and Last) is required in the "Full Name" field.', item);
    interror = true;
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valcompany(item)
	checks that company is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valcompany( item )
{
	interror = false;

	if (!valnull(item)) {
		error('Your "Company" name is required.', item);
    interror = true;
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valissuedescription(item)
	checks that the issue description is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valissuedescription( item )
{
	interror = false;

	if (!valnull(item)) {
		error('Please enter a detailed Description of the issue being reported.', item);
    interror = true;
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valprerequisites(item)
	checks that prerequisites is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valprerequisites(item)
{
	var interror = false;

	if (!valnull(item)) {
		error('Please enter all elements required to recreate the issue.', item);
    interror = true;
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valdesiredresults(item)
	checks that desired results is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valdesiredresults(item)
{
	interror = false;

	if (!valnull(item)) {
		error('Please enter the desired results you would expect to receive if this issue did not exist.', item);
    interror = true;
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valrecreate(item)
	checks that the steps to recreate field is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valrecreate(item)
{
	interror = false;

	if (!valnull(item)) {
		error('Please detail the steps taken to generate the issue.', item);
    interror = true;
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valrecreated(item)
	checks that a radio button for the has this issue been recreated on an unmodified database has
	been selected

	if it finds an error, output an error message, otherwise clear the error message field

**********************************************************************************************/
function valrecreated()
{

	var requestselected = false;

	for (i=0;i<document.forms[0].elements.length;i++) {

			var formobj = document.forms[0].elements[i];
			if ( formobj.name == 'recreated' && formobj.checked == true ) {
				  requestselected = true;
  		}
	}

	if ( requestselected == true ) {
		var errhandler = 'errrecreated';

		if ( document.getElementById(errhandler) ) {
		  document.getElementById(errhandler).style.display = 'none';
		}
	} else {
		errorbylayer('Please specify if you have recreated the issue on an unmodified <%= PartnerShort %> Database.', 'recreated');

		var errhandler = 'errrecreated';

		if ( document.getElementById(errhandler) ) {
		  document.getElementById(errhandler).style.display = 'block';
		}

		errorflag = true;
		return false;
	}

}

/**********************************************************************************************

function name: valrecreated(item)
	checks that a radio button for the select request type has
	been selected

	if it finds an error, output an error message, otherwise clear the error message field

**********************************************************************************************/
function checkreq(fromsubmit) {

	var requestselected = false;

	for (i=0;i<document.forms[0].elements.length;i++) {

			var formobj = document.forms[0].elements[i];
			if ( formobj.checked == true ) {
				  requestselected = true;
  		}
	}

	if ( requestselected == true ) {
		errorbylayer('', 'requestmsg');
	} else if ( fromsubmit != '' ) {
		errorbylayer('Please select what type of request you wish to make.', 'requestmsg');
		return false;
	}

}

/**********************************************************************************************

function name: valincorrectresults(item)
	checks that the incorrect results is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valincorrectresults(item) {
	interror = false;

	if (!valnull(item)) {
		error('Please enter the incorrect results this issue produced.', item);
    interror = true;
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valphone(item)
	checks that the phone number field is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valphone(item)
{

	interror = false;

	if (!valnull(item)) {
		error('The "Contact Phone Number" is required in order to complete your request.', item);
    interror = true;
	}

	if ( !valminlen(item, 10) ) {
		error('The "Contact Phone Number" must be at least 10 digits.', item);
    interror = true;
	}

	if ( !valmaxlen(item, 20) ) {
	  // display error
	  error('The "Contact Phone Number" must not exceed 20 digits.', item);
		interror = true;
	}

	if ( !valproperphone(item) ) {
		error('The "Contact Phone Number" must be numeric.', item);
		interror = true;
	}

	if ( !valint(trim(item.value)) ) {
	  // display error
	  error('The "Contact Phone Number" must be numeric.', item);
		interror = true;
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valfax(item)
	checks that the steps to recreate field is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valfax(item)
{

	if ( submit1flag == true && item.value != '' ) {

		interror = false;

		if (!valnull(item)) {
			error('A "Contact Fax Number" is required in order to complete your request.', item);
    	interror = true;
		}

		if ( !valminlen(item, 10) ) {
			error('The "Contact Fax Number" number must be at least 10 digits, ensure you have specified the area code.', item);
   	 interror = true;
		}

		if ( !valmaxlen(item, 20) ) {
		  // display error
		  error('The "Contact Fax Number" must not exceed 20 digits.', item);
 			interror = true;
 		}

	  if ( !valproperphone(item) ) {
		  error('The "Contact Fax Number" must be numeric.', item);
		  interror = true;
	  }

		if ( !valint(trim(item.value)) ) {
	 	 // display error
	 	 error('The "Contact Fax Number" must be numeric.', item);
			interror = true;
 		}

 		if ( interror == true ) {
 			highlighterrobj(item);
			errorflag = true;
			return false;
		} else {
			unhighlighterrobj(item);
			return true;
		}
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valcertnum(item)
	checks that the certification number is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valcertnum(item)
{

	var interror = false;

	if ( trim(item.value) != "" ) {
		if ( !valminlen(trim(item.value), 5) ) {
			error('The "Certification Number" must be at least 5 digits.', item);
			interror = true;
		}

		if ( !valmaxlen(trim(item.value), 7) ) {
			error('The "Certification Number" must not exceed 7 digits.', item);
			interror = true;
		}

		if ( !valpropercertnum(item) ) {
			error('The "Certification Number" must be numeric.', item);
			interror = true;
		}

		if ( !valint(trim(item.value)) ) {
			error('The "Certification Number" must be numeric.', item);
			interror = true;
		}
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valemail(item)
	checks that the email address is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valemail( item )
{
	var interror = false;

	if ( item.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1 ) {
		error('A valid e-mail must be entered.', item);
		interror = true;
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

/**********************************************************************************************

function name: valcountry(item)
	checks that the country is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valcountry(item)
{

interror = false;

if (!valnull(item)) {
	error('Please select the country.', item);
  interror = true;
}

if ( interror == true ) {
	highlighterrobj(item);
	errorflag = true;
	return false;
} else {
	unhighlighterrobj(item);
	return true;
}

}

/**********************************************************************************************

function name: valdatabaseversion(item)
	checks that a database version is selected

	if it finds an error, output error response, otherwise clear error object

**********************************************************************************************/
function valdatabaseversion(item)
{

interror = false;

if (!valnull(item)) {
	error('Please select the Database version.', item);
  interror = true;
}

if ( interror == true ) {
	highlighterrobj(item);
	errorflag = true;
	return false;
} else {
	unhighlighterrobj(item);
	return true;
}

}

/**********************************************************************************************

function name: valexecutableversion(item)
	checks that a executable version is selected

	if it finds an error, output error response, otherwise clear error object

**********************************************************************************************/
function valexecutableversion(item)
{

  interror = false;

  if (!valnull(item)) {
	  error('Please select the Executable version.', item);
    interror = true;
  }

  if ( interror == true ) {
	  highlighterrobj(item);
	  errorflag = true;
	  return false;
  } else {
	  unhighlighterrobj(item);
	  return true;
  }

}

/**********************************************************************************************

function name: valpropercertnum(item)
	checks that the certification number doesnt contain illegal characthers

	if it finds an error, returns false

**********************************************************************************************/
function valpropercertnum( item ) {

   if (!item) {
	 	 return false;
	 }

	 var invalidchars = " ~`!@#$%^&*()_-=+[]{}\|;:'\"<,>.?/ ";

   for (var i = 0; i < item.value.length; i++) {
     if (invalidchars.indexOf(item.value.charAt(i)) != -1) {
       return false;
		 }
   }

   return true;
}

/**********************************************************************************************

function name: valproperphone(item)
	checks that the phone number doesnt contain illegal characthers

	if it finds an error, returns false

**********************************************************************************************/
function valproperphone( item ) {

   if (!item) {
	 	 return false;
	 }

   var invalidchars = "~`!@$%^&*_=+{}\|;:'\"<,>?/";

   for (var i = 0; i < item.value.length; i++) {
     if (invalidchars.indexOf(item.value.charAt(i)) != -1) {
       return false;
		 }
   }

   return true;
}

/**********************************************************************************************

function name: checkmodule(fromsubmit)
	checks that an option is selected for the area within hr

	if it finds an error, outputs error, otherwise it clears the error object
**********************************************************************************************/
function checkmodule(fromsubmit) {

	var requestselected = false;

	for (i=0;i<document.forms[0].elements.length;i++) {

			var formobj = document.forms[0].elements[i];
			if ( formobj.name == 'affectedmodule' && formobj.checked == true ) {
				  requestselected = true;
  		}
	}

	if ( requestselected == true ) {
		errorbylayer('', 'affectedmodule');
		if ( document.getElementById('erraffectedmodule') ) {
			document.getElementById('erraffectedmodule').style.display = 'none';
		}
	} else if ( fromsubmit != '' ) {
		if ( document.getElementById('erraffectedmodule') ) {
			document.getElementById('erraffectedmodule').style.display = 'block';
		}

		errorbylayer('Please select the area(s) with the issue.', 'affectedmodule');
		errorflag = true;
		return false;
	}

}

var submit1flag; // declared when the user tries to submit the form to validate any optional fields

var submit1flag = false;

/**********************************************************************************************

function name: validateform()
	cycles through contact information form elements and triggers each objects onblur event,
	cancels form submission if an error is found

	if it finds an error, shows alert, returns false

**********************************************************************************************/
function validateform()
{
	submit1flag = true;
	errorflag = false;

  for (i=0;i<document.forms[0].elements.length;i++) {

	  var formobj = document.forms[0].elements[i];

	  if ( formobj.onblur ) {
		  formobj.onblur();
	  }
	}

	// because error flag will return true if an error was present, in order for the form to cancel the submit send the opposite of errorflag
	if ( errorflag == true ) {
		alert('Please review your entries.  Not all fields are valid.');
	}
	return !errorflag;

}

/**********************************************************************************************

function name: validateform_support()
	cycles through support request form elements and triggers each objects onblur event,
	cancels form submission if an error is found

	if it finds an error, returns false

**********************************************************************************************/
function validateform_support()
{
	submit2flag = true;
	errorflag = false;

	var requestselected = false;

	for (i=0;i<document.forms[0].elements.length;i++) {

	  var formobj = document.forms[0].elements[i];

		if ( formobj.onblur ) {
		  formobj.onblur();
	  }

		if ( formobj.onchange ) {
			formobj.onchange();
		}
	}

	checkmodule('1');
	valrecreated();

	if ( errorflag == true ) {
		alert('Please review your entries.  Not all fields are valid.');
	}
	// because error flag will return true if an error was present, in order for the form to cancel the submit send the opposite of errorflag
	return !errorflag;
}

function validateform_genericsupport()
{
	submit2flag = true;
	errorflag = false;

	var requestselected = false;

	for (i=0;i<document.forms[0].elements.length;i++) {

	  var formobj = document.forms[0].elements[i];

		if ( formobj.onblur ) {
		  formobj.onblur();
	  }

		if ( formobj.onchange ) {
			formobj.onchange();
		}
	}

	if ( errorflag == true ) {
		alert('Please review your entries.  Not all fields are valid.');
	}
	// because error flag will return true if an error was present, in order for the form to cancel the submit send the opposite of errorflag
	return !errorflag;
}

/**********************************************************************************************

function name: valenhancementclassification(fromsubmit)
	checks that the enhancement classification has been filled out

	if it finds an error and the fromsubmit is set, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valenhancementclassification(fromsubmit) {

	var requestselected = false;

	for (i=0;i<document.forms[0].elements.length;i++) {

			var formobj = document.forms[0].elements[i];
			if ( formobj.name == 'enhancementclassification' && formobj.checked == true ) {
				  requestselected = true;
  		}
	}

	if ( requestselected == true ) {
		errorbylayer('', 'enhancementclassification');
		if ( document.getElementById('errenhancementclassification') ) {
			document.getElementById('errenhancementclassification').style.display = 'none';
		}
	} else if ( fromsubmit != '' ) {
		errorbylayer('Please specify a classification for your enhancement request.', 'enhancementclassification');
		errorflag = true;
		return false;
	}

}

/**********************************************************************************************

function name: valenhancementdescription(item)
	checks that the enhancement description is valid

	if it finds an error, highlight the object, otherwise unhighlight the object

**********************************************************************************************/
function valenhancementdescription( item )
{
	interror = false;

	if (!valnull(item)) {
		error('Please enter a detailed description of the enhancement being requested.', item);
    interror = true;
	}

	if ( interror == true ) {
		highlighterrobj(item);
		errorflag = true;
		return false;
	} else {
		unhighlighterrobj(item);
		return true;
	}

}

function validateform_enhancement()
{
	submit5flag = true;
	errorflag = false;

	var requestselected = false;

	for (i=0;i<document.forms[0].elements.length;i++) {

	  var formobj = document.forms[0].elements[i];

		if ( formobj.onblur ) {
		  formobj.onblur();
	  }

		if ( formobj.onchange ) {
			formobj.onchange();
		}
	}

	valenhancementclassification('1');
	checkmodule('1');

	if ( errorflag == true ) {
		alert('Please review your entries.  Not all fields are valid.');
	}
	// because error flag will return true if an error was present, in order for the form to cancel the submit send the opposite of errorflag
	return !errorflag;
}

/***************************************************************************************************

REQUEST FORM MENU HELP FUNCTIONS

***************************************************************************************************/
var helpmsgfullname = 'Please enter your full name (First and Last).';
var helpmsgphone = 'Please enter a phone number including the area code, at which our Support Technician(s) can contact you.';
var helpmsgfax = 'Please enter a fax number to which documents may be sent to you regarding the Issue reported.';
var helpmsgcertificationnumber = 'For verification purposes, please enter your <%= PartnerShort %> Certification Number.';
var helpmsgcompany = 'Please enter your Company name.';
var helpmsgemail = 'Please enter a valid e-mail address at which you can be contacted.';
var helpmsgremember = 'Click if you would like to have your contact information saved.';
var helpmsgrememberradio = 'Click if you would like to have your contact information saved.';
var helpmsgforgetradio = 'Click if you would like to have your saved contact information discarded.';
var helpmsgrequesttype = 'Please indicate what type of request you would like to make.';
var helpmsgrequestnext = 'Once you have made your selection, please click the Next button to proceed.';
var helpmsgnext = 'Once you have entered in your contact information, please click the Next button to proceed.';
var helpmsgissuedescription = 'Please enter a detailed Description of the issue you are reporting.';
var helpmsgdisplayerror = 'Please specify whether or not an error message was displayed.';
var helpmsgaffectedmodule = 'Please select the area(s) associated with the reported issue.';
var helpmsgcountry = 'Please select the country.';
var helpmsgdatabaseversion = 'Please select your current database version.';
var helpmsgexecutableversion = 'Please select your current executable version.';
var helpmsgrecreated = 'Please specify if you have recreated the issue on an unmodified <%= PartnerShort %> Database.';
var helpmsglastimprovement = 'Enter the number of the last improvement imported to the database.';
var helpmsgsupportrequestfinish = 'Once you have entered in the details of your Support Request, please click the Finish button to process your request.';
var helpmsgissuerequestnext = 'Once you have completed the required fields, please click the Next button to continue.';
var helpmsgreasoning = 'If known, please enter the issue source.';
var helpmsgprerequisites = 'Please enter all elements required to recreate the issue.';
var helpmsgstepstorecreate = 'Please list in detail the steps needed to recreate the issue.';
var helpmsgincorrectresults = 'Please enter the incorrect results that were produced by this issue.';
var helpmsgdesiredresults = 'Please enter the desired result you would expect to receive if this issue did not exist.';
var helpmsgworkaround = 'Please enter a possible workaround solution you have come up with or proved to be successful.';
var helpmsgissuerequestfinish = 'When you have completed the details of your Issue, please click the Finish button to submit your Issue Report.';
var helpmsgprogramerror = 'Please document the error message displayed.';

var helpmsgenhancementaffectedmodule = 'Please select the area(s) associated with the enhancement.';
var helpmsgenhancementdescription = 'Please specify a description for your enhancement request.';
