
// FUNCTION initFuncs, loads all other event driven functions...
function initFuncs() {
	addBlurs();
	if (window.location == "http://www.onlinewellnessassociation.com/index.php?v=techlinks" | window.location == "https://www.onlinewellnessassociation.com/index.php?v=techlinks" | window.location == "http://onlinewellnessassociation.com/index.php?v=techlinks" | window.location == "https://onlinewellnessassociation.com/index.php?v=techlinks") {
		getLinks();
	}
	if (window.location == "http://www.onlinewellnessassociation.com/index.php?v=articles" | window.location == "https://www.onlinewellnessassociation.com/index.php?v=articles" | window.location == "http://onlinewellnessassociation.com/index.php?v=articles" | window.location == "https://onlinewellnessassociation.com/index.php?v=articles") {
		addSampleLinks();
	}
}

function getLinks() {
	var anchors = document.getElementsByTagName('a');

	for (var i=0;i<anchors.length;i++) {
		if (anchors[i].className == 'jsLink') {
			addEvent(anchors[i],'click',addPop,false);
		}
	}
}

function addSampleLinks() {
	var spans = document.getElementsByTagName('span');
	for (var i=0;i<spans.length;i++) {
		if (spans[i].className == 'sampleLink') {
			spans[i].className = 'sampleLink off';
			addEvent(spans[i],'click',viewSample,false);
		}
	}
}

function viewSample(e) {
	if (!e) var e = window.event;
	var src = (window.event) ? e.srcElement : e.target;
	var sample = src.childNodes[1];
	if (src.className == 'sampleLink off') {
		sample.style.display = 'block';
		src.className = 'sampleLink on';
		src.innerHTML = src.innerHTML.replace('(view sample)','(hide sample)');
	} else {
		sample.style.display = 'none';
		src.className = 'sampleLink off';
		src.innerHTML = src.innerHTML.replace('(hide sample)','(view sample)');		
	}	
}

function addPop(e) {
	openJSLinks(e.currentTarget);
	e.preventDefault();
}
function redirect() {
	if (document.getElementById('loginSuccess')) {
	setTimeout("location.href='https://www.onlinewellnessassociation.com/'",2000);
	}
	if (document.getElementById('loginSuccessCMS')) {
	setTimeout("location.href='https://www.onlinewellnessassociation.com/therapist_directory_cms.php5'",2000);	
	}
	if (document.getElementById('articleLogin')) {
		setTimeout("location.href='https://www.onlinewellnessassociation.com/index.php?v=articles'",1000);
	}
	if (document.getElementById('loginAdminCMS')) {
		setTimeout("location.href='https://www.onlinewellnessassociation.com/admin/'",1000);
	}
	
}

function addBlurs() {
	redirect();
	if (document.getElementById && document.getElementsByTagName) {
		if (document.getElementById('registration')) {
			var div = document.getElementById('registration');
		} 
		if (document.getElementById('tdcms')) {
			var div = document.getElementById('tdcms');
		}
		if (div) {
			var form = div.getElementsByTagName('form');
			var inputs = form[0].getElementsByTagName('input');
			var options = form[0].getElementsByTagName('option');
			for (var i=0;i<inputs.length; i++) {
				if (inputs[i].id == "submitBtn")
					break;
				inputs[i].onfocus = function() {
					this.style.background = "#ABBEBC";
				} 
				inputs[i].onblur = function() {
					this.style.background = "#fff";
				}
			}
		}
	}
}

function formFocus() {
	var inputs = document.getElementsByTagName('input');
	for (var i=0;i<inputs.length;i++) {
		inputs[i].onfocus = highlight;
		inputs[i].onblur = unhighlight;
	}
	var tarea = document.getElementsByTagName('textarea');
	for (var i=0;i<tarea.length;i++) {
		tarea[i].onfocus = highlight;
		tarea[i].onblur = unhighlight;
	}
}

