function checkCustomerInfo(){
	var errorList = '';

	errorList = checkField('ContactFirstName', 'NOTNULL', 'First Name', errorList);
	errorList = checkField('ContactLastName', 'NOTNULL', 'Last Name', errorList);
	errorList = checkField('Address1', 'NOTNULL', 'Address Line 1', errorList);
	errorList = checkField('City', 'NOTNULL', 'City', errorList);
	errorList = checkField('CountryCode', 'NOTFIRST', 'Country', errorList);
	errorList = checkField('ProvStateID', 'NOTFIRST', 'Province/State', errorList);
	errorList = checkField('PostalCode', 'NOTNULL', 'Postal/Zip Code', errorList);

	errorList = checkField('EmailAddress', 'NOTNULL', 'Email', errorList);
	errorList = checkField('confirmEmail', 'NOTNULL', 'Confirm Email', errorList);

	errorList = checkField('Password', 'NOTNULL', 'Password', errorList);
	errorList = checkField('confirmPassword', 'NOTNULL', 'Confirm Password', errorList);
	
	if(getFieldValue('EmailAddress') != getFieldValue('confirmEmail')){
		errorList += 'The Email and Confirm Email fields must match\n';
	}
	
	if(getFieldValue('Password') != getFieldValue('confirmPassword')){
		errorList += 'The Password and Confirm Password fields must match\n';
	}
	
	if(errorList.length > 0){
		alert(errorList);
		return false;
	}
	
	if(confirm('Are you sure this information is correct?')){
		return true;
	}
	else{
		return false;
	}
}


function checkBillingInfo(){
	var errorList = '';

	errorList = checkField('ContactFirstName', 'NOTNULL', 'First Name', errorList);
	errorList = checkField('ContactLastName', 'NOTNULL', 'Last Name', errorList);
	errorList = checkField('Address1', 'NOTNULL', 'Address Line 1', errorList);
	errorList = checkField('City', 'NOTNULL', 'City', errorList);
	errorList = checkField('CountryCode', 'NOTFIRST', 'Country', errorList);
	errorList = checkField('ProvStateID', 'NOTFIRST', 'Province/State', errorList);
	errorList = checkField('PostalCode', 'NOTNULL', 'Postal/Zip Code', errorList);

	errorList = checkField('EmailAddress', 'NOTNULL', 'Email', errorList);
	errorList = checkField('confirmEmail', 'NOTNULL', 'Confirm Email', errorList);

	errorList = checkField('Password', 'NOTNULL', 'Password', errorList);
	errorList = checkField('confirmPassword', 'NOTNULL', 'Confirm Password', errorList);
	
	if(getFieldValue('EmailAddress') != getFieldValue('confirmEmail')){
		errorList += 'The Email and Confirm Email fields must match\n';
	}
	
	if(getFieldValue('Password') != getFieldValue('confirmPassword')){
		errorList += 'The Password and Confirm Password fields must match\n';
	}
	
	if(errorList.length > 0){
		alert(errorList);
		return false;
	}
	
	if(confirm('Are you sure this billing information is correct?')){
		return true;
	}
	else{
		return false;
	}
}

function checkSubscriptions(rowsToCheck){
	var errorList = '';
	var errorThreshold = 12;
	
	for(var row=1; row <= rowsToCheck; row++){
		errorList = checkField('SubscriptionTypeID' + row, 'NOTNULL', 'Subscription Type on subscription ' + row, errorList);

		errorList = checkField('', 'CHECKONE(eJournalYes' + row + ';eJournalNo' + row + ')', 'eJournal subscription ' + row, errorList);
		
		if(getCheckedState('eJournalYes' + row)){
			if(getCheckedState('eJOTLoginRegular' + row)){
				errorList = checkField('eJOTEmail' + row, 'NOTNULL', 'eJournal Email Address on subscription ' + row, errorList);
				errorList = checkField('eJOTPassword' + row, 'NOTNULL', 'eJournal Password on subscription ' + row, errorList);
			}
			else if(getCheckedState('eJOTLoginIP' + row)){
				errorList = checkField('eJOTIPRangeStart' + row, 'NOTNULL', 'eJournal IP Range Start on subscription ' + row, errorList);
				
				var ipStart = getFieldValue('eJOTIPRangeStart' + row);
				var ipEnd = getFieldValue('eJOTIPRangeEnd' + row);
				
				//Validate Start Address
				if(!validIPAddress(ipStart)){
					errorList += "IP Range Start on row " + row + " is not a valid address.\n";
				}
				
				//Validate End Address only if one is provided
				if(!(ipEnd.length == 0 || validIPAddress(ipEnd))){
					errorList += "IP Range End on row " + row + " is not a valid address.\n";
				}
			}
			else{
				errorList += "You must select a Login Method for your eJournal on Subscription " + row + "\n";
			}
		}
		
		errorList = checkField('Copies' + row, 'NOTNULL,NUMERIC,MIN(1)', 'Number of Copies on subscription ' + row, errorList);
		errorList = checkField('ContactFirstName' + row, 'NOTNULL', 'First Name on subscription ' + row, errorList);
		errorList = checkField('ContactLastName' + row, 'NOTNULL', 'Last Name on subscription ' + row, errorList);
		errorList = checkField('Address1' + row, 'NOTNULL', 'Address Line 1 on subscription ' + row, errorList);
		errorList = checkField('City' + row, 'NOTNULL', 'City on subscription ' + row, errorList);
		errorList = checkField('CountryCode' + row, 'NOTFIRST', 'Country on subscription ' + row, errorList);
		errorList = checkField('ProvStateID' + row, 'NOTFIRST', 'Province/State on subscription ' + row, errorList);
		errorList = checkField('PostalCode' + row, 'NOTNULL', 'Postal/Zip Code on subscription ' + row, errorList);
		
		if(errorList.split('\n').length > errorThreshold){
			errorList += '\n{TOO MANY ERRORS - TRUNCATING LIST}';
			break;
		}
	}
	
	if(errorList.length > 0){
		alert(errorList);
		return false;
	}
	
	if(confirm('Are you sure you want to submit this information for you subscription(s)?')){
		setFieldValue('ProcessOrder','True');
		return true;
	}
	else{
		return false;
	}
}


