// change it to false if you do not want
// the credit card number to be encrypted
var encrypt_it = true;


/* ==================================================================
   THIS FUNCTION IS TAKEN DIRECTLY FROM NETSCAPE FROM:
   http://developer.netscape.com/library/examples/...
                    .../javascript/formval/FormChek.js
   which is a bunch of functions to validate forms

   FUNCTION: isCreditCard(st)
   INPUT:    st - a string representing a credit card number
   RETURNS:  true, if the credit card number passes the Luhn Mod-10 test
	         false, otherwise
   ================================================================== */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);
}

function getCCNum(default_val) {
 msg = 'Please enter your creditcard number here '
  + 'so it can be '
  + ((encrypt_it) ? "tested and encrypted. " : "");

 return prompt(msg,default_val);
}

// takes in a credit card number, adds one to each digit
// (9 becomes 0), and then returns the encrypted credit
// card number with an 'e' tacked on to the end to signal
// the number has been encrypted
function encrypt(val) {
 val = "" + val;
 var result = "";
 for (i=0;i<val.length;i++) {
  character = val.charAt(i);
  if ("0123456789".indexOf(character) != -1) {
   character = parseInt(character);
   character = (character+1)%10;
  }
  result += character;
 }
 if (result != "")
  result += "e";
 return result;
}

function unencrypt(val) {
 val = "" + val;
 for (n=0;n<9;n++)
  val = encrypt(val);
 return (val.substring(0,val.indexOf('e')));
}

function strip(val) {
 val = "" + val;
 if (!val)
  return "";
 var result = "";
 for (i=0;i<val.length;i++) {
  character = val.charAt(i);
  if ("0123456789".indexOf(character) != -1)
   result += character;
 }
 return result;
}

var last_entry = "";
function doCCStuff(form_element) {
 if (blur_reset) {
  last_entry = form_element.value;
  if (last_entry && last_entry.indexOf('e') != -1)
   last_entry = unencrypt(last_entry);
  entry = getCCNum(last_entry);
  stripped_entry = strip(entry);
  while (entry && (!isCreditCard(stripped_entry))) {
   alert('Your cardnumber is not rightful. '
    + 'Pleasa try again.');
   last_entry = entry;
   entry = getCCNum(last_entry);
   stripped_entry = strip(entry);
  }
  if (entry) {
   if (encrypt_it)
    form_element.value = encrypt(entry);
   else
    form_element.value = entry;
  }
  blur_form(form_element);
 }
 return false;
}
var blur_reset = true;
function blur_form(form_element) {
 form_element.blur();
 blur_reset = false;
 setTimeout("blur_reset=true",2000);
}

var submitCount = 0;
var whitespace = " \t\n\r";
function isEmpty(s)
 {
	return ((s == null) || (s.length == 0))
}


function isWhitespace (s)
{
	var i;
	// Is s empty?
	if (isEmpty(s))
		return true;
	// Search through string's characters one by one
	// until we find a non-whitespace character.
	// When we do, return false; if we don't, return true.
	for (i = 0; i < s.length; i++)
 {
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (whitespace.indexOf(c) == -1)
			return false;
	}

	// All characters are whitespace.
	return true;
}


function isEmail (s)
 {
	// is s whitespace?
	if (isWhitespace(s))
		return false;
	// there must be >= 1 character before @, so we
	// start looking at character position 1
	// (i.e. second character)
	var i = 1;
	var sLength = s.length;
	// the last character must not be a .
	if (s.charAt(sLength -1) == ".")
		return false;
	// look for @

	while ((i < sLength) && (s.charAt(i) != "@"))
    {
		 i++
	}


	if ((i >= sLength) || (s.charAt(i) != "@"))
		return false;
	else i += 2;
	// look for .
	while ((i < sLength) && (s.charAt(i) != "."))
 {
		i++
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (s.charAt(i) != "."))
		return false;
	else return true;
}


function namesync(form_element)
{
	document.FrontPage_Form1.CardHolderName.value = form_element.value;
}

function namesyncGjafabref(form_element)
{
	document.FrontPage_Form1.CardHolderName.value = form_element.value;
}

function ValidForrit(theForm)
{
	if (theForm.Product.value == "TXT")
	{
		alert("Vinsamlega veldu forrit sem á að panta.");
		theForm.Product.focus();
	}
}

