var ajaxCallValidation=false;

$.fn.appearCenter = function(){
    this.css("position", "absolute");
	var top = ($(window).height() - this.height()) / 2 + $(window).scrollTop();
	var left = ($(window).width() - this.width()) / 2 + $(window).scrollLeft();
	top = (top < 0) ? 0 : top;
	left = (left < 0) ? 0 : left;
    this.css("top", top + "px");
    this.css("left", left + "px");
    return this;
}

$(function(){
/* send invitations pop-up */
	$('#module_AddressBook #ab_import7').click(function(){
	     document.getElementById('errorMsg1').innerHTML='';
	     document.getElementById('errorMsg1').style.display="none";
	     var countContact = document.getElementById('contactLength').value;
	     if(countContact==0){
	     // alert("You do not have any contacts saved");
	        err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please Add a Contact to your Address Book."
		    }else{
			$('#ab_curtain').appendTo('body').css({opacity: '0.5', height: $(document).height() + "px" }).show();
			$('#add_from_add_book').appendTo('body').appearCenter().show();
			$('body #add_from_add_book').css({display:'block'});
		  }      
	  });
	
	/* address book module - high level - filter tabs */
	$('#module_AddressBook #ab_tabs li').click(function(){
	   
		$(this).siblings().each(function(){
			$(this).removeClass('active');
		});
		$(this).addClass('active');
		
		/* DO MORE FILTERING UPDATES HERE */
	});
	
	/* address book module - high level - filter list */
	$('#module_AddressBook #ab_filter li').click(function(){
	    var contentVal = $(this).html();
	    contentVal=contentVal.split('(')[1];
	    contentVal=contentVal.split(')')[0];
	    if(contentVal=="0") {return false;}
		$(this).siblings().each(function(){
			$(this).removeClass('active');
		});
		$(this).addClass('active');
		
		/* DO MORE FILTERING UPDATES HERE */
	});
	
	/* address book module - high level - custom group - hover */
	$('#module_AddressBook #ab_filter li.custom').hover(function(){
		$(this).children('.remove_icn').show();
	}, function(){
		$(this).children('.remove_icn').hide();
	});
	
	/* address book module - high level - custom group - remove click */
	$('#module_AddressBook #ab_filter li.custom a.remove_icn').click(function(e){
		$('#ab_curtain').appendTo('body').css({opacity: '0.5', height: $(document).height() + "px" }).show();
		$('#delete_group').appendTo('body').appearCenter().show();
		e.stopPropagation();
	});
	
	/* address book module - view contacts screen - contact click */
	$('#module_AddressBook #ab_contacts li').click(function(){
		$(this).siblings().removeClass('selected');
		$(this).addClass('selected');
		$('#ab_contact_info').show();
	});
	
	/* address book module - view contacts screen - delete contact click */
	$('#module_AddressBook #ab_contact_info .delete_contact_link').click(function(){
		$('#ab_curtain').appendTo('body').css({opacity: '0.5', height: $(document).height() + "px" }).show();
		$('#delete_contact').appendTo('body').appearCenter().show();
	});
	
	/* address book module - add contact screen - new group option */
	$('#module_AddressBook #ab_add_contact #ab_group').change(function(){
		if ($(this).val() == "new-group") {
			$('#new_group').appendTo('body').appearCenter().show();	
		}
	});
	
	/* address book module - sent invitations screen - reset checkboxes - checkbox click event */
	$('#module_AddressBook #ab_sent_invitations input[type=checkbox]').each(function(){
		/* reset checkboxes - checkbox click event */
		$(this).attr('checked', '').change(function(){
			/* find the number of checkboxes checked*/
			var total_selected = $('#module_AddressBook #ab_sent_invitations input[type=checkbox]').filter(':checked').length;
			if (total_selected > 0) {
				$('#module_AddressBook #ab_selected #count').html(total_selected);
				$('#module_AddressBook #full_col').addClass('with_full_footer with_background');
				$('#module_AddressBook #full_footer').show();
			} else {
				$('#module_AddressBook #full_footer').hide();
				$('#module_AddressBook #full_col').removeClass('with_full_footer with_background');
			}
		});
	});
	
	/* address book module - modals - select all click */
	$('.import_add_select input.select_all').click(function(){
		$(this).siblings('#layer_section').find('input[type=checkbox]').attr('checked', $(this).is(':checked'));	
	});
	
	
	$('#module_AddressBook #ab_add_contact #addPhone a').click(function(){
		addNewPhone();
	});
	$('#module_AddressBook #ab_add_contact #addEmail a').click(function(){
		addNewEmail();
	});
	
	$('#module_AddressBook #ab_add_contact #addAddress a').click(function(){
		addNewAddress();
	});
	
	$('#module_AddressBook #ab_add_contact #addImportantDate a').click(function(){
		addNewImportantDate();
	});
	
	$('#module_AddressBook #ab_add_contact #saveContact a').click(function(){
		validateContact();
	});
	
	
	/* address book module - add contact screen - "add additional" areas */
	// set up the duplicate rows
	var email_section = $('#module_AddressBook #ab_add_contact #ab_email_section div:first').clone(); 		var email_count = 1;
	var phone_section = $('#module_AddressBook #ab_add_contact #ab_phone_section div:first').clone(); 		var phone_count = 1;
	var address_section = $('#module_AddressBook #ab_add_contact #ab_address_section div:first').clone(); 	var address_count = 1;
	var id_section = $('#module_AddressBook #ab_add_contact #ab_id_section div:first').clone(); 			var id_count = 1;
	
	// bind the clicks
	$('#module_AddressBook #ab_add_contact .ab_add_button a').live('click', addAnotherClick);
	
	var email_message = '<span class="icn_small_plus"></span>Add Additional Email Address <div class="message_box">You can save up to three email addresses. Please remove an existing email address to add a new one.</div>';
	var phone_message = '<span class="icn_small_plus"></span>Add Additional Phone Number <div class="message_box">You can save up to three phone numbers. Please remove an existing phone number to add a new one</div>';
	var address_message = '<span class="icn_small_plus"></span>Add Additional Address <div class="message_box">You can save up to three addresses. Please remove an existing address to add a new one.</div>';
	var id_message = '<span class="icn_small_plus"></span>Add Additional Important Date <div class="message_box">You can save up to three important dates. Please remove an existing important date to add a new one.</div>';
	
	// click method
	function addAnotherClick() {
		switch ($(this).parent('div').parent('div').parent('div').attr('id')) {
			case "ab_email_section":
				var new_email = $(email_section).clone();
				email_count++;
				if (email_count >= 3) {
					$(new_email).find('.ab_add_button').html(email_message);
				}
				$(new_email).appendTo('#ab_email_section');
				break;
			case "ab_phone_section":
				var new_phone = $(phone_section).clone();
				phone_count++;
				if (phone_count >= 3) {
					$(new_phone).find('.ab_add_button').html(phone_message);		
				}
				$(new_phone).appendTo('#ab_phone_section');
				break;
			case "ab_address_section":
				var new_address = $(address_section).clone();
				address_count++;
				if (address_count >= 3) {
					$(new_address).find('.ab_add_button').html(address_message);	
				}
				$(new_address).appendTo('#ab_address_section');
				break;
			case "ab_id_section":
				var new_id = $(id_section).clone();
				id_count++;
				if (id_count >= 3) {
					$(new_id).find('.ab_add_button').html(id_message);		
				}
				$(new_id).appendTo('#ab_id_section');
				break;
		}
		$(this).parent('div').remove();
	}
	
	/* address book module - import & add modals */
	$('#module_AddressBook #ab_import_contacts_link').click(function(){
		// set up the curtain
		$('#ab_curtain').appendTo('body').css({opacity: '0.5', height: $(document).height() + "px" }).show();
		//$('body').css('overflow', 'hidden');
		
		// show the layer
		$('#add_contacts').appendTo('body').centerOnScreen().show();
	});
	
/* These are temporary */
	$('#module_AddressBook #ab_import1').click(function(){
		$('#ab_curtain').appendTo('body').css({opacity: '0.5', height: $(document).height() + "px" }).show();
		$('#import_from_site').appendTo('body').centerOnScreen().show();
	});
	$('#module_AddressBook #ab_import2').click(function(){
		$('#ab_curtain').appendTo('body').css({opacity: '0.5', height: $(document).height() + "px" }).show();
		$('#import_from_gmail').appendTo('body').centerOnScreen().show();
	});
	$('#module_AddressBook #ab_import3').click(function(){
		$('#ab_curtain').appendTo('body').css({opacity: '0.5', height: $(document).height() + "px" }).show();
		$('#import_from_outlook').appendTo('body').centerOnScreen().show();
	});
	$('#module_AddressBook #ab_import4').click(function(){
		$('#ab_curtain').appendTo('body').css({opacity: '0.5', height: $(document).height() + "px" }).show();
		$('#import_select_contacts').appendTo('body').centerOnScreen().show();
	});
	$('#module_AddressBook #ab_import5').click(function(){
		$('#ab_curtain').appendTo('body').css({opacity: '0.5', height: $(document).height() + "px" }).show();
		$('#add_contacts').appendTo('body').centerOnScreen().show();
	});
	$('#module_AddressBook #ab_import6').click(function(){
		$('#ab_curtain').appendTo('body').css({opacity: '0.5', height: $(document).height() + "px" }).show();
		$('#add_from_add_book').appendTo('body').centerOnScreen().show();
	});
/* End temporary */
	
	
	
	
	
	
	
	$('#ab_close_layer,  #ab_close_link').live('click', function(){
		$('#ab_curtain').hide();
		$('.ab_layer').hide();
		$('#msg_ab_curtain').hide();//Change to hide curtain - Kala
	});
	$('#bottom_close').live('click', function(){
		fnDoneAdding();
		$('#ab_curtain').hide();
		$('.ab_layer').hide();
	});
	$('#module_AddressBook #ab_add_contact #ab_phone_1').change(function(){
			if(document.getElementById('ab_phone_1')){
				var value=document.getElementById('ab_phone_1').value;
				if(value.length == 0){
					document.getElementById("ab_phone_type_1").selectedIndex = 0;
				}
			}
	});
	$('#module_AddressBook #ab_add_contact #ab_id_month_1').change(function(){
			if(document.getElementById('ab_id_month_1')){
				var value=document.getElementById('ab_id_month_1').value;
				if(value == 0){
					document.getElementById("ab_id_occasion_1").selectedIndex = 0;
				}
			}
	});
	
	/* Address field onChange */
	$('#module_AddressBook #ab_add_contact #ab_address1_1').change(function(){
			if(document.getElementById('ab_address1_1')){
				fnOnchangeForAddress(1);
			}
});
	$('#module_AddressBook #ab_add_contact #ab_address2_1').change(function(){
			if(document.getElementById('ab_address2_1')){
				fnOnchangeForAddress(1);
			}
	});
	$('#module_AddressBook #ab_add_contact #ab_city_1').change(function(){
			if(document.getElementById('ab_city_1')){
				fnOnchangeForAddress(1);
			}
	});
	$('#module_AddressBook #ab_add_contact #ab_zip_1').change(function(){
			if(document.getElementById('ab_zip_1')){
				fnOnchangeForAddress(1);
			}
	});
});