function toggleSameAsBilling(buttonName){
	toggleButton(buttonName);
	checkSameAsBilling(buttonName);
	SubscriptionCountryChange(1);
}

function checkSameAsBilling(buttonName){
	var checkedState = getCheckedState(buttonName);
	var CountryCode, ProvStateID;
	
	if(checkedState != null){
		if(checkedState){
			setAddressField('ContactFirstName1', getFieldValue('ContactFirstName'), true);
			setAddressField('ContactLastName1', getFieldValue('ContactLastName'), true);
			setAddressField('CompanyName1', getFieldValue('CompanyName'), true);

			setAddressField('eJOTEmail1', getFieldValue('EmailAddress'), true);
			setAddressField('eJOTPassword1', getFieldValue('Password'), true);

			setAddressField('DepartmentName1', getFieldValue('DepartmentName'), true);
			setAddressField('ContactTitle1', getFieldValue('ContactTitle'), true);
			setAddressField('Address11', getFieldValue('Address1'), true);
			setAddressField('Address21', getFieldValue('Address2'), true);
			setAddressField('City1', getFieldValue('City'), true);
			
			setAddressField('CountryCode1', getFieldValue('CountryCode'), true);
			CountryChange(1);
			
			setAddressField('ProvStateID1', getFieldValue('ProvStateID'), true);
			setAddressField('PostalCode1', getFieldValue('PostalCode'), true);
			
			new Element('CountryName').style.display="inline";
			new Element('CountryCode1').style.display="none";

			new Element('ProvStateName').style.display="inline";
			new Element('ProvStateID1').style.display="none";
		}
		else{
			setAddressField('ContactFirstName1', getDefaultFieldValue('ContactFirstName1'), false);
			setAddressField('ContactLastName1', getDefaultFieldValue('ContactLastName1'), false);

			setAddressField('eJOTEmail1', getDefaultFieldValue('eJOTEmail1'), false);
			setAddressField('eJOTPassword1', getDefaultFieldValue('eJOTPassword1'), false);

			setAddressField('CompanyName1', getDefaultFieldValue('CompanyName1'), false);
			setAddressField('DepartmentName1', getDefaultFieldValue('DepartmentName1'), false);
			setAddressField('ContactTitle1', getDefaultFieldValue('ContactTitle1'), false);
			setAddressField('Address11', getDefaultFieldValue('Address11'), false);
			setAddressField('Address21', getDefaultFieldValue('Address21'), false);
			setAddressField('City1', getDefaultFieldValue('City1'), false);
			
			CountryCode = getDefaultFieldValue('CountryCode1');
			ProvStateID = getDefaultFieldValue('ProvStateID1');
			
			setAddressField('CountryCode1', CountryCode, false);
			CountryChange(1);
			
			setAddressField('ProvStateID1', ProvStateID, false);
			setAddressField('PostalCode1', getDefaultFieldValue('PostalCode1'), false);
			
			new Element('CountryName').style.display="none";
			new Element('CountryCode1').style.display="inline";

			new Element('ProvStateName').style.display="none";
			new Element('ProvStateID1').style.display="inline";
		}
	}
}

function setAddressField(fieldName, strValue, readonly){
	setReadOnly(fieldName, readonly);
	if(getFieldType(fieldName) == 'select-one' && strValue == null){
		var field = new Element(fieldName);
		field.obj.options[0].selected = true;
		field = null;
	}
	else{
		setFieldValue(fieldName, strValue);
	}
	
	setBackgroundColor(fieldName, (readonly) ? '#EEEEEE' : '#FFFFFF');
}

