// JavaScript Document: form.js
//Serves as the main area of form validation for the MittRomney.com form elements
function display_tabs() {
		var tabs = $S('.displaytab');
		var boxes = $S('.displaydiv');
		
		tabs[0].className += ' selected';
		boxes[0].style.display = 'block';
		
		
	
	
	}
	
	function move_tabs(num) {
		var tabs = $S('.displaytab');
		var boxes = $S('.displaydiv');
		
		tabs.each(function(el, i){
			var CleanClass = scrub_class(el, ' selected');
			el.className = CleanClass;
			
		})
		
		
			
		tabs.each(function(el, i){	
			if (i == num) {
				tabs[i].className += ' selected'
				boxes[i].style.display = 'block';
			}
			else{
				boxes[i].style.display = 'none';	
			}
		})
	}

function write_date() {
	
			var months = new Array(13);
				   months[0]  = "January";
				   months[1]  = "February";
				   months[2]  = "March";
				   months[3]  = "April";
				   months[4]  = "May";
				   months[5]  = "June";
				   months[6]  = "July";
				   months[7]  = "August";
				   months[8]  = "September";
				   months[9]  = "October";
				   months[10] = "November";
				   months[11] = "December";

					var now    = new Date();
					var mday = now.getDate();
					var month = months[now.getMonth()];
					var year = now.getYear();
				
					var newDate;
					newDate = year.toString();
					
					if(newDate.indexOf('10')!= -1)
					{  year += 1900;}	
						
		   document.write(month + " " + mday + ", " + year );
	}

function clear_Type(elem) {
		var val = elem;
		var valclass = val.className;
		//var check
		//alert(valclass);
		var check = valclass.lastIndexOf(' entered');
		
		if(check == -1) {
			val.value = "";
			
			CleanClass= scrub_class(val, ' frmerror');
			val.className = CleanClass;
			
			val.className += " entered";
			return false;
		}
		else if(val.value == "") {
			val.className = val.className.substring(0, check)			
			check_Empty(val);
			return false;
		}
		else {
			return;
		}	
	}
	
//This function sets up the Invite form.  Can also prepopulate any array of fields with the proper string

function initialize_form(elem, string){
		elem.each(function(el){
			//alert(el.className);
	 		initialize_single(el, string);
		})
	
	}
function initialize_single(el, string) {
		el.value = string;
		//alert(el.id);
		el.addEvent('focus', function(){clear_Type(this)});
		el.addEvent('blur', function(){check_Empty(this)});
	}
	
	
function check_Empty(elem) {
	var val = elem;
	var check = val.className.lastIndexOf(' entered');
	var cleanClass = scrub_class(val, " ");
	
	if (val.value == "" && check != -1){
		val.className = val.className.substring(0, check);
			
		switch (cleanClass) {			
			case "FirstName": val.value = "First Name";break;
			case "LastName":val.value = "Last Name";break;
			case "Email": val.value = "Email Address";break;
			case "EmailConfirm": val.value ="Confirm Email Address";break;
			case "ZIP": val.value ="ZIP Code";break;
			case "Search": val.value = "Search Site";break;
			case "Zipcode": val.value = "Zip";break;
			case "Emailaddress": val.value = "Email";break;
			case "Middleinital": val.value = "MI";break;
			case "City": val.value = "City";break;			
			case "Address": val.value = "Address";break;			
			
		}}
	else if (val.value == "") {
		switch (cleanClass) {			
			case "FirstName": val.value = "First Name";break;
			case "LastName":val.value = "Last Name";break;
			case "Email": val.value = "Email Address";break;
			case "EmailConfirm": val.value ="Confirm Email Address";break;
			case "ZIP": val.value ="ZIP Code";break;
			case "Search": val.value = "Search Site";break;
		}	
	}
	else if(check == -1 & val.value != "" ) { 
		//alert('you left ' + valclass + ' with value: ' +val.value);
		//val.style.color ='#000';
		val.className += " entered";
			
		//alert(valclass);
		}
	else {return;}
}

function Sm_clear_errors(elem) {
	//alert('Im here');
	
	var CleanClass ="";
	
	//elem[0].className;
	
	elem.each(function(el){
		CleanClass= scrub_class(el, ' error');
		
		el.className = CleanClass;
	})
	
	elem.each(function(el){
	   //alert(el.parentNode);
		CleanClass= scrub_class(el.parentNode, ' frmerror');
		el.parentNode.className = CleanClass;
	})
	
}


