
function initMM(m) {
    document.getElementById(m).className = 'thisnav';
}
function initSM(m) {
    document.getElementById(m).style.display = 'inline';
}
function hideSM(m) {
    document.getElementById(m).style.display = 'none';
}
function tidyString(s) {
	var r1 = new RegExp('<','g');
	var r2 = new RegExp('>','g');
	var t = s.replace(r1,' ');
	t = t.replace(r2,' ');
	return t;
}
function countWords(w) {
  var n=1;
  for (j=0; j<w.length; j++) {
    if (w.charAt(j)==' ') {
      n++;
      while (w.charAt(j)==' ') {j++;} 
    }
  }
  return n;
}
function getMax(w,t) {
  var n=1;
  for (j=0; j<w.length; j++) {
    if (w.charAt(j)==' ') {
      n++;
      while (w.charAt(j)==' ') {j++;} 
    }
    if (n>t) {return j;}
  }
  return j;  
}
function ignoreSpaces(string) {
var temp = "";
string = '' + string;
splitstring = string.split(" ");
for(i = 0; i < splitstring.length; i++)
temp += splitstring[i];
return temp;
}
function ignoreTrailingSpaces(string,tidy) {
	var temp = string;
	while(''+temp.charAt(0)==' ') {
		temp=temp.substring(1,temp.length);
	}
	while(''+temp.charAt(temp.length-1)==' ') {
		temp=temp.substring(0,temp.length-1);
	}
	if (tidy) {
		var x = temp;
		temp = tidyString(temp);
		if (x != temp) {
			alert("Sorry, but you cannot use the '<' or '>' symbols in your text-inputs. They have been replaced with spaces. You may want to re-write this entry.");
		}
	}
	return temp;
}
function ignoreTrailingSpacesAndCut(s,f,x,t) {
 var st = ignoreTrailingSpaces(s,t);
 if (countWords(st)>x) {
   alert("Your entry for "+f+" has been truncated to the maximum allowed for this field ("+x.toString()+" words)\nYou may wish to re-enter it.");
   st = st.substring(0,getMax(st,x));
   return st;
 }
// st = st.substring(0,x);
 return st;
} 
function checkEmail(source, args) {
	// var em = document.forms[0].txtEmail.value;
	var em = args.Value;
	args.IsValid = true;
	if (em.length > 0) {
		var regex = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
		args.IsValid = regex.test(em);
	}
  	return;
}


function checkFile(what) {
   var t='';
   var fileTypeAllowed = (function(){
		this.array = arguments;
		return function(arg){
                for(var j = this.array.length;j--;){
	               t += this.array[j] + ', ';
	            }
				for(var i = this.array.length;i--;){
							if(arg == this.array[i]){return true;}
				}return false;
		};
   })('jpeg','png','gif','jpg','doc','docx','pdf','swf','mp3','mp4','wmv','webm','mpeg4','3gpp','mov','avi','qt','ppt','pptx')// allowed file extensions 
   var source=what.value;
   var i = source.lastIndexOf("\\");
   var j = source.lastIndexOf(".");
   var fName = source.substring(i+1,j);
   var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
   //alert(fileTypeAllowed(ext))
   if (fileTypeAllowed(ext)) {
       var regex = /^[A-Za-z0-9_ -]{1,50}$/;
       if (!regex.test(fName)) {
          alert('The file name contains illegal characters, or is too long\nPlease re-name the file using only alphanumeric characters, hyphens, spaces and underscores\nand restrict it to a maximum of 50 characters\n' + fName);                   
       }
   } else {
       alert('Please upload files of the following types only:\n' + t.substring(0,t.length-2));
   }
}

function checkImg(what) {
   var t='';
   var fileTypeAllowed = (function(){
		this.array = arguments;
		return function(arg){
                for(var j = this.array.length;j--;){
	               t += this.array[j] + ', ';
	            }
				for(var i = this.array.length;i--;){
							if(arg == this.array[i]){return true;}
				}return false;
		};
   })('png','gif','jpg','jpeg')// allowed (image)file extensions 
   var source=what.value;
   var i = source.lastIndexOf("\\");
   var j = source.lastIndexOf(".");
   var fName = source.substring(i+1,j);
   var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
   //alert(fileTypeAllowed(ext))
   if (fileTypeAllowed(ext)) {
       var regex = /^[A-Za-z0-9_ -]{1,50}$/;
       if (!regex.test(fName)) {
          alert('The file name contains illegal characters, or is too long\nPlease re-name the file using only alphanumeric characters, hyphens, spaces and underscores\nand restrict it to a maximum of 50 characters\n' + fName);                   
       }
   } else {
       alert('Please upload files of the following types only:\n' + t.substring(0,t.length-2));
   }
}



