var formInUse = false;

function brRemove(thing){//This converts <br>. 
	var newThing = thing.replace(/<br>/g, '');//g is the global property that will replace all the occurances of <br>.
	return newThing;
}

function charConvert(thing){//This converts encoded new line characters back to new lines. This should be used on data after ajax has returned it. The new line data needs to be converted to %nl% in php before being handed over to ajax.
	thing = thing.replace(/%nl%/g, "\n");//g is the global property that will replace all the occurances of %nl%.
	thing = thing.replace(/%cr%/g, "\r");
	thing = thing.replace(/%dq%/g, '"');
	thing = thing.replace(/%sq%/g, "'");
	return thing;
}

function cookieDomain(){//Sets the cookie domain depending on local or remote server.
	return (document.location.href.indexOf('localhost') || document.location.href.indexOf('8888')) ?  '' : '.markproaudio.com';
}

function createEmailAddress(address1, address2, address3, subject, message) {
	var x = address1 + '&' + address2 + '*' + address3;
	var y = 'mai';
	var z = 'lto';
	var s = '?subject=' + subject;
	var m = '&body=' + message;
	var n = '';
	var o = '';
	x = x.replace('&','@');
	x = x.replace('*','.');
	x = x.replace('_','');
	x = x.replace('_','');

	var b = y + z +':'+ x + s + m + n + o;
	window.location=b;
}

function deleteCookie(name, path, domain){//Deletes a cookie
	if(getCookie(name)){
		document.cookie = name + "=" + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "") + ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

function emailValidate(email){//DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
	var at = "@";
	var dot = ".";
	var lat = email.indexOf(at);
	var lemail = email.length;
	var ldot = email.indexOf(dot);
	if(email == ''){
		return 'Enter an email address.';
	}else if(email.indexOf(at) == -1){
		return "Missing @ character.";
	}else if(email.indexOf(at) == 0){
		return "Missing characters before @.";
	}else if(email.indexOf(at) == lemail-1){
		return "Missing domain name.";
	}else if(email.indexOf(dot) == -1){
		return "Missing dot character.";
	}else if(email.indexOf(dot) == 0){
		return "Missing characters before dot.";
	}else if(email.indexOf(dot) == lemail-1){
		return "Missing characters after dot.";
	}else if(email.indexOf(at,(lat+1))!=-1){
		return "1.";
	}else if(email.substring(lat-1,lat) == dot || email.substring(lat+1,lat+2) == dot){
		return "Missing characters between @ and dot.";
	}else if(email.indexOf(dot,(lat+2)) == -1){
		return "3.";
	}else if(email.indexOf(" ")!=-1){//Spaces in email?
		return 'spaces in the email.';
	}else{
		return true;
	}
}

function getCookie(name){// this function gets the cookie, if it exists
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if((!start) && (name != document.cookie.substring(0, name.length))){
		return null;
	}
	if(start == -1) return null;
	var end = document.cookie.indexOf(";", len);
	if(end == -1) end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

function passwordValidate(pass){
	var len = pass.length;
	var regEx = /^[A-Za-z\d!@]+$/;
	if(pass == ''){
		return 'Enter your password.'
	}else if(len < 6 || len > 20){
		return 'Password must be between 6 and 20 characters.';
	}else if(!regEx.test(pass)){
		return 'Only a-z 0-9 ! @ are allowed.';
	/*else if(pass.indexOf('[') != -1 || pass.indexOf(']') != -1 || pass.indexOf('{') != -1 || pass.indexOf('}') != -1 || pass.indexOf('\\') != -1 || pass.indexOf('[') != -1 || pass.indexOf('#') != -1 || pass.indexOf('$') != -1 || pass.indexOf('%') != -1 || pass.indexOf('^') != -1 || pass.indexOf('&') != -1 || pass.indexOf('*') != -1 || pass.indexOf('(') != -1 || pass.indexOf(')') != -1){
		return 'Only a-z 0-9 ! and @ are allowed.'*/
	}else{
		return true;
	}
}

function redirect(address){
    window.location = address;
}

function setCookie(name, value, expires, path, domain){
/* Set cookies
Use 'domain' on the Javascript cookie if you are using it on a subdomain, like widgets.yoursite.com, where
the cookie is set on the widgets subdomain, but you need it to be accessible over the whole yoursite.com domain.

It's good practice to not assume the path to the site root will be set the way you want it by default, so do
this manually as a rule, '/'. If no value is set for expires, it will only last as long as the current session
of the visitor, and will be automatically deleted when they close their browser. 

Set the time as milliseconds
*/
	var today = new Date();
	today.setTime(today.getTime());
	if(expires){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date(today.getTime() + (expires));
	document.cookie = name + "=" +escape(value) +
	((expires) ? ";expires=" + expires_date.toGMTString() : "") +
	((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "");
}

function setFocus(formID) {
	if(!formInUse) {
		document.getElementById(formID).focus();
	}
}
