//5328
var EventId

function ConfirmEmail(obj)
{
	var email = document.getElementById('email')
	//alert(email)
	if (!email){ return false }
	if (!email.value || email.value== ''){ alert("Please enter an email address or indicate that you do not have an email address."); return false }
	if (email.value.indexOf("@") > -1)
	{
		if (obj.value == '')
		{
			alert("Please confirm your email address")
			return false
		}
		else if (obj.value != email.value)
		{
			alert("Your email addresses do not match. Please rectify.")
			return false
		}
	}
	return true

}

function ConfirmPurchases(previousAction, updateOccurred)
{
	/*
	if (previousAction != 'purchases'){ return }
	if (updateOccurred == '1'){ alert('Your purchases have been changed, and your invoice adjusted') }
	*/
}

function CheckName()
{
	var sponsorName = document.getElementById('sponsorName')
	if (!sponsorName){ return }
	
	var organization = document.getElementById('organization')
	
	if (sponsorName.value=="")
	{
		sponsorName.value=organization.value
	}
	
	/*
	if (document.forms[0].buyerQQQQname.value=="")
	{
		document.forms[0].buyerQQQQname.value=document.forms[0].buyerQQQQorganization.value
	}
	*/
}

function FillEmail()
{
	if (document.forms[0].NOEMAIL.checked==true)
	{ 
		document.forms[0].contactQQQQemail.value="noemail"
	}
	else
	{ 
		if (document.forms[0].contactQQQQemail.value=="noemail")
		{  
			document.forms[0].contactQQQQemail.value=""
		}
	}
}

function newin(targ)
{
	var wind = window.open(targ,'','width=400,height=500,scrollbars,resizable')
}

function Save(id)
{
	document.forms[0].action.value='saveRegistrant'
	document.forms[0].id.value=id
	document.forms[0].saveRegistrant.value=1
	document.forms[0].submit()
}
/* 
	this is the old function before we switched over the sending of
	emails via Outlook
*/
function SendEmail(id,tregId,session_id)
{
	/* 3317 - First name is required, too, which means we need to
	 * rearrange the logic here.
	var targ = document.getElementById('email'+id)
	var targEmail =targ.value
	 */

	// 5328 - some problems with capitalization in various 
	// scirpts. There isn't time to successfully iron
	// this out and be certain we have hit all areas
	// so we're assuming, after a cursory check, that this is just a type!
	// and we'll declare EventId as a global above
	if (!EventId){ EventId = eventId }

	var targName = document.getElementById('lastName'+id)
	var targFName = document.getElementById('firstName'+id)
	var targEmail = document.getElementById('email'+id)
	var message = ''
	
	/*
	if (targEmail.indexOf('@')<0 || !targName.value)
	{
		var message='';
		if (targEmail.indexOf('@')<0){ message = 'a valid email address' }
		if (!targName.value)
		{
			if (message !=''){ message += ' and' }
			message += ' a last name'
		}
		alert('You must first enter '+message)
	}
	*/
	// 4113 - better email validation.  NOTE: validation.js must be loaded!
	//if (!targEmail.value || targEmail.value.indexOf('@') < 0 )
	if (!targEmail.value || ! IsValidEmail(targEmail.value) )
	{
		message += 'a valid email address'
	}
	if (!targName.value || !targFName.value)
	{
		if (message !='') { message += ' and' }
		message += ' first and last names'
	}
	if (message != '')
	{
		alert('You must enter '+ message +'.')
	}
	else
	{
		//newin('email_team.cgi?id='+id+'&treg_id='+tregId+'&session_id='+session_id+'&email='+targEmail+'&first_name='+targFName.value+'&last_name='+targName.value)
		newin('email_team.cgi?event_id='+EventId+'&id='+id+'&treg_id='+tregId+'&session_id='+session_id+'&email='+targEmail.value+'&first_name='+targFName.value+'&last_name='+targName.value)
	// 3317
	}
}

function ShowSponsors(eventId)
{
         var NewWindow=window.open("sponsorpackagelist.cgi?id="+eventId+"&from=registratio","","width=500, height=500, scrollable,resizable, menubar=0")
}