function clear_errors() {
	var CleanClass ="";
	var elabels = $S('.error');
	var efields = $S('.frmerror');
	
	elabels.each(function(el){
		CleanClass= scrub_class(el, ' error');
		el.className = CleanClass;
	})
	
	efields.each(function(el){
		CleanClass= scrub_class(el, ' frmerror');
		el.className = CleanClass;
	})
	
	$E('#Error').style.display = 'none';
	$E('#errorMsg').innerHTML = "";
}

function scrub_class(el, str) {
	var check = "";
	var CleanClass ="";
	
	check = el.className.indexOf(str);
	if(check != -1){	
		CleanClass = el.className.substring(0, check);
		}
	else CleanClass = el.className;
	return CleanClass;
	}

function validate_required(elem, message){	
	var CleanClass;
	elem.each(function(el){
	
					   
		if(el.value == "") {
			
			CleanClass = scrub_class(el, ' ');
			//alert(CleanClass);
			set_error(el);
			if(CleanClass != 'FinalDonation')
			{
				message += "<li>" + CleanClass + " is a required field</li>\n";
			}
			//alert(el.parentNode.Id);
		}
	})
	return message;
}


function set_error(elem, whichclass) {
	var CleanClass;
	CleanClass = scrub_class(elem,' ');
	
	if ($E('.'+ CleanClass).type != 'text'){
		$E('.'+ CleanClass).className += ' error';
	}

	elem.parentNode.className += " frmerror";
	
	if(CleanClass == 'CreditCardExpDate' || elem == 'CreditCardExpYear' ) {
		$E('.CreditCardExp').className += ' error';
		}
	
	}
	
function set_error2(elem) {
	var CleanClass;
	CleanClass = scrub_class(elem,' ');
	
	if ($E('.'+ CleanClass).type != 'text'){
		$E('.'+ CleanClass).className += ' error';
	}
	elem.className += " frmerror";
	
	if(CleanClass == 'CreditCardExpDate' || elem == 'CreditCardExpYear' ) {
		$E('.CreditCardExp').className += ' error';
		}
	
	}
function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function validate_email(elem, message){
	var CleanClass;
	
	elem.each(function(el,i) {
		CleanClass = scrub_class(el,' entered');
					   
		if (el.value != 'Email Address' && !isBlank(el.value)){
			if (check_email(el.value)!= true){
				message+='<li>Email Address Field ' +  /*i +*/ ' is invalid</li>';
				//alert(el.className);
				$E('.'+ CleanClass).className += ' error';
				el.parentNode.className += ' frmerror';
				
				//alert('email error!');
			}
		}
		if (el.value == 'Email Address' || trim(el.value) == '')
		{
			message+='<li>Email Address Field is invalid</li>';
		}
	
	});
	
	return message;	
}

function check_phone(str) 
{
	var objRegExp  = /^\(\d\d\d\) \d\d\d-\d\d\d\d$/; 
	return objRegExp.test(str);
}	

function fundraiseridIsOk(str) 
{
	var objRegExp  = /^\d*$/; 
	return objRegExp.test(str);
}

function check_ZIP(elem) {
	var str = elem.value
	
	if(str.length == 5 && !isNaN(str) && !isBlank(str))
		{return true;}
	else
		{return false;}

}

function check_email(str) {
	//alert('ended up here');
	var at=str.indexOf("@");
	var dot=str.indexOf(".");
	var lastdot=str.lastIndexOf(".");
	
		if((dot < 0) || (at < 0)) {return false;}
		else if (lastdot < at) {return false;}
		else if (str.length < 3) {return false;}
		else if (lastdot+1 == str.length) {return false;}
		else {return true;}	
}

function isBlank(val){
	if(val==null){return true;}
	for(var i=0;i<val.length;i++) {
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){return false;}
		}
	return true;
	}
	
function fieldname(fld)
{ // get the field label text or name
  if(fld.id && document.getElementsByTagName)
  {
    for(var i= 0, lbl= document.getElementsByTagName('LABEL'); i < lbl.length; i++)
      if(lbl[i].htmlFor==fld.id) return lbl[i].nodeValue||lbl[i].textContent||lbl[i].innerText;
    for(var i= 0, lbl= document.getElementsByTagName('label'); i < lbl.length; i++)
      if(lbl[i].htmlFor==fld.id) return lbl[i].nodeValue||lbl[i].textContent||lbl[i].innerText;
  }
  return fld.name||fld.type;
}

