var errorMsg = new Array();
var errorCounter = 0;
var htmlError = '';
var fieldId = new Array ('firstName|First Name is required','lastName|Last Name is Required','address|Address is Required','email|Email is required','phone|Phone is required','city|City is Required','state|State is Required','postalCode| Postal Code is required');


function dispalyRegToWin(){
	var t = new Date();
	var drawingID = $('#drawingIdField').val();
	var url = '/registerToWin/index.cfm?drawingID=' + drawingID + '&nocache=' + t.getTime();
	$('#registerToWin').load(url);
}

function fn_submit() {
	$('#submit').click(function(e){
		
		removeErrorMsg();
		
		for(i=0;i<fieldId.length;i++){
			 validateField(fieldId[i]);				
		}
	
		if(errorMsg.length==0){
			$.ajax({
			  type: 'POST',
			  url: '/registerToWin/action.cfm',
			  data: $("#registrationForm").serialize(),
			  success: function(msg){
										$('#addressDiv').html(msg);
									}
			});
		}
		
		for(var c=0;c<errorMsg.length;c++){
			htmlError = htmlError + errorMsg[c] + '<br/>'
		}
	
		$('#errorDiv').append(htmlError);
		$('#errorDiv').show();
		
		htmlError = '';
		errorMsg = new Array();
		errorCounter = 0;
		e.preventDefault();
	});

}
fn_submit();

removeErrorMsg = function(){
	$('#errorDiv').html('');
	for(i=0;i<fieldId.length;i++){
		var str = fieldId[i].split('|');
		var id  = str[0];
		$('#' + id).css({'border':'1px solid #cecece'});
	}
}

validateField = function(values){
	var str = values.split('|');
	var id  = str[0];
	var msg = str[1];	
	var htmlString = '';
	
	if(id == 'email'){
		validateEmail(id,msg);
	}else if(id == 'phone'){
		validatePhone(id,msg);
	}else if(id == 'postalCode'){
		validatePostalCode(id,msg);
	}else if($('#' + id).val()==''){	    
	    $('#' + id).css({'border':'1px solid #c61800'});
	    htmlString = '<span class="err" onclick="setFocus(';
	    htmlString = htmlString + "'" + id + "'";
	    htmlString = htmlString + ');">' + msg + '</span>';
	    errorMsg[errorCounter] = htmlString;
	    errorCounter++;
	}	
}

validateEmail = function(id,msg){
	var reEmail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var fieldValue = $('#' + id).val();
	
	if (!reEmail.test(fieldValue)) {
		createErrorMsg(id,msg);	
	} 
}

validatePhone = function(id,msg){
	var valid = true;
	var fieldValue = $('#' + id).val();
    var valStripped = fieldValue.replace(/[\(\)\.\-\ ]/g, '');    

   if (fieldValue == "") {
        valid = false;
    } else if (isNaN(parseInt(valStripped))) {
        valid = false;
    } else if (!(valStripped.length == 10)) {
        valid = false;
    }
   
   if(!valid){
   		createErrorMsg(id,msg);
   }
   
}

validatePostalCode = function(id,msg){
	valid = false;
	var value = $('#' + id).val(); 
	if(value.length = 10){
		var re = /^\d{5}([\-]\d{4})?$/;	
		if(re.test(value)){
			valid = true;
		}
	}
	if(value.length = 5){
		var re = /^\d{5}?$/;
		if(re.test(value)){
			valid = true;
		}
	}
	if(!valid){
		createErrorMsg(id,msg);
	}
}

createErrorMsg = function(id,msg){
	$('#' + id).css({'border':'1px solid #c61800'});
	    htmlString = '<span class="err" onclick="setFocus(';
	    htmlString = htmlString + "'" + id + "'";
	    htmlString = htmlString + ');">' + msg + '</span>';
	    errorMsg[errorCounter] = htmlString;
	    errorCounter++;	
}

setFocus = function(fieldID){
	$('#' + fieldID).focus();
}
function fn_NationalGuardLeadOptin(){
	$('#NationalGuardLeadOptin').click(function(){
			if ($('#NationalGuardLeadOptin').is(':checked')){
				fieldId.push('birthdate_mField|Please select the day of your date of birth');
				fieldId.push('birthdate_dField|Please select the month of your date of birth');
				fieldId.push('birthdate_yField|Please select the year of your date of birth');
				$('.dob').show();
			}else{
				fieldId.splice(8,3);
				$('.dob').hide();
			}
		}
	);
}
fn_NationalGuardLeadOptin();

$('#cancel').click(function(){
	$('#container').hide();
});	
$('#closeBTN').click(function(){
	$('#container').hide();
});	
$('#closeConfirmBTN').click(function(){
	$('#container').hide();
});
