var emailFilter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
var form;

function emptyBox(theBox) {
	if ((theBox.value == null) || (theBox.value.length == 0))
		return true
	else
		return false
}

function emailSelect(theForm) {
    if(!isNaN(theForm.submissiontype.length)){
        if (theForm.submissiontype[0].checked == false && theForm.submissiontype[1].checked == false) {
		    alert('Please select subscribe or unsubscribe');
		    return false;
	    }

        if (theForm.submissiontype[0].checked == true) {
		    return emailSignup(theForm);
		    }
	    else if (theForm.submissiontype[1].checked == true) { 
		    return emailSignupRemove(theForm);
	    }
    }
    else{
        if (theForm.submissiontype.checked == false){
            alert('Please select subscribe');
        }
        else {
            return emailSignup(theForm);
        }
    }
}

function emailSignup(theForm) {	
	if (emptyBox(theForm.emailAddress)) {
		alert('Please enter your email address');
		return false;
	}
	
	if (emailFilter.test(theForm.emailAddress.value) == false) {
		alert('Your email address is invalid, please re-enter');
		return false;
	}
	
	if (emptyBox(theForm.Store)) {
		alert('Process setup incorrectly');
		return false;
	}
		
	var parms = new Array();
	parms[parms.length] = theForm.emailAddress.value;
	parms[parms.length] = theForm.Store.value;
	parms[parms.length] = window.location.hostname;
			
	jsrsExecute("/common/comm/emailSignup/server_v2.asp", emailSignupCallBack, "sp_addRecipient", parms, false);
	
	form = theForm;
	
	return false;
}

function emailSignupCallBack(str) {    
	if(str == "") {	   
	    var parms = new Array();
	    parms[parms.length] = form.emailAddress.value;
	    parms[parms.length] = form.Store.value;
	    parms[parms.length] = window.location.hostname;
	    
    	jsrsExecute("/common/comm/emailSignup/server.asp", emailSignupCallBack, "emailSignup", parms, false);
	}
	else {
	    if (str.substr(0, 5) != 'ERROR')
		    form.reset();
		
	    alert(str);		
	}
}

function emailSignupRemove(theForm) {
		
	if (emptyBox(theForm.emailAddress)) {
		alert('Please enter your email address');
		return false;
	}
	
	if (emailFilter.test(theForm.emailAddress.value) == false) {
		alert('Your email address is invalid, please re-enter');
		return false;
	}
	
	if (emptyBox(theForm.Store)) {
		alert('Process setup incorrectly');
		return false;
	}
		
	var parms = new Array();
	parms[parms.length] = theForm.emailAddress.value;
	parms[parms.length] = theForm.Store.value;
	parms[parms.length] = window.location.hostname;
			
	jsrsExecute("/common/comm/emailSignup/server_v2.asp", emailSignupCallBack, "sp_optoutRecipient", parms, false);
	
	form = theForm;
	
	return false;
}

