function validateContact ( ) {
	
	var title = document.getElementById ( 'title' ) ;
	var firstName = document.getElementById ( 'firstName' ) ;
	var lastName = document.getElementById ( 'lastName' ) ;
	var company = document.getElementById ( 'company' ) ;
	var jobTitle = document.getElementById ( 'jobTitle' ) ;
	var email = document.getElementById ( 'email' ) ;
	var telWork = document.getElementById ( 'telWork' ) ;
	
	var pass1 = document.getElementById ( 'password' ) ;
	var pass2 = document.getElementById ( 'password2' ) ;

//	var contactPerson = document.getElementById ( 'contactPerson' ) ;
	
	var error = "" ;
	
	if ( pass1 != undefined ) {
		if ( pass1.value != "" ) {
			if ( pass1.value != pass2.value ) error += "New passwords don't match\n" ;
		}
	}
	
	if ( title.value == "" ) error += "Title\n" ;
	if ( firstName.value == "" ) error += "First name\n" ;
	if ( lastName.value == "" ) error += "Last name\n" ;
	if ( company.value == "" ) error += "Company\n" ;

//	if ( contactPerson != undefined ) {
//		if ( contactPerson.value == "" ) error += "SAB Miller contact\n" ;
//	}

	if ( jobTitle.value == "" ) error += "Job title\n" ;
	if ( email.value == "" ) error += "Email address\n" ;
	else {
		if ( ! validateEmail ( email.value ) ) error += "Incorrect Email address\n" ;
	}
	if ( telWork.value == "" ) error += "Business number\n" ;
	
	if ( error != "" ) {
		var errorStr = "Please complete the following fields:\n" ;
		alert ( errorStr + error ) ;
	} else {
		var form = document.getElementById ( "formContact" ) ;
		form.submit ( ) ;
	}
}

function validateEmail ( str ) {
	if ( str.indexOf ( "@" ) == -1 ) return false ;
	if ( str.indexOf ( "@" ) == -1 || str.indexOf ( "@" ) == 0 || str.indexOf ( "@" ) == str.length ) return false ;
	if ( str.indexOf ( "." ) == -1 || str.indexOf ( "." ) == 0 || str.indexOf ( "." ) == str.length ) return false ;
	if ( str.indexOf ("@" , ( str.indexOf ( "@" ) + 1 ) ) != -1 ) return false ;
	if ( str.substring ( str.indexOf ( "@" ) -1 , str.indexOf ( "@" ) ) == "." || str.substring ( str.indexOf ( "@" ) + 1 , str.indexOf ( "@" ) + 2 ) == "." ) return false ;
	if ( str.indexOf ( "." , ( str.indexOf ( "@" ) + 2 ) ) == -1 ) return false ;
	if ( str.indexOf ( " " ) != -1 ) return false ;
	return true ;
}

function validateAsset ( ) {
	
	var title = document.getElementById ( "addTitle" ) ;
	var keywords = document.getElementById ( "addTags" ) ;
	
	var error = "" ;
	
	if ( title.value == "" ) error += "Title\n" ;
	if ( keywords.value == "" ) error += "Keywords\n" ;
	
	if ( error != "" ) {
		var errorStr = "Please complete the following fields:\n" ;
		alert ( errorStr + error ) ;
	} else {
		var form = document.getElementById ( "formAddAsset" ) ;
		form.submit ( ) ;
	}
}

function validateImportantDate ( isEdit ) {
	
	var error = new String ( ) ;

	var day = document.getElementById ( "dateDay" ).value ;
	var month = document.getElementById ( "dateMonth" ).value ;
	var year = document.getElementById ( "dateYear" ).value ;
	
	var now = new Date ( ) ;
	var importantDate = new Date ( ) ;
	
	importantDate.setDate ( ( day == "" || day == "DD" ? now.getDate ( ) : day ) ) ;
	importantDate.setMonth ( ( month == "" || month == "MM" ? now.getMonth ( ) : month - 1 ) ) ;
	importantDate.setFullYear ( ( year == "" || year == "YY" ? now.getFullYear ( ) : "20" + year ) ) ;
	
	day = importantDate.getDate ( ) ;
	month = importantDate.getMonth ( ) ;
	year = importantDate.getFullYear ( ) ;

	if ( importantDate.getTime ( ) < now.getTime ( ) && ! isEdit ) error += "The date you entered is older than today's date\n" ;
	
	var title = document.getElementById ( "title" ).value ;
	var abstract = document.getElementById ( "abstract" ).value ;
	
	if ( title == "" ) error += "Title is a required field\n" ;
	if ( abstract == "" ) error += "Abstract is a required field\n" ;
	
	if ( error != "" ) {
		var errorStr = "The form contains the following errors:\n\n" ;
		errorStr += error ;
		alert ( errorStr ) ;
		return false ;
	}

	var form = document.getElementById ( "dateForm" ) ;
	form.submit ( ) ;
}

function confirmAssetRemoval ( asset , redirect ) {
	
	var confStr = "Are you sure you want to delete \"" + asset + "\"?" ;
	if ( confirm ( confStr ) ) {
		if ( document.all ) {
			document.location = redirect ;
		} else {
			location.href = redirect ;
		}
	} else {
		return ;
	}
}

function confirmDateRemoval ( date ) {

	var confStr = "Are you sure you want to delete \"" + date + "\"?" ;
	if ( confirm ( confStr ) ) return true ;
	return false ;
}

function getDownloads ( ) {
	
	var confStr = "Your assets are ready to be downloaded.\n\nWould you like to download them and empty your basket?" ;
	var basketForm = document.getElementById ( 'basketForm' ) ;
	
	if ( confirm ( confStr ) ) {
		window.open ( basketForm.action , "basket" , "height=100,width=100" ) ;
	} else {
		return ;
	}
}
/*
function getDownloads ( ) {
	
	var confStr = "Your assets are ready to be downloaded.\n\nWould you like to download them and empty your basket?" ;
	var basketForm = document.getElementById ( 'basketForm' ) ;
	
	if ( confirm ( confStr ) ) {
		window.open ( basketForm.action , "basket" , "height=150,width=300" ) ;
		location.href = location.href ;
	} else {
		return ;
	}
}*/

$('document').ready( function () {
	$('input, select').css('background-color','white');
});