function SubmitForm()
{
	document.forms[0].submit()
}
function Sure(id)
{
	if (confirm('Are you sure you want to clear this information?'))
	{
		document.forms[0].action.value='erase'
		document.forms[0].id.value=id
		document.forms[0].submit()
	}
}
/* MENLO CHANGES */
function DeleteRegistrant(id)
{
	if (confirm('Are you sure you want to permanently delete this registrant  #'+id) )
	{
		location=scriptName+"?action=deleteTeam&previous_action=purchases&event_id="+eventId+"&session_id="+sessionId+"&team_id="+id
	}
}
function DeleteTeam(id)
{
	if (confirm('Are you sure you want to permanently delete this group #'+id) )
	{
		location=scriptName+"?action=deleteTeam&previous_action=purchases&event_id="+eventId+"&session_id="+sessionId+"&team_id="+id
	}
}
// 0314
function DeleteTeammate(id)
{
	if (confirm('Are you sure you want to permanently delete this guest #'+id) )
	{
		location=scriptName+"?action=deleteTeammate&previous_action=purchases&event_id="+eventId+"&session_id="+sessionId+"&registrant_id="+id
	}
}
function ShowInstructions(){
	var targ = document.getElementById('instructions');
	if (targ)
	{
		targ.style.visibility='visible'
		targ.style.height='100%'
	}
}
function HideInstructions(){
	var targ = document.getElementById('instructions');
	if (targ)
	{
		targ.style.visibility='hidden'
		targ.style.height='0px'
	}
}

// 4698
function DeletePurchase(id)
{
	//alert('scriptName='+scriptName+' eventId='+eventId+' sessionId='+sessionId+' id='+id)
	if ( confirm('Are you sure you want to delete this purchase? #'+id) )
	{
		location=scriptName+"?action=deletePurchase&previous_action=purchases&event_id="+eventId+"&session_id="+sessionId+"&purchase_id="+id		
	}
}
// 4698

/* END OF MENLO CHANGES */



function Fax(thisAction)
{
            var NewWindow=window.open(scriptName+"?event_id="+eventId+"&session_id="+sessionId+"&action=fax&wait="+waitList+"&source="+source+"&type="+type,'','resizable,width=750,height=575,scrollbars=1,menubar=0')
            NewWindow.moveTo(15,15)
			// 5220 - Added 'eo' parameter to control the public wrapper.
            //document.location=scriptName+"?event_id="+eventId+"&session_id="+sessionId+"&action="+thisAction+"&type="+type+"&source="+source+"&previous_action="+previousAction
			document.location=scriptName+"?event_id="+eventId+"&session_id="+sessionId+"&action="+thisAction+"&type="+type+"&source="+source+"&previous_action="+previousAction+"&eo="+EO
}
function Payment_Methods()
{
	document.location=scriptName+"?event_id="+eventId+"&session_id="+sessionId+"&action=>payment_methods&type="+type+"&wait="+waitList
}
function Invoice(num)
{
	var NewWindow=window.open(scriptName+"?event_id="+eventId+"&session_id="+sessionId+"&action=printable_invoice&wait="+waitList+"&source="+source+"&type="+source+"&window="+num,'','resizable,width=750,height=575,scrollbars=1,menubar=0')
	NewWindow.moveTo(15,15)
}
function ConfirmCancel()
{
	if (confirm("Are you sure you want to permanently cancel your registration?"))
	{
		var loc = scriptName+"?action=cancel&event_id="+EVENTID+"&session_id="+sessionId
		//alert(loc)
		document.location=loc
		//document.forms[0].action.value="cancel"
		//document.forms[0].submit()
	}
}
function ConfirmUpload()
{
	if (document.forms[0].logo.value == "")
	{
		var go_ahead = confirm("You have not selected a logo. Do you still wish to contine?")
		if (go_ahead){ document.forms[0].submit() }
	}
	else{ document.forms[0].submit() }
}

function MailOptions(email,id,sid,type)
{
	var target="email_registrant_options.cgi?source=registration&event_id="+eventId+"&id="+id+"&email="+email+"&sid="+sid+"&type="+type
	var NewWindow= window.open(target,"",'resizable,width=550,height=600, scrollbars=1,menubar=0')
	NewWindow.moveTo(15,15)
}

function GoToContactInfo()
{
	document.forms[0].action.value='contact_info'
	SubmitForm()
}

function PrintInvoiceQuestion(){
	if ( confirm('Do you wish to print your invoice now?')){window.print()	} 
	else{window.close() }
}

