// JavaScript Document

addNewAgency = function()
{
	document.location = 'index.php?Get=manageAgencies&action=new';
}

editAgency = function(id)
{
	document.location = 'index.php?Get=manageAgencies&action=edit&agencyId=' + id;
}

deleteAgency = function(id)
{
	document.location = 'index.php?Get=manageAgencies&action=delete&agencyId=' + id;
}

cancelActionAgency = function()
{
	document.location = 'index.php?Get=manageAgencies';
}

confirmedDeleteAgency = function(id)
{
	document.location = 'index.php?Get=manageAgencies&deleteAgency=Y&agencyId=' + id;
}

validateSurvey = function()
{
	var pleaseContinue = true;
	var errorString = '';
	if(document.getElementById('applicantname').value == '')
	{
		errorString += 'The Applicants Name is required.\n';
		document.getElementById('applicantname').style.border = '1px solid #CC0000';
		pleaseContinue = false;
	}
	if(document.getElementById('positionsought').value == '')
	{
		errorString += 'The applicants "Position Sought" is required.\n';
		document.getElementById('positionsought').style.border = '1px solid #CC0000';
		pleaseContinue = false;
	}
	
	for(i=1;i<22;i++)
	{
		if(document.getElementById('q' + i).value == '')
		{
			errorString += 'Question ' + i + ' is required.\n';
			document.getElementById('q' + i).style.border = '1px solid #CC0000';
			pleaseContinue = false;
		}
	}
	
	if(document.getElementById('refname').value == '')
	{
		errorString += 'Your name is required.\n';
		document.getElementById('refname').style.border = '1px solid #CC0000';
		pleaseContinue = false;
	}
	if(document.getElementById('refemail').value == '' && document.getElementById('refphone').value == '')
	{
		errorString += 'Either an email or phone number is required.\n';
		document.getElementById('refemail').style.border = '1px solid #CC0000'
		document.getElementById('refphone').style.border = '1px solid #CC0000';;
		pleaseContinue = false;
	}
	
	if(!pleaseContinue)
	{
		alert(errorString);
		return;
	}
	
	document.forms.refSurveyForm.submit();
}

getHTTPObject = function()
{
	if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	else {
		alert("Your browser does not support AJAX.");
		return null;
	}
}

setOutput = function()
{
	if(httpObject.readyState == 4)
	{
		if(httpObject.responseText == 'true')
		{
			validateSurvey();
		}
		else
		{
			alert('The security code you entered does not match the image. Please try again.');
			document.getElementById('security_code').style.border = '1px solid #CC0000';
			return false;
		}
	}
}

testCaptcha = function()
{
	var captcha = document.getElementById('security_code').value;
	if(captcha == '')
	{
		alert('You must fill in the security code.\n');
		document.getElementById('security_code').style.border = '1px solid #CC0000';
		return false;
	}
	else
	{
		httpObject = getHTTPObject();
		if (httpObject != null)
		{
			httpObject.open("GET", "securimage/verify.php?securitycode=" + captcha, true);
			httpObject.send(null);
			httpObject.onreadystatechange = setOutput;
		}		
	}
}

checkPasswords = function()
{
	var first = document.getElementById('password').value;
	var second = document.getElementById('verifypassword').value;
	var uname = document.getElementById('username').value;
	if(first.length > 0 && second.length > 0 && uname.length > 0)
	{
		if(first == second)
		{
			document.forms.changeLogin.submit();
		}
		else
		{
			alert('The passwords do not match. Please try again.');
		}
	}
	else
	{
		alert('You must fill in all fields to complete this process.');
	}
}

checkPreferredPasswords = function(formName)
{
	var first = document.getElementById('password' + formName).value;
	var second = document.getElementById('verifypassword' + formName).value;
	//var uname = document.getElementById('username' + formName).value;
	if(first.length > 0 && second.length > 0/* && uname.length > 0*/)
	{
		if(first == second)
		{
			document.forms['changeLogin' + formName].submit();
		}
		else
		{
			alert('The passwords do not match. Please try again.');
		}
	}
	else
	{
		alert('You must fill in all fields to complete this process.');
	}
}

checkNewPreferredPasswords = function()
{
	var first = document.getElementById('newpassword').value;
	var second = document.getElementById('newverifypassword').value;
	var uname = document.getElementById('newusername').value;
	if(first.length > 0 && second.length > 0 && uname.length > 0)
	{
		if(first == second)
		{
			document.forms['newLogin'].submit();
		}
		else
		{
			alert('The passwords do not match. Please try again.');
		}
	}
	else
	{
		alert('You must fill in all fields to complete this process.');
	}
}

checkUploads = function()
{
	var t,i;
	var s = 0; 
	for(i=0; i<document.form1.elements.length; i++)
	{
		t = i + 1;
		if(document.form1.elements[i].type == 'file')
		{
			if(document.form1.elements[i].value != '')
			{
				s = 1;
				if(document.form1.elements[t].value == '')
				{
					alert('All files require a display name.');
					return false;
				}
			}
		}
	}
	if(s > 0)
	{
		document.forms.form1.submit();
	}
}

checkPreferredUploads = function()
{
	var t,i;
	var s = 0;
	
	if(document.getElementById('filesFor').value == 0)
	{
		alert('You must select an agency.');
		return false;
	}
	for(i=0; i<document.form1.elements.length; i++)
	{
		t = i + 1;
		if(document.form1.elements[i].type == 'file')
		{
			if(document.form1.elements[i].value != '')
			{
				s = 1;
				if(document.form1.elements[t].value == '')
				{
					alert('All files require a display name.');
					return false;
				}
			}
		}
	}
	if(s > 0)
	{
		
		document.forms.form1.submit();
		
	}
	
}

deleteAgencyFile = function(id,fname)
{
	document.location = 'index.php?Get=preferredUploadFiles&action=delete&fileId=' + id;
}

cancelActionAgencyFile = function()
{
	document.location = 'index.php?Get=preferredUploadFiles';
}

confirmedDeleteAgencyFile = function(id)
{
	document.location = 'index.php?Get=preferredUploadFiles&deleteFile=Y&fileId=' + id;
}
deleteAppFile = function(id,fname)
{
	document.location = 'index.php?Get=preferredAppFiles&action=delete&fileId=' + id;
}

cancelActionAppFile = function()
{
	document.location = 'index.php?Get=preferredAppFiles';
}

confirmedDeleteAppFile = function(id)
{
	document.location = 'index.php?Get=preferredAppFiles&deleteFile=Y&fileId=' + id;
}