function fixMoney(fld,sep)
{ // monetary field check
	if(!fld.value.length||fld.disabled) 
		return true; // blank fields are the domain of requireValue 
	
	var val= fld.value;
	if(typeof(sep)!='undefined') 
		val= val.replace(new RegExp(sep,'g'),'');

	if(val.indexOf('$') == 0)
		val= parseFloat(val.substring(1,40));
	else
		val= parseFloat(val);

	if(isNaN(val))
	{ // parse error 
		status= 'The '+fieldname(fld)+' field must contain a dollar amount.';
		return false;
	}

	var sign= ( val < 0 ? '-': '' );
	val= Number(Math.round(Math.abs(val)*100)).toString();
	while(val.length < 2) val= '0'+val;
	var len= val.length;
	val= sign + ( len == 2 ? '0' : val.substring(0,len-2) ) + '.' + val.substring(len-2,len+1);
	fld.value= val;
	return true;
}

function display_message(msg) {
	if(msg == ""){
			return true;
		}
		else {
			$E('#Error').style.display = 'block';
			$E('#errorMsg').innerHTML = msg;
			//alert(window.pageYOffset.value);
			window.scrollTo(0,0);
			//alert('false');
			return false;
		}
	
	}
	
function initialize_PreEntered(){
		var emfields = $S('#EmailUpdates input.Email');
		var ZIPfields = $S('#EmailUpdates input.ZIP' );
		//alert(ZIPfields.className);
		//initialize_single(emfields, "Email Address");
		//initialize_single(ZIPfields, "Enter ZIP Code");
	}

function initialize_EmailUpdate(){
		var emfields = $S('#EmailUpdates input.Email');
		var ZIPfields = $S('#EmailUpdates input.ZIP');
		//alert(ZIPfields.className);
		
		initialize_form(emfields, 'Email Address');
		initialize_form(ZIPfields, 'ZIP Code');
		//
		//initialize_single(emfields, "Email Address");
		//initialize_single(ZIPfields, "Enter ZIP Code");
	}
	

function initialize_Join(){
	 initialize_single($('txtTMFirstName'), 'First Name');
	 initialize_single($('txtTMLastName'), 'Last Name');
	 initialize_single($('txtTMEmail'), 'Email Address');
	 initialize_single($('txtTMEmailConfirm'), 'Confirm Email Address');
	 initialize_single($('txtTMZIP'), 'ZIP Code');
}
function initialize_Join2(){
	 initialize_single($('txtTMFirstName'), 'First Name');
	 initialize_single($('txtTMMiddleInitial'), 'MI');
	 initialize_single($('txtTMLastName'), 'Last Name');
	 initialize_single($('txtTMAddress'), 'Address');
	 initialize_single($('txtTMCity'), 'City');
	 initialize_single($('txtTMEmail'), 'Email');
	 initialize_single($('txtTMZIP'), 'Zip');
	 
}

function initialize_Join_Email_Zip(email, zipCode){
	initialize_single($('txtTMFirstName'), 'First Name');
	initialize_single($('txtTMLastName'), 'Last Name');
	if (email != "" && zipCode != "") {
		document.getElementById('txtTMEmail').value = email;
		document.getElementById('txtTMEmailConfirm').value = email;
		document.getElementById('txtTMZIP').value = zipCode;
		check_Empty(document.frmJoin.txtTMEmail);
		check_Empty(document.frmJoin.txtTMEmailConfirm);
		check_Empty(document.frmJoin.txtTMZIP);
	}
	else {
		initialize_single($('txtTMEmail'), 'Email Address');
	 	initialize_single($('txtTMEmailConfirm'), 'Confirm Email Address');
	 	initialize_single($('txtTMZIP'), 'ZIP Code');
	}
}

function show_Join_Success()
{
	get_QueryString(window.location);
}

function initialize_Search()
{
	if ($('txtSearch'))
	{
		initialize_single($('txtSearch'), 'Search Site');
	}
}

function check_TM_default(fields, message){
	
	var flag = false;
	fields.each(function(el, i){
		switch(i)
		{
		case 0:
			if (el.value == 'First Name' || trimAll(el.value) == "") {
				flag=true;
				el.parentNode.className += " frmerror";
				}
		break;
		
		case 1:
		if (el.value == 'Last Name' || trimAll(el.value) == "") { flag=true;el.parentNode.className += " frmerror";}
		break;
		
		case 2:
		if (el.value == 'Email Address') {flag=true;el.parentNode.className += " frmerror";}
		break;
		
		case 3:
		if (el.value == 'Confirm Email Address') {flag=true;el.parentNode.className += " frmerror";}
		break;
		
		case 4:
		if (el.value == 'ZIP Code') {flag=true;el.parentNode.className += " frmerror";}
		break;
		}
	
	})
	
	if (flag == true){message += '<li>All fields are required</li>';}
	return message;
}

