// 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 = 'Vinsamlega sláðu inn kortanúmerið þitt hér. '
  + 'til að '
  + ((encrypt_it) ? "dulkóða og gildisprófa það. " : "");

 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('Kortanúmerið sem þú slóst inn var ekki rétt. '
    + 'Vinsamlega athugaðu númerið og reyndu aftur.');
   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 ktsync(form_element)
{
	document.FrontPage_Form1.CardHolderKennitala.value = form_element.value;
}

function namesyncGjafabref(form_element)
{
	document.FrontPage_Form1.CardHolderName.value = form_element.value;
}

function ktsyncGjafabref(form_element)
{
	document.FrontPage_Form1.CardHolderKennitala.value = form_element.value;
}

function FrontPage_Form1_Validator(theForm)
{
	if (submitCount == 0)
	{
		submitCount++;
	}
	else
	{
		return(false);
	}

	if ( theForm.Product.value == "TXT")
 	{
    	alert("Vinsamlega veldu forrit sem á að panta.");
    	theForm.Product.focus();
		submitCount = 0;
    	return (false);
  	}
	
	if (theForm.Name.value == "")
  	{
    	alert("Vinsamlega gefðu upp nafn.");
    	theForm.Name.focus();
    	submitCount = 0;
    	return (false);
  	}
	
	if ((theForm.CustomerKennitala.value.length < 10) || (theForm.CustomerKennitala.value.length > 10))
    {
        alert("Vinsamlega gefðu upp kennitölu - 10 stafir.");
        theForm.CustomerKennitala.focus();
        submitCount = 0;
        return (false);
 	}

    var checkOK = "0123456789";
    var checkStr = theForm.CustomerKennitala.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)
    {
		alert("Kennitalan á aðeins að innihalda tölustafi.");
		theForm.CustomerKennitala.focus();
		submitCount = 0;
		return (false);
    }
	
	if (theForm.Address.value == "")
	{
		alert("Vinsamlega gefðu upp heimilisfang.");
		theForm.Address.focus();
		 submitCount = 0;
		return (false);
	}

	if (theForm.Postcode.value == "")
	{
		alert("Vinsamlega gefðu upp póstnúmer.");
		theForm.Postcode.focus();
		submitCount = 0;
		return (false);
	}
	
	if (theForm.City.value == "")
	{
		alert("Vinsamlega gefðu upp kaupstað.");
		theForm.City.focus();
		submitCount = 0;
		return (false);
	}
	
	if (theForm.Phone.value == "")
	{
		alert("Vinsamlega gefðu upp símanúmer.");
		theForm.Phone.focus();
		submitCount = 0;
		return false;
	}
	
	if (theForm.Product.value == "WOF" || theForm.Product.value == "WOG")
	{
		if (theForm.Email.value == "")
		{
			alert("Vinsamlega gefðu upp netfang.");
			theForm.Email.focus();
			submitCount = 0;
			return false;
		}
			
		if (theForm.Email2.value != theForm.Email.value)
		{
				alert("Netfangið stemmir ekki. Vinsamlega lagaðu það og reyndu aftur.");
				theForm.Email2.focus();
				submitCount = 0;
				return false;
		}
		
		if (!isEmail(theForm.Email.value))
		{
			alert("Vinsamlega gefðu upp gilt netfang.");
			theForm.Email.focus();
			submitCount = 0;
			return false;
		}
	}
	
	if(document.FrontPage_Form1.Product.value != "WO2")
	{
		if(theForm.payment.value == "GRE")
		{
			alert("Vinsamlega veljið greiðslumáta.");
			theForm.payment.focus();
			submitCount = 0;
			return false;
		}
	}
  
	//Verður að borga 3 mánaða áksrift með korti
	if(document.FrontPage_Form1.Askrift[2].checked == true)
	{
		if(document.FrontPage_Form1.payment.value == "GIRO" || document.FrontPage_Form1.payment.value == "MIF" )
		{
			alert("Vinsamlegast athugið að ekki er hægt að borga 3 mánaða ákrift nema með kreditkorti.");
			theForm.payment.focus();
			submitCount = 0;
			return false;
		}
	}
	
	//ekki hægt að skipta 3 mánaða greiðslu upp
	if(document.FrontPage_Form1.Askrift[2].checked == true)
	{	
		if(this.FrontPage_Form1.skipta_greidslu.checked == true)
		{
			alert("Vinsamlegast athugið að ekki er hægt að skipta 3 mánaða áksrift í tvennt.");
			theForm.skipta_greidslu.focus();
			submitCount = 0;
			return false;
		}
	}
	
  //ekki hægt að skipta greiðslu nema það sé greitt með kreditkorti
	if(this.FrontPage_Form1.skipta_greidslu.checked == true)
	{
		if(document.FrontPage_Form1.payment.value == "GIRO" || document.FrontPage_Form1.payment.value == "MIF" )
		{
			alert("Vinsamlegast athugið að ekki er hægt að skipta greiðslu nema greitt sé með kreditkorti.");
			theForm.payment.focus();
			submitCount = 0;
			return false;
		}
	}
  
	//worldfengur						  //flutn, íslfengur->Woldfengur      //worldfengur og íslandsfengur											//gjafabréf
	if (( theForm.Product.value == "WOF") || ( theForm.Product.value == "WO2") || ( theForm.Product.value == "WIF") || ( theForm.Product.value == "WOG"))
	{
	
		if (theForm.username.value == "")
		{
			alert("Vinsamlega gefðu upp notandanafn.");
			theForm.username.focus();
			submitCount = 0;
			return false;
		}
		
		if (theForm.password.value == "")
		{
			alert("Vinsamlega gefðu upp lykilorð.");
			theForm.password.focus();
			submitCount = 0;
			return false;
		}
		
		if (theForm.password2.value == "")
		{
			alert("Vinsamlega gefðu upp staðfestingu á lykilorði.");
			theForm.password2.focus();
			submitCount = 0;
			return false;
		}
		
		if (theForm.password.value != theForm.password2.value)
		{
			alert("Lykilorð og staðfesting á lykilorði eru ekki eins. Vinsamlegast reynið aftur.");
			theForm.password2.focus();
			submitCount = 0;
			return false;
		}
	}
	
	if (document.FrontPage_Form1.Product.value != "WO2")
	{
		if((document.FrontPage_Form1.payment.value == "VISA") || (document.FrontPage_Form1.payment.value == "Eurocard") || (document.FrontPage_Form1.payment.value == "DinersClub"))
		{
			if (theForm.CardHolderName.value == "")
			{
				alert("Vinsamlega gefðu upp nafn korthafa.");
				theForm.CardHolderName.focus();
				submitCount = 0;
				return (false);
			}
	
			if ((theForm.CardHolderKennitala.value.length < 10) || (theForm.CardHolderKennitala.value.length > 10))
			{
				alert("Vinsamlega gefðu upp kennitölu korthafa.");
				theForm.CardHolderKennitala.focus();
				submitCount = 0;
				return (false);
			}
			
			var checkOK = "0123456789";
			var checkStr = theForm.CardHolderKennitala.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)
			{
				alert("Kennitalan á aðeins að innihalda tölustafi.");
				theForm.CardHolderKennitala.focus();
				submitCount = 0;
				return (false);
			}
			
			if (theForm.CardNumber.value == "")
			{
				alert("Vinsamlega gefðu upp kortanúmer.");
				theForm.CardNumber.focus();
				submitCount = 0;
				return(false);
			}
			
			if (theForm.CardNumber.value.length != 17)
			{
				alert("Vinsamlega gefðu upp 16 stafa kortanúmer.");
				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("Gildistími á að innihalda 4 tölustafi.");
				theForm.CardExpiration.focus();
				submitCount = 0;
				return (false);
			}
		}
	}
	
	
	return (true);
	}
	
	
	function comboVal()
	{
		//það sem tilheyrir greiðslumátanum ef verið að skipta úr íslansfeng í wof þarf ekki að velja greiðslumáta
		if(document.FrontPage_Form1.Product.value == "WO2")
		{
			document.FrontPage_Form1.CardHolderName.disabled = true;
			document.FrontPage_Form1.CardHolderKennitala.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.CardHolderKennitala.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.CardHolderKennitala.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.CardHolderKennitala.disabled = false;
			document.FrontPage_Form1.CardNumber.disabled = false;
			document.FrontPage_Form1.CardExpiration.disabled = false;
			document.FrontPage_Form1.skipta_greidslu.disabled = false;
			
			this.FrontPage_Form1.CardHolderName.style.backgroundColor=(this.disable)? '#cccccc':'#ffffff';
			this.FrontPage_Form1.CardHolderKennitala.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.CardHolderKennitala.value = this.FrontPage_Form1.CustomerKennitala.value;
			this.FrontPage_Form1.CardNumber.value = "";
			this.FrontPage_Form1.CardExpiration.value = "";
		}
		
		//það sem tilheyrir bara worldfeng
		if(document.FrontPage_Form1.Product.value == "WOF" || document.FrontPage_Form1.Product.value == "WO2" || document.FrontPage_Form1.Product.value == "WIF" || document.FrontPage_Form1.Product.value == "WOG")
		{
			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;
			document.FrontPage_Form1.Askrift[2].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;
			document.FrontPage_Form1.Askrift[2].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 == "GIRO" || document.FrontPage_Form1.payment.value == "MIF")
		{
			document.FrontPage_Form1.CardHolderName.disabled = true;
			document.FrontPage_Form1.CardHolderKennitala.disabled = true;
			document.FrontPage_Form1.CardNumber.disabled = true;
			document.FrontPage_Form1.CardExpiration.disabled = true;
			document.FrontPage_Form1.skipta_greidslu.disabled = true;
			
			this.FrontPage_Form1.CardHolderName.style.backgroundColor=(this.disable)? '#ffffff':'#cccccc';
			this.FrontPage_Form1.CardHolderKennitala.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.CardHolderKennitala.value = "";
			this.FrontPage_Form1.CardNumber.value = "";
			this.FrontPage_Form1.CardExpiration.value = "";
			this.FrontPage_Form1.skipta_greidslu.checked = false;
		}
		else
		{
			document.FrontPage_Form1.CardHolderName.disabled = false;
			document.FrontPage_Form1.CardHolderKennitala.disabled = false;
			document.FrontPage_Form1.CardNumber.disabled = false;
			document.FrontPage_Form1.CardExpiration.disabled = false;
			document.FrontPage_Form1.skipta_greidslu.disabled = false;
			
			this.FrontPage_Form1.CardHolderName.style.backgroundColor=(this.disable)? '#cccccc':'#ffffff';
			this.FrontPage_Form1.CardHolderKennitala.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.CardHolderKennitala.value = this.FrontPage_Form1.CustomerKennitala.value;
		}
	}
