//===========Zen Library===============

//==== checkForm function that checks for blank and valid email in a form ====
//pre-condition: Must have an Email field with id='Email' for it to check Email properly
//Can use real world names for ids of other form fields, such as id="First Name"
//HTMLcontent can be used to ignore the HTML flag as an ID
//second parameter - IGNORE ARRAY
//A global variable used to contain input field IDs that are to be ignored.


function checkForm(formID,ignoreArray)
{
	
ignoreArray = ignoreArray.split(",");

	if(document.getElementById(formID))
	{
		theForm		= document.getElementById(formID);
		numofInputs	= theForm.length-2; //minus the submit button and key

//=======Cycle through each input=======
		for(i=0;i<numofInputs;i++)
		{

		ignoreVal	= false;
//======Cycle through ignore list=======
		for(ig=0;ig<ignoreArray.length;ig++)
		{
			if(theForm[i].id == ignoreArray[ig])
			{
			ignoreVal = true;
			break;
			}
		}


			if(theForm[i].value == "" && !ignoreVal)
			{
			alert("Please specify a value for: " + theForm[i].id);
			theForm[i].focus();
			return false;
			}
			else if(theForm[i].value != "")
			{

/* CMS SPECIFIC FIELDS, THIS STUFF CAN BE COMMENTED OUT IF NOT BEING USED */

//==========PICTURE VALIDATION============
/*
if(theForm[i].id == "picture1" || theForm[i].id == "picture2" || theForm[i].id == "picture3" || theForm[i].id == "picture4" || theForm[i].id == "picture5")
			{

			picInputValid = 0;

			checkPic = checkMIME(".jpg",theForm[i].value);
				if(checkPic)
				picInputValid =1;
			/*
			checkPic = checkMIME(".png",theForm[i].value);
				if(checkPic)
				picInputValid =1;			
			checkPic = checkMIME(".gif",theForm[i].value);
				if(checkPic)
				picInputValid =1;			
			*/
/*
				if(picInputValid == 0)
				{
				alert("Only jpg is an acceptable image format.");
				theForm[i].focus();
				return false;		
				}
			}	
 */
//==========HTML CONTENT IS A RESERVED ID FOR A TEXT AREA THAT ALLOW HTML CONTENT============			
		
		if(theForm[i].id == "Comments")
				{
				var HTMLtemp = NoHTML(theForm[i].value);

				if(!HTMLtemp)
				{
				alert("<,>, and ~ are invalid characters.");
				theForm[i].focus();
				return false;					
				}
				}
		

//==========CHECK FOR numeric============
/*
			if(theForm[i].id == "Phone")
			{
				testNumeric = true;
				testNumeric = isNumeric(theForm[i].value);
				
				if(!testNumeric)
				{
				alert("Please enter phone numbers as: 7021112222");
				theForm[i].focus();
				return false;
				}
			}
*/
//==========CHECK FOR numeric============
/*
			if(theForm[i].id == "Phone")
			{
				testPhoneLen = theForm[i].value;
				
				if(testPhoneLen.length != 10)
				{
				alert("Please enter phone numbers as: 7021112222");
				theForm[i].focus();
				return false;
				}
			}
*/
//===========CHECK FOR VALID EMAIL=============			
			if(theForm[i].id == "Email")
			{
				var isEmailTemp = IsEmail(theForm[i].value);
				if(!isEmailTemp)
				{
				alert("Please enter a valid email.");
				theForm[i].focus();
				return false;					
				}		
			}

		if(theForm[i].id == "Email 1")
			{
				var isEmailTemp = IsEmail(theForm[i].value);
				if(!isEmailTemp)
				{
				alert("Please enter a valid email.");
				theForm[i].focus();
				return false;					
				}		
			}

		if(theForm[i].id == "Email 2")
			{
				var isEmailTemp = IsEmail(theForm[i].value);
				if(!isEmailTemp)
				{
				alert("Please enter a valid email.");
				theForm[i].focus();
				return false;					
				}		
			}
			
		if(theForm[i].id == "Email 3")
			{
				var isEmailTemp = IsEmail(theForm[i].value);
				if(!isEmailTemp)
				{
				alert("Please enter a valid email.");
				theForm[i].focus();
				return false;					
				}		
			}
			
		if(theForm[i].id == "Email 4")
			{
				var isEmailTemp = IsEmail(theForm[i].value);
				if(!isEmailTemp)
				{
				alert("Please enter a valid email.");
				theForm[i].focus();
				return false;					
				}		
			}
			
		if(theForm[i].id == "Email 5")
			{
				var isEmailTemp = IsEmail(theForm[i].value);
				if(!isEmailTemp)
				{
				alert("Please enter a valid email.");
				theForm[i].focus();
				return false;					
				}		
			}			

//===========CHECK FOR Password validation=============			
//pre-condition: needs password and password2 as IDs for this portion to work
			if(theForm[i].id == "password2")
			{
			checkPw = verifyPasswords();
			
				if(!checkPw)			
				{
				alert("Your passwords do not match.");
				theForm[i].focus();
				return false;						
				}
			}
			
			
		if(formID == 'setupForm')
		{
			if(theForm[i].id == "Location Name")
			{
				checkApos	= NoApos(theForm[i].value);
											 
				if(!checkApos)
				{
				alert("You cannot have apostrophes or quotes in your Location name.");
				theForm[i].focus();
				return false;
				}
				
			}
		}	
		
		if(formID =='signupForm')
		{
			if(theForm[i].id == "Username" || theForm[i].id == "password")
			{
				checkApos	= NoApos(theForm[i].value);
											 
				if(!checkApos)
				{
				alert("You cannot have apostrophes or quotes in your username or password.");
				theForm[i].focus();
				return false;
				}
				
			}			
		}
			
			}	//end else
		} //end for	
		
		/*
		if(formID == 'signupForm')
		{
		var checkCnt = false;
		if(document.getElementById('videoconferencing') && document.getElementById('videoconferencing').checked == true)
		checkCnt = true;
		
		if(document.getElementById('securelivemotion') && document.getElementById('securelivemotion').checked == true)
		checkCnt = true;		
		
		if(!checkCnt)
		{
			alert("You must choose at least 1 product");
			return false;
			document.getElementById('securelivemotion').focus();
		}
		}
		*/
		
		document.getElementById('key').value = getURL();
		return true;
	}
	else
	alert("No Form Specified with that ID.");
}

