var other_info_1= other_info_2= other_info_3= other_info_4= other_info_5=other_info_6="";
var other_info_1_state= other_info_2_state= other_info_3_state= other_info_4_state= other_info_5_state= other_info_6_state=true;

var poken_card_info_obj, poken_card_info_obj_length, total_other_info_length, preview_card_elements_start_from;
function addCard(){

	var valid=validateCardDetail();
	if(valid){

		var baseUrl=document.getElementById('P_SITE_BASE_URL').value;
		var action='ajax_add_card';
		
		var js_code=document.getElementById('js_code').value;		
		var cboox=$("#cboox").val();
						
		var query =	"ajax=1&js="+js_code+"&formsubmit=1&cboox="+cboox;
				
		var actionUrl = baseUrl + action;		
		
		$("#frmDiv_settings").show();
		$("#frmDiv_settings").empty().html('<div align="center"><img src="'+baseUrl+'images/loading.gif" /></div>');						
		$.ajax({
		type: "POST",
		url: actionUrl,
		data: query,
		cache: false,
		success: function(responseMsg){
				var responseObj=eval(responseMsg);
				if(responseObj[0].status==1){													 
					 $("#div_use_existing").hide();			
					 configureCard(baseUrl,cboox);					 
					}
				else{
					$("#frmDiv_settings").hide();
					$("#errDiv").show();												
					document.getElementById("errDiv").innerHTML = responseObj[0].msg;											
				}
		}
		});
		
	}
		
}


function configureCard(baseUrl,cboox){
	var action='settings';
	
	var query =	"ajax=1";
	
	if(cboox!=''){
		query =query+"&cboox="+cboox;
	}				
			
	var actionUrl = baseUrl + action;
	$.ajax({
	type: "POST",
	url: actionUrl,
	data: query,
	cache: false,
	dataType: "html",
	success: function(responseMsg){		
		$("#frmDiv_settings").show();						
		document.getElementById("frmDiv_settings").innerHTML = responseMsg;
		fnCardSettingInIt();
		$("#errDiv").hide();							 			 
	}
	});	
}

function validateCardDetail()
{			
		var js_code=trim(document.getElementById('js_code').value);				
		
		if(js_code=='')
		{
			$("#errDiv").show();
			document.getElementById("errDiv").innerHTML = 'Please enter java script code.';
			return false;
		}

	$("#errDiv").hide();
		
		
return true;		
}

function changeDisplayRow(){

	var rows=$('#rows').val();
	
	var cols=$('#cols').val();
	
	if(cols==1){
		$('#cols').val(2);
	}
	
	changeDisplayRowCol();
}

function changeDisplayCol(){
	var cols=$('#cols').val();
	
	var rows=$('#rows').val();
	
	if(rows==1){
		$('#rows').val(2);
	}
	
	changeDisplayRowCol();
}


function changeDisplayOrder(order_by){	
	$('#order_by').val(order_by);		
	document.forms['change_display'].submit();
}

function changeDisplayRowCol()
{
	document.forms['change_display'].submit();
}

function fnDeleteCard(url){
	if(confirm("Are you sure want to delete this card ?")){
		window.location.href= url;
	}
}

function fnChangeCbIconPosition(){

var cnt=fnCountCheckedNetworkingIcons();

	if(cnt>=10){
	
		$("#lcd_up_pos").show();
		$("#lcd_down_pos").hide();
	
	}else{	
		
		$("#lcd_up_pos").hide();
		$("#lcd_down_pos").show();	
	}
}

function fnCountCheckedNetworkingIcons(){

var count=0;
var icon_nm=""; 

for(i=0;i<10;i++){
	icon_nm="#id_editable_networking_icons_"+i;
	
	if($(icon_nm).length){
		var icon_chkd=$(icon_nm).is(':checked');	
		if(icon_chkd)
		{
			count++;
			if(i==9){
				$('#id_networking_icons_8').removeClass('poken_last');
			}
			
		}else{		
			if(i==9){
				$('#id_networking_icons_8').addClass('poken_last');
			}	
		}		
	}
}


return count; 

}