function fnOnchangeForAddress(count){
	var address1=document.getElementById('ab_address1_'+count).value;
	var address2=document.getElementById('ab_address2_'+count).value;
	var city = document.getElementById('ab_city_'+count).value;
	var zipCode = document.getElementById('ab_zip_'+count).value;
	
	if(address1.length == 0 && address2.length == 0 && city.length == 0 
			&& zipCode.length==0){
		document.getElementById("ab_location_"+count).selectedIndex = 0;
	}
}


function fnShowScreenNameModal(){
	if($('div#loginLayer').length == 0){
                        //Create the curtain
                        $('<div id="loginCurtain"></div>').appendTo('body').css({
                                        'position': 'absolute',
                                        'zIndex': '9998',
                                        'top': '0',
                                        'left': '0',
                                        'backgroundColor': '#000000',
                                        'opacity': '0.5',
                                        'display': 'none'
                        });

                        // Create the layer container
                        $('<div id="loginLayer"></div>').appendTo('body').css({
                                        'position': 'absolute',
                                        'zIndex': '9999',
                                        'top': '0',
                                        'left': '0',
                                        'display': 'none'
                        });

                        $('#loginCurtain').css({
                                        height: $(document).height(),
                                        width: $(document).width()
                        }).show();
        	}
        	$('div#loginCurtain').show();
        	$('div#loginLayer').show().centreScreenName();
        	$.get("ScreennameLoginModalView", { storeId: "10153", catalogId: "12605"},
       	 function(data){
            parent.document.getElementById('loginLayer').innerHTML=data;
       	 });

}

