var the_error = '';
var captcha_end = 0;

function validate_this(formname) {
	        the_error = '';
		testform = eval("document."+formname);
		if(document.getElementById('captcha_utility')) {
			captcha_end = 0;
			document.getElementById('captcha_pass_code').value = null;
			captcha_validate();
		}
		else {
			captcha_end = 1;
		}
		for(i=0;i<testform.elements.length;i++) {
		 	if((testform.elements[i].value.search(/^R/)!=-1)&&(testform.elements[i].name.search(/^q/i)!=-1)) {
                                testanswer = testform.elements[i].name.replace(/q/,"a");
                                the_question = testform.elements[i].value.replace(/^.*:::/,"");
                                tmp_type = testform.elements[i].value.replace(/:::.*/,"");
                                answer_type = tmp_type.replace(/^R,/,"");
		 		answer_name = testform.elements[testanswer].name;
		 		not_found = 0;
		 		if((answer_type=="T")||(answer_type=="A")||(answer_type=="O")) {
                      if(testform.elements[testanswer].value=='') {
					  not_found++;
                                     }
		 		}

		 		else if(answer_type=="R") {
		 		     checkbox_test = 0;
		 		     for(j=0;j<testform.elements[testanswer].length;j++) {
					  if(testform.elements[testanswer][j].checked) {
					        checkbox_test++;
					  }
		 		}
		 		     if(checkbox_test==0) {
		 		          not_found++;
		 		     }
		 		}
		 		else if(answer_type=="S") {
		 		     if(testform.elements[testanswer].options[testform.elements[testanswer].options.selectedIndex].value=='') {
					  not_found++;
		 		     }
		 		}
		 		else if(answer_type=="M") {
		 		     select_test = 0;
		 		     for(j=0;j<testform.elements[testanswer].options.length;j++) {
					  if(testform.elements[testanswer].options[j].selected) {
					        select_test++;
					  }
		 		     }
		 		     if(select_test==0) {
		 		          not_found++;
		 		     }
		 		}
		 		else if(answer_type=="C") {
		 		     checkbox_test = 0; checkbox_values = '';
		 		     for(j=0;j<testform.elements[testanswer].length;j++) {
					  if(testform.elements[testanswer][j].checked) {
					        checkbox_test++;
					        checkbox_values += testform.elements[testanswer][j].value+",";
					  }
		 		     }
		 		     if(checkbox_test==0) {
		 		          not_found++;
		 		     }
		 		     else {
				     	all_answers = checkbox_values.substr(0,checkbox_values.length-1);
		 		     	for(j=0;j<testform.elements[testanswer].length;j++) {
 						testform.elements[testanswer][j].value = all_answers;
				      	}
		 		     }
		 		}

		 		if(not_found>0) {
				if(the_question=="adarea") {the_question="Area Code";}
				if(the_question=="adphone") {the_question="Phone Number";}
				the_error += the_question+"\n";
				}
		 	}
		 	else if((testform.elements[i].value.search(/,C/)!=-1)&&(testform.elements[i].name.search(/^q/i)!=-1)) {
                                testanswer = testform.elements[i].name.replace(/q/,"a");
                                the_question = testform.elements[i].value.replace(/^.*:::/,"");
                                tmp_type = testform.elements[i].value.replace(/:::.*/,"");
                                answer_type = tmp_type.replace(/^R,/,"");
				if(testform.elements[testanswer]) {
			 		answer_name = testform.elements[testanswer].name;
					 checkbox_test = 0; checkbox_values = '';
					 for(j=0;j<testform.elements[testanswer].length;j++) {
					 	if(testform.elements[testanswer][j].checked) {
							checkbox_test++;
						        checkbox_values += testform.elements[testanswer][j].value+",";
						}
			 		     }
			 		     if(checkbox_test>0) {
					     	all_answers = checkbox_values.substr(0,checkbox_values.length-1);
			 		     	for(j=0;j<testform.elements[testanswer].length;j++) {
 							testform.elements[testanswer][j].value = all_answers;
					      	}
			 		     }
				}
	 		}

		 }
		if(document.getElementById('captcha_utility')) {
			if(!document.getElementById('captcha_pass_code').value) {
				 function wait() {
					if(captcha_end==0) {
						setTimeout("wait()",1000);
					}
				}
			}
			if(document.getElementById('captcha_pass_code').value==0) {
				the_error += "Correct validation characters";
			}
		}
		 if(!check_email(testform.a2.value)){
			alert("You have entered an invalid email.");
			testform.a2.focus(); 
			if(document.all || document.getElementByID){
				testform.a2.style.background = "yellow";
			}
			return false;
		}
		if(the_error!='') {
			reload_captcha();
		      alert("Please provide the following:\n"+the_error);
		      return false;
       	       	}
		else if(the_error == '') {
			return true;;
		}

		

	}