//Card parse start
function fnChangeCardCurrentPreview(element_name){


	var element_id_in_preview_card = "id_"+element_name;
	var element_id_in_editable_card = "id_editable_"+element_name;

	element_obj_in_preview_card = document.getElementById(element_id_in_preview_card);
	element_obj_in_editable_card = document.getElementById(element_id_in_editable_card);

	var event_fired = 'show'; 
	if(element_obj_in_editable_card){
		if(element_obj_in_editable_card.checked)
			event_fired = 'show';
		else
			event_fired = 'hide';
	}

	if(element_name.match("networking_icons")){			
		element_name = "networking_icons"
		}
	if(element_name.match("heading_words")){
		element_name = "heading_words"
		}
		
	switch(element_name){

		case 'first_name':

		case 'last_name':

		case 'title':

		case 'networking_icons':
			if(event_fired == 'show')
				element_obj_in_preview_card.style.display='';
			else
				element_obj_in_preview_card.style.display='none';
			break;		
		
		case 'heading_words':
			if(event_fired == 'show')
				element_obj_in_preview_card.style.display='';
			else
				element_obj_in_preview_card.style.display='none';
			break;
		default:
		
		case 'photo':
			if(element_obj_in_preview_card){
				if(event_fired == 'show'){
					if(element_name.match("other_info"))
						eval(element_name+"_state="+true);
					else
						element_obj_in_preview_card.style.visibility='visible';
				}else if(event_fired == 'hide'){
					if(element_name.match("other_info"))
						eval(element_name+"_state="+false);
					else
						element_obj_in_preview_card.style.visibility='hidden';
				}
			}
		break;
	}
	
	if(element_name.match("other_info")){		
		fnReshuffleOtherInfo();
	}
	
}

function fnReshuffleOtherInfo(){
	for(i=1;i<=total_other_info_length;i++){
		eval("document.getElementById('id_other_info_"+i+"').innerHTML = ''");
	}
	
	is_found = false;
	for(i=1;i<=total_other_info_length && !is_found;i++){
		eval("cur_element_value=other_info_"+i);
		eval("cur_element_state=other_info_"+i+"_state");
		if(!cur_element_state){
			j=i+1;			
			while(j<=total_other_info_length){
				eval("next_element_state=other_info_"+j+"_state");		
				if(next_element_state){
					eval("next_element_value=other_info_"+j);
					if(next_element_value){
						is_found = true;
						eval("document.getElementById('id_other_info_"+i+"').innerHTML = next_element_value");
						i++;
					}
				}
				j++;
			}
		}else if(cur_element_state){
			eval("document.getElementById('id_other_info_"+i+"').innerHTML = cur_element_value");
		}
	}
}

function fnCardSettingInIt(){
	poken_card_info_obj = document.getElementById("id_poken_card_details").getElementsByClassName("poken_card-gap");
	poken_card_info_obj_length = poken_card_info_obj.length;	
	total_other_info_length = poken_card_info_obj_length;
	preview_card_elements_start_from = 0;

	for(i=preview_card_elements_start_from; i<poken_card_info_obj_length; i++){
		var_name = "other_info_"+(i+1);
		/*if(false && poken_card_info_obj[i].childNodes.length > 1){
			eval(var_name+"='"+poken_card_info_obj[i].childNodes[1].innerHTML+"'");
			poken_card_info_obj[i].childNodes[1].innerHTML = "<span style='visibility:visible;' id='id_"+var_name+"'>"+poken_card_info_obj[i].childNodes[1].innerHTML+"</span>";
		}else{*/
			eval(var_name+"='"+poken_card_info_obj[i].innerHTML+"'");			
			poken_card_info_obj[i].innerHTML = "<span style='visibility:visible;' id='id_"+var_name+"'>"+poken_card_info_obj[i].innerHTML+"</span>";
		//}
	}
}

function validateCardSettings()
{
	var agree=$('#agree_chk').is(':checked');
	
	if(!agree)
	{	
		if ($('#errDiv').length)
		{
			$("#errDiv").show();
			$("#errDiv").text('Please confirm terms and conditions.');
			return false;
		}			
		return false;
	}
	return true;
}