function ValidateMember(id, defaultId, eventId)
{
	var obj = new HTTPObject(SITEURL+CGIPATH+"ajax.cgi")
	// this is a synchronous call
	//obj.SetSync(false)
	obj.SetClass('service')
	obj.SetSubClass('memberValidation')
	
	var lnInput = document.getElementById('lastName')
	var bdInput = document.getElementById('birthday')
	var last_name = '-'
	var birthdate = '-'
	if (lnInput){ last_name = lnInput.value }
	if (bdInput){ birthdate = bdInput.value }
	
	if (last_name == '')
	{
		alert('You must enter a last name before we can validate you.')
		return SetFeeId(defaultId)
	}
	if ( birthdate == '')
	{
		alert('You must enter a birthdate before we can validate you.')
		return SetFeeId(defaultId)
	}
	var queryString = "action=ValidateMember&event_id="+eventId+"&last_name="+last_name+"&birthdate="+birthdate
	obj.postData = queryString;
	obj.GetPage()
}
function ValidationResponseHandler(obj)
{
	var result = obj.HTML()
	if (result == 0 )
	{
		alert("We do not recognize your membership information. Please try again.")
		SetFeeId(result)
		SetValidated(0)
	}
	else
	{
		//alert(result)
		var rexp=/\+/g
		var rexp2=/%40/g
		var rexp3=/%26/g
		var rexp4=/%2B/g
		var keyVals = result.split('&')
		for (var pair in keyVals)
		{
			//if (keyVals[pair].indexOf('=')<0){ continue }
			var key_val = keyVals[pair].split('=')
			var inpNam =key_val[0]
			//alert(inpNam)
			if (inpNam)
			{
				var targ = document.getElementById('buyerQQQQ'+inpNam)
				//alert('buyerQQQQ'+inpNam+' targ='+targ)
				if (targ)
				{
					if (targ.value == '' && key_val[1])
					{
						var val =key_val[1]
						var newVal = val.replace(rexp,' ')
						var newVal2 = newVal.replace(rexp2,'@')
						var newVal3 = newVal2.replace(rexp3,'&')
						var newVal4 = newVal3.replace(rexp4,'+')
						targ.value= newVal4
					}
				}
			}
		}
		var mId = document.getElementById('buyerQQQQmember_id')
		alert("memberId = "+mId.value)
		alert("Membership validated!" )
		SetValidated(1)
	}
}
function SetFeeId(id)
{
	var fee = document.getElementById('fee')
	if (fee)
	{
		fee.value=id
	}
}
function SetValidated(val)
{
	var targ = document.getElementById('validated')
	validated = val
	if (targ)
	{
		targ.value=val
	}
}

/*----------------------------------------
	CHECKBOX LOGIC
------------------------------------------*/
var totalChecks = new Array();

function trackChecks(checkboxes, thisCheckbox, name, max) {
    if(!totalChecks[name]) {
        totalChecks[name] = 0;
    }

    if(!thisCheckbox.checked) {
        totalChecks[name]--;
        if(totalChecks[name] < max) {
            enableCheckboxes(checkboxes);
        }
    }
    else {
        if(max && totalChecks[name] > max-1) {
            thisCheckbox.checked = false;
            alert('Error: you can only select up to '+max+' checkboxes at a time');
            return totalChecks[name];
            //disableCheckboxes(checkboxes);
        }
        totalChecks[name]++;
    }
    return totalChecks[name];
}
/* ------------------------------------------
	DATE LOGIC
---------------------------------------------*/
function AssembleDate(time,obj, id){
	ValidateDate(time, obj.value,id)
	var year = document.getElementById('year'+id).value
	var month = document.getElementById('month'+id).value
	var day = document.getElementById('day'+id).value
	if(month || day || year) {
		document.getElementById('date'+id).value=month+'/'+day+'/'+year
	}
}

function ValidateDate(time, value,id)
{
	var match = /\D/
	if (match.exec(value))
	{ 
		alert("Date must be numbers only");
		document.getElementById('year'+id).value = ''
		return false
	}
	if (time == 'year' && (value.length > 4  || value.length<4 ))
	{
		alert('Invalid year')
		document.getElementById('year'+id).value = ''
		return false
	}
	if (time == 'month' && (value > 12 || value < 1 || value.length > 2))
	{
		alert('Invalid month')
		document.getElementById('month'+id).value = ''
		return false
	}
	if (time == 'day' && (value > 31 || value < 1 || value.length > 2))
	{
		alert('Invalid day')
		document.getElementById('day'+id).value = ''
		return false
	}
	return true
}

function CheckForNext(time,obj,id)
{
	var entry = obj.value
	if (entry.length == 2)
	{
		if(time == 'day')
		{
			//obj.blur()
			var x =document.getElementById('year'+id)
			x.focus()
		}
		else if (time == 'month')
		{
			//obj.blur()
			var x = document.getElementById('day'+id)
			x.focus()
		}
	}
}

