// common.js: JavaScript Document
// validateEmail 
// formatCurrency
// UpdateTotal
// validate


// Email Validation Javascript
// copyright 23rd March 2003, by Stephen Chapman, Felgall Pty Ltd

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.
//
// addr	  Email Address
// man	  Mandatory field - 0 if email address is not mandatory, 1 if it is
// db	  Display messages - 0 if no, 1 if yes
//
function validateEmail(addr,man,db) 
{
	if (addr == '') 
	{
	   	if (db && man) 
	   	{
			alert('email address is mandatory');
	   		return false;
	   	}
	   	else
		{
			//alert('email address is blank but that is ok');
			return true;
		}
	}

	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;
}


/************************************************************
FUNCTION: stringLength()

DESCRIPTION:
Returns the length of the given string.

HISTORY:
27/05/2007 Susanna Hedges	Created

Example:  length = stringLength(myString)
*************************************************************/
function stringLength(sInputString) 
{
	len = 0;
	len = sInputString.length
	return (len);
}



/******************* formatCurrency() Comments *****************
This function is called in store.asp to format the currency with
a $ sign, and 2 decimal places.

Created:	@01/04/2007		Susanna Hedges
Modified:	20/05/2007		Susanna Hedges
	Moved into common javascript module: common.js

Example:  UpdateTotal(parent,this)
*************************************************************/
function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));

	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

/******************* UpdateTotal() Comments *****************
This function is called in store.asp to update the total based
on the user selected number of copies of Integard to purchase.

Created:	@01/04/2007		John Hedges
Modified:	20/05/2007		Susanna Hedges
	Moved into common javascript module: common.js

Example:  UpdateTotal(parent,this)
*************************************************************/
function UpdateTotal(parObj,selObj)
{ 
	Qty  = selObj.options[selObj.selectedIndex].value;
	Qty = Qty*1;
	Price = document.BillingForm.nUnitPrice.value;
    TotalPrice = (Qty * Price);
	
	// Update hidden value actually sumbitted in the form
	SubmitValue = document.getElementsByName('TotalAmount');
	SubmitValue[0].value = TotalPrice;
	 	
	// Update Read-Only Total price displayed to customer
	TotalPriceFull = formatCurrency(TotalPrice);
	TotalPriceFull += " USD";

	elements = document.getElementById('roTotalVal');
    elements.innerHTML = TotalPriceFull;

	//Update the string price which is sent through to verify_transaction.asp on submit
	document.BillingForm.sTotalVal.value = TotalPriceFull;
	document.BillingForm.nQuantity.value = Qty;
}

/******************* validate() Comments *****************
This function is called in store.asp to validate the form
contents.

Created:	@01/05/2007		Susanna Hedges
Modified:	20/05/2007		Susanna Hedges
	Moved into common javascript module: common.js

Example:  validate()
*************************************************************/
function validate()
{
	if (document.BillingForm.firstname.value == "") 
	{ 
		alert("First name is required"); return false; 
	}
	if (document.BillingForm.lastname.value == "") 
	{ 
		alert("Last name is required"); return false; 
	}

	mandatory = "1"
	messages = "1"
	if (!validateEmail(document.BillingForm.emailaddr.value, mandatory, messages))
	{
		return false;
	}
	
	return true;
}
