function validateSupportLoginForm()
{
  var frm = document.main_form;
	var errField = -1;
	var msg = "The following fields are required:\n";
	
	if(frm.cust_name.value == "")
	{
	  msg += "\nLogin";
	  errField = 0;
  }
	if(frm.cust_pwd.value == "")
	{
	  msg += "\nPassword";
		if(errField == -1) errField = 1;
	}
	if(errField != -1)
	{
		alert(msg);
		document.main_form[errField].focus();
	  return false;
	}
}
function openwin(url,width,height) 
{
  x = (screen.width-width)/2;
  y = (screen.height-height)/2;
  str='"toolbar=no,width=' + width + ',height=' + height + 
    ',left=' + x + ',top=' + y +
    ',status=no,scrollbars=no,resize=no,menubar=no"';
  window.open(url,"forgotpass",str);
}
function hideShowDiv(selected)
{
	var payroll = document.all.payrollDiv;
	var errorDetails = document.all.errorDetailsDiv;
    var attachment = document.all.attachmentDiv;
	/*if (div.style.display == "none")
	{
		div.style.display = "block";
	}
	else
	{
		div.style.display = "block";
	}*/
	if (selected == "softwaredetails")
	{
		payroll.style.display = "none";
		errorDetails.style.display = "block";
		attachment.style.display = "block";
		//href.innerText = "Payroll details";
	}
	else if (selected == "payrolldetails")
	{
		payroll.style.display = "block";
		errorDetails.style.display = "none";
		attachment.style.display = "block";
		//href.innerText = "Error details";
	}
}

function validate()
{
	if (document.main_form.selectOption.value == "")
	{
		alert ("Please choose call type");
		return false;
	}
}

function clickIE() 
{ 
	//if (document.all) { return false; } 
} 
function clickNS(e) 
{ 
	if (document.layers || (document.getElementById && !document.all)) 
	{ 
		if (e.which==2 || e.which==3) { return false; } 
	} 
	
} 
if (document.layers) 
{ 
	document.captureEvents(Event.MOUSEDOWN); 
	document.onmousedown=clickNS; 
} 
else 
{ 
	document.onmouseup=clickNS; 
	document.oncontextmenu=clickIE; 
} 
document.oncontextmenu=new Function("return false");

function launch(param)
{
	var width, height, extraOps, ref;
	if (document.all)
	{
		// if either of these mod param
		if (param == "outstandingcalls" || param == "completedcallsbydate")
		{
			width=screen.availWidth-100;
			height=screen.availHeight-100;
			//document.main_form.site_code.value = "EBSL0002";
			param = "customerdata/html/" + document.main_form.site_code.value.toUpperCase() + "." + param + ".html";
			extraOps = ",top=0,left=0";
		}

		ref = window.open("launchPage.htm?file=" + param, "pageLauncher",
			"resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,width=" + width + ",height=" + height + extraOps);
		ref.focus();
	}
	else
	{
		alert('You are using an incompatible browser for this function, you must be using Internet Explorer version 4.0 or later');
	}
}
function openPDF(param)
{
	var width, height, extraOps, ref;
	if (document.all)
	{
		// if either of these mod param
		if (param == "outstandingcalls" || param == "completedcallsbydate")
		{
			width=screen.availWidth-100;
			height=screen.availHeight-100;
			//document.main_form.site_code.value = "EBSL0002";
			param = "customerdata/html/" + document.main_form.site_code.value.toUpperCase() + "." + param + ".pdf";
			extraOps = ",top=0,left=0";
		}

		ref = window.open("launchPage.htm?file=" + param, "pageLauncher",
			"resizable=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=auto,width=" + width + ",height=" + height + extraOps);
		ref.focus();
	}
	else
	{
		alert('You are using an incompatible browser for this function, you must be using Internet Explorer version 4.0 or later');
	}
}