function checkDirectoryForm(form) {
	if (form.firstName.value=='' | form.firstName.value == 'your first name') {
		alert('Your first name is required.');
		form.firstName.focus();
		return false;
	}
	if (form.lastName.value=='' | form.lastName.value == 'your last name') {
		alert('Your last name is required.');
		form.lastName.focus();
		return false;
	}	
	if (form.license.value=='' | form.license.value == 'license(s)') {
		alert('Your license is required.');
		form.license.focus();
		return false;
	}
	if (form.stateLicense.value=='' | form.stateLicense.value == 'state') {
		alert('Your state of licensure is required.');
		form.state.focus();
		return false;
	}
	if (form.specOne.value=='' | form.specOne.value == 'specialty one') {
		alert('At least one specialty is required.');
		form.specOne.focus();
		return false;
	}	
	if (form.license.value=='' | form.license.value == 'license(s)') {
		alert('Your license is required.');
		form.license.focus();
		return false;
	}	
	pic = document.getElementById('therPicture');
	if (!pic) {
			if (form.picture.value == '' | form.picture.value == 'path to file') {
			alert('You must upload a picture of yourself');
			form.picture.focus();
			return false;
		}
	}
	if (form.bio.value == '' | form.bio.value == 'your bio here') {
		alert('You must provied a mini profile (420 characters max)');
		form.bio.focus();
		return false;
	}
	if (form.bio.value.length > '420') {
		alert("420 characters is the maximum for your profile.\nYou have "+form.bio.value.length+".  Please re-write your profile.");
		return false;
	}
//	alert('Your information has been updated!');
	return true;
}

function resetThis(form) {
	var form = document.getElementsByTagName('form');
	var form = form[0];
	form.reset();
	checkBio();
	updatePreview(form);
	return false;
}

function checkBio() {
	var bio = document.getElementById('bio');
	var biocounter = document.getElementById('bioCounter');
	biocounter.innerHTML = bio.value.length +'/420';
	if (bio.value.length > 380) {
		biocounter.style.color = 'red';
		biocounter.innerHTML = bio.value.length +'!!!/420';		
	}
	if (bio.value.length < 380) {	
		biocounter.style.color = '#ffc20e';
	}
}

// Function: fires on load of Window, loads initFuncs()...  
addEvent(window,'load',initFuncs,false);
function addEvent(elm, type, fn, cap) {
	if (elm.addEventListener) {
		elm.addEventListener(type,fn,cap);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on'+type,fn);
		return r;
	}
	else {
		elm['on'+type] = fn;
	}
}

function updatePreview(form) {

	// Grab the preview vars...
	var name = document.getElementById('prevName');
	var s1 = document.getElementById('prevSpecOne');	
	var s2 = document.getElementById('prevSpecTwo');		
	var s3 = document.getElementById('prevSpecThree');		
	var s4 = document.getElementById('prevSpecFour');		
	var bio = document.getElementById('prevBio');		
	var state = document.getElementById('prevState');		
	var phone = document.getElementById('prevPhone');			
	var email = document.getElementById('prevEmail');			
	var web = document.getElementById('prevWeb');			
	var sched = document.getElementById('prevSched');			
	
	// & now update them!
	var fullNamePlusLicense = form.firstName.value+" "+form.lastName.value+"<span class=\"license\">"+form.license.value+"</span>";	
	name.innerHTML = fullNamePlusLicense;
	state.innerHTML = "State of Operation: "+form.stateLicense.options[form.stateLicense.selectedIndex].text;
	s1.innerHTML = form.specOne.value;
	s2.innerHTML = form.specTwo.value;	
	s3.innerHTML = form.specThree.value;	
	s4.innerHTML = form.specFour.value;	
	sched.innerHTML = form.operation.value;
	email.innerHTML = form.email.value;
	email.href = "mailto:"+form.email.value;
	web.innerHTML = form.website.value;
	web.href = "http://"+form.website.value;
	phone.innerHTML = form.phone.value;
	bio.innerHTML = form.bio.value;
	
}

function openWin(elm) {
	var src = elm.href;
	var newWin = window.open(src,'window','left=40,right=40,height=600,toolbar=0,resize=0,menubar=0,scrollbars=1');				
}
function openJSLinks(elm) {
	var src = elm.href;
	var newWin = window.open(src,'window','left=40,right=40,height=600,toolbar=1,resize=1,menubar=1,scrollbars=1');				
}

 // clears textfields 

function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}

