// Form validation interface methods 

function validateOrdForm(f)
{		
	if(!CheckAgainstLinks2(f))
		return(false);
	if(!validateEmail(f.email.value, true, true))
		return(false); 	
	return(true);
}

function validateSuppForm(f)
{		
	if(!CheckAgainstLinks2(f))
		return(false);
	if(!validateEmail(f.email.value, true, true))
		return(false); 	
	return(true);
}

function validateCtcForm(f)
{		
	if(!CheckAgainstLinks2(f))
		return(false);
	if(!validateEmail(f.email.value, true, true))
		return(false); 	
	return(true);
}

// ------------------------------------------

function CheckAgainstLinks(fval)
{
	if (fval.indexOf("http:") > -1 || fval.indexOf("https:") > -1) 
	{
    	alert ("No hyperlinks allowed !" );
    	return(false);
	}
	return(true);
}

function CheckAgainstLinks2(f)
{	   
	var elArr = f.elements; 
    for(var i = 0; i < elArr.length; i++) 
	{	
		if(elArr[i].name != "redirect" )
		{
            var fval = elArr[i].value; 
 			if (!CheckAgainstLinks(fval))
    		{
         		return(false);
			}
		}
    }
	return(true);
}		

// Email Validation Javascript
// (c) 2003, by Stephen Chapman, Felgall Pty Ltd
function validateEmail(addr,man,db) 
{
	if (addr == '' && man) 
	{
   		if (db) alert('email address is mandatory');
   		return false;
	}
	var invalidChars = "\/\'\\ ;:?!()[]\{\}^|";
	for (i=0; i<invalidChars.length; i++) 
	{
   		if (addr.indexOf(invalidChars.charAt(i),0) > -1) 
		{
      		if (db) alert('email address contains invalid characters');
      		return false;
   		}
	}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      if (db) alert("email address contains non ascii characters.");
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   if (db) alert('email address must contain an @');
   return false;
}
if (atPos == 0) {
   if (db) alert('email address must not start with @');
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   if (db) alert('email address must contain only one @');
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   if (db) alert('email address must contain a period in the domain name');
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   if (db) alert('period must not immediately follow @ in email address');
   return false;
}
if (addr.indexOf('.@',0) != -1){
   if (db) alert('period must not immediately precede @ in email address');
   return false;
}
if (addr.indexOf('..',0) != -1) {
   if (db) alert('two periods must not be adjacent in email address');
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   if (db) alert('invalid primary domain in email address');
   return false;
}
return true;
}

// Address encrytion interface methods

function decrypt_mail(crypted_string)
{
	var decrypted_string = decrypt_string(crypted_string,0,0,false);
	// replace domain with url-domain
	var n1 = decrypted_string.lastIndexOf(".");
    var n2 = document.domain.lastIndexOf(".");
	parent.location = decrypted_string.substring(0, n1) + document.domain.substring(n2);
}

function decrypt_echo(crypted_string)
{
	var decrypted_string = decrypt_string(crypted_string,0,0,true);
	// replace domain with url-domain
	var n = document.domain.lastIndexOf(".");
	if (document.domain.substring(n) == ".com")
		document.write(decrypted_string.replace(".de", ".com"));
	else
		document.write(decrypted_string.replace(".com", ".de"));
	return true;
}

function decrypt_and_email(crypted_string,n,decryption_key) 
{
	if(!n || !decryption_key) { n = 0; decryption_key = 0; }
	if(!crypted_string) crypted_string = 0;

	var decrypted_string = decrypt_string(crypted_string,n,decryption_key,false);
	parent.location = decrypted_string;
}

function decrypt_and_echo(crypted_string,n,decryption_key) 
{
	if(!n || !decryption_key) { n = 0; decryption_key = 0; }
	if(!crypted_string) crypted_string = 0;

	var decrypted_string = decrypt_string(crypted_string,n,decryption_key,true);
	document.write(decrypted_string);
	return true;
}

// ---------------------------------------

if(!addresses) 
{
	var addresses = new Array();
	addresses.push("7367 5741 2204 4074 3815 5544 3805 5616 1270 4300 6786 5102 7364 3815 2273 6786 1574 4505 5616 2204 4732 6786 7364 3757 2273 5616 2204 2943");
	addresses.push("5251 3403 3283 4250 2405 4723 1349 2371 825 709 2405 2497 2911 58 1349 1262 1349 1974 2911 1105 1188 2547 1150 2405 2310 2310 1105 2497 2547 1262 2497 4250 3283 1105 1188 2547 762 1105 4115 2405 2911 2405 1105 2497 1349 2547 1262 4720 4250 4723 58 1105 1188 2547 4839 1105 1263 3283 4250 2405 4723 4845 4290 2371 3283 2310 1105 4720 2818 2310 1105 2547 2033");
}

// This script is (c) 2006 Jim Tucek under the
// GNU General Public License (http://www.gnu.org/licenses/gpl.html)
// For more information, visit www.jracademy.com/~jtucek/email/ 

var decryption_cache = new Array();

function decrypt_string(crypted_string,n,decryption_key,just_email_address) 
{
	var cache_index = "'"+crypted_string+","+just_email_address+"'";

	if(decryption_cache[cache_index])					// If this string has already been decrypted, just
		return decryption_cache[cache_index];				// return the cached version.

	if(addresses[crypted_string])						// Is crypted_string an index into the addresses array
		var crypted_string = addresses[crypted_string];			// or an actual string of numbers?

	if(!crypted_string.length)						// Make sure the string is actually a string
		return "Error, not a valid index.";

	if(n == 0 || decryption_key == 0) {					// If the decryption key and n are not passed to the
		var numbers = crypted_string.split(' ');			// function, assume they are stored as the first two
		n = numbers[0];	decryption_key = numbers[1];			// numbers in crypted string.
		numbers[0] = ""; numbers[1] = "";				// Remove them from the crypted string and continue
		crypted_string = numbers.join(" ").substr(2);
	}

	var decrypted_string = '';
	var crypted_characters = crypted_string.split(' ');

	for(var i in crypted_characters) {
		var current_character = crypted_characters[i];
		var decrypted_character = exponentialModulo(current_character,n,decryption_key);
		if(just_email_address && i < 7)				// Skip 'mailto:' part
			continue;
		if(just_email_address && decrypted_character == 63)	// Stop at '?subject=....'
			break;
		decrypted_string += String.fromCharCode(decrypted_character);
	}
	
	decryption_cache[cache_index] = decrypted_string;			// Cache this string for any future calls

	return decrypted_string;
}

// Finds base^exponent % y for large values of (base^exponent)
function exponentialModulo(base,exponent,y) 
{
	if (y % 2 == 0) {
		answer = 1;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	} else {
		answer = base;
		for(var i = 1; i <= y/2; i++) {
			temp = (base*base) % exponent;
			answer = (temp*answer) % exponent;
		}
	}
	return answer;
}

