// JavaScript Document
var menu = new Array();

function more( id, calling )
{
	var elem = getElem( 'item_' + id, null );
	elem.style.height = '275px';
	calling.innerHTML = 'Less';
	calling.onclick = function ()
						{
							less( id, this);
						}
}

function less( id, calling )
{
	var elem = getElem( 'item_' + id, null );
	elem.style.height = '25px';
	calling.innerHTML = 'More';
	calling.onclick = function ()
						{
							more( id, this);
						}
}

function getElem( elem, prop )
{
	var x = null;
	var ret = null;
	if (document.getElementById)
	{
		// this is the way the standards work
		x = document.getElementById(elem);
	}
	if (document.all)
	{
		// this is the way old msie versions work
		x = document.all[elem];
	}
	if( prop != null )
	{
		
		eval('ret=x.' + prop);
		return ret;
	}
	else
	{
		return x;
	}
}

function getXMLObject(xml)
{
	var xmlObj;
	if (window.ActiveXObject)
	{
		xmlObj = new ActiveXObject("Microsoft.XMLDOM");
		xmlObj.async = "false";
		xmlObj.loadXML(xml);
	}
	// code for Mozilla, Firefox, Opera, etc.
	else
	{
		var parser = new DOMParser();
		xmlObj = parser.parseFromString(xml, "text/xml");
	}
	return xmlObj;
}

function validate()
{
	var fname = getElem( 'first_name', null );
	var lname = getElem( 'last_name', null );
	var address = getElem( 'address', null );
	var city = getElem( 'city', null );
	var province = getElem( 'province', null );
	var country = getElem( 'country', null );
	var postal_code = getElem( 'postal_code', null );
	var phone_number = getElem( 'phone_number', null );
	var email = getElem( 'email', null );
	var same = getElem( 'same', null );
	var shipping_fname = getElem( 'shipping_fname', null );
	var shipping_lname = getElem( 'shipping_lname', null );
	var shipping_address = getElem( 'shipping_address', null );
	var shipping_city = getElem( 'shipping_city', null );
	var shipping_province = getElem( 'shipping_province', null );
	var shipping_country = getElem( 'shipping_country', null );
	var shipping_postal_code = getElem( 'shipping_postal_code', null );
	var shipping_number = getElem( 'shipping_number', null );
	var shipping_email = getElem( 'shipping_email', null );

	var error = false;

	if( Trim( fname.value ) == '' )
	{
		fname.style.backgroundColor = '#cc0000';
		error = true;
	}
	else
	{
		fname.style.backgroundColor = '#ffffff';
	}
	if( Trim( lname.value ) == '' )
	{
		lname.style.backgroundColor = '#cc0000';
		error = true;
	}
	else
	{
		address.style.backgroundColor = '#ffffff';
	}
	if( Trim( lname.value ) == '' )
	{
		address.style.backgroundColor = '#cc0000';
		error = true;
	}
	else
	{
		address.style.backgroundColor = '#ffffff';
	}
	if( Trim( city.value ) == '' )
	{
		city.style.backgroundColor = '#cc0000';
		error = true;
	}
	else
	{
		city.style.backgroundColor = '#ffffff';
	}
	if( Trim( province.value ) == '' )
	{
		province.style.backgroundColor = '#cc0000';
		error = true;
	}
	else
	{
		province.style.backgroundColor = '#ffffff';
	}
	if( Trim( country.value ) == '' )
	{
		country.style.backgroundColor = '#cc0000';
		error = true;
	}
	else
	{
		country.style.backgroundColor = '#ffffff';
	}
	if( validPostal( postal_code.value ) == false || Trim( postal_code.value ) == '' )
	{
		postal_code.style.backgroundColor = '#cc0000';
		
		error = true;
	}
	else
	{
		postal_code.style.backgroundColor = '#ffffff';
	}
	if( validateNumber( phone_number.value ) == false || Trim( phone_number.value ) == '' )
	{
		phone_number.style.backgroundColor = '#cc0000';
		error = true;
	}
	else
	{
		phone_number.style.backgroundColor = '#ffffff';
	}
	if( validateEmail( email.value ) == false || Trim( email.value ) == '' )
	{
		email.style.backgroundColor = '#cc0000';
		error = true;
	}
	else
	{
		email.style.backgroundColor = '#ffffff';
	}
	if( same.checked )
	{
		shipping_fname.value = fname.value;
		shipping_lname.value = lname.value;
		shipping_address.value = address.value;
		shipping_city.value = city.value;
		shipping_province.value = province.value;
		shipping_country.value = country.value;
		shipping_postal_code.value = postal_code.value;
		shipping_number.value = phone_number.value;
		shipping_email.value = email.value;
		
		shipping_fname.style.backgroundColor = '#ffffff';
		shipping_lname.style.backgroundColor = '#ffffff';
		shipping_address.style.backgroundColor = '#ffffff';
		shipping_city.style.backgroundColor = '#ffffff';
		shipping_province.style.backgroundColor = '#ffffff';
		shipping_country.style.backgroundColor = '#ffffff';
		shipping_postal_code.style.backgroundColor = '#ffffff';
		shipping_number.style.backgroundColor = '#ffffff';
		shipping_email.style.backgroundColor = '#ffffff';
	}
	else
	{
		if( Trim( shipping_fname.value ) == '' )
		{
			shipping_fname.style.backgroundColor = '#cc0000';
			error = true;
		}
		else
		{
			shipping_fname.style.backgroundColor = '#ffffff';
		}
		if( Trim( shipping_lname.value ) == '' )
		{
			shipping_lname.style.backgroundColor = '#cc0000';
			error = true;
		}
		else
		{
			shipping_lname.style.backgroundColor = '#ffffff';
		}
		if( Trim( shipping_address.value ) == '' )
		{
			shipping_address.style.backgroundColor = '#cc0000';
			error = true;
		}
		else
		{
			shipping_address.style.backgroundColor = '#ffffff';
		}
		if( Trim( shipping_city.value ) == '' )
		{
			shipping_city.style.backgroundColor = '#cc0000';
			error = true;
		}
		else
		{
			shipping_city.style.backgroundColor = '#ffffff';
		}
		if( Trim( province.value ) == '' )
		{
			shipping_province.style.backgroundColor = '#cc0000';
			error = true;
		}
		else
		{
			shipping_province.style.backgroundColor = '#ffffff';
		}
		if( Trim( shipping_country.value ) == '' )
		{
			shipping_country.style.backgroundColor = '#cc0000';
			error = true;
		}
		else
		{
			shipping_country.style.backgroundColor = '#ffffff';
		}
		if( validPostal( shipping_postal_code.value ) == false || Trim( pshipping_ostal_code.value ) == '' )
		{
			shipping_postal_code.style.backgroundColor = '#cc0000';
			error = true;
		}
		else
		{
			shipping_postal_code.style.backgroundColor = '#ffffff';
		}
		if( validateNumber( shipping_number.value ) == false || Trim( shipping_number.value ) == '' )
		{
			shipping_number.style.backgroundColor = '#cc0000';
			error = true;
		}
		else
		{
			shipping_phone_number.style.backgroundColor = '#ffffff';
		}
		if( validateEmail( shipping_email.value ) == false || Trim( shipping_email.value ) == '' )
		{
			shipping_email.style.backgroundColor = '#cc0000';
			error = true;
		}
		else
		{
			shipping_email.style.backgroundColor = '#ffffff';
		}
	}
	
	if( error )
	{
		var msg = getElem('message', null);
		msg.style.textAlign = 'center';
		msg.style.color = '#cc0000';
		msg.innerHTML = 'Please fix the highlighted fields before continuing!';
	}
	else
	{
		document.forms[1].submit();
	}
}