function fnSaveConfiguration(baseUrl){

var valid=validateCardSettings();

if(valid){
	card_configure_obj = document.forms['card_configure'];
	var url_parameter = '';
	var cboox=$("#cboox").val();
	var card_id=$("#edit_card_id").val();
	var page_id=$("#page_id").val();
	
	var card_configure_total_elements = card_configure_obj.length;
	for(i=0;i<card_configure_total_elements;i++){
		if(card_configure_obj[i].type=='checkbox'){
			if(url_parameter!='')
				url_parameter +="&";
			is_checkbox_checked = true;
			if(!card_configure_obj[i].checked)
				is_checkbox_checked = false;
			url_parameter += card_configure_obj[i].name+"="+is_checkbox_checked;
		}
	}
	
	var action='settings';
	
	var query =	"ajax=1&formsubmit=1&"+url_parameter+"&card_id="+card_id;				
			
	var actionUrl = baseUrl + action;
	$.ajax({
	type: "POST",
	url: actionUrl,
	data: query,
	cache: false,
	success: function(responseMsg){	
	
		if ($('#successDiv').length)
		{
			//$("#successDiv").show();												
			//document.getElementById("successDiv").innerHTML = 'You have successfully added card.<br>Thank you.';
			if ($('#edit_successDiv').length){
				$("#edit_successDiv").hide();
			}
		}else{
			$("#edit_successDiv").show();												
			document.getElementById("edit_successDiv").innerHTML = 'Your card configurations are saved.';
		}
		
		if($('#frmDiv_addcard').length){
			$("#frmDiv_addcard").hide();
		}
				
		
		if($('#errDiv').length){
			$("#errDiv").hide();
		}
						
		var extendUrl='';
		if(cboox!=''){
			 extendUrl=extendUrl+cboox;
		}
		
		if(page_id!=''){
			extendUrl=extendUrl+'/page_id/'+page_id;
		}
		
		if ($('#successDiv').length)
		{
			reload_page(baseUrl+extendUrl);
		}else{
			window.setTimeout('reload_page(\"'+baseUrl+extendUrl+'\")',3000);
		}											 			 
	}
	});
	
	
}


}

function fnOnloadCardEdit(){
	card_configure_obj = document.forms['card_configure'];

	var card_configure_total_elements = card_configure_obj.length;
		
	for(i=0;i<card_configure_total_elements;i++){
		if(card_configure_obj[i].type=='checkbox'){
			if(card_configure_obj[i].name.match("other_info")){
				eval(card_configure_obj[i].name+"_state="+card_configure_obj[i].checked);
				}
			else{
				if(!card_configure_obj[i].checked){
					fnChangeCardCurrentPreview(card_configure_obj[i].name);
				}
			}
		}
	}
	fnReshuffleOtherInfo();
}
//Card parse end

$(document).ready(function(){
	
  if ($('#order_by').length) {
  		var order_by= $('#order_by').val();
   		var filter_id='#filter_'+order_by;
   		
   		if ($(filter_id).length)
   		{
   		$(filter_id).removeClass('filter_nt_selected').addClass('filter_selected');   		
   		}
  } 
	
});

function showExistingCardList(){
	$('#existing_list').slideDown('slow');
	$('#add_Div').hide();
	
	if ($('#errDiv').length)
   	{
   		$('#errDiv').text('');
	}
	
}

function showAddCard(){
$('#existing_list').hide();
$('#add_Div').slideDown('slow');
}

function changeSelCardPrvw(id)
{

var last_sel_id=$('#sel_card_id').val();
var last_sel_html_div='#sel_html_'+last_sel_id;
$(last_sel_html_div).hide();

var sel_html_div='#sel_html_'+id;
$(sel_html_div).show();
$('#sel_card_id').val(id);

}

function addExistingCard(){

	var baseUrl=document.getElementById('P_SITE_BASE_URL').value;
	var action='ajax_add_card';
	var actionUrl = baseUrl + action;
	var cboox=$("#cboox").val();
	var card_id=$("#sel_card_id").val();
	
	var query =	"ajax=1&formsubmit=1"+"&card_id="+card_id+"&cboox="+cboox+"&use_existing=1";						
	
	$.ajax({
	type: "POST",
	url: actionUrl,
	data: query,
	cache: false,
	success: function(responseMsg){		
		if ($('#successDiv').length)
		{
			$("#successDiv").show();												
			document.getElementById("successDiv").innerHTML = 'You have successfully added card.<br>Thank you.';
			if ($('#edit_successDiv').length){
				$("#edit_successDiv").hide();
			}
		}
		
		if($('#frmDiv_addcard').length){
			$("#frmDiv_addcard").hide();
		}
				
		
		if($('#errDiv').length){
			$("#errDiv").hide();
		}
						
		var extendUrl='';
		if(cboox!=''){
			 extendUrl=extendUrl+cboox;
		}						
		window.setTimeout('reload_page(\"'+baseUrl+extendUrl+'\")',2000);											 			 
	}
	});

}

function fnChangeCardCurrentPreviewPosSettings(element_name){

	fnChangeCardCurrentPreview(element_name);
	fnChangeCbIconPosition();
}

function fnCheckForLastNetworkingIcon(){

}
