var OURMAIL = '@thingysoft.com';
var OURSITE = 'http://www.thingysoft.com/_t';

function CharInRange(asc, start, finish){
	//Check character has ASCII value between start and end
	//If so return true, otherwise false
	for (var j = start;j <= finish; j++){
		if (asc == j )return true;
	}
		
	//If got this far then is false
	return false;
}

function CheckBox(formname,name,min,max,message,mustfill){
	var boxer = eval('document.' + formname + '.' + name + '.value;')
	if ((boxer=='')&&(mustfill=='no'))
		{
		return true;
		}
	   else
		{ 
		//Check length > min and < max
		if (boxer.length<min || boxer.length>max )
			{
			alert(message + ' is ' + boxer.length + ' chars long - must be from ' + min + ' to ' + max);
			validated=false;
			return false;
			}
		}
	return true;
}

function CheckBoxChars(formname,name){
	var content = eval('document.' + formname + '.' + name + '.value;');      
	if (content==''){
		return true;
	}
	else{ 
		//Check only Numeric or Alphabetic chars
		for (var i=0;i<content.length;i++){
		    var asc = content.charCodeAt(i);		
			if( (asc!=10) && (asc!=13) && (CharInRange(asc,32,59)==false) && (CharInRange(asc,32,59)==false) && (CharInRange(asc,63,90)==false) && (CharInRange(asc,95,122)==false) ){
				alert('Sorry! Character "' + String.fromCharCode(asc) + '" is not allowed in ' + name);
				validated=false;
				return false;		
			}			
		}
	}
	return true;
}

function CheckBoxSpaces(formname,name){
	var content = eval('document.' + formname + '.' + name + '.value;');
	if (content==''){
		return true;
		}
	   else
		{ 
			//Check NO spaces
			for (var i=0;i<content.length;i++){
			    var asc = content.charCodeAt(i);		
				if(asc==32){
					alert('Spaces are not allowed in ' + name);
					validated=false;
					return false;		
				}			
			}
		}
	return true;
}

function CheckBoxesSame(formname,name1,name2){
	var word1 = eval('document.' + formname + '.' + name1 + '.value;');
	var word2 = eval('document.' + formname + '.' + name2 + '.value;');
	if (word1 != word2)
		{
		window.alert("Invalid Password - Both entries must be Identical" );
		validated=false;
		return false;
		}
	return true;
}

function CheckEmail(formname,name,message,mustfill){
	var emailaddress = eval('document.' + formname + '.' + name + '.value;');
	if ((emailaddress=='')&&(mustfill=='no'))
		{
		return true;
		}
	   else
		{ 
		//Check Email Address length > 6 and < 50
		var length = emailaddress.length;
		if (length < 6 || length > 50 )
			{
			alert(message + ' is invalid - it is ' + length + ' chars long - must be from 6 to 50');
			validated=false;
			return false;
			}
		//Check no commas in Email Address
		var posComma = emailaddress.indexOf(",");
		if (posComma != -1)
			{
			alert(message + ' is invalid - no commas allowed');
			validated=false;
			return false;
			}
		//Check no spaces in Email Address
		var posSpace = emailaddress.indexOf(" ");
		if (posSpace != -1)
			{
			alert(message + ' is invalid - no spaces allowed');
			validated=false;
			return false;
			}
		//Check no ">" in Email Address
		var posSpace = emailaddress.indexOf(">");
		if (posSpace != -1)
			{
			alert(message + ' is invalid - \'>\' is not allowed');
			validated=false;
			return false;
			}
		//Check no "<" in Email Address
		var posSpace = emailaddress.indexOf("<");
		if (posSpace != -1)
			{
			alert(message + ' is invalid - \'<\' is not allowed');
			validated=false;
			return false;
			}
		//Check precisely ONE "@" sign in Email Address
		var positionFront = emailaddress.indexOf("@");
		var positionBack = emailaddress.lastIndexOf("@");
		if (positionFront == -1 || positionFront != positionBack)
			{
			alert(message + ' is invalid - must be ONE "@" Symbol');
			validated=false;
			return false;
			}
		//Check something in front of "@" sign in Email Address
		var positionFront = emailaddress.indexOf("@");
		if (positionFront == 0)
			{
			alert(message + ' is invalid - must be character in front of "@" Symbol');
			validated=false;
			return false;
			}
		//Check Email Address ends in .xx .xxx or .xxxx
		var positionBack = emailaddress.lastIndexOf(".") + 1;
		var length = emailaddress.length;
		var suffixLength = length - positionBack;
		if (suffixLength <= 1 || suffixLength >= 5 )
			{
			alert(message + ' has invalid suffix - eg as in "abc@xyz.commm"');
			validated=false;
			return false;
			}
		}
	return true;
}

