var OURMAIL = '@thingysoft.com';
var OURSITE = 'http://www.thingysoft.com/_t';

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 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 CheckOK(){	
	//Used by agentemailprefs.asp
	with(document.agentreports)
	{
	if ((R1[0].checked==true)&&((C1.checked==false)&&(C2.checked==false)&&(C3.checked==false)))
			{
			alert('Please Tick one or more Boxes OR request No Emails');
			return false;					
			}
	}			
    return true; 			
}

function CheckPassword(formname,name){
	var password = eval('document.' + formname + '.' + name + '.value;');
	//Check Password 7 to 20 long
	if (password.length < 7 || password.length > 20 )
		{
		alert('Password is ' + password.length + ' chars long - must be from 7 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');
		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');
		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');
		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 CheckSurvey(){
	//Goes through each line checking for a tick
	var ticked = false;
	var thing;
	for (var i = 1;  i < 6;  i++)
		{
		thing = eval('document.survey.R' + i);
		ticked = false;
		for (var j = 0;  j < thing.length;  j++)
			{
			if(eval('thing[' + j + '].checked')==true)ticked=true;			
			}
			if(ticked!=true)
				{
				alert('Please tick a Radio Button EVERY line');
				return false;
				}
		}
return true;
}

function ClickedC(){	
	//Used by agentemailprefs.asp
	with(document.agentreports)
	{
	if (R1[0].checked==false)			
			{			
			if ((C1.checked==true)||(C2.checked==true)||(C3.checked==true))
					{
					R1[0].checked=true
					}
			}			
	}
}

function ClickedR(){	
	//Used by agentemailprefs.asp
	with(document.agentreports)
	{
	if (R1[1].checked==true)
			{			
			C1.checked=false;
			C2.checked=false;			
			C3.checked=false;			
			}
	}			
}

function Emailer(){		//Unscrambles Email as defence against spammers
	var dum=("comrobinsoftthingy");
	var result = 'mailto:' + dum.substr(3,5) + '@' + dum.substr(12,6) +  dum.substr(8,4) + '.' + dum.substr(0,3);
	result = result + '?subject=Problem with&amp;body=Hi Robin, I have a problem with';   
	//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 MouseClickDown(imgName){
	if (document.images)
	{
	imgDown=eval(imgName + "down.src");
	document[imgName].src=imgDown;
	//alert(imgName);
	//if (imgName=='pic3')FaqFetch();
	}
}

function MouseLightUp(imgName){
	if (document.images)
		{
		imgOn=eval(imgName + "on.src");
		document[imgName].src= imgOn;
		}
}

function MouseTurnOff(imgName){
	if (document.images)
	{
	imgOff=eval(imgName + "off.src");
	document[imgName].src= imgOff;
	}
}

function NotReady(){
      alert('Sorry! Left clicking is not yet operational. In due course it will enable you to automatically request details of properties');
}

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 RestoreDetails(intEmailPrefs){
	//Used by agentemailprefs.asp
	// 0 = Send no Emails  1 = Send Sales only  2 = Send First Run only	
	// 3 = Send Downloads only 4 = Send Sales & Runs  5 = Send Sales & Downloads  
	// 6 = Send Runs & Downloads  7 = Send Sales, Runs & Downloads
	with(document.agentreports)
	{	  
	if (intEmailPrefs==0)
		{
		R1[1].checked=true;
		C1.checked=false;
		C2.checked=false;
		C3.checked=false;
		}	
	if (intEmailPrefs==1)
		{
		R1[0].checked=true;
		C1.checked=true;
		C2.checked=false;
		C3.checked=false;
		}	
	if (intEmailPrefs==2)
		{
		R1[0].checked=true;
		C1.checked=false;
		C2.checked=true;
		C3.checked=false;
		}	
	if (intEmailPrefs==3)
		{
		R1[0].checked=true;
		C1.checked=false;
		C2.checked=false;
		C3.checked=true;
		}	
	if (intEmailPrefs==4)
		{
		R1[0].checked=true;
		C1.checked=true;
		C2.checked=true;
		C3.checked=false;
		}	
	if (intEmailPrefs==5)
		{
		R1[0].checked=true;
		C1.checked=true;
		C2.checked=false;
		C3.checked=true;
		}	
	if (intEmailPrefs==6)
		{
		R1[0].checked=true;
		C1.checked=false;
		C2.checked=true;
		C3.checked=true;
		}	
	if (intEmailPrefs==7)
		{
		R1[0].checked=true;
		C1.checked=true;
		C2.checked=true;
		C3.checked=true;
		}	
	}
}

function submitform(price,agent){
	//Used by paypal_button.asp
	with(document.paypal)
	{	
	cmd.value='_xclick';
	business.value='robin' & OURMAIL;
	item_name.value='Web Thingy';
	item_number.value='Version 1.00';
	amount.value=price;
	custom.value=agent;
	cancel_return.value = OURSITE + '/paypalcancelled.asp';
	cn.value='Optional Comments';
	currency_code.value='USD';
	}
	return true;
}

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);
}