
function getPostageCode( code )
{
	if (code=="AF") return 1;
	if (code=="AX") return 2;
	if (code=="AL") return 0;
	if (code=="DZ") return 1;
	if (code=="AD") return 0;
	if (code=="AO") return 1;
	if (code=="AI") return 1;
	if (code=="AG") return 1;
	if (code=="AR") return 1;
	if (code=="AM") return 0;
	if (code=="AW") return 1;
	if (code=="AU") return 2;
	if (code=="AT") return 0;
	if (code=="AZ") return 0;
	if (code=="BS") return 1;
	if (code=="BH") return 1;
	if (code=="BD") return 1;
	if (code=="BB") return 1;
	if (code=="BY") return 0;
	if (code=="PW") return 2;
	if (code=="BE") return 0;
	if (code=="BZ") return 1;
	if (code=="BJ") return 1;
	if (code=="BM") return 1;
	if (code=="BT") return 1;
	if (code=="BO") return 1;
	if (code=="BA") return 0;
	if (code=="BW") return 1;
	if (code=="BR") return 1;
	if (code=="IO") return 1;
	if (code=="VG") return 1;
	if (code=="BN") return 1;
	if (code=="BG") return 0;
	if (code=="BF") return 1;
	if (code=="BI") return 1;
	if (code=="KH") return 1;
	if (code=="CM") return 1;
	if (code=="CA") return 1;
	if (code=="CV") return 1;
	if (code=="KY") return 1;
	if (code=="CF") return 1;
	if (code=="TD") return 1;
	if (code=="CL") return 1;
	if (code=="CN") return 2;
	if (code=="CX") return 1;
	if (code=="CC") return 1;
	if (code=="CO") return 1;
	if (code=="KM") return 1;
	if (code=="CG") return 1;
	if (code=="CD") return 1;
	if (code=="CR") return 1;
	if (code=="CI") return 1;
	if (code=="HR") return 0;
	if (code=="CU") return 1;
	if (code=="CA") return 1;
	if (code=="CY") return 0;
	if (code=="CZ") return 0;
	if (code=="DK") return 0;
	if (code=="DJ") return 1;
	if (code=="DM") return 1;
	if (code=="DO") return 1;
	if (code=="TL") return 2;
	if (code=="EC") return 1;
	if (code=="EG") return 1;
	if (code=="SV") return 1;
	if (code=="GQ") return 1;
	if (code=="ER") return 1;
	if (code=="EE") return 0;
	if (code=="ET") return 1;
	if (code=="FK") return 1;
	if (code=="FO") return 0;
	if (code=="FJ") return 2;
	if (code=="FI") return 0;
	if (code=="FR") return 0;
	if (code=="GF") return 1;
	if (code=="PF") return 2;
	if (code=="TF") return 2;
	if (code=="GA") return 1;
	if (code=="GM") return 1;
	if (code=="GE") return 0;
	if (code=="DE") return 0;
	if (code=="GH") return 1;
	if (code=="GI") return 0;
	if (code=="GR") return 0;
	if (code=="GL") return 0;
	if (code=="GD") return 1;
	if (code=="GP") return 1;
	if (code=="GU") return 2;
	if (code=="GT") return 1;
	if (code=="GN") return 1;
	if (code=="GW") return 1;
	if (code=="GY") return 1;
	if (code=="HT") return 1;
	if (code=="VA") return 0;
	if (code=="HN") return 1;
	if (code=="HK") return 1;
	if (code=="HU") return 0;
	if (code=="IS") return 0;
	if (code=="IN") return 1;
	if (code=="ID") return 1;
	if (code=="IR") return 1;
	if (code=="IQ") return 1;
	if (code=="IE") return 0;
	if (code=="IL") return 1;
	if (code=="IT") return 0;
	if (code=="JM") return 1;
	if (code=="JP") return 2;
	if (code=="JO") return 1;
	if (code=="KZ") return 0;
	if (code=="KE") return 1;
	if (code=="KI") return 2;
	if (code=="KP") return 2;
	if (code=="KR") return 2;
	if (code=="KW") return 1;
	if (code=="KG") return 0;
	if (code=="LA") return 1;
	if (code=="LV") return 0;
	if (code=="LB") return 1;
	if (code=="LS") return 1;
	if (code=="LR") return 1;
	if (code=="LY") return 1;
	if (code=="LI") return 0;
	if (code=="LT") return 0;
	if (code=="LU") return 0;
	if (code=="MO") return 1;
	if (code=="MK") return 0;
	if (code=="MG") return 1;
	if (code=="MW") return 1;
	if (code=="MY") return 1;
	if (code=="MV") return 1;
	if (code=="ML") return 1;
	if (code=="MT") return 0;
	if (code=="MH") return 2;
	if (code=="MQ") return 1;
	if (code=="MR") return 1;
	if (code=="MU") return 1;
	if (code=="MX") return 1;
	if (code=="FM") return 2;
	if (code=="MD") return 0;
	if (code=="MC") return 0;
	if (code=="MN") return 2;
	if (code=="MS") return 1;
	if (code=="MA") return 1;
	if (code=="MZ") return 1;
	if (code=="MM") return 1;
	if (code=="NA") return 1;
	if (code=="NR") return 2;
	if (code=="NP") return 1;
	if (code=="NL") return 0;
	if (code=="AN") return 1;
	if (code=="NC") return 2;
	if (code=="NZ") return 2;
	if (code=="NI") return 1;
	if (code=="NE") return 1;
	if (code=="NG") return 1;
	if (code=="NF") return 2;
	if (code=="MP") return 2;
	if (code=="NO") return 0;
	if (code=="OM") return 1;
	if (code=="PK") return 1;
	if (code=="PW") return 2;
	if (code=="PS") return 1;
	if (code=="PA") return 1;
	if (code=="PG") return 2;
	if (code=="PY") return 1;
	if (code=="PE") return 1;
	if (code=="PH") return 2;
	if (code=="PN") return 2;
	if (code=="PL") return 0;
	if (code=="PT") return 0;
	if (code=="PR") return 1;
	if (code=="QA") return 1;
	if (code=="RE") return 1;
	if (code=="RO") return 0;
	if (code=="RU") return 0;
	if (code=="RW") return 1;
	if (code=="WS") return 2;
	if (code=="SM") return 0;
	if (code=="KN") return 1;
	if (code=="SH") return 1;
	if (code=="LC") return 1;
	if (code=="PM") return 1;
	if (code=="VC") return 1;
	if (code=="ST") return 1;
	if (code=="SA") return 1;
	if (code=="SN") return 1;
	if (code=="CS") return 0;
	if (code=="SC") return 0;
	if (code=="SL") return 1;
	if (code=="SG") return 1;
	if (code=="SK") return 0;
	if (code=="SI") return 0;
	if (code=="SB") return 2;
	if (code=="SO") return 1;
	if (code=="ZA") return 1;
	if (code=="GS") return 1;
	if (code=="GS") return 1;
	if (code=="ES") return 0;
	if (code=="LK") return 1;
	if (code=="SD") return 1;
	if (code=="SR") return 1;
	if (code=="SZ") return 1;
	if (code=="SE") return 0;
	if (code=="CH") return 0;
	if (code=="SY") return 1;
	if (code=="TW") return 2;
	if (code=="TJ") return 0;
	if (code=="TZ") return 1;
	if (code=="TH") return 1;
	if (code=="TL") return 2;
	if (code=="TG") return 1;
	if (code=="TO") return 2;
	if (code=="TT") return 1;
	if (code=="TA") return 1;
	if (code=="TN") return 1;
	if (code=="TR") return 0;
	if (code=="TM") return 0;
	if (code=="TC") return 1;
	if (code=="TV") return 2;
	if (code=="UG") return 1;
	if (code=="UA") return 0;
	if (code=="AE") return 1;
	if (code=="GB") return -1;
	if (code=="US") return 1;
	if (code=="UM") return 1;
	if (code=="UY") return 1;
	if (code=="UZ") return 0;
	if (code=="VU") return 2;
	if (code=="VA") return 0;
	if (code=="VE") return 1;
	if (code=="VN") return 1;
	if (code=="VG") return 1;
	if (code=="VI") return 1;
	if (code=="WF") return 2;
	if (code=="YE") return 1;
	if (code=="ZM") return 1;
	if (code=="ZW") return 1;

  return -999;
}