/* start main form requestformif.html*/
// convert \ filenames to / filenames and any other processing required before form is submitted
function cleanUp()
{
	var frm = document.main_form;
	var filenames = frm.filenames;

	//return false;
	if (document.main_form.selectOption.value == "")
	{
		alert ("Please choose call type")
		return false;
	}
	if (document.main_form.selectOption.value == "payrolldetails")
	{
		if (document.main_form.payroll_date.value == "")
		{
			alert ("Please choose Payroll Processing Day")
			return false;
		}
		if (document.main_form.bacs_date.value == "")
		{
			alert ("Please choose Bacs Processing Day")
			return false;
		}
		if (document.main_form.payroll_comp.value == "")
		{
			alert ("Please Enter Live Company Names")
			return false;
		}
		if (document.main_form.data_format.value == "")
		{
			alert ("Please Choose Data Format")
			return false;
		}
		if (document.main_form.payroll_run.value == "")
		{
			alert ("Please Choose Payroll Run")
			return false;
		}
	}


	for(var i=0; i<filenames.length; i++)
		filenames.options[i].value = filenames.options[i].value.split("\\").join("/");
		
	frm.files_st_src.value = frm.files_st_src.value.split("\\").join("/");
		
	//alert(frm.call_priority_dd.value);
	var faultCode 	= frm.call_priority_dd.options[frm.call_priority_dd.selectedIndex].text; // call priority dropdown
	var priority	= frm.call_priority_dd.value;
	faultCode 		= faultCode.split("/")[0];
			
	// set hidden field before submit values
	frm.fault_code.value 	= faultCode;
	frm.call_priority.value	= priority;
	frm.site_contact.value 	= frm.originated_by.value;

	if(parent.frames.document.all.scrollpoint)	
		parent.frames.document.all.scrollpoint.scrollIntoView(true);
	
	// remove the last file input first
	//alert(frm.filename.length);
	/*
	if(frm.filename.length)
	{
		//alert('hmmm, says length');
		frm.filename[frm.filename.length-1].removeNode(true);
		for (var i=0; i<frm.filename.length; i++)
		{
			//parent.frames.document.all.scrollpoint.innerHTML = document.all.theFileDiv.innerHTML;
			parent.frames.document.all.scrollpoint.insertAdjacentElement("beforeEnd", frm.filename[i]);
			//alert(parent.frames.document.all.scrollpoint.innerHTML);
					//document.all.theFileDiv.insertAdjacentElement("beforeEnd", newFileInput);

		}
		//alert(parent.frames.document.all.scrollpoint.innerHTML);
	}*/
	
}
		
/* fills in automated form fields */		
function documentSetup()
{
	var frm = document.main_form;
	var da = document.all;
	
	// Note: for this to work, the function event handler must be defined before this call or on <form tag
	frm.onsubmit = cleanUp;	
	
	// test data
	//document.all.error_desc.value = "It just kind of broke, I didn't do anything, it was innocent, just... died on me, no idea how, locks up everytime I try accessing certain areas of the system, like... the hard-disk, or, the power button...";
	//document.all.steps_taken.value = "Gentle slapping, nudging, shaking, screaming, pen, light mallet, small hammer, size 12 doc martins, larger mallet, sledgehammer, glue - crying, sobbing, shouting, screaming, rather windy ride for about 15 floors.";
			
	// sort querystring values
	sortQuerystringValues();
	//alert(parent.frames.reqFormIFrame);
	if(parent.frames.reqFormIFrame)
	{
		parent.frames.reqFormIFrame.height = 800;
		//alert(parent.frames.reqFormIFrame.height);
	}
	
	//alert(frm.files_st.value);
	/*
	if(frm.files_st.value != "")
	{
		recreateAttachments();	// recreates attachments if error (so we can repopulate with previously selected attachments)
	}
	else
		if(parent.document.all.scrollpoint)
			parent.document.all.scrollpoint.innerHTML = "";
	*/
}

