function regFormValidation(){
    if(!checkBirthDate()){
        return false;
    }
    if(!validateEmail()){
        return false;
    }else if(( jQuery('#chkGiveaway').length && jQuery('#chkGiveaway').is(':checked')) || jQuery('#chkNewsLetter').is(':checked')){
        submit();
    }else{
        //jQuery('#registration-msg').html("Please select one or both of the options above to register.");
        jQuery("#registration-msg").css({'display' : 'block'});
        jQuery("#registration-msg-option").css({'display' : 'block'});
        return false;
    }
        
}
var invalidDate = false;
function checkBirthDate(){
    var day=document.regForm.day.value;
    
    var month=document.regForm.month.value;
    var year =document.regForm.year.value;

    if( day == "" || month == "" || year == "" ){
        var dateErrorMsg = jQuery('#invalid-date-msg').html();
        dateErrorMsg = jQuery.trim(dateErrorMsg);
        if(dateErrorMsg.length == 0){
            alert( "Please select a valid date");
        }else{
            alert(dateErrorMsg);
        }
        
        invalidDate = true;
        return false;
    }


    if (!checkDate(day,month,year)){
        jQuery.cookie('infamous2','true',{ path: '/' });
        //jQuery('#registration-msg').html("This feature is not available to your age group.");
        jQuery("#registration-msg").css({'display' : 'block'});
        jQuery("#registration-msg-agegroup").css({'display' : 'block'});
        jQuery("#btnRegister").attr("disabled", "true");
        
        jQuery.cookie('age_group',true,{ path: '/' });
        jQuery("#age_check_error").css({'display' : 'block'});
         enterSiteButton = true;
        return false;
    }else{
        jQuery.cookie('age_group',false,{ path: '/' });
        
    }
    return true;
}

function validateEmail() {
    var str = document.regForm.email.value;
    var at="@";var dot="."; var lat=str.indexOf(at); var lstr=str.lengthvar;
    ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1){
        alert("Please enter a valid E-mail ID "); 
        return false;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
        alert("Please enter a valid E-mail ID"); return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
        alert("Please enter a valid E-mail ID"); return false;
    }
    if (str.indexOf(at,(lat+1))!=-1){
        alert("Please enter a valid E-mail ID"); return false;
    } 
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        alert("Please enter a valid E-mail ID"); return false;
    } 
    if (str.indexOf(dot,(lat+2))==-1){ 
        alert("Please enter a valid E-mail ID"); 
        return false ;
    } 
    if (str.indexOf(" ")!=-1){ 
        alert("Please enter a valid E-mail ID") ;
        return false;
    } 
    return true;
}

/*function checkDate(day,month,year){
    var bod= new Date(year ,month,day);
    if( new Date().getYear() - bod.getYear() > 13 )
        return true;    
}*/

function checkDate(day,month,year){
    var min = 13;
    var territory = jQuery.cookie('territory')
    if(territory == null){
    	var matches = window.location.href.match('/[a-z]{2}_[A-Z]{2}/');
		if(matches[0]){
		    territory = matches[0].replace(/\//g,'');
		    jQuery.cookie('territory',territory,{path: '/'});
		}
    }
    if(territory == "jp_JP" || territory == "kr_KR")
        min = 18;
    var date_now = new Date();
    var date_age = new Date();
    if(jQuery("#age_check").length > 0){
        date_age = new Date(parseInt(jQuery("#age_check select[name='year']").val()) + parseInt(min), parseInt(jQuery("#age_check select[name='month']").val()) - 1, parseInt(jQuery("#age_check select[name='day']").val()));
    }else{
        date_age = new Date(parseInt(jQuery("#age select[name='year']").val()) + parseInt(min), parseInt(jQuery("#age select[name='month']").val()) - 1, parseInt(jQuery("#age select[name='day']").val()));
    }
    
    if((date_now.getTime() - date_age.getTime()) < 0 || isNaN(date_now.getTime() - date_age.getTime())) {
        return false;
    }
    else
        return true;
}
function submit(){
    document.forms[0].submit();
}