function join_validate(num) 
{

	var fields = $S('#frmJoin input');
	
	Sm_clear_errors($S('#frmJoin input'));
	
	var message ="<ul class='error'>";
	

	message = check_TM_default(fields, message);
	
	if (!check_ZIP($E('#txtTMZIP')) && $E('#txtTMZIP').value != 'ZIP Code' || (isBlank($E('#txtTMZIP').value)) ) {
			//alert($E('#txtTMZIP').type);
			message += '<li>Invalid ZIP Code</li>';
			set_error($E('#txtTMZIP'));
			}
			
	if ( (!check_email($E('#txtTMEmail').value)) || (isBlank($E('#txtTMEmail').value) && ($E('#txtTMEmail').parentNode.className.indexOf('error')== -1))) {
		message+= '<li>Please enter a valid Email Address</li>';
		set_error($E('#txtTMEmail'));
	}
	
	if (($E('#txtTMEmailConfirm').value != $E('#txtTMEmail').value) && ($E('#txtTMEmailConfirm').parentNode.className.indexOf('error')== -1) ) {
		message+= '<li>Confirm Email Does Not Match</li>';
		set_error($E('#txtTMEmailConfirm'));
	}
	if(message == "<ul class='error'>"){
		if(num=='1'){
			document.frmJoin.submit();
		}
		return true; 
	}
	else {
		message += '</ul>';
		
		if($('ErrorMsgJoin')) { 
			errDiv = $('ErrorMsgJoin')
		} else { 
			errDiv = $('ErrorMsg')
		}
		
		errDiv.innerHTML = message;
		return false;
	}
}
function splash_validate(num) 
{

	var fields = $S('#frmJoin input');
	
	Sm_clear_errors($S('#frmJoin input'));
	
	var message ="";
	
	if ($E('#txtTMFirstName').value == 'First Name' || (isBlank($E('#txtTMFirstName').value)) ) {
			//alert($E('#txtTMZIP').type);
			message = 'All fields are required';
			set_error2($E('#txtTMFirstName'));
			}
	if ($E('#txtTMMiddleInitial').value == 'MI' || (isBlank($E('#txtTMMiddleInitial').value)) ) {
			//alert($E('#txtTMZIP').type);
			message = 'All fields are required';
			set_error2($E('#txtTMMiddleInitial'));
			}
			
	if ($E('#txtTMLastName').value == 'Last Name' || (isBlank($E('#txtTMLastName').value)) ) {
			//alert($E('#txtTMZIP').type);
			message = 'All fields are required';
			set_error2($E('#txtTMLastName'));
			}
		if ($E('#txtTMAddress').value == 'Address' || (isBlank($E('#txtTMAddress').value)) ) {
			//alert($E('#txtTMZIP').type);
			message = 'All fields are required';
			set_error2($E('#txtTMAddress'));
			}		
		if ($E('#txtTMState').value == 'State' || (isBlank($E('#txtTMState').value)) ) {
			//alert($E('#txtTMZIP').type);
			message = 'All fields are required';
			set_error2($E('#txtTMState'));
			}		
		if ($E('#txtTMCity').value == 'City' || (isBlank($E('#txtTMCity').value)) ) {
			//alert($E('#txtTMZIP').type);
			message = 'All fields are required';
			set_error2($E('#txtTMCity'));
			}	
			
	if (!check_ZIP($E('#txtTMZIP'))  ) {
			//alert($E('#txtTMZIP').type);
			message = 'All fields are required';
			set_error2($E('#txtTMZIP'));
			}
			
			
			
			
			
	if ( (!check_email($E('#txtTMEmail').value)) || (isBlank($E('#txtTMEmail').value) && ($E('#txtTMEmail').parentNode.className.indexOf('error')== -1))) {
		message= 'All fields are required';
		set_error2($E('#txtTMEmail'));
	}
	

	if(message == ""){
		if(num=='1'){
			document.frmJoin.submit();
		}
		return true; 
	}
	else {
		
		if($('ErrorMsgJoin')) { 
			errDiv = $('ErrorMsgJoin')
		} else { 
			errDiv = $('ErrorMsg')
		}
		
		$E('#errorMsg').innerHTML = message;
		return false;
	}
}