function recreateAttachments()
{
	var frm = document.main_form;
	// create a new fileinput then we hide the other putting new input in its place
	//var newFileInput = document.createElement("<input type=\"file\" class=\"uploadBtn\" name=\"filename\"" +
	//"size=\"30\" id=\"file0\" onclick=\"setId(this.id)\">");
	
	//alert("top of recreateAttachments");
	
	if(frm.files_st.value != "")
	{
		theInputs = parent.document.all.scrollpoint.innerHTML;
		//alert(theInputs);
	}
	
	var fileList 		= frm.files_st.value.split(",");
	var filePathList 	= frm.files_st_src.value.split(","); 
	
	
	if(frm.files_st.value.split(",").length > 1)
	{
		//alert(frm.files_st.value.split(",").length);

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

			var newOption 	= document.createElement("OPTION");
			
			// create a new fileinput then we hide the other putting new input in its place
			//newFileInput = document.createElement("<input type=\"file\" class=\"uploadBtn\" name=\"filename\"" +
			//"size=\"30\" id=\"file" + i + "\" onclick=\"setId(this.id)\">");
	
			newOption.text 	= fileList[i];
			newOption.value = filePathList[i];
			
			frm.filenames.add(newOption);
			
			if(frm.filename.length)
				frm.filename[i].style.display = "none";
			else
				frm.filename.style.display = "none";
			
//alert(fileList[i] + "\n" + filePathList[i]);
			//frm.filenames.add(newOption);
	
		}
		
		//frm.filename[i-1].style.display = "none";
		
	}
	else
	{
		// defo 1 here so get the sodding id
		//var newIndex = document.main_form.filename[document.all.filenames.length]
		// remove the existing input so we can add the old one (with value filled in as not allowed to set file src values)
		if(document.main_form.filename)
			document.main_form.filename.removeNode(true);
			
		var newFileInput = document.createElement("<input type=\"file\" class=\"uploadBtn\" name=\"filename\"" +
		"size=\"30\" id=\"file1\" onclick=\"setId(this.id)\">");
	
		var newOption 	= document.createElement("OPTION");
		
		newOption.text 	= fileList;
		newOption.value = filePathList;
		//alert(newOption.text + "\n" + newOption.value);

		frm.filenames.add(newOption);

		//frm.filename.style.display = "none";
		//document.all.theFileDiv.insertAdjacentElement("beforeEnd", newFileInput);

	}
	
	
	//alert(frm.filename.id + "\nabout to add theInputs\n" + theInputs + "\ndiv: " + document.all.theFileDiv);
	if(!newFileInput)
		var newFileInput = document.createElement("<input type=\"file\" class=\"uploadBtn\" name=\"filename\"" +
		"size=\"30\" id=\"file" + i + "\" onclick=\"setId(this.id)\">");

	
	//alert(theInputs + "\nnew: " + newFileInput);
	//alert(document.all.theFileDiv.innerHTML + "\nBefore adding to div");

	// add existing file inputs
	document.all.theFileDiv.insertAdjacentHTML("beforeEnd", theInputs);
	
	
//alert(theInputs);
	//alert(frm.filename.id + "\nabout to add theNewInput");
//	alert(document.all.theFileDiv.innerHTML + "\nAdded theInputs");

	// the new input
	document.all.theFileDiv.insertAdjacentElement("beforeEnd", newFileInput);
	
	//alert("how many: " + document.main_form.filename.length + "\nSrc of first: " + document.main_form.filename[0].value);

	
	if(frm.filenames.length)
	{
		//salert(frm.filenames.length);
		//alert("list.len: " + frm.filenames.length + "\n" + frm.filenames[0].value + "\n" + frm.filename.length);
	}
	else
	{
		//alert('no inputs available');
	}
	
}
		