function createElem( id, type )
{
	var d = document.createElement(type);
	d.setAttribute( 'id', id);
	return d;
}

function remElem( elem, container )
{
	var rem = getElem( elem );
	container.removeChild( rem);
}

/******************/
/* TRIM FUNCTIONS */
/******************/
function Trim( s )
{
	s = LTrim(s);
	return RTrim(s);
}

function RTrim(s)
{
	while( s.charAt( ( s.length - 1 ) ) == " " )
	{
		s = s.substring( 0, s.length - 1 );
	}
	return s;
}


function LTrim( s )
{
	while( s.charAt( 0 ) == " " )
	{
		s = s.replace( s.charAt(0), "" );
	}
	return s;
}
/**********************/
/* END TRIM FUNCTIONS */
/**********************/
function validPostal(zip)
{
	if (zip.match(/^[0-9]{5}$/))
	{
		return true;
	}
	zip=zip.toUpperCase();
	if (zip.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/))
	{
		return true;
	}
	if (zip.match(/^[A-Z][0-9][A-Z]-[0-9][A-Z][0-9]$/))
	{
		return true;
	}
	if (zip.match(/^[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$/))
	{
		return true;
	}
	return false;
}

function validateEmail(strEmail)
{
	AtPos = strEmail.indexOf("@");
	StopPos = strEmail.lastIndexOf(".");
	Message = true;

	if (strEmail == "") 
	{
		Message = false;
	}
	if (AtPos == -1 || StopPos == -1) 
	{
		Message = false;
	}
	if (StopPos < AtPos) 
	{
		Message = false;
	}
	if (StopPos - AtPos == 1)
	{
		Message = false;
	}	
	return Message;
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 7;
var Phone;

function validateNumber( number )
{
	if( ( number == null ) || ( Trim( number ) == "") )
	{
		return false;
	}
	if( checkInternationalPhone( number ) == false )
	{
		return false;
	}
	return true;
}

function isInteger(s)
{   var i;
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag)
{   var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character isn't whitespace.
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function checkInternationalPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}







/*
function slide_open(id)
{
	var elem = getElem( id );
	elem.style.display = 'block';
	elem.previousSibling.attributes["onclick"].value = "close_slide('" + id + "')";
}

function close_slide( id )
{
	var elem = getElem( id );
	elem.style.display = 'none';
	elem.previousSibling.attributes["onclick"].value = "slide_open('" + id + "')";
}

*/