function updatePostage( countryCode )  {

        postage = 0;
		postageCode = getPostageCode( countryCode );

		if (document.purchaseForm.currency.value == "GBP")
		{
           if (postageCode==-1) // UK - 1st class recorded
		       postage = "3.50";
		   else if (postageCode==0) // Airmail international signed for - europe
		       postage = "5.50";
		   else  // Airmail international signed for - world zone 1 / 2
   		       postage = "7.00";
	   }
	   
	   else if (document.purchaseForm.currency.value == "USD")
	   {
	       if (postageCode==-1) // UK - 1st class recorded
		       postage = "7.00";
		   else if (postageCode==0) // Airmail signed for - europe
		       postage = "11.00";
		   else  // Airmail signed for - world zone 1 / 2
   		       postage = "14.00";
	   } else if (document.purchaseForm.currency.value == "EUR")
	   {

	       if (postageCode==-1) // UK - 1st class recorded
		       postage = "5.20";
		   else if (postageCode==0) // Airmail signed for - europe
		       postage = "8.15";
		   else  // Airmail signed for - world zone 1 / 2
   		       postage = "10.40";
	   } else if (document.purchaseForm.currency.value == "AUD")
	   {

	       if (postageCode==-1) // UK - 1st class recorded
		       postage = "8.20";
		   else if (postageCode==0) // Airmail signed for - europe
		       postage = "13.00";
		   else  // Airmail signed for - world zone 1 / 2
   		       postage = "16.50";
	   }
	   

      document.purchaseForm.M_deliveryPrice.value = roundOff(eval( postage + '*' + document.purchaseForm.M_quantity.value ) );
      document.purchaseForm.deliveryPriceWithSymbol.value = currencySymbol(document.purchaseForm.currency.value) + roundOff(eval( postage + '*' + document.purchaseForm.M_quantity.value ) );

	}