function submitScreenname(){    
	
	//For Fb Share Public profile
	var fbShare = 'false';
	if((document.getElementById('fbShareVar')!=null) && (document.getElementById('fbShareVar').value == 'true'))   {
	var fbShare =  document.getElementById('fbShareVar').value;
	
	}
	//For Fb Share Public profile
	
	     
	var ScreennameLogonCmdName = 'ScreennameLogonCmd';
    var scrName = document.getElementById('screen_name');
    if(IsEmpty(scrName)){
        document.getElementById('error_box').style.display='block';
        document.getElementById('error_box').innerHTML = 'Please enter a valid screen name';
    }else if((typeof(scrName) != 'undefined') && (ab_screenNameValidation(scrName.value)==0)){
              document.getElementById('error_box').style.display='block';
              document.getElementById('error_box').innerHTML = "Screen name should be between 2 and 18 characters long and contain only letters and numbers and '-', but must begin with a letter"; 
    }else{
    	var fromHT='';
    		var currentURL = window.location.href;
		var splitURL = currentURL.split('shc/s');
		var domain = splitURL[0];
		var ifHT = splitURL[1].indexOf('DisplayHolidayTree');
		if(ifHT != -1)
		{
			fromHT = 'true';
		}
		var secureCmd = domain.indexOf('https:');
		if(secureCmd==-1)
		{
		ScreennameLogonCmdName = 'ScreennameLogonCmdHttp';
		}
		else
		{
		ScreennameLogonCmdName = 'ScreennameLogonCmd';
		}
            $.get(ScreennameLogonCmdName, { storeId: "10153", catalogId: "12605", flag:'true', screenName:$('#screen_name').val(), redirectURL:parent.location.href, fromHT:fromHT, fbShare: fbShare  },
                        function(data){                 
                        document.getElementById('screennameResponse').innerHTML = data;
                        var response = document.getElementById('response').innerHTML;
                        if(response =='Y'){
                        if((document.getElementById('fbShareResponse')!=null) && (document.getElementById('fbShareResponse').innerHTML!=null) && (document.getElementById('fbShareResponse').innerHTML=='true'))
                        	{
		                                document.getElementById('screennameAvailable').value = 'true'; 
		                                closeScreennameModalPopup();
		                                sharePPAction(); 
		                    }
		                   
                        var frmHtpage = document.getElementById('frmHolidayTree').innerHTML;
                        	if(frmHtpage == 'true')
                        	{	

                        		var pageURL = window.location.href;

								pageURL += '&gotScreenName=true';
								window.location.href = pageURL ;

                        	}
                        	else 
                        	{
                            	parent.location.href=parent.location.href;
                        	}
                        	
                        
		                              
                        }
                        else if(response =='N'){
                            document.getElementById('error_box').style.display='block';
                            document.getElementById('error_box').innerHTML = 'This screenname already exists. Please enter another Screen name.';                                                                                
                        }else if(response =='PF'){
                         document.getElementById('error_box').style.display='block';
                         document.getElementById('error_box').innerHTML = 'Profanity check failed. Please enter another screenname.';                                                                   
                        }else{
	                        document.getElementById('error_box').style.display='block';
	                        document.getElementById('error_box').innerHTML = 'Error occurred. Please try again.';
                        }    
        	});           
      }
}

function closeScreennameModalPopup(){
				parent.document.getElementById('loginCurtain').style.display='none';
				parent.document.getElementById('loginLayer').style.display='none';
				parent.document.getElementById('loginLayer').innerHTML='';
		}

function IsEmpty(aTextField) {
	var IsEmpty = false;
   	if ((typeof(aTextField) != 'undefined') && ((aTextField.value.length==0)||(aTextField.value==null))){
      	IsEmpty = true;
   	}
  	return IsEmpty;
}
			
function ab_screenNameValidation(name){
	  if(!((name.charCodeAt(0)>64 && name.charCodeAt(0)<91)||(name.charCodeAt(0)>96 && name.charCodeAt(0)<123))){
	        return 0;
       }
      if(name.length < 2){
		return 0;
	  }
       
   	  for(iLoop=1;iLoop<name.length;iLoop++){
              if(!((name.charCodeAt(iLoop)>64 && name.charCodeAt(iLoop)<91)||(name.charCodeAt(iLoop)>96 && name.charCodeAt(iLoop)<123)||(name.charCodeAt(iLoop)>47 && name.charCodeAt(iLoop)<58)||(name.charCodeAt(iLoop)==45))){
                     return 0;
              }
	  }                       
	  if(iLoop == name.length){
	            return 1;
	   }
}

			
function nameValidation(name){
	  if(!((name.charCodeAt(0)>64 && name.charCodeAt(0)<91)||(name.charCodeAt(0)>96 && name.charCodeAt(0)<123))){
	        return 0;
       }
   	  for(iLoop=1;iLoop<name.length;iLoop++){
              if(!((name.charCodeAt(iLoop)>64 && name.charCodeAt(iLoop)<91)||(name.charCodeAt(iLoop)>96 && name.charCodeAt(iLoop)<123)||(name.charCodeAt(iLoop)>47 && name.charCodeAt(iLoop)<58)||(name.charCodeAt(iLoop)==32)||(name.charCodeAt(iLoop)==39)||(name.charCodeAt(iLoop)==45)||(name.charCodeAt(iLoop)==46))){
                     return 0;
              }
	  }                       
	  if(iLoop == name.length){
	            return 1;
	   }
}
function textValidation(name){ 
   	  for(iLoop=0;iLoop<name.length;iLoop++){
              if(!((name.charCodeAt(iLoop)>64 && name.charCodeAt(iLoop)<93)||(name.charCodeAt(iLoop)>96 && name.charCodeAt(iLoop)<123)||(name.charCodeAt(iLoop)>43 && name.charCodeAt(iLoop)<58)||(name.charCodeAt(iLoop)==32)
              ||(name.charCodeAt(iLoop)==39)||(name.charCodeAt(iLoop)==37) || (name.charCodeAt(iLoop)==40) || (name.charCodeAt(iLoop)==41) || (name.charCodeAt(iLoop)==33) || (name.charCodeAt(iLoop)==36) || (name.charCodeAt(iLoop)==34 ) || (name.charCodeAt(iLoop)==10 ))){
                     return 0;
              }
	  }                       
	  if(iLoop == name.length){
	            return 1;
	   }
}