function email_updates(){
		
		var email = $E('#EmailUpdates #txtEUEmail');
		var ZIP = $E('#EmailUpdates #txtEUEmail');
		var error = $E('#EmailUpdates div.description');
		
		//alert(error.innerHTML);
		
		var message="";

		if (!check_email($E('#txtEUEmail').value) && !isBlank($E('#txtEUEmail').value)) {
			message+= 'Invalid Email <br/>';
			$E('#txtEUEmail').parentNode.className += " frmerror";
		}
		if (!check_ZIP($E('#txtEUZIP'))) { 
			message += 'Invalid ZIP Code';
			$E('#txtEUZIP').parentNode.className += " frmerror";
			} 
		if(message == ""){
			var requeststring = '';
			requeststring = "/index.jsp?do=signup&u_email=" + $E('#txtEUEmail').value + "&u_zipcode=" + $E('#txtEUZIP').value;
			
			//alert($E('#EmailUpdates').offsetHeight);			
			$E('div#ThankYou').style.top = $E('#EmailUpdates').offsetTop + 100;			
			$E('div#ThankYou').style.left = $E('#main').offsetWidth - 500;
			$E('span.Email').innerHTML = $E('#txtEUEmail').value;
 			$E('span.ZIP').innerHTML = $E('#txtEUZIP').value;
			
			makeRequest(requeststring);
		}
		else {	
			$E('#EmailUpdates div.description').className += ' error';
			$E('#EmailUpdates div.imgbox').className += ' imgerror';
			$E('#EmailUpdates div.description').innerHTML = message;
			return false;
		}
		//if($E('#TYdiv').innerHTML == 'You have successfully signed up to receive our newsletter at:'){			
			//redirect to volunteer form
			//window.location = "/volunteer/form"
			//}
		//else {
			//show that you've already signed up
		openDHTML($E('#ThankYou'), $E('#EmailUpdates'));
		//} 
		//document.getElementById('InvitePreview'), document.getElementById('EmailMessage'));
		//$E('#ThankYou').style.visibility = 'visible';
	}

function send_email(){
		
		
		var emailto = $E('#txtemailto').value;
		var emailfrom = $E('#txtemailfrom').value;
		var emailsubject= $E('#txtemailsubject').value;
		var emailmessage = $E('#txtemailmessage').value;
		var pageurl= $E('#pageurl').value;
		
		var message="";
		var error = $E('#sendmail div.description2');
		
		
		
		var message="";

		if (!check_email(emailto) || emailto =="") {
			message+= 'Invalid To Email <br/>';
			$E('#txtemailto').parentNode.className += " Emailerror";
		}
		else{
			$E('#txtemailto').parentNode.className = "clearfix formrow";
		}
		
		if (!check_email(emailfrom) || emailfrom=="") {
			message+= 'Invalid From Email <br/>';
			$E('#txtemailfrom').parentNode.className += " Emailerror";
		}
		else {
		
		$E('#txtemailfrom').parentNode.className = " clearfix formrow";
		}
		
		
		if (emailsubject =="") {
			message+= 'Subject is Empty <br />' ;
			$E('#txtemailsubject').parentNode.className += " Emailerror";
		}
		else {
		
			$E('#txtemailsubject').parentNode.className = "clearfix formrow";
		}
		
		
	//message is not required.. uncomment if it is if (emailmessage =="") {
//		message+= 'Subject is Empty <br />' ;
//			$E('#txtemailmessage').parentNode.className += " Emailerror";
//		}
//		else{
		
//		$E('#txtemailmessage').parentNode.className += " clearfix formrow";
//		}
		
		
	
		if(message == ""){
			var requeststring = '';
			
			
			//requeststring = '/test.xml';
			requeststring = "/index.jsp?do=etp&txtemailto=" + emailto + "&txtemailfrom=" + emailfrom + "&txtemailsubject=" + emailsubject + "&txtemailmessage=" + emailmessage + "&pageUrl=" + pageurl;	
			
			//alert($E('#EmailUpdates').offsetHeight);			
			//$E('div#ThankYou').style.top = $E('#EmailUpdates').offsetTop + 100;			
			//$E('div#ThankYou').style.left = $E('#main').offsetWidth - 500;
			//$E('span.Email').innerHTML = $E('#txtEUEmail').value;
			//$E('span.ZIP').innerHTML = $E('#txtEUZIP').value;
			$E('#description2').className = '';
			$E('#description2').style.display = 'block';
			$E('#description2').innerHTML = 'Your email has been sent';
		
			$E('#description2').style.padding = '8px;';
		
			$E('#formcontent').style.display = 'none';
			$E('#sendmail div.containerBody').style.height= '35px';
			
			makeRequest(requeststring);
			
	
		}
		else {	
			$E('#description2').style.display = 'block';
			$E('#description2').className += ' error imgerror2';
			//$E('#sendmail div.imagebox2').className += ' imgerror';
			
			$E('#description2').innerHTML = message;
			return false;
			}
		//openDHTML($E('#ThankYou'), $E('#EmailUpdates')); 
		//document.getElementById('InvitePreview'), document.getElementById('EmailMessage'));
		//$E('#ThankYou').style.visibility = 'visible';
	}
	
	function send_email_spanish(){
		var emailto = $E('#txtemailto').value;
		var emailfrom = $E('#txtemailfrom').value;
		var emailsubject= $E('#txtemailsubject').value;
		var emailmessage = $E('#txtemailmessage').value;
		var pageurl= $E('#pageurl').value;
		var message="";
		var error = $E('#sendmail div.description2');
		var message="";
		
		if (!check_email(emailto) || emailto =="") {
			message+= 'Correo-e de tu amigo es nulo <br/>';
			$E('#txtemailto').parentNode.className += " Emailerror";
		}
		else{
			$E('#txtemailto').parentNode.className = "clearfix formrow";
		}
		
		if (!check_email(emailfrom) || emailfrom=="") {
			message+= 'Su correo-e es nulo <br/>';
			$E('#txtemailfrom').parentNode.className += " Emailerror";
		}
		else {
		
		$E('#txtemailfrom').parentNode.className = " clearfix formrow";
		}
		
		
		if (emailsubject =="") {
			message+= 'Necesita poner el tema <br />' ;
			$E('#txtemailsubject').parentNode.className += " Emailerror";
		}
		else {
			$E('#txtemailsubject').parentNode.className = "clearfix formrow";
		}
		
		if(message == ""){
			var requeststring = '';
			requeststring = "/index.jsp?do=etp&txtemailto=" + emailto + "&txtemailfrom=" + emailfrom + "&txtemailsubject=" + emailsubject + "&txtemailmessage=" + emailmessage + "&pageUrl=" + pageurl;	
			
			$E('#description2').className = '';
			$E('#description2').style.display = 'block';
			$E('#description2').innerHTML = 'Su correo electr—nico ha sido enviado.';
			$E('#description2').style.padding = '8px;';
			$E('#formcontent').style.display = 'none';
			$E('#sendmail div.containerBody').style.height= '35px';
			
			makeRequest(requeststring);
		}
		else {	
			$E('#description2').style.display = 'block';
			$E('#description2').className += ' error imgerror2';
			
			$E('#description2').innerHTML = message;
			return false;
		}
		$E('#TYhead').style.display = 'none';
	}