function sortQuerystringValues()
{
	var frm = document.main_form;
	
	if (location.search)
	{
		var ary=window.location.search.substr(1).split("&");
		for (i=0;i<ary.length;i++) 
		{
			ary[i]=ary[i].split("=");
		}
		var qStr=new Array();
		for (i=0;i<ary.length;i++) 
		{
			ary[i][0]=ary[i][0].replace(/\+/g," ");
			ary[i][0]=unescape(ary[i][0]);
			ary[i][1]=ary[i][1].replace(/\+/g," ");
			ary[i][1]=unescape(ary[i][1]);
			qStr[ary[i][0]]=ary[i][1];
		}
		
		//$querystring = "?custName=" . $custName . "&siteCode=" . $siteCode . "&siteContact=" . $siteContact . 
		//"&siteTelNr=" . $siteTelNr . "&callAction=" . $callAction . "&callPriority=" . $callPriority;
			
		// form values
		if(qStr['siteTelNr'])
			frm.contact_number.value 	= qStr['siteTelNr'];
		if(qStr['siteContact'])
			frm.originated_by.value 	= qStr['siteContact'];
		if(qStr['custName'])
			frm.customer_desc.value 	= qStr['custName'];
			
		// stored as 1/2/3 so...
		if(qStr['callPriority'])
 			frm.call_priority_dd.selectedIndex = parseInt(qStr['callPriority']) - 1;
		
		if(qStr['siteCode'])
			frm.site_code.value 	= qStr['siteCode'];
		if(qStr['siteTelNr'])
			frm.site_telnr.value 	= qStr['siteTelNr'];
		if(qStr['callAction'])
			frm.call_action.value	= qStr['callAction'];
		if(qStr['emailAddr'])		
			frm.email_addr.value	= qStr['emailAddr'];
		
		// if already created a successfull call, then clear *some* fields
		if(qStr['success'] && qStr['success'] == "true")
		{
			frm.steps_taken.value 		= "";
			frm.error_desc.value 		= "";
			frm.program_code.value		= "";
			frm.version_number.value	= "";
		}
	}
}
		
// remove selected file from list
function removeAttachment()
{
	var frm = document.main_form;
	var filename = frm.filename;		// input file objects
	
	// check file available and selected in filenames select
	if(frm.filenames.selectedIndex > -1)
	{
		// remove file input with same value as selected index - if is first, doesnt matter
		if(filename.length)
		{
			for(var i=0; i<filename.length; i++)
			{
				// remove file upload input
				if(filename[i].value == frm.filenames.options[frm.filenames.selectedIndex].value)
				{
					filename[i].removeNode(true);
					// and the temp one
					//parent.document.tmpform.filename[i].removeNode(true);
				}
			}
		}
		
		var justTheFilename = frm.filenames.options[frm.filenames.selectedIndex].text;
		var fileAndPath		= frm.filenames.options[frm.filenames.selectedIndex].value;
		
		// if filename is in files_st
		if(frm.files_st.value.indexOf(justTheFilename) > -1)
		{
			if(frm.files_st.value.split(",").length > 1)
			{
				frm.files_st.value 		= frm.files_st.value.split("," + justTheFilename).join("");
				frm.files_st_src.value 	= frm.files_st_src.value.split("," + fileAndPath).join("");
			}
			else
			{
				frm.files_st.value 		= "";
				frm.files_st_src.value 	= "";
			}
		}
		//else
		//	alert(frm.files_st.value);
		
		frm.filenames.remove(frm.filenames.selectedIndex);
		
	}
	else
		alert("No files selected or list empty");
}

// this is so we know each buttons ID, need a counter to dynamically create the file inputs and grab for interrigation
function getFileUploadBtnIndex(whichId)
{
	var filenames = document.main_form.filename;
	if(filenames.length)
	{
		for(var i=0; i<filenames.length; i++)
			if(filenames[i].id == whichId)
				return i;
	}
	else
		return 0;
}
		
var fileCtr = 0;
		