function CheckPassword(formname,name){
	var password = eval('document.' + formname + '.' + name + '.value;');
	//Check Password 6 to 20 long
	if (password.length < 6 || password.length > 20 )
		{
		alert('Password is ' + password.length + ' chars long - must be from 6 to 20');
		validated=false;
		return false;
		}
	//Does password contain '=' ( this is dangerous as can allow SQL attack )
	if (password.indexOf('=') != -1)
		{
		window.alert('The equals character, \'=\' is NOT allowed in a Password');
		validated=false;
		return false;
		}
	//Does password contain ' ' ( 'space' can be confusing)
	if (password.indexOf(' ') != -1)
		{
		window.alert('The space character, \' \' is NOT allowed in a Password');
		validated=false;
		return false;
		}
	//Does password contain '>' ( this is dangerous as can allow SQL attack )
	if (password.indexOf('>') != -1)
		{
		window.alert('The character, \'>\' is NOT allowed in a Password');
		validated=false;
		return false;
		}
	//Does password contain '<' ( this is dangerous as can allow SQL attack )
	if (password.indexOf('<') != -1)
		{
		window.alert('The character, \'<\' is NOT allowed in a Password');
		validated=false;
		return false;
		}
	return true;
}

function CheckPhone(formname,name,mustfill){
	var found;
	var phone = eval('document.' + formname + '.' + name + '.value;');
	if ((phone=='')&&(mustfill=='no'))
		{
		return true;
		}
	   else
		{ 
		//Check phone number length > 6 and < 20
		if (phone.length < 10 || phone.length > 20 )
			{
			alert('Phone number is ' + phone.length + ' chars long - must be from 10 to 15');
			validated=false;
			return false;
			}
		//Check only numeric chars OR " " ( space )
		for (var i=0;i<phone.length;i++)
			{
			found = false;
			for (var j=48;j<58;j++)
				{
				if (phone.charCodeAt(i) == j || phone.charCodeAt(i) == 32)found = true;
				}
				if (found == false)
					{
					alert('Invalid Phone Number as "' + phone.charAt(i) + '" is not a valid character');
					validated=false;
					return false;
					}
			}
		//Check phone number starts with zero
		if (phone.indexOf('0') != 0)
			{
			window.alert('Invalid Phone Number - Must start with Zero');
			validated=false;
			return false;
			}
		}
	return true;
}

function CheckURL(formname,name,mustfill){
	var found;
	var URL = eval('document.' + formname + '.' + name + '.value;');
	if ((URL=='')&&(mustfill=='no'))
		{
		return true;
		}
	   else
		{ 
		//Check URL length > 12 and < 100
		if (URL.length < 13 || URL.length > 100 )
			{
			alert('URL is ' + URL.length + ' chars long - must be from 13 to 100');
			validated=false;
			return false;
			}
		//Check URL starts with 'http://'
		if (URL.indexOf('http://') != 0)
			{
			window.alert('Invalid URL - it must start with http://');
			validated=false;
			return false;
			}
		//Check URL contains at least one dot
		if (URL.indexOf('.') == -1 )
			{
			window.alert('Invalid URL - it must contain al leat one dot!');
			validated=false;
			return false;
			}
		//Check URL does NOT contain '>'
		if (URL.indexOf('>') != -1 )
			{
			window.alert('Invalid URL - must NOT contain \'>\'');
			validated=false;
			return false;
			}
		//Check URL does NOT contain '<'
		if (URL.indexOf('<') != -1 )
			{
			window.alert('Invalid URL - must NOT contain \'<\'');
			validated=false;
			return false;
			}
		}
	return true;
}

function Emailer(regarding){		//Unscrambles Email as defence against spammers
	alert('Please leave FIDO in the subject line as it helps our spam filter');
	var dum=("orgbobfido-uk");
	var result = 'mailto:' + dum.substr(3,3) + '@' + dum.substr(6,7) + '.' + dum.substr(0,3);
	result = result + '?subject=FIDO ' + regarding + ': ' + '&amp;body=Dear Bob,';   
	//alert(result);
	window.location.href = result;
}

function FaqFetch(){
	alert('Frequently Asked Questions will open in a new window. If it is not immediately apparent please look for the little box at the bottom of your screen and click on it');
	if(window.screen) 
		{
		var aw = screen.availWidth;
		var ah = screen.availHeight;
		}
	var sgs='location=1,scrollbars=1,titlebar=1,toolbar=1,menubar=1,directories=1,status=1,resizable=1';
	if(navigator.appName.indexOf("Microsoft")>-1) sgs = 'width=' + aw + ',height=' + ah + ',left=0,top=0,' + sgs;
	winfaq=window.open('faq.asp','winfaq',sgs);
}

function Message(){			//Message re Help
	alert('To use the help facility just rest your mouse arrow over the Question Mark Image - there is no need to click!')
}

function PrintPage() {
	var message = 'This page contains useful information and \nwe recommend you print a copy right now! \n\nOK to print now?';
	if (window.print) 
	{
	agree = confirm(message);
	if (agree) window.print(); 
   }
}

function Trim (InStr){	//Trims zeros from front and back
	var OutStr=InStr;
	for (Count=0; Count < InStr.length; Count++)  
		{
		TempChar=InStr.substring (Count, Count+1);
		if (TempChar!=" ") 
			{
			OutStr=InStr.substring (Count, InStr.length);
			break;
			}
		}
	InStr=OutStr;
	for (Count=InStr.length; Count > 0; Count--)  
		{
		TempChar=InStr.substring (Count-1, Count);
		if (TempChar!=" ") 
			{
			OutStr=InStr.substring (0, Count);
			break;
			}
		}
	return (OutStr);
}