function doEmailReset() {
//clears out all email fields and resets size for 

$E('#description2').className ='';
$E('#description2').style.display = 'none';
$E('#formcontent').style.display = 'block';
$E('#description2').className = '';
$E('#txtemailto').parentNode.className = "clearfix formrow";
$E('#txtemailfrom').parentNode.className = " clearfix formrow";
$E('#txtemailsubject').parentNode.className = "clearfix formrow";
$E('#sendmail div.containerBody').style.height= 'auto';

	$E('#txtemailto').value ='';
	$E('#txtemailfrom').value='';
	$E('#txtemailsubject').value='';
	$E('#txtemailmessage').value='';
}


function get_QueryString(url) {
	var CleanUrl;
	url = url.toString();
	
	var check = url.indexOf('emailedFriends=1');
	if(check != -1)
	{
		toggleSelect2();
		s.events='event3'; 
		s.t();
		$E('#Success').style.display = 'block';
		$E('#nothanks').addEvent('click', function(){
									$E('#Success').style.display = 'none';
									return false;})
	}
	
	check = url.indexOf('emailedFriends=2');
	if(check != -1)
	{
		//toggleSelect2();
		$E('#Success2').style.display = 'block';
	}
	
	check = url.indexOf('thanks=');
	if(check != -1)
	{
		s.events="event2"
		s.t();
		$('Success').style.display = 'block';
		$('nothanks').onclick = function(){
								$E('#Success').style.display = 'none';
								return false;
								}
	}
	
	CleanClass = url.substring(0,check);
	
	
	}
	

function initialize_invite(){
	//alert();
	 get_QueryString(window.location);
	
	 var fname = $S('#Friends input.FirstName', '#MoreFriends input.FirstName', '#frmJoin input.FirstName');
	 var lname = $S('#Friends input.LastName', '#MoreFriends input.LastName', '#frmJoin input.LastName');
	 var email = $S('#Friends input.Email', '#MoreFriends input.Email', '#frmJoin input.Email');
	 var emailconfirm = $S('#frmJoin input.EmailConfirm');
	 var zip = $S('#frmJoin input.ZIP');
	
	 initialize_form(fname, 'First Name');
	 initialize_form(lname, 'Last Name');
	 initialize_form(email, 'Email Address');
	 initialize_form(emailconfirm, 'Confirm Email Address');
	 initialize_form(zip, 'ZIP Code');
}



