﻿$(function() {
    //HideFriends();
    $('.error').hide();
	$('.edit').hide();
	$('.friendinput').hide();
	$('.friendinpute').hide();
	var toggle = 0;
	
   function TestEmail(email,number){
        var dataString = 'email_address=' + email;
        $.ajax({
            type: "POST",
            url: "ProcessEmail.aspx",
            data: dataString,
            success: function(response) {
                // Replace the div's content with the page method's return.
                if (response == "Success") {
                    $('label#notice').text("Already registered!");
                    $('label#AlreadyRegistered' + number).show();
                    $('label#AlreadyRegistered' + number).text(email);
                } else {
                    $('label#AlreadyRegistered' + number).hide();
                }
            }
        });   
    }	
	
	$("#friend1").click(function() {
	    if(toggle != 1){
	        $('.error').hide();
	        ProcessHide();
            $('#friend_name_1_ph').hide();
		    $('input#friend_name_1').show();
		    $('input#friend_name_1').focus();
		    $('#friend_email_1_ph').hide();
		    $('input#friend_email_1').show();
		}
		toggle = 1;
    });
    
	$("#friend_email_1").keyup(function() {
	    var email = $("input#friend_email_1").val();
        TestEmail(email,1);
    });
    
    $("#friend2").click(function() {
	    if(toggle != 2){
	        $('.error').hide();
	        ProcessHide();
            $('#friend_name_2_ph').hide();
		    $('input#friend_name_2').show();
		    $('input#friend_name_2').focus();
		    $('#friend_email_2_ph').hide();
		    $('input#friend_email_2').show();
		}
		toggle = 2;
    });
    
	$("#friend_email_2").keyup(function() {
	    var email = $("input#friend_email_2").val();
        TestEmail(email,2);
    });    
    
    $("#friend3").click(function() {
	    if(toggle != 3){
	        $('.error').hide();
	        ProcessHide();
            $('#friend_name_3_ph').hide();
		    $('input#friend_name_3').show();
		    $('input#friend_name_3').focus();
		    $('#friend_email_3_ph').hide();
		    $('input#friend_email_3').show();
		}
		toggle = 3;
    });
    
	$("#friend_email_3").keyup(function() {
	    var email = $("input#friend_email_3").val();
        TestEmail(email,3);
    });       
    
    $("#friend4").click(function() {
	    if(toggle != 4){
	        $('.error').hide();
	        ProcessHide();
            $('#friend_name_4_ph').hide();
		    $('input#friend_name_4').show();
		    $('input#friend_name_4').focus();
		    $('#friend_email_4_ph').hide();
		    $('input#friend_email_4').show();
		}
		toggle = 4;
    });
    
	$("#friend_email_4").keyup(function() {
	    var email = $("input#friend_email_4").val();
        TestEmail(email,4);
    });       
    
    $("#friend5").click(function() {
	    if(toggle != 5){
	        $('.error').hide();
	        ProcessHide();
            $('#friend_name_5_ph').hide();
		    $('input#friend_name_5').show();
		    $('input#friend_name_5').focus();
		    $('#friend_email_5_ph').hide();
		    $('input#friend_email_5').show();
		}
		toggle = 5;
    });  
    
	$("#friend_email_5").keyup(function() {
	    var email = $("input#friend_email_5").val();
        TestEmail(email,5);
    });       
    
    $("#friend6").click(function() {
	    if(toggle != 6){
	        $('.error').hide();
	        ProcessHide();
            $('#friend_name_6_ph').hide();
		    $('input#friend_name_6').show();
		    $('input#friend_name_6').focus();
		    $('#friend_email_6_ph').hide();
		    $('input#friend_email_6').show();
		}
		toggle = 6;
    });
    
	$("#friend_email_6").keyup(function() {
	    var email = $("input#friend_email_6").val();
        TestEmail(email,6);
    });       
    
    $("#friend7").click(function() {
	    if(toggle != 7){
	        $('.error').hide();
	        ProcessHide();
            $('#friend_name_7_ph').hide();
		    $('input#friend_name_7').show();
		    $('input#friend_name_7').focus();
		    $('#friend_email_7_ph').hide();
		    $('input#friend_email_7').show();
		}
		toggle = 7;
    });
    
	$("#friend_email_7").keyup(function() {
	    var email = $("input#friend_email_7").val();
        TestEmail(email,7);
    });       
    
    $("#friend8").click(function() {
	    if(toggle != 8){
	        $('.error').hide();
	        ProcessHide();
            $('#friend_name_8_ph').hide();
		    $('input#friend_name_8').show();
		    $('input#friend_name_8').focus();
		    $('#friend_email_8_ph').hide();
		    $('input#friend_email_8').show();
		}
		toggle = 8;
    });
    
	$("#friend_email_8").keyup(function() {
	    var email = $("input#friend_email_8").val();
        TestEmail(email,8);
    });       
    
    $("#friend9").click(function() {
	    if(toggle != 9){
	        $('.error').hide();
	        ProcessHide();
            $('#friend_name_9_ph').hide();
		    $('input#friend_name_9').show();
		    $('input#friend_name_9').focus();
		    $('#friend_email_9_ph').hide();
		    $('input#friend_email_9').show();
		}
		toggle = 9;
    });
    
	$("#friend_email_9").keyup(function() {
	    var email = $("input#friend_email_9").val();
        TestEmail(email,9);
    });       

    
    function ProcessHide() {
        for(var i = 1; i<10; i++){
            if($("input#friend_email_"+i).val() == ""){
			    $('#friend_email_'+i+'_ph').show();
			    $('input#friend_email_'+i).hide();
			    $("#friend"+i).addClass("tablelist");
		    }else{
		        var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
                var email_address = $("input#friend_email_"+i).val();
                if (!emailPattern.test(email_address)) {
                    //$('#friend_email_'+i+'_ph').css("color","#ff0000");
                    $("#friend"+i).addClass("tablelisterror");
                }else{
                    $('#friend_email_'+i+'_ph').css("color","#000000");
                    $("#friend"+i).addClass("tablelistdone");

                }
			    $('#friend_email_'+i+'_ph').text($("input#friend_email_"+i).val());
			    $('#friend_email_'+i+'_ph').show();
		    }
            $('input#friend_email_'+i).hide();
            
            if($("input#friend_name_"+i).val() == ""){
                $('#friend_name_'+i+'_ph').show();
            }else{
                $('#friend_name_'+i+'_ph').text($("input#friend_name_"+i).val());
                $('#friend_name_'+i+'_ph').show();
            }
            $('input#friend_name_'+i).hide();
        }
    } 
    
    $("#buttonframe").click(function() {
        //alert("Clicked");
        ProcessHide();
        $('.error').hide();
        var error = false;
        for(var i = 1; i<10; i++){
            var name = $("input#friend_name_"+i).val();
            var email = $("input#friend_email_"+i).val();
            //alert(email);
            if(email != ""){
                var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
                if (emailPattern.test(email)) {
                    //alert("Sending");
                    DoReferral2(name, email, i);
                    $("input#friend_name_"+i).val("");
                    $("input#friend_email_"+i).val("");
                }else{
                    error = true;
                }
            }
        }
        if(error){
            $('#NotepadEnter').show();
        }
        toggle = 0;
    });
    
    /*$("input#Send").click(function() {
        var error = false;
        //Validation
        HideFriends();
        $('.error').hide();

        var friend_name_1 = $("input#friend_name_1").val();
        var friend_email_1 = $("input#friend_email_1").val();
        var friend_name_2 = $("input#friend_name_2").val();
        var friend_email_2 = $("input#friend_email_2").val();
        var friend_name_3 = $("input#friend_name_3").val();
        var friend_email_3 = $("input#friend_email_3").val();
        var friend_name_4 = $("input#friend_name_4").val();
        var friend_email_4 = $("input#friend_email_4").val();

        if (friend_name_4 != "" || friend_email_4 != "") {
            if (friend_email_4 == "") {
                $('#Friend4').show();
                $("label#friend_email_4_error").show();
                $("input#friend_email_4").focus();
                error = true;
            }

            var emailPattern4 = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
            if (!emailPattern4.test(friend_email_4)) {
                $('#Friend4').show();
                $("label#friend_email_4_error").show();
                $("input#friend_email_4").focus();
                error = true;
            }

            if (friend_name_4 == "") {
                $('#Friend4').show();
                $("label#friend_name_4_error").show();
                $("input#friend_name_4").focus();
                error = true;
            }
        }

        if (friend_name_3 != "" || friend_email_3 != "") {
            if (friend_email_3 == "") {
                $('#Friend3').show();
                $("label#friend_email_3_error").show();
                $("input#friend_email_3").focus();
                error = true;
            }

            var emailPattern3 = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
            if (!emailPattern3.test(friend_email_3)) {
                $('#Friend3').show();
                $("label#friend_email_3_error").show();
                $("input#friend_email_3").focus();
                error = true;
            }

            if (friend_name_3 == "") {
                $('#Friend3').show();
                $("label#friend_name_3_error").show();
                $("input#friend_name_3").focus();
                error = true;
            }
        }

        if (friend_name_2 != "" || friend_email_2 != "") {
            if (friend_email_2 == "") {
                $('#Friend2').show();
                $("label#friend_email_2_error").show();
                $("input#friend_email_2").focus();
                error = true;
            }

            var emailPattern2 = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
            if (!emailPattern2.test(friend_email_2)) {
                $('#Friend2').show();
                $("label#friend_email_2_error").show();
                $("input#friend_email_2").focus();
                error = true;
            }

            if (friend_name_2 == "") {
                $('#Friend2').show();
                $("label#friend_name_2_error").show();
                $("input#friend_name_2").focus();
                error = true;
            }
        }

        if ((friend_name_1 != "" || friend_email_1 != "")) {
            if (friend_email_1 == "") {
                $('#Friend1').show();
                $("label#friend_email_1_error").show();
                $("input#friend_email_1").focus();
                error = true;
            }
            
            var emailPattern1 = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
            if (!emailPattern1.test(friend_email_1)) {
                $('#Friend1').show();
                $("label#friend_email_1_error").show();
                $("input#friend_email_1").focus();
                error = true;
            }

            if (friend_name_1 == "") {
                $('#Friend1').show();
                $("label#friend_name_1_error").show();
                $("input#friend_name_1").focus();
                error = true;
            }
        }

        if (error) {
            return false;
        }
        DoReferral(friend_name_1, friend_email_1, "invite_friend_1_msg");
        DoReferral(friend_name_2, friend_email_2, "invite_friend_2_msg");
        DoReferral(friend_name_3, friend_email_3, "invite_friend_3_msg");
        DoReferral(friend_name_4, friend_email_4, "invite_friend_4_msg");
    });*/

    /*$("#Friend1Labels").click(function() {
        HideFriends();
        $('#Friend1Labels').hide();
		$('#Friend1Edit').show();
    });

    $("#Friend2Labels").click(function() {
        HideFriends();
        $('#Friend2Labels').hide();
		$('#Friend2Edit').show();
    });

    $("label#invite_friend_3").click(function() {
        HideFriends();
        $('#Friend3').show();
    });

    $("label#invite_friend_4").click(function() {
        HideFriends();
        $('#Friend4').show();
    });

    function HideFriends() {
        $('#Friend1').hide();
        $('#Friend2').hide();
        $('#Friend3').hide();
        $('#Friend4').hide();
		$('#Friend1Edit').hide();
		$('#Friend2Edit').hide();

        $("label#friend_name_1_label").text($("input#friend_name_1").val());
        $("label#friend_email_1_label").text($("input#friend_email_1").val());
        $("label#friend_name_2_label").text($("input#friend_name_2").val());
        $("label#friend_email_2_label").text($("input#friend_email_2").val());
        $("label#friend_name_3_label").text($("input#friend_name_3").val());
        $("label#friend_email_3_label").text($("input#friend_email_3").val());
        $("label#friend_name_4_label").text($("input#friend_name_4").val());
        $("label#friend_email_4_label").text($("input#friend_email_4").val());
    }*/

    function DoReferral2(name, email, labelno) {
        if (email != "") {
            var dataString = 'name=' + name + '&email=' + email;

            //alert (dataString);return false;
            $.ajax({
                type: "POST",
                url: "ProcessTablePlanner.aspx",
                data: dataString,
                success: function(response) {
                    // Replace the div's content with the page method's return.
                    //$("label#" + label).text("Emailing status: " + response);
                    $("#friend" + labelno).fadeOut("slow");
                }
            });
        }
	}
	
	checkRow = function(row,rowMax){
		if(row==rowMax){
			
				$("#TableNext").hide();
				$("#NextIn").show();	
				$("#PrevIn").hide();	
				
				$("#TablePrev").show();
		}
		else if(row==0){
			
				$("#TablePrev").hide();
				$("#PrevIn").show();	
				$("#TableNext").show();
				$("#NextIn").hide();	

		}
		else{
			$("#TableNext").show();
			$("#TablePrev").show();
			$("#NextIn").hide();
			$("#PrevIn").hide();
		}
		
	}
	
	var slideAmount =$("#community_memberContainer").height();
	var fN = $("input#ctl00_LeftContent_FriendNumber").val();
	 if(fN>12){
		$("#NextIn").hide();
		$("#PrevIn").hide();
		 var row = 0;
		 var rowMax = Math.ceil((fN-12)/4);
		checkRow(row,rowMax);

		 
	 $("#TableNext").click(function() {
		var mtop="";
		var mtop2="";
		var mtopnew="";
		var mtopnew2="";
		var mtop = row*(-slideAmount);
		//var mtop2 = mtop.substring(0, mtop.length-2);  
		var mtopnew = mtop-slideAmount;
		var mtopnew2 = mtopnew+'px';
		
   		row ++;
		$("#friendInner").animate({marginTop:mtopnew}, 500);   
		checkRow(row,rowMax);
		});
	 $("#TablePrev").click(function() {
		var mtop="";
		var mtop2="";
		var mtopnew="";
		var mtopnew2="";

		var mtop = row*(-slideAmount);
		//var mtop2 = mtop.substring(0, mtop.length-2); 
		//var mtop3 = parseInt(mtop2);
		var mtopnew = mtop+slideAmount;
		var mtopnew2 = mtopnew+'px';
		row --;
   
		$("#friendInner").animate({marginTop:mtopnew}, 500);   
				checkRow(row,rowMax);

		});
	 
	 }
	 else{
		$("#TableNext").hide();
			$("#TablePrev").hide();
			$("#NextIn").show();
			$("#PrevIn").show(); 
	 }
	 
	
	Notechecknrow = function(nrow,nrowMax){
		if(nrow==nrowMax){
				$("#NoteNext").hide();
				$("#NoteNextIn").show();	
				$("#NotePrev").show();
				$("#NotePrevIn").hide();	

		}
		else if(nrow==0){
			
				$("#NotePrev").hide();
				$("#NotePrevIn").show();	
				$("#NoteNext").show();
				$("#NoteNextIn").hide();	

		}
		else{
			$("#NoteNext").show();
			$("#NotePrev").show();
			$("#NoteNextIn").hide();
			$("#NotePrevIn").hide();
		}
		
	}
	
	var nN = $("#notesInner").height();
	var notesSlideAmount = 200;
	 if(nN>390){
		nrow = 0;
		nrowMax = Math.ceil((nN-390)/notesSlideAmount);
		$("#NotePrevIn").hide();
		$("#NoteNextIn").hide();
		
		Notechecknrow(nrow,nrowMax);
		
		$("#NoteNext").click(function() {
	
		var mtop = nrow*(-notesSlideAmount);
		//var mtop2 = mtop.substring(0, mtop.length-2);  
		var mtopnew = mtop-notesSlideAmount;
		var mtopnew2 = mtopnew+'px';
		
   		nrow ++;
		$("#notesInner").animate({marginTop:mtopnew}, 1000);   
		Notechecknrow(nrow,nrowMax);
		});
	 $("#NotePrev").click(function() {

		var mtop = nrow*(-notesSlideAmount);
		//alert (mtop);
		//var mtop2 = mtop.substring(0, mtop.length-2); 
		//var mtop3 = parseInt(mtop2);
		var mtopnew = mtop+notesSlideAmount;
		var mtopnew2 = mtopnew+'px';
		nrow --;
   
		$("#notesInner").animate({marginTop:mtopnew}, 1000);   
				Notechecknrow(nrow,nrowMax);

		});
	
	
	 }
	  else{
		$("#NoteNext").hide();
			$("#NotePrev").hide();
			$("#NoteNextIn").show();
			$("#NotePrevIn").show(); 
	 }
	 
	 

    $("input#PostNote").click(function() {
        //alert("Join");
        var error = false;
        var clientidprefix = "ctl00_LeftContent_";
        //Validation
        $('.error').hide();

        var note = $("input#" + clientidprefix + "NoteTextBox").val();
        note = note.replace(/&/,"and");
        if (note == "") {
            $("#NoteTextBox_error").show();
            $("input#" + clientidprefix + "NoteTextBox").focus();
            error = true;
        }

        if (error) {
            return false;
        }

        var dataString = 'note=' + note;

        $.ajax({
            type: "POST",
            url: "ProcessPostNote.aspx",
            data: dataString,
            success: function(response) {
                // Replace the div's content with the page method's return.
                if (response == "Success") {
                    document.location = "ViewTable.aspx";
                } else {
                    $("#Note_error").show();
                }
            }
        });
    });
    
    $("div.notepad_a_bottom_d").click(function() {
	    //alert($(this).attr("id"));
	    var noteid = $(this).attr("id");
	    //alert(noteid);
	    var dataString = 'noteid=' + noteid;
        $.ajax({
            type: "POST",
            url: "ProcessDeleteNote.aspx",
            data: dataString,
            success: function(response) {
                // Replace the div's content with the page method's return.
                if (response == "Success") {
                    //alert("yay");
                    $("#Note" + noteid).fadeOut("slow");
                } else {
                    //alert("error");
                }
            }
        });
    });
    $("div.notepad_b_bottom_d").click(function() {
	    //alert($(this).attr("id"));
	    var noteid = $(this).attr("id");
	    //alert(noteid);
	    var dataString = 'noteid=' + noteid;
        $.ajax({
            type: "POST",
            url: "ProcessDeleteNote.aspx",
            data: dataString,
            success: function(response) {
                // Replace the div's content with the page method's return.
                if (response == "Success") {
                    //alert("yay");
                    $("#Note" + noteid).fadeOut("slow");
                } else {
                    //alert("error");
                }
            }
        });
    });
});
  