function ShowProfile(id) {
	window.open('locator_profile.htm?id='+id,'_'+id,'width=700,height=600,scrollbars=1,resizable=1');
}

function LocatorGetOptions(which) {
	var cn	= form.country[form.country.selectedIndex].value || '';
	if(! cn) { alert('Please select a country to begin your search'); return; }
	var st	= form.state[form.state.selectedIndex].value || '';
	var ct	= form.city[form.city.selectedIndex].value || '';

	LocatorGetResults();

	var http = null;
	try { http=new XMLHttpRequest(); }
	catch (e) { try { http=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { http=new ActiveXObject("Microsoft.XMLHTTP"); } }
	if (http==null) { alert ("Your browser does not support AJAX!"); return; } 
	http.open("GET","getlocator.htm?java=1&cn="+cn+"&st="+st,true);
	http.onreadystatechange = function() {
		if(http.readyState==4) {
			if(http.responseText.length > 0) {
				eval(http.responseText);
				for(i=form.state.length; i>=0; i--) { form.state[i] = null; }
				for(i=0; i < states.length; i++) {
					form.state[i]=new Option(states[i][1],states[i][0]);
					if(states[i][0]==st) { form.state[i].selected='true'; }
				}
				form.state[0].style.color='#999999';

				if(cities) {
					for(i=form.city.length; i>=0; i--) { form.city[i] = null; }
					if(cities.length==2) {
						form.city[0] = new Option(cities[1],cities[1]);
					} else {
						for(i=0; i < cities.length; i++) {
							form.city[i] = new Option(cities[i],cities[i]);
							if(cities[i]==ct) { form.city[i].selected='true'; }
						}
						form.city[0].value='%';
						form.city[0].style.color='#999999';
					}
					LocatorGetResults();
				}
			}
		}
	}
	http.send(null);
}

function LocatorGetResults() {
	var cn	= form.country[form.country.selectedIndex].value || '';
	if(! cn) { alert('Please select a country to begin your search'); return; }
	var st	= form.state[form.state.selectedIndex].value || '';
	var ct	= form.city[form.city.selectedIndex].value || '';
	var http = null;
	try { http=new XMLHttpRequest(); }
	catch (e) { try { http=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { http=new ActiveXObject("Microsoft.XMLHTTP"); } }
	if (http==null) { alert ("Your browser does not support AJAX!"); return; } 
	http.open("GET","getlocator.htm?java=1&rows=1&cn="+cn+"&st="+st+"&ct="+ct+"",true);
	http.onreadystatechange = function() {
		if(http.readyState==4) {
			if(http.responseText.length > 0) {
				div = document.getElementById('results');
				div.innerHTML = http.responseText;
			}
		}
	}
	http.send(null);
}

function CountWords(text,max) {
	max = (max - 0);
	myWords = text.split(/ |\n/);
	div = document.getElementById('words');
	var left = (max - myWords.length);			
	if(left < 1) {
		var str = "";
		for(i=0; i<max; i++) {
			str+=myWords[i]+" ";
		}
		form.description.value = str;
	}
	if(left==1) { s=''; } else { s='s'; }
	div.innerHTML = "<b>Practice Description:</b> ("+left+" word"+s+" left)";
}

function OnlyFiveSpecializations() {
	var total = 0;
	for(i=0; i<form["specialization[]"].length; i++) {
		if(form["specialization[]"][i].checked==true) {
			total += 1;
		}
	}
	if(total > 5) {
		alert('Please select only 5 specialties.');
		return false;
	} else {
		return true;
	}
}

function Degrees() {
	if(form.school_1.value=='Example University') {
		form.type_1.value='';
		form.title_1.value='';
		form.school_1.value='';
		form.year_1.value='';
		return true;
	}
}

function JoinFormRequiredFields() {
	var errs = 0;
	myArray = new Array ('fname','lname','add1','city','zip','email','phone1');
	for(i=(myArray.length-1); i>=0;  i--) {
		obj = eval("form."+myArray[i]);
		if(obj.value=="") {
			errs++;
			obj.style.background='#FFFF00';
			obj.focus();
		} else {
			obj.style.background='#FFFFFF';
		}
	}
	if(errs > 0) {
		alert('Please complete all required fields before continuing.');
		return false;
	}

	// now check the card if paying online...
	if((form.payby) && (form.payby.value=='card')) {
		return ValidateCard();
	}
}

function ChangePayDiv() {
	div = document.getElementById('paydiv');
	if(form.html.value == "") {
		form.html.value = div.innerHTML;
	}
	if(form.payby.value=='phone') {
		div.innerHTML ="Your membership will be processed and active for the next 3 days.<br>";
		div.innerHTML+="Please call the office at 1-888-290-0878 M-F 9am-5pm to pay by<br>";
		div.innerHTML+="credit card over the phone.<p>";
	}
	if(form.payby.value=='mail') {
		div.innerHTML ="Your membership will be processed and active for the next 7 days.<br>";
		div.innerHTML+="Please mail your check for the total above to the National Office at:<br><ul>";
		div.innerHTML+="Amercian Art Therapy Association, Inc.<br>";
		div.innerHTML+="P.O. Box 79167<br>";
		div.innerHTML+="Baltimore, MD 21279-0167</ul><p>";
	}
	if(form.payby.value=='card') {
		div.innerHTML=form.html.value;
	}
}

function RadioCheckbox(a,b) {
	obj = eval("form."+a);
	if(obj.checked==true) {
		oth = eval("form."+b);
		oth.checked=false;
	}
}

function MemberProfileForm() {
	if((form.pass1.value.length > 0) && (form.pass1.value.length < 4)) {
		alert('Passwords must be atleast 4 characters long.');
		form.pass1.focus();
		return false;
	}
	if((form.pass1.value.length > 0) && (form.pass1.value != form.pass2.value)) {
		alert('Password confirmation failed. Please enter and confirm your new password again.');
		form.pass1.focus();
		return false;
	}
}

function CheckUsername() {
	if(form.username.value.length < 3) {
		alert('Usernames must be atleast 3 charaters long. Please enter another username.');
		form.username.value='';
		form.username.focus();
		return false;
	}
	var user = UrlEncode(form.username.value);
	var http = null;
	try { http=new XMLHttpRequest(); }
	catch (e) { try { http=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch (e) { http=new ActiveXObject("Microsoft.XMLHTTP"); } }
	if (http==null) { alert ("Your browser does not support AJAX!"); return; } 
	http.open("GET","check_username.htm?un="+user+"&id="+form.mid.value,true);
	http.onreadystatechange = function() {
		if(http.readyState==4) {
			if(http.responseText.length > 0) {
				alert('The username your entered is already in use on the system. Please enter another username.');
				form.username.value='';
				form.username.focus();
				return false;
			}
		}
	}
	http.send(null);
}


function Login() {
	if((login.username.value.length < 1) || (login.password.value.length < 1)) {
		alert('Please enter both the username and password');
		login.username.focus();
		return false;
	}
}

function UrlEncode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function ValidateCard() {
	// CHECK THE CREDIT CARD NUMBER AND EXP DATE
	myArray = document.form.ccnumb.value.split('',100);
	var ccnumber='';
	var firstfour='';
	for (i=0; i<=myArray.length; i++) {
		if((myArray[i] >= "0") && (myArray[i] <= "9")) {
			ccnumber = ccnumber+myArray[i];
			if(firstfour.length < 4) { firstfour=firstfour+""+myArray[i]; }
		}
	}
	// reset the formated number
	form.ccnumb.value = ccnumber;

	// based on the credit card number, define the type of card and the required length of the number
	var ShouldLength='';
	var CardShouldBe='';
	     if ((firstfour >= 3400) && (firstfour <= 3499)) { ShouldLength=15; CardShouldBe='American Express'; }
	else if ((firstfour >= 3700) && (firstfour <= 3799)) { ShouldLength=15; CardShouldBe='American Express'; }
	else if ((firstfour >= 4000) && (firstfour <= 4999) && (ccnumber.length > 15)) { ShouldLength=16; CardShouldBe='VISA'; }
	else if ((firstfour >= 4000) && (firstfour <= 4999) && (ccnumber.length < 14)) { ShouldLength=13; CardShouldBe='VISA'; }
	else if ((firstfour >= 5100) && (firstfour <= 5599)) { ShouldLength=16; CardShouldBe='MasterCard'; }
	else if (firstfour == 6011) { ShouldLength=16; CardShouldBe='Discover/Novus'; }
	else {
		alert("ERROR: Please enter a valid credit card number for the type of card selected.");
		form.ccnumb.style.background='FFFF00';
		form.ccnumb.focus();
		return false;
	}

	// return true as our testing number
	if(form.ccnumb.value=="4444444444444444") { return true; }

	// make sure we accept the type of card the number belongs to
	if(form.cctype.value != CardShouldBe) {
		alert("ERROR: The number entered does not correspond to the type of\ncard selected. Please check both the card type and number again.");
		form.ccnumb.style.background='FFFF00';
		form.ccnumb.focus();
		return false;
	}

	// check the length of the credit card number to make sure it is correct
	if(ccnumber.length > ShouldLength) {
		alert("ERROR: The number entered is too long.\nPlease correct the credit card number.");
		form.ccnumb.style.background='FFFF00';
		form.ccnumb.focus();
		return false;
	}

	if(ccnumber.length < ShouldLength) {
		alert("ERROR: The number entered is too short.\nPlease correct the credit card number.");
		form.ccnumb.style.background='FFFF00';
		form.ccnumb.focus();
		return false;
	}

	// now lets do the Check Sum on the card number
	var myAddUp=0;
	var myCheck=0;
	var myDigit=1;
	myArray = ccnumber.split('',ccnumber.length);
	for (i=(myArray.length-1); i>=0; i--) {
		if(myDigit < 1) { myDigit=1; } else { myDigit=0; }
		if(myDigit > 0) {
			myAddUp=(myArray[i]*2);
			if(myAddUp > 9) {
				mySplit=""+myAddUp+"";
				mySplit=mySplit.split('',2);
				myAddUp = (mySplit[0]*1)+(mySplit[1]*1);
			}
		} else {
			myAddUp=(myArray[i]);
		}
		//alert("digit="+myArray[i]+"  times="+myDigit+" myAddUp="+myAddUp);
		myCheck=(myAddUp*1)+myCheck;
	}
	myMod10 = (myCheck % 10);
	if(myMod10 != 0) {
		alert("ERROR (M10): Please enter a valid credit card number.");
		form.ccnumb.style.background='FFFF00';
		form.ccnumb.focus();
		return false;
	}

	// reset the color if we make it here
	form.ccnumb.style.background='FFFFFF';

	form.ccname.style.background='FFFFFF';
	if(form.ccname.value=='') {
		alert("ERROR: Please enter the name as it appears on the card.");
		form.ccname.style.background='FFFF00';
		form.ccname.focus();
		return false;
	}

	form.cccode.style.background='FFFFFF';
	if(form.cccode.value=='') {
		alert("ERROR: Please enter the Security Code (CVV)");
		form.cccode.style.background='FFFF00';
		form.cccode.focus();
		return false;
	}

	// finally check the expiration date...
	var currentTime = new Date();
	var month = (currentTime.getMonth() + 1);
	var year  = currentTime.getFullYear();

	if((form.ccyear.value == year) && (form.ccmonth.value < month)) {
		alert('ERROR: Invalid expiration date. Please check the expiration date and your computers system time again.');
		form.ccmonth.style.background='FFFF00';
		form.ccyear.style.background='FFFF00';
		form.ccmonth.focus();
		return false;
	}

	// if we make it here, we're good to go!
	return true;
}