//Adding new row for Phone
function addNewPhone(){
	var phoneCount=document.getElementById('phoneCount').value;
	$('#module_AddressBook #ab_add_contact #ab_phone_section').find('.ab_add_button').remove();
	if(phoneCount<=3){
		document.getElementById('phoneCount').value++;
		var count=++phoneCount;
		var obj="<div class='form_row'><label>Phone Number</label>";
		obj+="<input type='text' name='ab_phone_"+count+"' id='ab_phone_"+count+"'/>"; 
		obj+="<label>Type</label><select name='ab_phone_type_"+count+"' id='ab_phone_type_"+count+"'>";				 
		obj+=document.getElementById('ab_phone_type_1').innerHTML;				
		obj+="</select>";
		obj+="<a href='javascript:removePhone("+count+");' class='removeChoice' id='ab_email_removePhone_"+count+"'>Remove</a>";
		if(phoneCount!=3){
			obj+="<div id='addPhone' class='ab_add_button'><a href='javascript:;'><span class='icn_small_plus'>";
			obj+="</span>Add Additional Phone Number</a></div>";
		}else if(phoneCount==3){
			obj+="<div id='addPhone' class='ab_add_button'><span class='icn_small_plus_new'>";
			obj+="</span>Add Additional Phone Number<div class='message_box'>You can save up to three phone numbers. Please remove an existing phone number to add a new one</div></div>";
		}
		obj+="</div>";
		
		$('#module_AddressBook #ab_add_contact #ab_phone_section').append(obj).show();
		$('#module_AddressBook #ab_add_contact #addPhone a').click(function(){
			addNewPhone();
		});
		
		$('#module_AddressBook #ab_add_contact #ab_phone_'+count).change(function(){
			if(document.getElementById('ab_phone_'+count)){
				var value=document.getElementById('ab_phone_'+count).value;
				if(value.length == 0){
					document.getElementById("ab_phone_type_"+count).selectedIndex = 0;
				}
			}
		});
		
		
		$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_type_'+count+' option').attr('selected','');
		document.getElementById("ab_phone_type_"+count).selectedIndex = 0;
		
		
		
	}
}
function removePhone(index){
	$('#module_AddressBook #ab_add_contact #ab_phone_section').find('.ab_add_button').remove();
	var loc=document.getElementById('ab_email_removePhone_'+index);
	$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_'+index).parent().remove();
	$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_type_'+index).parent().remove();
	var phoneCount=document.getElementById('phoneCount').value;
	document.getElementById('phoneCount').value=phoneCount-1;

	var obj="<div id='addPhone' class='ab_add_button'><a href='javascript:;'><span class='icn_small_plus'></span>Add Additional Phone Number</a></div>";
	if(index==2){
		if(document.getElementById('ab_phone_3')){
			$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_3').parent().append(obj);
			$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_3').attr('name','ab_phone_2');
			$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_type_3').attr('name','ab_phone_type_2');
			$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_3').attr('id','ab_phone_2');
			$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_type_3').attr('id','ab_phone_type_2');
			$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_email_removePhone_3').attr('href','javascript:removePhone(2)');
			$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_email_removePhone_3').attr('id','ab_email_removePhone_2');
		}else{
			$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_1').parent().append(obj);
		}
		
	}else if(index==3){
	     if(document.getElementById('ab_phone_2')){
			$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_2').parent().append(obj);
		}else{
			$('#module_AddressBook #ab_add_contact #ab_phone_section #ab_phone_1').parent().append(obj);
		}
	}
	$('#module_AddressBook #ab_add_contact #addPhone a').click(function(){
			addNewPhone();
	});
}
//Adding new row for Email
function addNewEmail(){
	var emailCount=document.getElementById('emailCount').value;
	$('#module_AddressBook #ab_add_contact #ab_email_section').find('.ab_add_button').remove();
	if(emailCount<=3){
		document.getElementById('emailCount').value++;
		var count=++emailCount;
		var obj="<div class='form_row'><label>Email Address</label><input type='text' name='ab_email_"+count+"' id='ab_email_"+count+"' maxlength='255'/> ";
		obj+="<a href='javascript:removeEmail("+count+")' class='removeChoice' id='ab_email_removeEmail_"+count+"'>Remove</a>";
		if(emailCount!=3){
			obj+="<div id='addEmail' class='ab_add_button'><a href='javascript:;'><span class='icn_small_plus'></span>Add Additional Email Address</a></div>";
		}else if(emailCount==3){
			obj+="<div id='addEmail' class='ab_add_button'><span class='icn_small_plus_new'></span>Add Additional Email Address <div class='message_box'>You can save up to three email addresses. Please remove an existing email address to add a new one.</div></div>";
		}
		obj+="</div>";
		
		$('#module_AddressBook #ab_add_contact #ab_email_section').append(obj).show();
		$('#module_AddressBook #ab_add_contact #addEmail a').click(function(){
			addNewEmail();
		});
		
	}
}

function removeEmail(index){
	$('#module_AddressBook #ab_add_contact #ab_email_section').find('.ab_add_button').remove();
	var loc=document.getElementById('ab_email_removeEmail_'+index);
	$('#module_AddressBook #ab_add_contact #ab_email_section #ab_email_'+index).parent().remove();
	var emailCount=document.getElementById('emailCount').value;
	document.getElementById('emailCount').value=emailCount-1;
	
	var obj="<div id='addEmail' class='ab_add_button'><a href='javascript:;'><span class='icn_small_plus'></span>Add Additional Email Address</a></div>";
	if(index==2){
		if(document.getElementById('ab_email_3')){
			$('#module_AddressBook #ab_add_contact #ab_email_section #ab_email_3').parent().append(obj);
			$('#module_AddressBook #ab_add_contact #ab_email_section #ab_email_3').attr('name','ab_email_2');
			$('#module_AddressBook #ab_add_contact #ab_email_section #ab_email_3').attr('id','ab_email_2');
			$('#module_AddressBook #ab_add_contact #ab_email_section #ab_email_removeEmail_3').attr('href','javascript:removeEmail(2)');
			$('#module_AddressBook #ab_add_contact #ab_email_section #ab_email_removeEmail_3').attr('id','ab_email_removeEmail_2');
		}else{
			$('#module_AddressBook #ab_add_contact #ab_email_section #ab_email_1').parent().append(obj);
		}
		
	}else if(index==3){
	     if(document.getElementById('ab_email_2')){
			$('#module_AddressBook #ab_add_contact #ab_email_section #ab_email_2').parent().append(obj);
		}else{
			$('#module_AddressBook #ab_add_contact #ab_email_section #ab_email_1').parent().append(obj);
		}
	}
	$('#module_AddressBook #ab_add_contact #addEmail a').click(function(){
			addNewEmail();
	});
}

//Adding new row for Address
function addNewAddress(){
	var addressCount=document.getElementById('addressCount').value;
	$('#module_AddressBook #ab_add_contact #ab_address_section').find('.ab_add_button').remove();
	if(addressCount<=3){
		document.getElementById('addressCount').value++;
		var count=++addressCount;
		var obj="<div class='form_row'><label>Address 1</label><input type='text' name='ab_address1_"+count+"' id='ab_address1_"+count+"'/>";
		obj+="<label>Location</label><select id='ab_location_"+count+"' name='ab_location_"+count+"'>";
		obj+=document.getElementById("ab_location_1").innerHTML;
		obj+="</select>"; 
		obj+="<br clear='all'><label>Address 2</label><input type='text' name='ab_address2_"+count+"' id='ab_address2_"+count+"'/><br clear='all'>";
		obj+="<label>City</label><input type='text' name='ab_city_"+count+"' id='ab_city_"+count+"'/><label>State</label>";
		obj+="<select class='small' name='ab_state_"+count+"' id='ab_state_"+count+"'>";				
		obj+=document.getElementById('ab_state_1').innerHTML;				 
		obj+="</select>";
		obj+="<label>ZIP Code</label><input type='text' maxlength='5' class='small' name='ab_zip_"+count+"' id='ab_zip_"+count+"'/>";
		obj+="<a href='javascript:removeAddress("+count+");' class='removeChoice' id='ab_email_removeAddress_"+count+"'>Remove</a>";
		if(addressCount!=3){
			obj+="<div id='addAddress' class='ab_add_button'><a href='javascript:;'><span class='icn_small_plus'></span>Add Additional Address</a></div>";
		}else if(addressCount==3){
			obj+="<div id='addAddress' class='ab_add_button'><span class='icn_small_plus_new'></span>Add Additional Address<div class='message_box'>You can save up to three addresses. Please remove an existing address to add a new one.</div></div>";
		}
		
		obj+="<br clear='all'><br clear='all'></div>";
		$('#module_AddressBook #ab_add_contact #ab_address_section').append(obj).show();
		$('#module_AddressBook #ab_add_contact #addAddress a').click(function(){
			addNewAddress();
		});
		$('#module_AddressBook #ab_add_contact #ab_address1_'+count).change(function(){
			if(document.getElementById('ab_address1_'+count)){
				fnOnchangeForAddress(count);
			}
	});
	$('#module_AddressBook #ab_add_contact #ab_address2_'+count).change(function(){
			if(document.getElementById('ab_address2_'+count)){
				fnOnchangeForAddress(count);
			}
	});
	$('#module_AddressBook #ab_add_contact #ab_city_'+count).change(function(){
			if(document.getElementById('ab_city_'+count)){
				fnOnchangeForAddress(count);
			}
	});
	$('#module_AddressBook #ab_add_contact #ab_zip_'+count).change(function(){
			if(document.getElementById('ab_zip_'+count)){
				fnOnchangeForAddress(count);
			}
	});
		document.getElementById("ab_location_"+count).selectedIndex = 0;
		document.getElementById("ab_state_"+count).selectedIndex = 0;
		
	}
}