function FrontPage_Form1_Validator(theForm)
{
	if (submitCount == 0)
	{
		submitCount++;
	}
	else
	{
		return(false);
	}

	if (theForm.Name.value == "")
  	{
    	alert("Please enter your name.");
    	theForm.Name.focus();
    	submitCount = 0;
    	return (false);
  	}
	
	if (theForm.Address.value == "")
	{
    	alert("Please enter your address.");
    	theForm.Address.focus();
	 	submitCount = 0;
    	return (false);
  	}

	if (theForm.Postcode.value == "")
  	{
    	alert("Please enter your zip code.");
    	theForm.Postcode.focus();
    	submitCount = 0;
    	return (false);
  	}

	if (theForm.City.value == "")
  	{
    	alert("Please enter your city.");
    	theForm.City.focus();
    	submitCount = 0;
    	return (false);
  	}
	
	if (theForm.Phone.value == "")
  	{
    	alert("Please enter your telephone number.");
    	theForm.Phone.focus();
    	submitCount = 0;
    	return false;
  	}
	
	if (theForm.Email.value == "")
  	{
    	alert("Please enter your e-mail.");
    	theForm.Email.focus();
    	submitCount = 0;
    	return false;
  	}
	
	if (theForm.Email2.value == "")
  	{
    	alert("Please enter your e-mail again.");
    	theForm.Email2.focus();
    	submitCount = 0;
    	return false;
  	}

    if (theForm.Email2.value != theForm.Email.value)
  	{
			alert("The e-mail address is not the same in both fields, please try again.");
	  	theForm.Email2.focus();
	  	submitCount = 0;
	  	return false;
  	}

 	if (!isEmail(theForm.Email.value))
  	{
		alert("Please enter valid e-mail");
	  	theForm.Email.focus();
	  	submitCount = 0;
	  	return false;
  	}
	
   	if(document.FrontPage_Form1.payment.value == "GRE")
   	{
	   	alert("Please choose payment type");
	   	theForm.payment.focus();
	   	submitCount = 0;
	   	return(false)
   	}
  
	//worldfengur	
	if (theForm.Product.value == "WOF") 
	{
		if (theForm.username.value == "")
		{
			alert("Please enter a username.");
			theForm.username.focus();
			submitCount = 0;
			return false;
		}

		if (theForm.password.value == "")
		{
			alert("Please enter password.");
			theForm.password.focus();
			submitCount = 0;
			return false;
		}

		if (theForm.password2.value == "")
		{
			alert("Please enter your password again.");
			theForm.password2.focus();
			submitCount = 0;
			return false;
		}

		if (theForm.password.value != theForm.password2.value)
		{
			alert("The password is not the same in both fields, please try again.");
			theForm.password2.focus();
			submitCount = 0;
			return false;
		}

	}

	/*
 	if((document.FrontPage_Form1.payment.value == "VISA") || (document.FrontPage_Form1.payment.value == "Eurocard") || (document.FrontPage_Form1.payment.value == "DinersClub"))	
	{
  	if (theForm.CardHolderName.value == "")
    {
			alert("Please enter cardholder name.");
			theForm.CardHolderName.focus();
			submitCount = 0;
			return (false);
		}
	
		if (theForm.CardNumber.value == "")
		{
			alert("Please enter card number.");
			theForm.CardNumber.focus();
			submitCount = 0;
			return(false);
		}
		
		if (theForm.CardNumber.value.length != 17)
		{
			alert("Card number must be 16 digits.");
			theForm.CardNumber.focus();
			submitCount = 0;
			return(false);
		}
	
		var checkOK = "0123456789";
		var checkStr = theForm.CardExpiration.value;
		var allValid = true;
		for (i = 0;  i < checkStr.length;  i++)
		{
			ch = checkStr.charAt(i);
			for (j = 0;  j < checkOK.length;  j++)
				if (ch == checkOK.charAt(j))
							break;
				if (j == checkOK.length)
				{
					allValid = false;
					break;
				}
		}
		
		if ((!allValid) || (theForm.CardExpiration.value.length < 4))
		{
			alert("Expiery date should only be 4 digits.");
			theForm.CardExpiration.focus();
			submitCount = 0;
			return (false);
		}
	}
	*/
	return (true);
}

