String.prototype.ltrim = new Function("return this.replace(/^\\s+/,'')")
String.prototype.rtrim = new Function("return this.replace(/\\s+$/,'')")
String.prototype.trim = new Function("return this.replace(/^\\s+|\\s+$/g,'')")
String.prototype.count = function(character) { return this.split(character).length - 1; }

function check_email(email) {
	if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)) {
		return true;
	}

	return false;
}

function clear_text(thefield){
	if(thefield.defaultValue == thefield.value) {
		thefield.value = "";
	}
}

function remove_attachment(title, filename, url) {
	response = confirm('Remove the ' + title + ' \(' + filename + '\) attachment?\n\nIf you click OK, the file will be removed and this page will reload.');
	
	if(response) {
		window.location = url;
	}
	else {
		return false;
	}
}

function remove_entry(date_formatted, entry_id, user_id) {
	response = confirm('Remove the ' + date_formatted + ' entry for this client?\n\nIf you click OK, the entire entry and all associtated file attachments will be removed and this page will reload.');
	
	if(response) {
		window.location = 'admin_client_entry.php?op=deleteentry&id=' + entry_id + '&user_id=' + user_id;
	}
	else {
		return false;
	}
}

function validate_client(theform) {
	if(theform.client_name.value.length == 0 || theform.email.value.length == 0 || theform.username.value.length == 0 || theform.password.value.length == 0) {
		alert("All the fields are required.");
		return false;
	}
	
	return true;
}

function validate_entry(theform) {
	if(theform.entry_date_month.value.length == 0 || theform.entry_date_day.value.length == 0 || theform.entry_date_year.value.length == 0) {
		alert("Please choose a date for this entry.");
		return false;
	}
	
	return true;
}

function validate_email_client(theform) {
	//field is empty
	if(theform.additional.value.trim().length == 0) {
		return true;
	}
	
	var error = "";
	items = theform.additional.value.split(",");
	
	//verify each email
	for(i = 0; i < items.length; i++) {
		if(!check_email(items[i].trim())) {
			error += "- " + items[i].trim() + "\n";
		}
	}
	
	if(error.length > 0) {
		alert("The following email addresses in the to Additional Emails field are invalid:\n\n" + error);
		return false;
	}
	
	return true;
}

function delete_client(title, id) {
	response = confirm('Remove this client?\n\nIf you click OK, ' + title + ' and all associated files will be removed and you will be redirected to the main admin page.');
	
	if(response) {
		window.location = 'admin_client.php?op=delete&id=' + id;
	}
	else {
		return false;
	}
}