function removeAddress(index){
	$('#module_AddressBook #ab_add_contact #ab_address_section').find('.ab_add_button').remove();
	var loc=document.getElementById('ab_email_removeAddress_'+index);
	$('#module_AddressBook #ab_add_contact #ab_address_section #ab_address1_'+index).parent().remove();
	$('#module_AddressBook #ab_add_contact #ab_address_section #ab_location_'+index).parent().remove();
	$('#module_AddressBook #ab_add_contact #ab_address_section #ab_address2_'+index).parent().remove();
	$('#module_AddressBook #ab_add_contact #ab_address_section #ab_city_'+index).parent().remove();
	$('#module_AddressBook #ab_add_contact #ab_address_section #ab_state_'+index).parent().remove();
	$('#module_AddressBook #ab_add_contact #ab_address_section #ab_zip_'+index).parent().remove();
	var addressCount=document.getElementById('addressCount').value;
	document.getElementById('addressCount').value=addressCount-1;

	var obj="<div id='addAddress' class='ab_add_button'><a href='javascript:;'><span class='icn_small_plus'></span>Add Additional Address</a></div>";
	if(index==2){
		if(document.getElementById('ab_address1_3')){
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_address1_3').parent().append(obj);
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_address1_3').attr('name','ab_address1_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_location_3').attr('name','ab_location_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_address2_3').attr('name','ab_address2_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_city_3').attr('name','ab_city_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_state_3').attr('name','ab_state_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_zip_3').attr('name','ab_zip_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_address1_3').attr('id','ab_address1_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_location_3').attr('id','ab_location_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_address2_3').attr('id','ab_address2_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_city_3').attr('id','ab_city_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_state_3').attr('id','ab_state_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_zip_3').attr('id','ab_zip_2');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_email_removeAddress_3').attr('href','javascript:removeAddress(2)');
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_email_removeAddress_3').attr('id','ab_email_removeAddress_2');
		}else{
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_address1_1').parent().append(obj);
		}
		
	}else if(index==3){
	     if(document.getElementById('ab_address1_2')){
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_address1_2').parent().append(obj);
		}else{
			$('#module_AddressBook #ab_add_contact #ab_address_section #ab_address1_1').parent().append(obj);
		}
	}
	$('#module_AddressBook #ab_add_contact #addAddress a').click(function(){
			addNewAddress();
		});
	
}
function addNewImportantDate(){
	var impDateCount=document.getElementById('importantDateCount').value;
	
	$('#module_AddressBook #ab_add_contact #ab_id_section').find('.ab_add_button').remove();
	if(impDateCount<=3){
		document.getElementById('importantDateCount').value++;
		var count=++impDateCount;
		var obj="<div class='form_row'><label>Important Date</label>";
		obj+="<select onchange='javascript:populateDaysForContact("+count+")' class='small' name='ab_id_month_"+count+"' id='ab_id_month_"+count+"'>"; 
		obj+=document.getElementById('ab_id_month_1').innerHTML;	
		obj+="</select>";
		obj+="<select class='small' name='ab_id_day_"+count+"' id='ab_id_day_"+count+"'><option value=0>Day</option></select>";
		obj+="<label>Occasion</label><select class='small' name='ab_id_occasion_"+count+"' id='ab_id_occasion_"+count+"'>";
		obj+=document.getElementById('ab_id_occasion_1').innerHTML;
		obj+="</select>";
		obj+="<a href='javascript:removeImportantDate("+count+");' class='removeChoice' id='ab_email_removeImpDate_"+count+"'>Remove</a>";
		if(impDateCount!=3){
			obj+="<div id='addImportantDate' class='ab_add_button'><a href='javascript:;'><span class='icn_small_plus'></span>Add Additional Important Date</a></div>";
		}else if(impDateCount==3){
			obj+="<div id='addImportantDate' class='ab_add_button'><span class='icn_small_plus_new'></span>Add Additional Important Date<div class='message_box'>You can save up to three important dates. Please remove an existing important date to add a new one.</div></div>";
		}
		obj+="</div>";
				 		 
		$('#module_AddressBook #ab_add_contact #ab_id_section').append(obj).show();
		$('#module_AddressBook #ab_add_contact #addImportantDate a').click(function(){
			addNewImportantDate();
		});
		$('#module_AddressBook #ab_add_contact #ab_id_month_'+count).change(function(){
			if(document.getElementById('ab_id_month_'+count)){
				var value=document.getElementById('ab_id_month_'+count).value;
				if(value == 0){
					document.getElementById("ab_id_occasion_"+count).selectedIndex = 0;
				}
			}
		});
		
		$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_month_'+count+' option').attr('selected','');
		$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_occasion_'+count+' option').attr('selected','');
		
		document.getElementById("ab_id_month_"+count).selectedIndex = 0;
		document.getElementById("ab_id_occasion_"+count).selectedIndex = 0;
		
	}
}
function removeImportantDate(index){
	$('#module_AddressBook #ab_add_contact #ab_id_section').find('.ab_add_button').remove();
	var loc=document.getElementById('ab_email_removeImpDate_'+index);
	$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_month_'+index).parent().remove();
	$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_day_'+index).parent().remove();
	$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_occasion_'+index).parent().remove();
	var impDateCount=document.getElementById('importantDateCount').value;
	document.getElementById('importantDateCount').value=impDateCount-1;

	var obj="<div id='addImportantDate' class='ab_add_button'><a href='javascript:;'><span class='icn_small_plus'></span>Add Additional Important Date</a></div>";
	if(index==2){
		if(document.getElementById('ab_id_month_3')){
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_month_3').parent().append(obj);

			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_month_3').attr('name','ab_id_month_2');
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_day_3').attr('name','ab_id_day_2');
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_occasion_3').attr('name','ab_id_occasion_2');
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_month_3').attr('id','ab_id_month_2');
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_day_3').attr('id','ab_id_day_2');
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_occasion_3').attr('id','ab_id_occasion_2');
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_email_removeImpDate_3').attr('href','javascript:removeImportantDate(2)');
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_email_removeImpDate_3').attr('id','ab_email_removeImpDate_2');
		}else{
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_month_1').parent().append(obj);
			
		}
		
	}else if(index==3){
	     if(document.getElementById('ab_id_month_2')){
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_month_2').parent().append(obj);
			
		}else{
			$('#module_AddressBook #ab_add_contact #ab_id_section #ab_id_month_1').parent().append(obj);
			
		}
	}
	$('#module_AddressBook #ab_add_contact #addImportantDate a').click(function(){
			addNewImportantDate();
		});
}