function frmInvite_validate(s)
{
	clear_errors();
	
	var fields = $S('#visitorInfo input');
	// fields[fields.length] = $('txtEmailSubject');
	// fields[fields.length] = $('txtEmailMessage');
	
	var emailFields = new Array($('txtYourEmail'))
	
	var contentJoined  = "At MittRomney.com, I've just joined Team Mitt, the national network of grassroots supporters of Governor Romney and his presidential campaign.\n\nWhen you get a chance, I'd encourage you to take a look at the following biographical video about Governor Romney.  The video does an impressive job of detailing the accomplishments, the character and the values of the man that I believe should be elected our next President in 2008:\n\nAfter you watch the video and learn more about Mitt Romney and his vision for America, I hope you'll join me as a member of Team Mitt and then encourage your friends and family to join us as I am now.\n\nIf we are to defeat the jihadists that threaten our way of life, if we are to compete economically and technologically with Asia, if we are to put a stop to the federal government's runaway spending, we have to work together and unite behind a candidate capable of facing these urgent challenges.  I believe that man is Mitt Romney and I know he needs our help.\n\nThanks and let me know what you think,";
	var contentVisitor = "At MittRomney.com, I've been learning about Team Mitt, the national network of grassroots supporters of Governor Romney and his presidential campaign.\n\nWhen you get a chance, I'd encourage you to take a look at the following biographical video about Governor Romney.  The video does an impressive job of detailing the accomplishments, the character and the values of the man that I believe should be elected our next President in 2008:\n\nAfter you watch the video and learn more about Mitt Romney and his vision for America, I hope you'll consider joining Team Mitt as I am and then we can encourage our friends and family to join as well.\n\nIf we are to defeat the jihadists that threaten our way of life, if we are to compete economically and technologically with Asia, if we are to put a stop to the federal government's runaway spending, we have to work together and unite behind a candidate capable of facing these urgent challenges.  I believe that man is Mitt Romney and I know he needs our help.\n\nThanks and let me know what you think,";
	// ----------------------------------------
	
	var mailContent = $('txtEmailMessage').value
	if(s != 2)
	{
		if(mailContent == '' && s == 1)
		{
			$('txtEmailMessage').value = contentJoined;
		}
		if(mailContent == '' && s == 0)
		{
			$('txtEmailMessage').value = contentVisitor;
		}
	}
	
	//var emailfields = $S('#InviteInfo input.Email');
	var message = "";
	
	message = validate_required(fields, message);
	message = validate_email(emailFields, message);
	display_message(message);
	
	if(message.length == 0) 
	{
		frmInvite_clearDefaults();
		return true;
	} 
	else 
	{
		return false;
		 
	}
}
	function frmInvite_clearDefaults()
{
	var firstNames = $$('.friends input.FirstName');
	var lastNames = $$('.friends input.LastName');
	var emails = $$('.friends input.Email');
	for(var i=0; i<firstNames.length; i++)
	{
		if(firstNames[i].value=='First Name'){ firstNames[i].value=''; }
	}
	for(i=0; i<lastNames.length; i++)
	{
		if(lastNames[i].value=='Last Name'){ lastNames[i].value=''; }
	}
	for(i=0; i<emails.length; i++)
	{
		if(emails[i].value=='Email Address'){ emails[i].value=''; }
	}
	return true;
}

function checkDonationRadios()
{
	for(var i=0; i<$$('input.DonationAmount').length; i++)
	{
		if($$('input.DonationAmount')[i].checked)
		{
			return true;
		}
	}
	return false;
}