// adds an attachment to select filenames by creating a new option
function addAttachment(whichId)
{
	var frm = document.main_form;
			var index = getFileUploadBtnIndex(whichId);
			var filename = (frm.filename != null && frm.filename.value != null) ? frm.filename.value : null;
			
			if(!filename) filename = (frm.filename[index] != null && frm.filename[index].value != null) ? frm.filename[index].value : null;
			
			//document.main_form.filename.value = "shit";
			//alert(document.main_form.filename.value);
			
			if(filename != null)
			{
				// slash direction
				var slash = (filename.lastIndexOf("/") > -1) ? "/" : null;
				if(!slash) slash = (filename.lastIndexOf("\\") > -1) ? "\\" : null;

				// check file already in list (same path && filename)
				var newOption 	= document.createElement("OPTION");
							
				if(filename.lastIndexOf("/") > -1)
				{
					newOption.text 	= filename.slice(filename.lastIndexOf(slash)+1, filename.length);	
					newOption.value	= filename;
				}
				else
				{
					newOption.text 	= filename.slice(filename.lastIndexOf(slash)+1, filename.length);
					newOption.value = filename;
				}
					
				var fileAndPathInList = false; // file and path already in list
			
				// check already in list
				for(var i=0; i<frm.filenames.length; i++)
				{
					var thisOp = frm.filenames.options[i];
					if(thisOp.value == newOption.value)
						fileAndPathInList = true;
				}
				
	
		if(!fileAndPathInList)
		{
			// create a new fileinput then we hide the other putting new input in its place
			var newFileInput = document.createElement("<input type=\"file\" class=\"uploadBtn\" name=\"filename\"" +
			"size=\"30\" id=\"file" + (index+1) + "\" onclick=\"setId(this.id)\">");
			
			if(frm.files_st.value != "")
			{
				frm.files_st.value 		+= "," + newOption.text;
				frm.files_st_src.value 	+= "," + newOption.value;
			}
			else
			{
				frm.files_st.value 		= newOption.text;
				frm.files_st_src.value 	= newOption.value;
			}
			
			frm.filenames.add(newOption);
			// replace existing file input with newly created one
			//alert("index: " + index + "\nlen: " + frm.filename.length);
			
			if(index == 0)
			{
				//alert("sp: " + parent.document.all.scrollpoint.innerHTML);
				
				//parent.document.all.scrollpoint.insertAdjacentHTML("beforeEnd", frm.filename);
				//alert(parent.document.tmpform);
				
				//alert("sp: " + parent.document.all.scrollpoint.innerHTML.name);
				
				frm.filename.style.display = "none";
				document.all.theFileDiv.insertAdjacentElement("beforeEnd", newFileInput);
				//alert(newFileInput);
				// for reloading on error
				
			}
			else
			{
				frm.filename[whichId].style.display = "none";
				document.all.theFileDiv.insertAdjacentElement("beforeEnd", newFileInput);
				//alert("FUCKOFF U WANKER " + parent.document.all.scrollpoint);
				// for reloading on error
				//parent.document.all.scrollpoint.insertAdjacentElement("beforeEnd", newFileInput);
			}
			//frm.filename.insertBefore(newFileInput);
			//frm.filename[index].swapNode(newFileInput);
		}
		else
		{
			alert("The file you are trying to add is already in the list");
		}
	}			
	else
	{
		alert('You must first enter the path and filename, or use the browse to find a file then add');
	}
}
		
var currentFileBtnId = null;
		
function setId(which)
{
	currentFileBtnId = which;
}
		
var keyCombo = 158;
var keyMod	 = 0;
		
function checkKeyPress()
{	
	var code 	= (document.all) ? event.keyCode:e.which;
	
	if(code==17||code==65||code==76)
	{
		if(keyMod >= keyCombo)
		{
			keyMod=0;
			document.all.new_cust_div.style.display = "none";
		}
		else
		{
			keyMod+=code;
		}
		//alert(keyMod);
		if(keyMod==keyCombo)
			document.all.new_cust_div.style.display = "block";	
	}
	else
	{
		if(keyMod >= keyCombo)
		{
			keyMod=0;
			document.all.new_cust_div.style.display = "none";
		}
	}
}
/* end main form requestformif.html */
/* start forgotpass.htm*/
function validateLostForm()
{
	var frm = document.lost_form;
	var errField = -1;
	var msg = "The following fields are required:\n";
	
	if(frm.site_name.value == "")
	{
		msg += "\nSite name";
		errField = 0;
	}
	if(frm.login_name.value == "")
	{
		msg += "\nLogin name";
		if(errField == -1) errField = 1;
	}
	if(frm.email_addr.value != frm.email_addr2.value || frm.email_addr.value.indexOf("@") == -1) 
	{
		msg += "\nEmail Address";
		if(errField == -1) errField = 2;
	}
	if(errField != -1)
	{
		alert(msg);
		document.lost_form[errField].focus();
		return false;
	}
}
/* end forgotpass.htm */