function populateDaysForContact(index){
		var month = document.getElementById("ab_id_month_"+index);
		month_val = month.value;
		if((month_val == 1)||(month_val == 3)||(month_val == 5)||(month_val == 7)||(month_val == 8)||(month_val == 10)||(month_val == 12))
		{
			var day = document.getElementById("ab_id_day_"+index);
			for(i=1;i<=31;i++){
			day.options[i] = new Option(i,i);
			}
		}
		if((month_val == 4)||(month_val == 6)||(month_val == 9)||(month_val == 11))
		{
			var day = document.getElementById("ab_id_day_"+index);
			for(i=1;i<=30;i++){
			day.options[i] = new Option(i,i);
			}
			day.options.length=31;
		}
		if(month_val == 2){
			var day = document.getElementById("ab_id_day_"+index);
			for(i=1;i<=29;i++){
			day.options[i] = new Option(i,i);
			}
			day.options.length=30;
		}if(month_val == 0){
			var day = document.getElementById("ab_id_day_"+index);
			day.options.length=1;
		}
} 


//js validation for ContactDetails
function validateContact(){
	document.getElementById('errorMsg1').innerHTML='';
	document.getElementById('errorMsg1').style.display="none";
	if(document.getElementById('errorMsg2')){
		document.getElementById('errorMsg2').innerHTML='';
		document.getElementById('errorMsg2').style.display="none";	
	}
	var result=true;
	
	//validate FirstName and LastName
	var firstName=document.contactform.ab_first_name.value;
	var lastName=document.contactform.ab_last_name.value;
	
	if(firstName == ""){
		err = document.getElementById("errorMsg1");
		err.style.display = "block";
		err.innerHTML = "Please enter a First Name.";
		$('#ab_add_contact').animate({scrollTop: 0}, 500);
		scroll(0,225);
		document.contactform.ab_first_name.select();
		document.contactform.ab_first_name.focus();
		return;
 	}else{
 		if(!nameValidation(firstName,"error")){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please do not use a number when creating the first or last names of your account. You can use letters, spaces, hyphens and apostrophes."
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
			document.contactform.ab_first_name.select();
			document.contactform.ab_first_name.focus();
			return;
		}
 	}
 	
	if(lastName == ""){
		err = document.getElementById("errorMsg1");
		err.style.display = "block";
		err.innerHTML = "Please enter a Last Name.";
		$('#ab_add_contact').animate({scrollTop: 0}, 500);
		scroll(0,225);
		document.contactform.ab_last_name.select();
		document.contactform.ab_last_name.focus();
		return;
 	}else{
 		if(!nameValidation(lastName,"error")){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "A valid Last name may only contain alphas and the following special characters: hyphen, space, apostrophe, and period. A name should not start with a special character."
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
			document.contactform.ab_last_name.select();
			document.contactform.ab_last_name.focus();
			return;
		}
 	}
	
	//validate Emails
	email1=document.contactform.ab_email_1.value;
	if(email1!=""){
		var checkEmail=checkContactEmail(email1);
		if(!checkEmail){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please enter a valid Email.";
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
			document.contactform.ab_email_1.select();
			document.contactform.ab_email_1.focus();
			return;
		}
	}
	if(document.contactform.ab_email_2){
		var email2=document.contactform.ab_email_2.value;
		if(email2!=""){
			var checkEmail=checkContactEmail(email2);
			if(!checkEmail){
				err = document.getElementById("errorMsg1");
				err.style.display = "block";
				err.innerHTML = "Please enter a valid Email.";
				$('#ab_add_contact').animate({scrollTop: 0}, 500);
		        scroll(0,225);
				document.contactform.ab_email_2.select();
				document.contactform.ab_email_2.focus();
				return;
			}
		}
	}
	if(document.contactform.ab_email_3){
		var email3=document.contactform.ab_email_3.value;
		if(email3!=""){
			var checkEmail=checkContactEmail(email3);
			if(!checkEmail){
				err = document.getElementById("errorMsg1");
				err.style.display = "block";
				err.innerHTML = "Please enter a valid Email.";
				$('#ab_add_contact').animate({scrollTop: 0}, 500);
		        scroll(0,225);
				document.contactform.ab_email_3.select();
				document.contactform.ab_email_3.focus();
				return;
			}
		}
	}
	
	//validate group
	/*var group=document.contactform.ab_group.value;
	if(!fnIsNumeric(group)){
		err = document.getElementById("errorMsg1");
		err.style.display = "block";
		err.innerHTML = "Invalid Group";
		document.contactform.ab_group.select();
		document.contactform.ab_group.focus();
	}*/
	
	//validate Gender
	var gender=document.contactform.ab_gender.value;
	if(gender!="Unknown" && gender!="Male" && gender!="Female"){
		err = document.getElementById("errorMsg1");
		err.style.display = "block";
		err.innerHTML = "Invalid Gender";
		document.contactform.ab_gender.select();
		document.contactform.ab_gender.focus();
	}
	
	//validate relationship
	/*var relationship=document.contactform.ab_relationship.value;
	if(!fnIsNumeric(relationship)){
		err = document.getElementById("errorMsg1");
		err.style.display = "block";
		err.innerHTML = "Invalid Relationship";
		document.contactform.ab_relationship.select();
		document.contactform.ab_relationship.focus();
	}*/
	
	
	//Validating Important Occasion
	impDay=document.contactform.ab_id_day_1.value;
	impMonth=document.contactform.ab_id_month_1.value;
	impOccasion=document.contactform.ab_id_occasion_1.value;
	result=fnValidateImpDate(impDay,impMonth,impOccasion,1);
	if(!result){
		return;
	}
	if(document.contactform.ab_id_day_2){
		impDay=document.contactform.ab_id_day_2.value;
		impMonth=document.contactform.ab_id_month_2.value;
		impOccasion=document.contactform.ab_id_occasion_2.value;
		result=fnValidateImpDate(impDay,impMonth,impOccasion,2);
		if(!result){
			return;
		}
	}
	if(document.contactform.ab_id_day_3){
		impDay=document.contactform.ab_id_day_3.value;
		impMonth=document.contactform.ab_id_month_3.value;
		impOccasion=document.contactform.ab_id_occasion_3.value;
		result=fnValidateImpDate(impDay,impMonth,impOccasion,3);
		if(!result){
			return;
		}
	}
	
	//validate PhoneNumber
	var phone1=document.contactform.ab_phone_1.value;
	var phone1Type=document.contactform.ab_phone_type_1.value;
	result=fnValidateContactPhone(phone1,phone1Type,1);
	if(!result){
		return;
	}
	if(document.contactform.ab_phone_2){
		var phone2=document.contactform.ab_phone_2.value;
		var phone2Type=document.contactform.ab_phone_type_2.value;
		result=fnValidateContactPhone(phone2,phone2Type,2);
		if(!result){
			return;
		}
	}
	if(document.contactform.ab_phone_3){
		var phone3=document.contactform.ab_phone_3.value;
		var phone3Type=document.contactform.ab_phone_type_3.value;
		var result=fnValidateContactPhone(phone3,phone3Type,3);
		if(!result){
			return;
		}
	}
	
	//Validate Address
	var addr1Check=false;
	var addr2Check=false;
	var addr3Check=false;
	
	var address1_1=document.contactform.ab_address1_1.value;
	var address2_1=document.contactform.ab_address2_1.value;
	var location_1=document.contactform.ab_location_1.value;
	var city_1=document.contactform.ab_city_1.value;
	var state_1=document.contactform.ab_state_1.value;
	var zipCode_1=document.contactform.ab_zip_1.value;
	if(address1_1!="" || address2_1!="" || location_1!="Unknown" || city_1!="" || zipCode_1!=""){
		result= fnValidateContactAddress(address1_1,address2_1,location_1,city_1,state_1,zipCode_1,1);
		if(!result){
			return;
		}else{
			addr1Check=true;
		}
	}
	if(document.contactform.ab_address1_2){
		address1_2=document.contactform.ab_address1_2.value;
		address2_2=document.contactform.ab_address2_2.value;
		location_2=document.contactform.ab_location_2.value;
		city_2=document.contactform.ab_city_2.value;
		state_2=document.contactform.ab_state_2.value;
		zipCode_2=document.contactform.ab_zip_2.value;
		if(address1_2!="" || address2_2!="" || location_2!="Unknown" || city_2!="" || zipCode_2!=""){
			result= fnValidateContactAddress(address1_2,address2_2,location_2,city_2,state_2,zipCode_2,2);
			if(!result){
				return;
			}else{
				addr2Check=true;
			}
		}
	}
	if(document.contactform.ab_address1_3){
		address1_3=document.contactform.ab_address1_3.value;
		address2_3=document.contactform.ab_address2_3.value;
		location_3=document.contactform.ab_location_3.value;
		city_3=document.contactform.ab_city_3.value;
		state_3=document.contactform.ab_state_3.value;
		zipCode_3=document.contactform.ab_zip_3.value;
		if(address1_3!="" || address2_3!="" || location_3!="Unknown" || city_3!="" || zipCode_3!=""){
			result= fnValidateContactAddress(address1_3,address2_3,location_3,city_3,state_3,zipCode_3,3);
			if(!result){
				return;
			}else{
				addr3Check=true;
			}
		}
	}
	
	/*document.getElementById("checkAjax").value="0";
	if(addr1Check){
		result=validateCityStateZipCode(city_1,state_1,zipCode_1);
		if(!result){
			return;
		}
	}if(addr2Check){
		result=validateCityStateZipCode(city_2,state_2,zipCode_2);
		if(!result){
			return;
		}
	}if(addr3Check){
		result=validateCityStateZipCode(city_3,state_3,zipCode_3);
		if(!result){
			return;
		}
	}*/
	
	
		if(pageId =='addEditContact' && messageFlag == 'TRUE'){
	
		document.contactform.action="MessageCenterCmd";
		}
		else{
		document.contactform.action="SaveContactDetailsCmd";}
		document.contactform.submit();
	
}