function CountryChange(row){
	var field, initialValue, country;
	var text, value, defaultSelected, selected;
	var counter = 0;
	
	country = getFieldValue('CountryCode' + row);
	initialValue = getFieldValue('ProvStateID' + row);
	clearSelectBox('ProvStateID' + row);
	
	field = new Element('ProvStateID' + row);
	Provinces.moveFirst();
	
	field.obj.options[0] = new Option('-- Select a Province/State --', '', false, false);
	while(!Provinces.EOF){
		if(country == Provinces.get('ProvCountryCode') || Provinces.get('ProvCountryCode') == ''){
			text = Provinces.get('ProvStateName');
			value = Provinces.get('ProvStateID');
			defaultSelected = (value == initialValue);
			selected = defaultSelected;

			field.obj.options[field.obj.options.length] = new Option(text, value, defaultSelected, selected);
		}
		Provinces.moveNext();
	}
	field = null;
}

function SubscriptionCountryChange(row){
	var country = '', SubscriptionType = '';
	var geo_region = '', cost = 0, value = '';
	var counter = 0;
	var eJournal = false;
	var duration = -1;
	
	if(getCheckedState('Regular' + row)){
		SubscriptionType = 'R';
	}
	
	if(getCheckedState('Library' + row)){
		SubscriptionType = 'L';
	}
	
	if(getCheckedState('Single' + row)){
		duration = 0;
	}
	else if(getCheckedState('Year' + row)){
		duration = 9;
	}
	else if(getCheckedState('TwoYear' + row)){
		duration = 21;
	}
	
	if(getCheckedState('eJournalYes' + row)){
		eJournal = true;
	}
	
	country = getFieldValue('CountryCode' + row);
	
	if(country != '' && SubscriptionType != ''){
		if(NORTH_AMERICA.search(eval('/' + country + '/gi')) != -1){
			geo_region = "NA";
		}
		else{
			geo_region = "I";
		}

		Subscriptions.moveFirst();
		
		while(!Subscriptions.EOF){
			//ID Name Cost MonthsTillExpiry Type Medium Geography NonSub
			var msg = "Type: " + SubscriptionType + " " + (SubscriptionType == Subscriptions.get('Type')) + "\n";
			msg += "Geo_Region: " + geo_region + "=" + Subscriptions.get('Geography') + " " + (geo_region == Subscriptions.get('Geography')) + "\n";
			msg += "Duration: " + duration + " " + (duration == Subscriptions.get('MonthsTillExpiry')) + "\n";
			msg += "Medium: " + (Subscriptions.get('Medium') + " eJOT: " + eJournal) + "\n";
			
			//alert(msg);
			
			if(SubscriptionType == Subscriptions.get('Type') && (geo_region == Subscriptions.get('Geography') || '' == Subscriptions.get('Geography')) && duration == Subscriptions.get('MonthsTillExpiry')){
				
				if((Subscriptions.get('Medium') == "Print" || eJournal) && duration == Subscriptions.get('MonthsTillExpiry')) {
					cost += Subscriptions.get('Cost');
					value += Subscriptions.get('ID') + ',';
				}
			}
			Subscriptions.moveNext();
		}
		
		cost = '$' + cost.toFixed(2);
		value = value.substr(0, value.length - 1);
		
		setFieldValue('SubscriptionCost' + row, cost);
		setFieldValue('SubscriptionTypeID' + row, value);
	}
	else{
		setFieldValue('SubscriptionCost' + row, cost);
		setFieldValue('SubscriptionTypeID' + row, value);
	}
}


function calculateSubscriptions(rowsToCheck){
	for(var row=1; row <= rowsToCheck; row++){
		SubscriptionCountryChange(row);
	}
}


function ProvinceSubscriptionReload(row){
	SubscriptionCountryChange(row);
	CountryChange(row);
}


function checkInvoice(){
	var errorList = '';
	
	errorList = checkField('invContact', 'NOTNULL', 'Contact Name', errorList);
	errorList = checkField('invAddress1', 'NOTNULL', 'Address Line 1', errorList);
	errorList = checkField('invCity', 'NOTNULL', 'City', errorList);
	errorList = checkField('CountryCode', 'NOTFIRST', 'Country', errorList);
	errorList = checkField('ProvStateID', 'NOTFIRST', 'Province/State', errorList);
	errorList = checkField('invPostalCode', 'NOTNULL', 'Postal/Zip Code', errorList);
	errorList = checkField('invPhoneNum', 'NOTNULL', 'Phone Number', errorList);
	errorList = checkField('invEmail', 'NOTNULL', 'Email Address', errorList);
	
	if(errorList.length > 0){
		alert(errorList);
		return false;
	}
	else{
		return confirm('Are you sure this information is correct?');
	}
}