function currencySymbol( currencyCode )
{
 return "";
/*   if (currencyCode=="USD")
     return "$";
   else if (currencyCode=="EUR")
     return "&euro;";
   else if (currencyCode=="AUD")
     return "$";	 
   else
     return "£";

*/
}


function updateSoftwarePrice( currencyCode )
{      

      if (currencyCode == "GBP")
	  {
         document.purchaseForm.M_softwarePrice.value =  roundOff( eval( "79.99 * " + document.purchaseForm.M_quantity.value ));
		 document.purchaseForm.softwarePriceWithSymbol.value = currencySymbol(currencyCode) + roundOff( eval( "79.99 * " + document.purchaseForm.M_quantity.value ));
	  }
	  else if (currencyCode == "EUR")
	  	  {
         document.purchaseForm.M_softwarePrice.value =  roundOff( eval( "114.99 * " + document.purchaseForm.M_quantity.value ));
		 document.purchaseForm.softwarePriceWithSymbol.value = currencySymbol(currencyCode) + roundOff( eval( "114.99 * " + document.purchaseForm.M_quantity.value ));
	  }	   
      else if (currencyCode == "USD")
	  	  {
         document.purchaseForm.M_softwarePrice.value =  roundOff( eval( "139.99 * " + document.purchaseForm.M_quantity.value ));
		 document.purchaseForm.softwarePriceWithSymbol.value = currencySymbol(currencyCode) + roundOff( eval( "139.99 * " + document.purchaseForm.M_quantity.value ));
	  }
	  else if (currencyCode == "AUD")
	  	  {
         document.purchaseForm.M_softwarePrice.value =  roundOff( eval( "184.99 * " + document.purchaseForm.M_quantity.value ));
		 document.purchaseForm.softwarePriceWithSymbol.value = currencySymbol(currencyCode) + roundOff( eval( "184.99 * " + document.purchaseForm.M_quantity.value ));
	  }
	  else  
	      document.purchaseForm.M_softwarePrice.value = "Currency error";
		  
}


function defocus()
{

 	document.purchaseForm.submitButton.focus();

}

function roundOff(value)
{
	if (value == 0)
	{
		return "0.00";
		exit;
	}

	precision = 2;

	value = "" + value //convert value to string

	precision = parseInt(precision);

	var whole = "" + Math.round(value * Math.pow(10, precision));

	var decPoint = whole.length - precision;

	if(decPoint != 0)
	{
		result = whole.substring(0, decPoint);
		result += ".";
		result += whole.substring(decPoint, whole.length);
	}
	else
	{
		result = value;
	}

    var p = 3 - (result.length - result.indexOf( "." ));

    while (p>0)
	{
	   result += "0";
	   p--;
	}
	return result;
}