function textLimit(field, maxlen) {
	if (field.value.length > maxlen) {
		field.value = field.value.substring(0, maxlen);
		alert('Your input has been truncated! There is a limit of 2048 characters.');
	} 
}

/* --- START CAPTCHA */

function createRequestObject() {
	try {
		xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
	}
	catch(e) {
		alert("Sorry, but your browser doesn\'t support XMLHttpRequest.");
	}
	return xmlhttp;
}

var http = createRequestObject();

function get_random_string() {
	var random_contents = new Array;
	random_string = "a,b,c,d,e,f,g,h,i,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,P,Q,R,S,T,U,V,W,Y,X,Z,2,3,4,5,6,7,8,9";
	var mykey = "";
	random_contents = random_string.split(",");
	for(i=0;i<6;i++) {
		mykey += random_contents[Math.floor ( Math.random ( ) * random_contents.length )];
	}
	return mykey;
}

function draw_captcha(storename,size) {
	var random_contents = new Array;
	var mykey = get_random_string();
	var mylocation = window.location.toString();
	var myimage_url = mylocation.replace(/\.com.*/,"")+".com/maxexp/main/captcha/draw/scale/"+size+"/sesent/00/key/"+mykey;
	var myimage = "<img src=\""+myimage_url+"\" id=\"capimage\" border=\"0\">";
	myimage += "<a href=\"javascript:reload_captcha();\"><img src=\"/"+storename+"/content/screens/images/reload_icon.gif\" width=\"19\" height=\"23\" hspace=\"5\" border=\"0\" alt=\"Reload\"></a>\n";
	myimage += "<input type=\"hidden\" name=\"captcha_key\" id=\"captcha_key\" value=\"\">\n";
	myimage += "<input type=\"hidden\" name=\"captcha_pass_code\" id=\"captcha_pass_code\" value=\"\">\n";
	myimage += "<p>Please type the validation characters listed above: <input type=\"text\" name=\"captcha_compare\" id=\"captcha_compare\" value=\"\" size=\"20\" maxlength=\"9\"><br>\n";
	myimage += "<br />The letters are case sensitive so please type the letters exactly how they are displayed. <br />Do not type spaces between the numbers and letters.</p>\n";
	document.write(myimage);
	document.getElementById('captcha_key').value = mykey;
}

function reload_captcha() {
	var mylocation = window.location.toString();
	var mykey = get_random_string();
	var size = +size
	var myimage_url = mylocation.replace(/\.com.*/,"")+".com/maxexp/main/captcha/draw/scale/"+size+"/sesent/00/key/"+mykey;
	document.getElementById('captcha_compare').value = '';
	document.getElementById('captcha_key').value = mykey;
	document.getElementById('capimage').src = myimage_url;
}

var captcha_validate = function() {
	var size = +size
	var submission = document.getElementById('captcha_compare').value;
	var captcha_key = document.getElementById('captcha_key').value;
	var mylocation = window.location.toString();
	var url = mylocation.replace(/\.com.*/,"")+".com/maxexp/main/captcha/verify/scale/"+size+"/captcha_code/" + submission + "/captcha_key/" + captcha_key;
	http.open('GET', url, false);
	http.send(null);
	//http.onreadystatechange = displaycheck;
	displaycheck();
}

var displaycheck = function() {
	if(http.responseText!='') {
		var showcheck = http.responseText;
		document.getElementById('captcha_pass_code').value = showcheck;
		if(showcheck != '0') {
			//document.getElementById('captcha_utility').style.border = '1px solid #49c24f';
		}
		if(showcheck == '0') {
			reload_captcha();
		}
		captcha_end = 1;
	}
}
/* --- END CAPTCHA */

function check_email(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < e.length ;i++){
		if(ok.indexOf(e.charAt(i))<0){ 
		return (false);
		}	
	} 

	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
		return (-1);		
		} 

		}

	}
			