function comboVal()
{
/*  if(document.FrontPage_Form1.Product.value == "WO2")
	{
		document.FrontPage_Form1.CardHolderName.disabled = true;
		document.FrontPage_Form1.CardNumber.disabled = true;
		document.FrontPage_Form1.CardExpiration.disabled = true;

    this.FrontPage_Form1.CardHolderName.style.backgroundColor=(this.disable)? '#ffffff':'#cccccc';
		this.FrontPage_Form1.CardNumber.style.backgroundColor=(this.disable)? '#ffffff':'#cccccc';
    this.FrontPage_Form1.CardExpiration.style.backgroundColor=(this.disable)? '#ffffff':'#cccccc';

		this.FrontPage_Form1.CardHolderName.value = "";
		this.FrontPage_Form1.CardNumber.value = "";
    this.FrontPage_Form1.CardExpiration.value = "";

    	//ÞÞÞ bætti inn 26. apríl 2002
 	   document.FrontPage_Form1.payment.disabled = true;
	}
	else
	{
		document.FrontPage_Form1.CardHolderName.disabled = false;
		document.FrontPage_Form1.CardNumber.disabled = false;
		document.FrontPage_Form1.CardExpiration.disabled = false;
	
  	this.FrontPage_Form1.CardHolderName.style.backgroundColor=(this.disable)? '#cccccc':'#ffffff';
		this.FrontPage_Form1.CardNumber.style.backgroundColor=(this.disable)? '#cccccc':'#ffffff';
   	this.FrontPage_Form1.CardExpiration.style.backgroundColor=(this.disable)? '#cccccc':'#ffffff';
		
		this.FrontPage_Form1.CardHolderName.value = this.FrontPage_Form1.Name.value;
		this.FrontPage_Form1.CardNumber.value = "";
    this.FrontPage_Form1.CardExpiration.value = "";
	
	}

	if(document.FrontPage_Form1.Product.value == "WOF" || document.FrontPage_Form1.Product.value == "WO2" || document.FrontPage_Form1.Product.value == "WIF")
	{
		document.FrontPage_Form1.username.disabled = false;
		document.FrontPage_Form1.password.disabled = false;
		document.FrontPage_Form1.password2.disabled = false;
		document.FrontPage_Form1.language.disabled = false;
	  document.FrontPage_Form1.Askrift[0].disabled = false;
    document.FrontPage_Form1.Askrift[1].disabled = false;
		this.FrontPage_Form1.username.style.backgroundColor=(this.enable)? '#cccccc':'#ffffff';
		this.FrontPage_Form1.password.style.backgroundColor=(this.enable)? '#cccccc':'#ffffff';
		this.FrontPage_Form1.password2.style.backgroundColor=(this.enable)? '#cccccc':'#ffffff';

    	//ÞÞÞ bætti inn 26. apríl 2002
    	if(document.FrontPage_Form1.Product.value == "WO2")
    	{
      		document.FrontPage_Form1.payment.disabled = true;
    	}
    	else
    	{
      		document.FrontPage_Form1.payment.disabled = false;
    	}
	}
	else
	{
		document.FrontPage_Form1.username.disabled = true;
		document.FrontPage_Form1.password.disabled = true;
		document.FrontPage_Form1.password2.disabled = true;
		document.FrontPage_Form1.language.disabled = true;
 		document.FrontPage_Form1.Askrift[0].disabled = true;
 		document.FrontPage_Form1.Askrift[1].disabled = true;
		this.FrontPage_Form1.username.style.backgroundColor=(this.disable)? '#ffffff':'#cccccc';
		this.FrontPage_Form1.password.style.backgroundColor=(this.disable)? '#ffffff':'#cccccc';
		this.FrontPage_Form1.password2.style.backgroundColor=(this.disable)? '#ffffff':'#cccccc';
		this.FrontPage_Form1.username.value = "";
		this.FrontPage_Form1.password.value = "";
		this.FrontPage_Form1.password2.value = "";

    //ÞÞÞ bætti inn 26. apríl 2002
    document.FrontPage_Form1.payment.disabled = false;
	}
*/
}

function comboPayment()
{
/*
	if(document.FrontPage_Form1.payment.value == "MIF")
	{

		document.FrontPage_Form1.CardHolderName.disabled = true;
		document.FrontPage_Form1.CardNumber.disabled = true;
   	document.FrontPage_Form1.CardExpiration.disabled = true;

  	this.FrontPage_Form1.CardHolderName.style.backgroundColor=(this.disable)? '#ffffff':'#cccccc';
		this.FrontPage_Form1.CardNumber.style.backgroundColor=(this.disable)? '#ffffff':'#cccccc';
  	this.FrontPage_Form1.CardExpiration.style.backgroundColor=(this.disable)? '#ffffff':'#cccccc';

		this.FrontPage_Form1.CardHolderName.value = "";
		this.FrontPage_Form1.CardNumber.value = "";
  	this.FrontPage_Form1.CardExpiration.value = "";
	}
	else
	{
		document.FrontPage_Form1.CardHolderName.disabled = false;
		document.FrontPage_Form1.CardNumber.disabled = false;
		document.FrontPage_Form1.CardExpiration.disabled = false;
	
		this.FrontPage_Form1.CardHolderName.style.backgroundColor=(this.disable)? '#cccccc':'#ffffff';
		this.FrontPage_Form1.CardNumber.style.backgroundColor=(this.disable)? '#cccccc':'#ffffff';
   	this.FrontPage_Form1.CardExpiration.style.backgroundColor=(this.disable)? '#cccccc':'#ffffff';
		
		this.FrontPage_Form1.CardHolderName.value = this.FrontPage_Form1.Name.value;
	}
*/
}