function fnValidateImpDate(day,month,occasion,index){
	if(month!='0'){
		if(day=='0'){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please select a Day.";
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
			document.getElementById("ab_id_day_"+index).select();
			document.getElementById("ab_id_day_"+index).focus();
			return false;
		}else if(occasion=='Unknown'){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please select an Occasion.";
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
			document.getElementById("ab_id_occasion_"+index).select();
			document.getElementById("ab_id_occasion_"+index).focus();
			return false;
		}
	}else if(day!='0'){
		if(month=='0'){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please select a Month.";
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
			document.getElementById("ab_id_month_"+index).select();
			document.getElementById("ab_id_month_"+index).focus();
			return false;
		}else if(occasion=='Unknown'){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please select an Occasion.";
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
			document.getElementById("ab_id_occasion_"+index).select();
			document.getElementById("ab_id_occasion_"+index).focus();
			return false;
		
		}	
	}else if(occasion!='Unknown'){
		if(month=='0'){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please select a Month.";
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
			document.getElementById("ab_id_month_"+index).select();
			document.getElementById("ab_id_month_"+index).focus();
			return false;
		}else if(day=='0'){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please select a Day.";
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
			document.getElementById("ab_id_day_"+index).select();
			document.getElementById("ab_id_day_"+index).focus();
			return false;
		}
	}
	return true;
}


