﻿function HandleSignup(){
	var email = jQuery(".EmailField input").val();
	var value = Trim(email);
	var pattern = /[^@_\.\w\d\-]|@@|\.\.|__|^@|^\.|^_|@$|\.$|_$|@\.|\.@|@_|_@|\._|_\.|(@)[^@]*\1/g;
	var chkValue = jQuery(".chkDailyLoop").val();
	
	if(email == "") {
		alert("Please enter your email address.");
		jQuery(".EmailField input").focus();
	}
	else if((((value.match(/@/)) && (value.match(/\./))) == null)||(value.match(pattern) != null)) {
		alert("Please double check the to email address.");
		jQuery(".EmailField input").focus();
	}
	else {
		jQuery.post("http://www.karmaloop.com/scripts/add_to_list.asp",{
		   Email: email,
		   contest: "karmalooptvsignup",
		   chkDailyLoop: chkValue	   
		}, 
		function(xml) {
			alert("Thank you for entering our contest.");
		});
	}
}

function LTrim(str) {
	for (var i=0; str.charAt(i)==" "; i++);
	return str.substring(i,str.length);
}

function RTrim(str) {
	for (var i=str.length-1; str.charAt(i)==" "; i--);
	return str.substring(0,i+1);
}

function Trim(str){
	return LTrim(RTrim(str));
}

jQuery(document).ready(function(){
	
	//handle the search button events
	jQuery(".EmailSubmit a").click(function(event){
		HandleSignup();
	});
	
	//if you click the enter key when your entering text into the advanced search box
	jQuery(".EmailSubmit a").keydown(function(e){
		if(e.keyCode == 13) {
			HandleSignup();
		}
	});
	
	//if you click the enter key when your entering text into the advanced search box
	jQuery(".EmailSubmit a").keyup(function(e){
		if(e.keyCode == 13) {
			HandleSignup();
		}
	});
					
	//if you click the enter key when your entering text into the advanced search box
	jQuery(".EmailField input").keydown(function(e){
		if(e.keyCode == 13) {
			HandleSignup();
		}
	});
	
	//if you click the enter key when your entering text into the advanced search box
	jQuery(".EmailField input").keyup(function(e){
		if(e.keyCode == 13) {
			HandleSignup();
		}
	});
});