/*
	Title: OzArtist 2008 JavaScript
	Author: James Hartcher
	Created: 4/10/2008
	
	Copyright © 2008 Internet Design Studios Pty Ltd, All Rights Reserved
	www.idstudios.com.au
*/


// Change Thumbnail (Voting)
function ChangeThumbnail(artist) {
	document.getElementById('ArtistThumbnail').src = 'images/artists/thumbnails/60x60/' + artist.replace(/ /g,'') + '.jpg';
}


// Check Vote (Entry Form)
function CheckVote() {

	// Reset Error Checking
	var Errors = '';

	// E-Mail Address
	if (CheckEmail(document.getElementById('EMail').value) == false) {
		Errors = Errors + '  - E-Mail Address\n';
	}

	// Check Name
	if (document.getElementById('FirstName').value == '') {
		Errors = Errors + '  - First Name\n';
	}
	if (document.getElementById('LastName').value == '') {
		Errors = Errors + '  - Last Name\n';
	}

	// Check Number
	if (CheckPhoneNumber('ContactNumber') == false) {
		Errors = Errors + '  - Contact Number\n';
	}
	
	// Check DOB
	if (CheckDate('DOB') == false) {
		Errors = Errors + '  - Date of Birth\n';
	}

	// Check Address
	if (document.getElementById('Street').value == '') {
		Errors = Errors + '  - Street Address\n';
	}
	if (document.getElementById('Suburb').value == '') {
		Errors = Errors + '  - Suburb\n';
	}
	if (isNaN(parseInt(document.getElementById('PostCode').value)) == true) {
		Errors = Errors + '  - Post Code\n';
	}
	
	// Check Terms & Conditions
	if (document.getElementById('AgreeLegal').checked == false) {
		Errors = Errors + '  - Legal Stuff\n';
	}

	// Did the entry form contain errors?
	if (Errors > '') {
		alert('Sorry, you didn\'t specify all the required fields correctly:\n' + Errors + '\nPlease check your details & try again.');	
		return false;
	}
	
	return true;
	
}


// Check E-Mail (Method)
function CheckEmail(email) {
		
	// Check E-Mail is Valid
    if (email.search(/^[^@]+@[^@]+.[a-z]{2,}$/i) == -1) {
      	return false;
    }
	
	return true;
	
}


// Check Date (Method)
function CheckDate(field) {
	
	// Get Values
	var Day 	= document.getElementById(field + '_day').value;
   	var Month 	= document.getElementById(field + '_month').value-1;
   	var Year 	= document.getElementById(field + '_year').value;

	// Create Date
	FormDate = new Date(Year,Month,Day);
	
	// Check Date is Valid
	if (Year != FormDate.getFullYear() || Month != FormDate.getMonth() || Day != FormDate.getDate()) {
		return false;
	}

	return true;
}


// Check Phone Number (Method)
function CheckPhoneNumber(field) {	

	// Declare Variables & Get Values
	var PhoneNumber		= document.getElementById(field).value;
	var ValidCharacters	= '0123456789';
	var CleanNumber		= '';

	// Clean String (Remove Invalid Characters)
	for (i=0; i<PhoneNumber.length; i++) {
		if (ValidCharacters.indexOf(PhoneNumber.charAt(i)) > -1) {
			CleanNumber += PhoneNumber.charAt(i);
		}
	}
	
	// Check Length
	if (CleanNumber.length < 8) {
		return false;
	}

	return true;

}