function validateCityStateZipCode(city,state,zipcode){
	document.getElementById("AJAXload").style.display = '';
	var url = "HandleAddressCmd";
	var urlString="city="+city;
    urlString=urlString+"&state="+state;
    urlString=urlString+"&zipCode="+zipcode;
    urlString=urlString+"&country"+"="+"US";
    
    $.ajax({
		type: "POST",
		url: url,
		dataType: "html",
		data: urlString,		
		success: function(responseHtml,textStatus){
				document.getElementById("ajaxResponse").innerHTML=responseHtml;
				var error = document.getElementById("errorMessageCount").innerHTML;
				var errorMessage = error.trim();
				if(errorMessage!=''){
					var err = document.getElementById("errorMsg1");
					err.innerHTML= errorMessage;
					err.style.display= "block";
					$('#ab_add_contact').animate({scrollTop: 0}, 500);
		            scroll(0,225);
					//$('.ab_add_contact').animate({scrollTop: 0}, 600);
					document.getElementById("checkAjax").value="0";
            	}else{
            		//No Errror
            		document.getElementById("checkAjax").value="1";
            	}		
	    	},
	    error: function(XMLHttpRequest, textStatus, errorThrown) {
           	var  err = document.getElementById("errorMsg1");  
            err.style.display= "";
            err.innerHTML =="Error processing your request, please try again later.";
            $('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
            document.getElementById("checkAjax").value="0";
        }
	});
	
	document.getElementById("AJAXload").style.display = 'none';
	
}


function checkContactEmail(email){
	var regex1 = /^[^\s@]+@([A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]\.|[A-Za-z0-9]\.)+([A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]|[A-Za-z0-9])$/;
    var regex2 = /^(root@|abuse@|spam@)/;
    var emailLC = email.toLowerCase();
    var retVal = true;   
    var err;
    if(!email.match(regex1)){
   	 	retVal = false;
    } else if(emailLC.match(regex2)){
		retVal = false;
    }
    return retVal;			

}

function fnValidateContactPhone(phone,phoneType,index){
	var status=true;
	if(phone!=""){
		if(!fnValidatePhone(trim(reformat(phone)))){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please enter a valid 10-digit Phone Number";
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
			scroll(0,225);
			document.getElementById("ab_phone_"+index).select();
			document.getElementById("ab_phone_"+index).focus();
			status=false;
		}else if(phoneType=="Unknown"){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please select a Phone Type";
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
			scroll(0,225);
			document.getElementById("ab_phone_type_"+index).select();
			document.getElementById("ab_phone_type_"+index).focus();
			status=false;
			}
	}else if(phoneType!="Unknown"){
				err = document.getElementById("errorMsg1");
				err.style.display = "block";
				err.innerHTML = "Please enter a Phone Number";
		$('#ab_add_contact').animate({scrollTop: 0}, 500);
		scroll(0,225);
				document.getElementById("ab_phone_"+index).select();
				document.getElementById("ab_phone_"+index).focus();
				status=false;
			}
	return status;
}


function fnValidateContactAddress(address1,address2,location,city,state,zipCode,index){
	
	if(address1==""){
		err = document.getElementById("errorMsg1");
		err.style.display = "block";
		err.innerHTML = "Please enter Address 1";
		$('#full_col').animate({scrollTop: 0}, 500);
		scroll(0,225);
		document.getElementById("ab_address1_"+index).select();
		document.getElementById("ab_address1_"+index).focus();
		return false;
	}
	if(location=="Unknown"){
		err = document.getElementById("errorMsg1");
		err.style.display = "block";
		err.innerHTML = "Please select Location";
		$('#full_col').animate({scrollTop: 0}, 500);
		scroll(0,225);
		document.getElementById("ab_location_"+index).select();
		document.getElementById("ab_location_"+index).focus();
		return false;
	}
	if(city==""){
		err = document.getElementById("errorMsg1");
		err.style.display = "block";
		err.innerHTML = "Please enter City";
		$('#ab_add_contact').animate({scrollTop: 0}, 500);
		scroll(0,225);
		document.getElementById("ab_city_"+index).select();
		document.getElementById("ab_city_"+index).focus();
		return false;
	}
	if(zipCode==""){
		err = document.getElementById("errorMsg1");
		err.style.display = "block";
		err.innerHTML = "Please enter a 5-digit ZIP Code";
		$('#ab_add_contact').animate({scrollTop: 0}, 500);
		scroll(0,225);
		document.getElementById("ab_zip_"+index).select();
		document.getElementById("ab_zip_"+index).focus();
		return false;
	}else{
		if(!fnIsNumeric(zipCode)){
			err = document.getElementById("errorMsg1");
			err.style.display = "block";
			err.innerHTML = "Please enter a 5-digit ZIP Code";
			$('#ab_add_contact').animate({scrollTop: 0}, 500);
		    scroll(0,225);
			document.getElementById("ab_zip_"+index).select();
			document.getElementById("ab_zip_"+index).focus();
			return false;
		}else{
			if(zipCode.length!=5){
				err = document.getElementById("errorMsg1");
				err.style.display = "block";
				err.innerHTML = "Please enter a valid 5-digit ZIP Code";
				$('#ab_add_contact').animate({scrollTop: 0}, 500);
		        scroll(0,225);
				document.getElementById("ab_zip_"+index).select();
				document.getElementById("ab_zip_"+index).focus();
				return false;
			}
		}
	}
	
	return true;
}

function fnDoneAdding(){
	var counter = document.getElementById('countDone').value;
	var email="";
	var contactid = "";
	var fullName ="";
	var currentEmails = '';
	var currentEmailsArr = '';
	var dupFlag = 'false';
	var fieldNotEmpty = 0;
	document.getElementById('emailURL').value = document.getElementById('ab_send_invitation_to_addresses').value;
	document.getElementById('popUpOrNot').value = 1;
	currentEmails = document.getElementById('ab_send_invitation_to_addresses').value;
	if(currentEmails != ''){
		currentEmailsArr = currentEmails.split(';');
	}
	for(i=0;i < counter;i++){
		if(document.getElementById('check_'+i).checked == true){
			dupFlag = 'false';
			 if(currentEmailsArr != ''){
			 	 for(j=0;j<currentEmailsArr.length;j++){
			 	 	if(currentEmailsArr[j] == document.getElementById('columnEmail_'+i).innerHTML){
			 	 		dupFlag = 'true';
			 	 	}
			 	 }
			 }
			 if(dupFlag == 'false'){			
			 email = email + document.getElementById('columnEmail_'+i).innerHTML + ";";
			 contactid = contactid + document.getElementById('contact_'+i).value + ";";
				 fullName = fullName + document.getElementById('fullName_'+i).value + ";";
		}
	}
	}
	document.getElementById('fullNames').value= fullName;
	document.getElementById('contactIDs').value = contactid;
		document.getElementById('ab_send_invitation_to_addresses').value = document.getElementById('ab_send_invitation_to_addresses').value + email;
	//alert(document.getElementById('ab_send_invitation_to_addresses').value);
	}
	
function fnInvitationCancel(){
document.getElementById('errorMsg1').innerHTML='';
document.getElementById('errorMsg1').style.display="none";
document.getElementById('ab_send_invitation_to_addresses').value="";
document.getElementById('ab_send_invitation_message').value = "You are invited to join my network";
document.getElementById('emailURL').value="";
document.getElementById('contactIDUrl').value="";
document.getElementById('fulNameUrl').value="";
document.getElementById('contactIDs').value="";
document.getElementById('fullNames').value="";
/* reset checkboxes  */
var counter = document.getElementById('countDone').value
	for(i=0;i < counter;i++){
		if(document.getElementById('check_'+i).checked == true){
		document.getElementById('check_'+i).checked = false;
}	
	}																												
}
	
$.fn.centreScreenName = function(){
    this.css("position", "absolute");
	var top = ($(window).height() - this.height()) / 2 + $(window).scrollTop();
	var left = ($(window).width() - 600) / 2 + $(window).scrollLeft();
	top = (top < 0) ? 0 : top;
	left = (left < 0) ? 0 : left;
    this.css("top", top + "px");
    this.css("left", left + "px");
    return this;
    
}

