/**
 * this id will a field with the id of export to a value of 1 so that the information will export to excel on submit
 */
$(function() {
	if ($("#downloadToExcel").length) {
		$("#downloadToExcel").click(function(){
			$("#Export").val(1);
			$("#" + $(this).parents('form').attr('id')).submit();
		});
	}

});

function MM_swapImgRestore() 

{ //v2.0
  if (document.MM_swapImgData != null)
	for (var i=0; i<(document.MM_swapImgData.length-1); i+=2)
	  document.MM_swapImgData[i].src = document.MM_swapImgData[i+1];
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function showHide(item){
	itemID = document.getElementById(item);
	if(itemID.style.display == ''){
		itemID.style.display = 'none'
	}else{
		itemID.style.display = ''
	}
}
function MM_swapImage() 

{ //v2.0
  var i,j=0,objStr,obj,swapArray=new Array,oldArray=document.MM_swapImgData;
  for (i=0; i < (MM_swapImage.arguments.length-2); i+=3) {
	objStr = MM_swapImage.arguments[(navigator.appName == 'Netscape')?i:i+1];
	if ((objStr.indexOf('document.layers[')==0 && document.layers==null) ||
		(objStr.indexOf('document.all[')   ==0 && document.all   ==null))
	  objStr = 'document'+objStr.substring(objStr.lastIndexOf('.'),objStr.length);
	obj = eval(objStr);
	if (obj != null) {
	  swapArray[j++] = obj;
	  swapArray[j++] = (oldArray==null || oldArray[j-1]!=obj)?obj.src:oldArray[j];
	  obj.src = MM_swapImage.arguments[i+2];
  } }
  document.MM_swapImgData = swapArray; //used for restore
}

function popUp_help(url) {

if (document.all) {var xMax = screen.width, yMax = screen.height} 
else if (document.layers) {var xMax = window.outerWidth, yMax = window.outerHeight} 
else {var xMax = 640, yMax=480}; var xOffset = (xMax - 670)/2, yOffset = (yMax - 650)/2; 

sealWin=window.open(url,"",'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=1,resizable=0,width=700,height=500,screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+'');
self.name = "";
}

var popupfeatures = "toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes";
function popUp(myurl) {var win = open(myurl,'popupwindow' + Math.floor(Math.random() * 100),popupfeatures + ',width=650,height=500');win.focus();}

function popupSized(myurl,height,width) {var win = open(myurl,'popupwindow',popupfeatures + ',width=' + width + ',height=' + height);win.focus();}

/*function enlarge(pic,title,width,height)
{
	preview = window.open("", title, "resizable=yes,toolbar=no,scrollbars=no,menubar=no,status=no,directories=no,width="+width+",height="+height+"");
	preview.document.write('<html><head><title>Picture<\/title><\/head>'+'<body onBlur="self.focus();" marginWidth="0" marginHeight="0" topmargin="0" leftmargin="0">'+'<center><img border="0" src="'+pic+'" width="'+width+'" height="'+height+'">'+'<\/center><\/body><\/html>');
	preview.document.close();
}*/

var counter = 0;
function count() 
{
	counter++;
	if(counter > 1) 
	{ 
		return false;
	}
	return true;
}
function ResetCounter() {
	counter = 0;
}
window.onunload = ResetCounter;






//------------------------------- Test for alphanumeric ---------------------------------------//

function IsAlpha(CheckString)
{
	return ValidAlphanumeric(CheckString,'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz');
}

function IsNumeric(CheckString)
{
	return ValidAlphanumeric(CheckString,'0123456789');
}

function ValidAlphanumeric(CheckString, ValidString)
{
	var LegalCount=0;
	for (i=0; i<CheckString.length; i++)
	{
		if (ValidString.indexOf(CheckString.substring(i,i+1)) !== -1) {LegalCount=LegalCount+1;}
	}
	if (LegalCount !== 0) return true;
	else return false;
}	



//------------------------------- Test for amounts ---------------------------------------//

function AmountIsNumeric(CheckString)
{
	return ValidAmount(CheckString,'0123456789.');
}

function ValidAmount(CheckString, ValidString)
{
	var IllegalCount=0;
	var DotCount=0;
	for (i=0; i<CheckString.length; i++)
	{
		if (ValidString.indexOf(CheckString.substring(i,i+1)) == -1) {IllegalCount=IllegalCount+1;}
		if (CheckString.substring(i,i+1) == '.') {DotCount=DotCount+1;}
	}
	if (IllegalCount == 0 && DotCount<=1 && CheckString.length!==DotCount) return true;
	else return false;
}	

//------------------------------- Test for email ---------------------------------------//

function IsEmail(email) 
{
	invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";
	
	// Check for null
	if (email == "") return false;

	// Check for invalid characters as defined above
	for (i=0; i<invalidChars.length; i++) 
	{
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0) > -1) return false;
	}
	
	lengthOfEmail = email.length;
	if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) return false;
	
	Pos = email.indexOf("@",1);
	if (email.charAt(Pos + 1) == ".") return false;
	
	while ((Pos < lengthOfEmail) && ( Pos != -1)) 
	{
		Pos = email.indexOf(".",Pos);
		if (email.charAt(Pos + 1) == ".") return false;
		if (Pos != -1) Pos++;
	}

	// There must be at least one @ symbol
	atPos = email.indexOf("@",1);
	if (atPos == -1) return false;

	// But only ONE @ symbol
	if (email.indexOf("@",atPos+1) != -1) return false;

	// Also check for at least one period after the @ symbol
	periodPos = email.indexOf(".",atPos);
	if (periodPos == -1) return false;

	if (periodPos+3 > email.length) return false;

	return true;
}


//------------------------------- Enlarge an image ---------------------------------------//

function enlarge(pic,title,width,height)
{
	
	if(typeof preview != 'undefined'){preview.close()};
	preview = window.open("", 'ImagePopup', "resizable=yes,toolbar=no,scrollbars=no,menubar=no,status=no,directories=no,width="+width+",height="+height+"");
	preview.document.write('<html><head><title>Picture<\/title><\/head>'+'<body onBlur="self.focus();" marginWidth="0" marginHeight="0" topmargin="0" leftmargin="0">'+'<center><img border="0" src="'+pic+'" width="'+width+'" height="'+height+'">'+'<\/center><\/body><\/html>');
	preview.document.close();
}
	

function Sanitise(StringToClean) {
	var CleanString = StringToClean;
	CleanString = CleanString.toLowerCase().replace(/[^a-z0-9]+/g, '-');
	CleanString = CleanString.replace(/^[\-]+/, '');
	CleanString = CleanString.replace(/[\-]+$/, '');
	CleanString = CleanString.slice(0, 80);
	return CleanString;
}

var TitleLastEdit = "";
var ForceFillClean = 0;
function FillClean(TitleField, TitleCleanField) {
	if(Sanitise(TitleLastEdit) == TitleCleanField.value || ForceFillClean) {
		TitleCleanField.value = Sanitise(TitleField.value);
		ForceFillClean = 0;
	};
	TitleLastEdit = TitleField.value;
}