//==== Password Verify Function ====
//Pre-condition: password and password2 must be the Name and IDs of the password fields
//Post-condtion: returns boolean 
function verifyPasswords()
{

pass1	= document.getElementById('password').value;
pass2	= document.getElementById('password2').value;

if(pass1 != pass2)
return false;
else
return true;


}

//==== Email Check function ====

function IsEmail(sText)
{
var whereisAT = sText.indexOf('@');

if(whereisAT == -1)
return false;
   
var whereisDOT = sText.indexOf('.');  

if(whereisDOT == -1)
return false;

//if makes through checks, return true     
return true;
}

//==== No Apostrophes =======

function NoApos(sText)
{
var whereisAT = sText.indexOf('\'');

if(whereisAT != -1)
return false;

var whereisAT = sText.indexOf('\"');

if(whereisAT != -1)
return false;


return true;
}

//==== HTML Check function ====
//checks to make sure no HTML tags are in the form

function NoHTML(sText)
{
var whereisAT = sText.indexOf('>');

if(whereisAT != -1)
return false;
   
var whereisDOT = sText.indexOf('<');  

if(whereisDOT != -1)
return false;

var whereisDOT = sText.indexOf('~');  

if(whereisDOT != -1)
return false;

//SPECIFICALLY FOR signup page
//in contactform_ext.js
temp = checkProductBoxes;

if(!temp)
{
alert("You must choose at least 1 product.");
return false;
}

//if makes through checks, return true     
return true;

}


//==== getURL function ====
//returns the URL in a hidden field to make sure people are not injecting our mail form
//by checking where the form is coming from
//pre-condition: requires a field with an id='key'
function getURL()
{
test = location.href;

test= test.split("/");
str = "";
for(i=0;i<(test.length-1);i++)
{
	if(i == 0)
	str = test[i]+"/";
	else
	str = str + test[i] + "/";
}
return str;
}


//FOR DELETING

function verifyDeleteItem(theItem)
{
for (i=0;i<document.getElementById(theItem).length;i++)
   {
     var current = document.getElementById(theItem).options[i];
		
		if(current.selected == true)
		groupname = document.getElementById(theItem).options[i].text;	
   }
var answer = confirm ("Are you sure you want to delete the item: " + groupname + "?")	

if(answer)
return true;
else
return false;	
}

//============FOR NUMBER VERIFICATION=============
function isNumeric(theVal)
{
pattern = /^\d+$/;
if(pattern.test(theVal) ==false)
return false;
else
return true;
}


//For MIME validation
//Pre-condition: Extension is a string such as ".gif" ".jpg", inputvalue is the value to be checked against
//Post returns whether the value is of an acceptable MIME type
function checkMIME(extension,inputValue)
{

	inputValue = inputValue.toLowerCase();	
	var getLength =  inputValue.length;
	//extension check
	var check = inputValue.substring(getLength-4,getLength)

//jpeg hack
if(check == ".jpeg")
check = ".jpg";

	if(check != extension)
	return false;
	else
	return true;
} //end checkMIME


function focusOn(theID)
{
document.getElementById(theID).focus();	
}