function updatePrices()
{
	
   document.purchaseForm.currency.value = '%%currency%%';



   updateSoftwarePrice( document.purchaseForm.currency.value );

	if (document.purchaseForm.MC_shippingCountry.value == "#")
	   updatePostage( document.purchaseForm.country.value );
	else
	   updatePostage( document.purchaseForm.MC_shippingCountry.value );

	softwarePrice = document.purchaseForm.M_softwarePrice.value; //.substring(1);
	deliveryPrice = document.purchaseForm.M_deliveryPrice.value; //.substring(1);

	document.purchaseForm.amountWithSymbol.value = currencySymbol(document.purchaseForm.currency.value) + roundOff( eval( softwarePrice + "+" + deliveryPrice ) ) ;
	document.purchaseForm.amount.value = roundOff( eval( softwarePrice + "+" + deliveryPrice ) ) ;

}

function SymError()
{
  return true;
}



function cart()
{

	now = new Date();

	y2k = new Date("Jan 01 2010 00:00:00");

	cartv = (y2k - now) / 1000;

	document.purchaseForm.cartId.value = cartv

}

function trim(s)
{
  // Remove leading spaces and carriage returns

  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}



function run2()
{


    document.purchaseForm.name.value = document.purchaseForm.title.value + " " + document.purchaseForm.firstname.value + " " + document.purchaseForm.surname.value;

	// if shipping address has not been completed, fill in values from billing address

	if ( trim( document.purchaseForm.MC_shippingName.value ) == "" )
       document.purchaseForm.MC_shippingName.value=document.purchaseForm.name.value;

	if ( trim( document.purchaseForm.MC_shippingAddress.value ) == "" )
       document.purchaseForm.MC_shippingAddress.value=document.purchaseForm.address.value;

  	if ( trim( document.purchaseForm.MC_shippingPostcode.value ) == "" )
       document.purchaseForm.MC_shippingPostcode.value=document.purchaseForm.postcode.value;

  	if ( trim( document.purchaseForm.MC_shippingCountry.value ) == "#" )
       document.purchaseForm.MC_shippingCountry.value=document.purchaseForm.country.value;

	if ( trim( document.purchaseForm.MC_shippingTel.value ) == "" )
       document.purchaseForm.MC_shippingTel.value=document.purchaseForm.tel.value;


 
	cart();

}


// This validates the details input by the customer. This is where you may wish to include more sophisticated checking.

function validateForm(x)
{

	var e = "";

	var r = true;



	if(trim(x.firstname.value) == "")
	{

		e += "\nPlease enter a first name";

		r = false;

	}

    if(trim(x.surname.value) == "")
	{

		e += "\nPlease enter a surname";

		r = false;

	}

	if(trim(x.address.value) == "")
	{

		e += "\nPlease enter a billing address";

		r = false;

	}

	if(trim(x.postcode.value) == "")
	{

		e += "\nPlease enter a postcode or ZIP code";

		r = false;

	}

	if(x.tel.value == "")
	{

		e += "\nPlease enter a telephone number";

		r = false;

	}



	if(x.email.value == "" )
	{

		e += "\nPlease enter an email address";

		r = false;

	} else // validate email
	{
    	andPos =  x.email.value.indexOf("@");
		dotPos = x.email.value.lastIndexOf( "." );

		if (andPos==-1 || dotPos==-1 || dotPos<andPos)
		{
		e += "\nPlease enter a valid email address";
		r = false;
		};


	}




	if(r == false)
	{

		alert("The following must be completed:\n"+e);

	} else
	{
	  run2();
	}


	return r;

}

function switchpage(select) {
// JavaScript function for switching to web page when user
// selects option from the given list box.
// Copyright Kelly Yancey, 1997, 1998.
  var index;

  for(index=0; index<select.options.length; index++)
    if(select.options[index].selected)
      {
        if(select.options[index].value!="")
          window.location.href=select.options[index].value;
        break;
      }
}



