var reEmail = /^((\"[^\"\f\n\r\t\v\b]+\")|([\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+(\.[\w\!\#\$\%\&\'\*\+\-\~\/\^\`\|\{\}]+)*))@((\[(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))\])|(((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9]))\.((25[0-5])|(2[0-4][0-9])|([0-1]?[0-9]?[0-9])))|((([A-Za-z0-9\-])+\.)+[A-Za-z\-]+))$/;

var validationArray=new Array();
var validationArrayIndex=0;
var exitValidation=false;
var multiRegForm = true; // Ruchee: Determines if event is set up as Multi Reg Event
var PPVEvent = false;
var notPreReg = true; // Added 8th Nov '05: Ruchee: If Multi Reg Event is NOT Pre Reg 
var badMultiRegSetup = false;


function init(){
 validationArray=new Array();
 validationArrayIndex=0;
 exitValidation=false;
}

String.prototype.trim = function(){
    return this.replace(/^\s+/,'').replace(/\s+$/,'');            
}

function isblank(s)
{
	for(var i = 0; i < s.length; i++)	{
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\t')) return false;
	}
	return true;
}

function isNumeric(sText)
{
	var ValidChars = "0123456789.";
	var Char;
	
	
	for (i = 0; i < sText.length; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			return false;
	}
	return true;

}

//helper function - can be used by custom javascript to perform operations based on input params
function findParam(strParamLabel) {
	var strParamValue = "";
	var intStartPos = document.location.search.indexOf(strParamLabel);
	var intEndPos = document.location.search.indexOf("&",intStartPos-0+1);
	var intParamLength = intEndPos - intStartPos - strParamLabel.length - 1;
	if (intEndPos>0) strRefParam = document.location.search.substr(intStartPos-0+strParamLabel.length+1,intParamLength);
	else strRefParam = document.location.search.substr(intStartPos-0+strParamLabel.length+1);
	return strRefParam;
}

function validatenone(ctrl,isrequired){	
	ctrl.value=ctrl.value.trim();	
	if (ctrl.value=="")
		if (isrequired){ 
			alert("This is a required field");
			ctrl.focus();
			exitValidation=true;
		}	
	return !exitValidation;	
}

function validatetextbox(ctrl,isrequired){	
	validatenone(ctrl,isrequired);	
}

function validatetextarea(ctrl,isrequired){	
	validatenone(ctrl,isrequired);
}

function validatenumeric(ctrl,isrequired){	
	validatenone(ctrl,isrequired);
	if (exitValidation) return false;
	
	var numberstr=ctrl.value;
	if (! isNumeric(numberstr)){
			alert("Please enter a numeric value in this field"); 
			ctrl.focus();
			exitValidation=true;
	}	
	return !exitValidation;	
}

function validateppv(ctrl,isrequired){	

    // No need to validate credit card info on a zero-dollar transaction
    if (isPPVZeroDollarTransaction()) {
        return true;
    }
    
	validatenone( ctrl, isrequired );
	if ( exitValidation ) return false;
	
	var numberstr = ctrl.value.trim();

	if (  !isNumeric( numberstr ) )
	{
			alert("Please enter a numeric value in this field"); 
			ctrl.focus();
			exitValidation=true;
	}	
	else 
	{
		if ( ctrl.name == 'ppv_securitycode' )
		{
			if ( document.regform.ppv_creditcardtype != 'undefined'
				&& document.regform.ppv_creditcardtype.selectedIndex >= 0 )
			{
				if (numberstr.length == 0) {
					alert("Invalid card security code"); 
					ctrl.focus();
					exitValidation=true;					
				}
			}
		} 
	}
	return !exitValidation;	
}

	function isEmail(str) {
	  // are regular expressions supported?
	  var supported = 0;
	  if (window.RegExp) {
	    var tempStr = "a";
	    var tempReg = new RegExp(tempStr);
	    if (tempReg.test(tempStr)) supported = 1;
	  }
	  if (!supported) 
	    return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	  else		
		return (reEmail.test(str));
	}

function validateemail(ctrl,isreq){ 	
	validatenone(ctrl,isreq);
	if (exitValidation) return false;
	var estr=ctrl.value.toLowerCase();
	if(!estr) return false;
	// var isEmail=(/^[\w~][\w\.\-~]*[\w~]@[\w~][\w\-~]*[\.][\w~][\w\.\-~]*[\w]$|^$/.test(estr) && !/[\.]{2}/.test(estr)); 
	if (!isEmail(estr)){
		alert("Please enter a valid email address in this field"); 
		ctrl.focus();
		exitValidation=true;
	}
	
	if(isBlockedEmail(ctrl.value,"")){
		alert("This event is currently full, and is not accepting any more registrations. Thank you for your interest."); 
		ctrl.focus();
		exitValidation=true;
	 }
	return !exitValidation;	
}
function isBlockedEmail(email,blockAllowStr){
	if(blockAllowStr=="" || blockAllowStr=="|") return false;
	email=email.toLowerCase();
	blockAllowStr=blockAllowStr.toLowerCase();
	var blockAllowArr=blockAllowStr.split("|");
	var userdomain=email.substring(email.indexOf("@")+1);
	if(blockAllowArr[0]!="" && blockAllowArr[1]==""){
		//has only block email list
		var blockArr=blockAllowArr[0].split(",");
		var hasSameDomain=false;
		for(var i=0;i<blockArr.length;i++){
			if(email==blockArr[i]) return true;
			else if(userdomain==blockArr[i]) hasSameDomain=true;
		}
		return hasSameDomain;
	} else if(blockAllowArr[0]=="" && blockAllowArr[1]!=""){
		//has only allow email list
		var allowArr=blockAllowArr[1].split(",");
		for(var i=0;i<allowArr.length;i++){
			if(userdomain==allowArr[i]) return false;
			else if(email==allowArr[i]) {return false;break;}
		}
		return true;
	}else if(blockAllowArr[0]!="" && blockAllowArr[1]!=""){
		//has both block and allow email list
		var blockArr=blockAllowArr[0].split(",");
		var hasSameDomain=false;
		for(var i=0;i<blockArr.length;i++){
			if(email==blockArr[i]) return true;
			else if(userdomain==blockArr[i]) hasSameDomain=true;
		}
		if(hasSameDomain){
			var allowArr=blockAllowArr[1].split(",");
			for(var i=0;i<allowArr.length;i++){
				if(email==allowArr[i]) {return false;break;}
			}
			return true;
		}
	}
	return false;
}

function validatephone(ctrl,isrequired)
{ 		
	validatenone(ctrl,isrequired);
	if (exitValidation) return false;
	var phonestr=ctrl.value;
	if(!phonestr) return false;
	phonestr=phonestr.replace(/[^\d]*/gi,"");
	if (!phonestr || phonestr.length<7){
			alert("Please enter a valid phone number in this field"); 
			ctrl.focus();
			exitValidation=true;
	}	
	if (phonestr.length==7)
		ctrl.value=(phonestr.substring(0,3)+"-"+phonestr.substring(3,7))
	else if	(phonestr.length==10)
		ctrl.value="("+phonestr.substring(0,3)+") "+phonestr.substring(3,6)+"-"+phonestr.substring(6,10);
	return !exitValidation;	
}

function validatelength(ctrl,maximumlength)
{ 
	ctrl.value=ctrl.value.trim();
	if (exitValidation) return false;
	var phonestr=ctrl.value;
	phonestr=phonestr.replace(/[^\d]*/gi,"");
	if (ctrl.value.length>maximumlength){
			alert("Maximum allowable length ("+maximumlength+") exceeded. Please modify the information in this field and try again)"); 
			ctrl.focus();
			exitValidation=true;
	}	
	return !exitValidation;	
}

function validateoption(ctrl,isrequired){
	if (isrequired){ 
		if(!optionselected(ctrl)){
			alert("This is a required field");
			if(ctrl.length+""=="undefined") ctrl.focus();
			else ctrl[0].focus();
			exitValidation=true;
		}
	}	
	return !exitValidation;	
}

function validatemulticheck(ctrl,targetctrl, isrequired){
	var checkboxvalue=getcheckboxvalue(ctrl);
	targetctrl.value=checkboxvalue;
	if (isrequired){ 
		if(checkboxvalue==""){
			alert("This is a required field");
			if(ctrl.length+""=="undefined") ctrl.focus();
			else ctrl[0].focus();
			exitValidation=true;
		}
	}	
	return !exitValidation;	
}

function validatelistbox(ctrl,isrequired,hasDefaultValue){	
	/*if(ctrl.options[ctrl.selectedIndex].unselectable=="true"){
		ctrl.options[ctrl.selectedIndex].value="";
	}*/
	if (isrequired && ctrl.length>1){ 
		if(ctrl.selectedIndex>=0 && ctrl.options[ctrl.selectedIndex].value==""){
			alert("This is a required field");
			exitValidation=true;
			ctrl.focus();
		}
		//if ( ctrl.name == 'ppv_billcountry' && )
	}	
	return !exitValidation;	
}

function validateppvlistbox(ctrl, isrequired, hasDefaultValue){

    // No need to validate credit card info on a zero-dollar transaction
    if (isPPVZeroDollarTransaction()) {
        return true;
    }
    
	if (isrequired && (ctrl.length > 1)) {
    
        if ((ctrl.name == 'ppv_billingstate') || (ctrl.name == 'ppv_billcountry')) {
        
            if (isCountryAndStateInSync()) {
                return true;
            } else {
                exitValidation = true;
                ctrl.focus();
                return false;
            }
        }

		if (!hasDefaultValue && (ctrl.selectedIndex <= 0)) {
            alert("This is a required field");
            exitValidation = true;
            ctrl.focus();
            return false;
        }
    }
    
    return true;
}

function isCountryAndStateInSync() {

    // If state not visible, everything's always okay
    if (!document.regform.ppv_billingstate) {
        return true;
    }
    
    // If country not visible, everything's always okay
    if (!document.regform.ppv_billcountry) {
        return true;
    }
    
    // User MUST always select a state, or "Outside USA"
    if (document.regform.ppv_billingstate.selectedIndex == 0) {  // Unselected
        alert('This is a required field');
        return false;
    }

    // If country is USA, cannot choose "Outside USA"
    if (document.regform.ppv_billcountry.selectedIndex == 211) {
    
        if (document.regform.ppv_billingstate.selectedIndex == 1) {
            alert('Country and State do not match.');
            return false;
        }
        
    // If country not USA, user MUST select "Outside USA"
    } else {
        if (document.regform.ppv_billingstate.selectedIndex > 1) { // A State within the USA selected
            alert('Country and State do not match.');
            return false;
        }
    }

    // All is well
    return true;
}

function changeState( index )
{
	if ( index != 211 ) // Index of United States
	{
		if ( document.regform.ppv_billingstate )
		{
			document.regform.ppv_billingstate.selectedIndex = 1; // Select "Outside USA" if so			
		}
	}
}

function validatecheckbox(ctrl,isrequired){
	if (isrequired){ 
		if(!ctrl.checked){
			alert("Please check the box to continue");
			ctrl.focus();
			exitValidation=true;
		}
	}	
	return !exitValidation;	
}

function validatehidden(ctrl, isrequired){
	if (isrequired){ 
		if(ctrl.value==""){
			alert("One or more required fields have not been set. Please contact technical support.");
			exitValidation=true;
		}
	}	
	return !exitValidation;	
}

function optionselected(ctrl){
	if(ctrl.length+""=="undefined") {
		return ctrl.checked;
	}
	else {
		for(var i=0;i<ctrl.length;++i){
			if(ctrl[i].checked) return true;
		}
		return false;
	}
}

function getcheckboxvalue(ctrl){
	var returnVal="";
	if(ctrl.length+""=="undefined") {
		if(ctrl.checked) returnVal=ctrl.value;
	}	
	else {
		for(var i=0;i<ctrl.length;++i){
			if(ctrl[i].checked) returnVal+=ctrl[i].value + "|";
		}
		if(returnVal!="") returnVal=returnVal.substring(0,returnVal.length-1);
	}
	return returnVal;
}

//calls the function specified by the extravalidation querystring parameter
//returns result of that _custom_ function. If true, the regform is submitted, if not, it is not submitted
function customvalidation(){	return true;}

var pageSubmitted = false;

function validate(){
	
	if(pageSubmitted){
		window.status = "Page already submitted";
		return false;
	}
	
	exitValidation=false;
	for(var i = 0; i < validationArrayIndex; i++){
		//alert(validationArray[i]+":"+exitValidation)
		eval(validationArray[i]);
		//alert(validationArray[i]+":"+exitValidation)
		if (exitValidation) return false;
	}	

	// Ruchee: If Event is set up as Multi Reg Event, redirect to MultiRegServlet else to eventRegistrationServlet
	if ( badMultiRegSetup )
	{		
		document.regform.regaction.value = "multireg";
		document.regform.action="https://event.on24.com/eventRegistration/MultiRegServlet";
	}
	else if ( !multiRegForm && !PPVEvent )
	{			
		document.regform.action="https://event.on24.com/eventRegistration/eventRegistrationServlet";
	}
	else if ( multiRegForm )
	{			
		// Added 8th Nov '05: Check that at least one sub event is selected.
		if ( notPreReg )
		{			
			if ( document.regform.subeventid )
			{				
				if ( document.regform.subeventid.length > 0 )
				{
					if ( !validateSubEvents() )
					{
						alert( 'Please select at least one sub event for registration.' );
						return false;
					}
				}
				else {
					// There is just one subeventid checkbox
					if ( !document.regform.subeventid.checked && !document.regform.subeventid.disabled ) {	
						alert( 'Please select sub event for registration.' );
						return false;
					}					
				}
			}
		}

		document.regform.regaction.value = "multireg";
		document.regform.action="https://event.on24.com/eventRegistration/MultiRegServlet";
	}
	else if ( PPVEvent )
	{
        if (!validatePPVPricing()) {
            return false;
        }
        
		document.regform.regaction.value = "purchase";
		document.regform.action="https://event.on24.com/eventRegistration/MultiRegServlet";
	}	
	
	if (customvalidation()){
		pageSubmitted = true;
		document.regform.submit();
	}
}

// PPV-specific validations
function validatePPVPricing() {

    // Check to make sure PPV code has been loaded.
    // If not, this is an older PPV event, or a non-ppv event.
    if (window['isPurchaseRequestValid'] && window['isTotalPriceValid']) {

        if (!isPurchaseRequestValid(true)) {
            return false;
        }
    
        if (!isTotalPriceValid()) {
            alert('Please apply coupons before submitting');
            return false;
        }
    }
    
    return true;
}

function validatenone_ppv(ctrl,isrequired){ 
    if (isPPVZeroDollarTransaction()) {
        return true;
    }
    return validatenone(ctrl,isrequired);
}

/*
function validateemail_ppv(ctrl,isreq) {
    if (isPPVZeroDollarTransaction()) {
        return true;
    }
    return validateemail(ctrl, isreq);
}
*/

function validatelistbox_ppv(ctrl,isrequired,hasDefaultValue) {  
    if (isPPVZeroDollarTransaction()) {
        return true;
    }
    return validatelistbox(ctrl, isrequired, hasDefaultValue);
}

// Check if this is a zero-dollar ppv transaction.
// If coupons have been entered
function isPPVZeroDollarTransaction() {

    // Check to make sure PPV code has been loaded.
    // If not, this is an older PPV event, or a non-ppv event.
    if (window['isTotalPriceValid'] && window['isPurchaseRequestValid'] && window['getTotalPrice']) {
        return isTotalPriceValid() && isPurchaseRequestValid(false) && (getTotalPrice() == 0);
    }
    
    return false;
}

function validateLoginForm(ctrl, isEmailField, lbl)
{
	if(pageSubmitted){
		window.status = "Page already submitted";
		return false;
	}
	
	ctrl.value=ctrl.value.trim();
	var loginFieldValue=ctrl.value;
	if(isblank(loginFieldValue)) {
		alert("Please enter " + lbl);
		ctrl.focus();
		return false;
	}
  	var isEmail=reEmail.test(loginFieldValue); 	
	if(!isEmailField || isEmail)
	{
		pageSubmitted = true;
		document.emailForm.action="https://event.on24.com/eventRegistration/eventRegistrationServlet";
		document.emailForm.submit();
	}
	else
	{
		alert("Please enter a valid " + lbl);
		ctrl.focus();
		return false;
	}
}

//delete all event cookies
function deleteEventCookies(deleteId) {
	var allcookies = document.cookie;
	if (allcookies != '') {
		var now = new Date();
		var expiration = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 1);
		var standardCookiePath = '/';
		var standardCookieDomain=location.host;
		standardCookieDomain=(standardCookieDomain.substring(standardCookieDomain.indexOf('.'),standardCookieDomain.length));
		document.cookie =
			'eventid' + deleteId + '=' 
			+ ';expires=' + expiration.toGMTString()
			+ ';path=' + standardCookiePath
			+ ';domain=' + standardCookieDomain
			+ ';' ; 
		/* to delete all
		var kp = allcookies.split('; ');
		var len = kp.length;
		for (var i=0;i<len;i++) {
			var valuepair=kp[i].split('='); 
			if (valuepair[0].indexOf('eventid')==0) {
				document.cookie =
					escape(valuepair[0]) + '=' 
					+ ';expires=' + expiration.toGMTString()
					+ ';path=' + standardCookiePath
					+ ';domain=' + standardCookieDomain
					+ ';' ; 
			}
		}
		*/
	}
}
function thisisnotme() {
	deleteEventCookies('36883');

	deleteEventCookies('36873');

	deleteEventCookies('36875');

	deleteEventCookies('36877');

	deleteEventCookies('36879');

	deleteEventCookies('36878');

	deleteEventCookies('36876');

	deleteEventCookies('36874');

	location.href='https://event.on24.com/eventRegistration/eventRegistrationServlet?eventid=36883&sessionid=1&key=D76A2FD29D7444AEC06765011A2D4953';
}

//populate standard fields
function populatePPVFields() {
	if ( document.regform.ppv_email  && document.regform.email ) {
		document.regform.ppv_email.value = document.regform.email.value;
	}
	if ( document.regform.ppv_billfirstname && document.regform.firstname) {
		document.regform.ppv_billfirstname.value = document.regform.firstname.value;
	}
	if ( document.regform.ppv_billlastname && document.regform.lastname) {
		document.regform.ppv_billlastname.value = document.regform.lastname.value;
	}
	if ( document.regform.ppv_billaddress && document.regform.address_street1) {
		document.regform.ppv_billaddress.value = document.regform.address_street1.value;
	}
	if ( document.regform.ppv_billingstate && document.regform.state ) {								
		document.regform.ppv_billingstate.selectedIndex = 0;
		for ( var i=0; i < document.regform.ppv_billingstate.options.length; i++ )
		{   
		 	if ( document.regform.ppv_billingstate.options[ i ].value == document.regform.state.value.toUpperCase() )
			{
				document.regform.ppv_billingstate.selectedIndex = i;
				break;
			}
     	}						
	}
	if ( document.regform.ppv_billcity && document.regform.city ) {
		document.regform.ppv_billcity.value = document.regform.city.value;
	}
	if ( document.regform.ppv_billzipcode && document.regform.zip) {
		document.regform.ppv_billzipcode.value = document.regform.zip.value;
	}
}

function clearPPVFields() {
	if ( document.regform.ppv_email ) {
		document.regform.ppv_email.value = '';
	}
	if ( document.regform.ppv_billfirstname ) {
		document.regform.ppv_billfirstname.value = '';
	}
	if ( document.regform.ppv_billlastname ) {
		document.regform.ppv_billlastname.value = '';
	}
	if ( document.regform.ppv_billaddress && document.regform.address_street1) {
		document.regform.ppv_billaddress.value = document.regform.address_street1.value;
	}
	if ( document.regform.ppv_creditcardtype ) {
		document.regform.ppv_creditcardtype.selectedIndex = 0;
	}
	if ( document.regform.ppv_creditcardnum ) {
		document.regform.ppv_creditcardnum.value = '';
	}
	if ( document.regform.ppv_expmonth ) {
		document.regform.ppv_expmonth.selectedIndex = 0;
	}
	if ( document.regform.ppv_expyear ) {
		document.regform.ppv_expyear.selectedIndex = 0;
	}
	if ( document.regform.ppv_securitycode ) {
		document.regform.ppv_securitycode.value = '';
	}
	if ( document.regform.ppv_billaddress ) {
		document.regform.ppv_billaddress.value = '';
	}
	if ( document.regform.ppv_billcity ) {
		document.regform.ppv_billcity.value = '';
	}
	if ( document.regform.ppv_billingstate ) {								
		document.regform.ppv_billingstate.selectedIndex = 0;					
	}
	if ( document.regform.ppv_billzipcode ) {
		document.regform.ppv_billzipcode.value = '';
	}
}

function validateSubEvents()
{	
    var selected = false;
	var alldiasbaled = true;
 
    for ( var intLoop = 0; intLoop < document.regform.subeventid.length; intLoop++) 
	{		
		//alert( document.regform.subeventid[ intLoop ]+document.regform.subeventid[ intLoop ].disabled );
		
    	if ( document.regform.subeventid[ intLoop ].checked 
			&& !document.regform.subeventid[ intLoop ].disabled ) 
		{
        	selected = true;
			break;
        }
    }       
	
	// If all are disabled(=> selected does not get set above) then do not display meesage
	for ( var intLoop = 0; intLoop < document.regform.subeventid.length; intLoop++) 
	{			
    	if ( !document.regform.subeventid[ intLoop ].disabled ) 
		{
        	alldiasbaled = false;			
        }
    }
	
	if ( alldiasbaled ) selected = true;
	
    return selected;
}

function launchForgotMyPassword(){
	window.open("https://event.on24.com/eventRegistration/forgotPassword.jsp?eventid=36883&sessionid=1","ForgotPassword","scrollbars=no,width=320,height=250");
}

var eventObject=new Object();
eventObject.eventid="36883";
eventObject.clientname="Intel";
eventObject.companyname="Intel";
eventObject.description="Multi-Core is Mainstream:  Are You Ready? Multithreading Tools and Techniques for Software Developers";
eventObject.eventabstract="";
eventObject.location="";
eventObject.sponsor="";
eventObject.category="";
eventObject.subcategory="";

eventObject.sessionid="1";
eventObject.sdescription="";
eventObject.speaker="";