function frmContribute_validate() 
{
		var message = "";
		clear_errors();
		
		
		/*if($('otherDonationAmount2').checked)
		{	
			if(!fixMoney($('txtCCAmountOther')))
			{
				message += '<li>Please Enter a Valid Contribution Amount</li>';
				set_error($E('#txtCCAmountOther'));
			}
			
			$('finalAmount').value = $('txtCCAmountOther').value;
			if (parseInt($('finalAmount').value) < 5 || $('finalAmount').value == '') { 
				message += '<li>Your contribution must be $5.00 or more</li>';
				set_error($E('#txtCCAmountOther'));
			}
			if (parseInt($('finalAmount').value) > 2300) { 
				message += '<li>Your contribution must be less than $2,300</li>';
				set_error($E('#txtCCAmountOther'));
			}
		}*/
		
		
		if($('otherDonationAmount').checked)
		{	
			if(!fixMoney($('txtCCAmountOther')))
			{
				message += '<li>Please Enter a Valid Contribution Amount</li>';
				set_error($E('#txtCCAmountOther'));
			}
			
			$('finalAmount').value = $('txtCCAmountOther').value;
			if (parseInt($('finalAmount').value) < 5 || $('finalAmount').value == '') { 
				message += '<li>Your contribution must be $5.00 or more</li>';
				set_error($E('#txtCCAmountOther'));
			}
			if (parseInt($('finalAmount').value) > 2300) { 
				message += '<li>Your contribution must be less than $2,300</li>';
				set_error($E('#txtCCAmountOther'));
			}
		}
		
		
		if (!checkDonationRadios())
		{
			message += '<li>You must provide a contribution amount.</li>';
			$$('.lblCreditCardAmount')[0].style.color='#D81E05';
		}
		var fields = $S('.required');
		
		message = validate_required(fields, message);

		if (!check_ZIP($E('#txtZIP'))) 
		{ 
			message += '<li>Invalid ZIP Code</li>';
			set_error($E('#txtZIP'));
		}
		if(check_phone($('txtPhone').value && !isBlank($('txtPhone').value) )) 
		{
			message+= '<li>Please enter a valid Phone Number - (###) ###-####</li>';
			set_error($E('#txtPhone'));
		}
		if( ($('cc_expmonth').value == '' || $('cc_expyear').value == '') || ($('cc_expyear').value == '06' && parseInt($('cc_expmonth').value) < 12) ) 
		{
			message+= '<li>Please enter a valid Expiration Date.</li>';
			set_error($E('#cc_expmonth'));
			set_error($E('#cc_expyear'));
		}
			
		if (!checkCreditCard($('txtCreditCardNum').value, $('slcCreditCardType').value))
		{
			set_error($E('#txtCreditCardNum'));
			message+= '<li>'+ ccErrors[ccErrorNo] + '</li>';
		}
		
		if (!$E('#chkConfirm').checked){
			$E('.Confirm').className += ' error'
			message+='<li>Please confirm your eligibility before submitting</li>';
		}
		
		/*if($('u_FundraiserID').value != '')
		{
			if( !fundraiseridIsOk($('txtFundraiserID').value) )
			{
				set_error($E('#txtFundraiserID'));
				message += '<li>Please Enter a Valid Fundraiser ID</li>';
			}
		}*/
		
		if (message == "") 
		{
			if (setfinalAmount(document.frmContribute.rdoDonation)) {
				document.frmContribute.submit();
			}
			else {
				message += '<li>You must provide a contribution amount.</li>';
				display_message(message);
			}
		}
		else
		{
			display_message(message) 
		}
}

function display_message(msg) {
	if(msg == ""){
			return true;
		}
		else {
			$E('#Error').style.display = 'block';
			$E('ul#errorMsg').innerHTML = msg;
			//alert(window.pageYOffset.value);
			window.scrollTo(0,0);
			//alert('false');
			return false;
		}
	}


function initialize_success() {
	get_QueryString(window.location);
	}

	function move_Join(anc){
		var elem = $E('#Join');
		elem.style.position = 'absolute';
		var coord = findPos(anc);
		$E('#Jclose').style.display = 'block';
		//alert(coord[0]);
		elem.style.top = coord[1]+'px';
		elem.style.left = coord[0]+'px';
	
	}
	function close_Join() {
		$E('#divSupport').style.visibility = 'visible';
		$E('#Jclose').style.display = 'none';
		var elem = $E('#Join');
		elem.style.position = '';
		
	}
	
function select_other_amount()
{
	var donateRadios = $S('#donationAmt .DonationAmount');
	donateRadios.each(function(el, i){
			//var CleanClass = scrub_class(el, ' selected');
			//el.className = CleanClass;

		

		})
}

function setfinalAmount(buttonGroup) {
	for (i=0; i < (buttonGroup.length-1); i++) {
		if (buttonGroup[i].checked == true) {
			$('finalAmount').value = buttonGroup[i].value;
		}
	}

	if (($('finalAmount').value == "") || ($('finalAmount').value == null) ) {
		return false;
	}
	else {
		return true;
	}
}

function frmRegister_validate() 
{
	clear_errors();
	
	var fields = $S('#frmRegister .required');
	
	var emailFields = new Array($('txtEmail'))
	
	var message = "";
	
	message = validate_required(fields, message);
	message = validate_email(emailFields, message);
	
	display_message(message);
	
	if(message.length == 0) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}

function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}