/*****    Dynamic Load Block      *******/

function loadIpclDynamicBlock(id, sUrl, iBlockID){
	
   // $('div#ipcl_comunity_001').prepend('<span class="icpl_loading">loading..</span>');
    $.get(sUrl, {
        time: Math.random(),
        pageBlock: iBlockID,
        divID: id
    }, function(data){
        var data_arr = data.split('^^^^||||^^^^');
        $('#' + id).html(data_arr[0]);
        if (data_arr.length == 2) {
            $('div#ipcl_community_paging_txt').html(data_arr[1]);
        }else
		{
			 $('div#ipcl_community_paging_txt').html('');
		}
    });
    return false;
}

function loadIpclAjaxData(sId, sUrl){
    $("div#" + sId).show();
    $("div.browse_community_cont").prepend('<span class="icpl_loading">loading..</span>');
    $.get(sUrl, {
        time: Math.random()
    }, function(data){
        $('div#ipcl_comunity_groop_002').html(data);
    });
}

function loadIpclAjaxPollData(sId, sUrl){
    $("div#" + sId).prepend('<span class="icpl_loading">loading..</span>');
    $.get(sUrl, {
        time: Math.random()
    }, function(data){
        document.getElementById(sId).innerHTML = data;
    });
}


function loadIpclSendAjaxReq(id, sUrl){

    $(id).prepend('<span class="icpl_loading">loading..</span>');
    
    $.ajax({
        type: "GET",
        url: sUrl,
        cache: false,
        dataType: "script",
        success: function(msg){
            eval(msg);
        }
    });
    return false;
}

function IpclReqConfirm(msg, id, sUrl){
    if (confirm(msg)) {
        loadIpclSendAjaxReq(id, sUrl);
    }
}


/**Form validation**/
function ipclFormValidation(formid, sUrl){
    var str = $("form#" + formid).serialize();
    
    document.getElementById('message_display').innerHTML = '<span class="icpl_loading">Processing..</span>';
    $.ajax({
        type: "POST",
        url: sUrl,
        cache: false,
        data: str,
        dataType: "script"
    });
    return false;
}


/*Function for main tab */
function loadIpclDynamicBlock2(sUrl, sId, sId2, iTabIndex){
    $('div#' + sId2).prepend('<span class="icpl_loading">loading..</span>');
    $.get(sUrl, {
        time: Math.random(),
        tabIndex: iTabIndex
    }, function(data){
        var data_arr = data.split('^^^^||||^^^^');
        document.getElementById(sId2).innerHTML = data_arr[0];
        if (data_arr.length == 2) {
            document.getElementById(sId).innerHTML = data_arr[1];
        }
    });
    return false;
}




function loadIpclSearchResult(sUrl, sId, sId2, iTabIndex, sFormID){
    $('div.show_listing').append('<span class="icpl_loading">loading..</span>');
    var str = $("form#" + sFormID).serialize();
    var sUrl = sUrl + '&' + str;
    //onclick="alert(searchParam[\'searchStr\'])"
    searchParam['searchStr'] = str;
    
    $.get(sUrl, {
        time: Math.random(),
        tabIndex: iTabIndex
    }, function(data){
    
        var data_arr = data.split('^^^^||||^^^^');
		
        $('#' + sId2).html(data_arr[0]);
        if (data_arr.length == 3) {
            $('#' + sId).html(data_arr[2]);
            $('#ipcl_paginationLink').html(data_arr[1]);
            //document.getElementById(sId).innerHTML=data_arr[2];
            //document.getElementById('ipcl_paginationLink').innerHTML=data_arr[1];
        }
    });
}

function loadIpclSearchResultOnTabClick(sId, sId2, iTabIndex, sParam){
    $('div.show_listing').append('<span class="icpl_loading">loading..</span>');
    var str = searchParam['searchStr'];
    
    var sUrl = $("#icpl_search_options").val();
    sUrl = sUrl + '?a=community_search&' + str;
    
    $.get(sUrl, {
        time: Math.random(),
        tabIndex: iTabIndex,
        extraParam: sParam
    }, function(data){
        var data_arr = data.split('^^^^||||^^^^');
        
        //document.getElementById(sId2).innerHTML=data_arr[0];
        $('#' + sId2).html(data_arr[0]);
        if (data_arr.length == 3) {
            $('#' + sId).html(data_arr[2]);
            $('#ipcl_paginationLink').html(data_arr[1]);
            /*document.getElementById(sId).innerHTML=data_arr[2];
             document.getElementById('ipcl_paginationLink').innerHTML=data_arr[1];*/
        }
    });
}

function loadIpclSearchResultOnPaging(sId, sId2, iTabIndex, sParam, iPage, sOrderBy){
    $('div.show_listing').append('<span class="icpl_loading">loading..</span>');
    var str = searchParam['searchStr'];
    
    var sUrl = $("#icpl_search_options").val();
    sUrl = sUrl + '?a=community_search&' + str;
    
    
    $.get(sUrl, {
        time: Math.random(),
        tabIndex: iTabIndex,
        extraParam: sParam,
        page: iPage,
        sortby: sOrderBy
    }, function(data){
        var data_arr = data.split('^^^^||||^^^^');
        $('#' + sId2).html(data_arr[0]);
        if (data_arr.length == 3) {
            $('#' + sId).html(data_arr[2]);
            $('#ipcl_paginationLink').html(data_arr[1]);
        }
    });
}



function loadIpclListingOnPaging(sUrl, sId, sId2, iPage){
    $('div#' + sId).prepend('<span class="icpl_loading">loading..</span>');
    
    $.get(sUrl, {
        time: Math.random(),
        page: iPage
    }, function(data){
        var data_arr = data.split('^^^^||||^^^^');
        
        $('#' + sId).html(data_arr[0]);
        if (data_arr.length == 2) {
            $('#' + sId2).html(data_arr[1]);
        }
    });
}



function loadIpclAjaxPollResData(sId, sUrl){
    $("div#" + sId).prepend('<span class="icpl_loading">loading..</span>');
    $.get(sUrl, {
        time: Math.random()
    }, function(data){
        document.getElementById(sId).innerHTML = data;
        $("SPAN.ipclPollprogressBar").progressBar();
    });
}


function limitChars(textid, limit, infodiv){
    var text = $('#' + textid).val();
    
    var textlength = text.length;
    
    if (textlength > limit) {
    
        $('#' + infodiv).html('You cannot write more then ' + limit + ' characters!');
        
        $('#' + textid).val(text.substr(0, limit));
        
        return false;
        
    }
    else {
        $('#' + infodiv).html('You have ' + (limit - textlength) + ' characters left.');
        return true;
    }
}

$(function(){
    $('#icpl_my_comment').keyup(function(){
        limitChars('icpl_my_comment', 500, 'charlimitinfo');
    })
    $('#icpl_comment_submit').click(function(){
        var postComment = $('#icpl_my_comment').val();
        var toID = $('#icpl_profile_id').val();
        var CmtParent = 0;
        if (postComment == '') {
            $('#icpl_my_comment').focus();
            $('#charlimitinfo').html('Please enter comment')
            return false;
        }
        $.ajax({
            type: "GET",
            data: {
                action: "CmtPost",
                CmtText: postComment,
                id: toID,
                CmtParent: CmtParent,
                time: Math.random()
            },
            url: "ICPLcmts.php",
            dataType: "script",
            success: function(data){
                $('#icpl_my_comment').val('');
            }
        });
    })
    $('#icpl_view_all_comments').click(function(){
        var toID = $('#icpl_profile_id').val();
		var previewLink=gArr['editMode'];
        loadIpclDynamicBlock('icpl_profile_comments', 'show_cmts_ajax.php?'+previewLink+'a=list&ID=' + toID, '0|100');
    })
    $('#termsncondition').click(function(){
        $("div#agreement").toggle("drop", {}, 1);
    })
    
    /*** FOR FOTO UPLOAD********/
    $('#icpl_submit_btn').click(function(){
        if ($('input:checked').length == false) {
            alert('Please read the terms and conditions!');
            return false;
        }
    })
    
    $('#playlistbox').change(function(){
        $('#icpl_edited_value').hide();
        $('#icpl_edited_value').val('');
        $('#icpl_edit_play_list_smd').html('[ Edit Playist ]');
        if (this.value != '') {
			
			var previewLink=gArr['editMode'];
			var limit=gArr['limitMode'];
			
            loadIpclAjaxPollData('icpl_play_list_smd', 'mymusic_playlist_ajax.php?'+previewLink+'a=list&limit='+limit+'&ID=' + this.value + '&time=' + Math.random());
        }
    })
    
	$('#icpl_edit_play_list_smd').click(function(){
        if ($('#playlistbox').val() == '') {
            alert('Please choose a playlist!');
            return false;
        }
        if ($('#icpl_edit_play_list_smd').html() == '[ Edit Playist ]') {
            $('#icpl_edited_value').show();
            var optionObj = document.getElementById('playlistbox').options;
            var sValue = optionObj[optionObj.selectedIndex];
            
            $('#icpl_edited_value').val(sValue.text);
            $('#icpl_edit_play_list_smd').html('[ Save ]');
        }
        else {
        
            var sNewLabel = $('#icpl_edited_value').val();
            if (sNewLabel == '') {
                alert('Please enter new playlist name!');
                return false;
            }
            /**
             * send ajax req ipcl_select_fav
             */
            $.get("mymusic_playlist_ajax.php", {
                title: sNewLabel,
                ID: $('#playlistbox').val(),
                a: "updateplaylist"
            }, function(data){
            
                var optionObj = document.getElementById('playlistbox').options;
                var sValue = optionObj[optionObj.selectedIndex];
                sValue.text = data;
                
                $('#icpl_edited_value').hide();
                $('#icpl_edit_play_list_smd').html('[ Edit Playist ]');
            });
        }
    })
	
	 $('#icpl_delete_play_list_smd').click(function(){
        if (!confirm("Do you want to delete this?")) {
            return false;
        }
        if ($('#playlistbox').val() == '') {
            alert('Please choose a playlist!');
            return false;
        }
        
        /**
         * send ajax req
         */
        var selIndex;
        $.get("mymusic_playlist_ajax.php", {
            ID: $('#playlistbox').val(),
            a: "delete"
        }, function(data){
            var optionObj = document.getElementById('playlistbox').options;
            selIndex = optionObj.selectedIndex;
            optionObj[optionObj.selectedIndex] = null;
            var s = 0;
            if (optionObj[1]) {
                optionObj[1].selected = true;
                var s = optionObj[1].value;
            }
			var previewLink=gArr['editMode'];
			
            loadIpclAjaxPollData('icpl_play_list_smd', 'mymusic_playlist_ajax.php?'+previewLink+'a=list&ID=' + s)
        });
    })
	
	$('#videoplaylistbox').change(function(){
        $('#icpl_edited_value_video').hide();
        $('#icpl_edited_value_video').val('');
        $('#icpl_edit_play_list_smd_video').html('[ Edit Playist ]');
        if (this.value != '') {
			var previewLink=gArr['editMode'];
			var limitMode=gArr['limitMode'];
			
            loadIpclAjaxPollData('icpl_play_list_smd_video', 'myvideo_playlist_ajax.php?'+previewLink+'a=list&ID=' + this.value +'&limit='+ limitMode + '&time=' + Math.random());
        }
    })
	
	$('#icpl_edit_play_list_smd_video').click(function(){
        if ($('#videoplaylistbox').val() == '') {
            alert('Please choose a playlist!');
            return false;
        }
        if ($('#icpl_edit_play_list_smd_video').html() == '[ Edit Playist ]') {
            $('#icpl_edited_value_video').show();
            var optionObj = document.getElementById('videoplaylistbox').options;
            var sValue = optionObj[optionObj.selectedIndex];
            
            $('#icpl_edited_value_video').val(sValue.text);
            $('#icpl_edit_play_list_smd_video').html('[ Save ]');
        }
        else {
        
            var sNewLabel = $('#icpl_edited_value_video').val();
            if (sNewLabel == '') {
                alert('Please enter new playlist name!');
                return false;
            }
            /**
             * send ajax req ipcl_select_fav
             */
            $.get("myvideo_playlist_ajax.php", {
                title: sNewLabel,
                ID: $('#videoplaylistbox').val(),
                a: "updateplaylist"
            }, function(data){
            
                var optionObj = document.getElementById('videoplaylistbox').options;
                var sValue = optionObj[optionObj.selectedIndex];
                sValue.text = data;
                
                $('#icpl_edited_value_video').hide();
                $('#icpl_edit_play_list_smd_video').html('[ Edit Playist ]');
            });
        }
    })
	
	 $('#icpl_delete_play_list_smd_video').click(function(){
        if (!confirm("Do you want to delete this?")) {
            return false;
        }
        if ($('#videoplaylistbox').val() == '') {
            alert('Please choose a playlist!');
            return false;
        }
        
        /**
         * send ajax req
         */
        var selIndex;
        $.get("myvideo_playlist_ajax.php", {
            ID: $('#videoplaylistbox').val(),
            a: "delete"
        }, function(data){
            var optionObj = document.getElementById('videoplaylistbox').options;
            selIndex = optionObj.selectedIndex;
            optionObj[optionObj.selectedIndex] = null;
            var s = 0;
            if (optionObj[1]) {
                optionObj[1].selected = true;
                var s = optionObj[1].value;
            }
			//var previewLink=gArr['editMode'];
            loadIpclAjaxPollData('icpl_play_list_smd_video', 'myvideo_playlist_ajax.php?a=list&ID=' + s)
        });
    })
	
    $('#ipcl_select_fav2').change(function(){
        if (this.value != '') {
            loadIpclAjaxPollData('icpl_fav_list', 'my_favlist_ajax.php?a=list&type=' + this.value + '&ProfileID=' + $('#icpl_profile_id').val());
        }
    })
    
	$('#ipcl_select_fav').change(function(){
        if (this.value != '') {
			var previewLink=gArr['editMode'];
			var limit=gArr['limitMode'];
            loadIpclAjaxPollData('icpl_fav_list', 'my_favlist_ajax.php?'+previewLink+'a=list&limit='+limit+'&type=' + this.value + '&ProfileID=' + $('#icpl_profile_id').val());
        }
    })
	
    
    
    /**
     * for pwd
     */
    $('#icpl_change_pwd_link').click(function(){
    
        if (document.getElementById('password_form_div').style.display == 'none') {
            $('#password_form_div').show();
        }
        else {
            $('#password_form_div').hide();
        }
    })
    
    $('#icpl_change_pwd_submit').click(function(){
    
        var str = $("form#change_pwd_smd").serialize();
        $.ajax({
		  type: "GET",
		  url: "changePassword.php?"+str,
		  dataType: "script"
		});

    })
    
   
	
	$('#icpl_search_options').change(function(){
		if(this.value=='member_ajax_actions.php')
		{
			//$('#icpl_chk_bxk').html('<li class="heading">In</li><li><input type="checkbox" class="ipcl_srh_chkbx" name="searchby[]" value="sName" />&nbsp;Name<br /></li><li><input type="checkbox" class="ipcl_srh_chkbx" name="searchby[]" value="sDescription"  />&nbsp;Description<br /></li><li><input type="checkbox" id="srh_all" value="" />&nbsp;All<br /><br /></li>');
		}else{
			//$('#icpl_chk_bxk').html('<li class="heading">In</li><li><input type="checkbox" class="ipcl_srh_chkbx" name="searchby[]" value="sTitle" />&nbsp;Titles<br /></li><li><input type="checkbox" class="ipcl_srh_chkbx" name="searchby[]" value="sDescription"  />&nbsp;Description<br /></li><li><input type="checkbox" class="ipcl_srh_chkbx" name="searchby[]" value="sTags" />&nbsp;Tags<br /><br /></li><li><input type="checkbox" id="srh_all" value="" />&nbsp;All<br /><br /></li>');
		}
	})
});

function replyComment(id,userid){
	if($('#'+id).html()==''){
		$('#'+id).html('<textarea id="icpl_my_reply_'+id+'" cols="50" rows="3" ></textarea><br /><br /><a href="javascript:void(0)"><img src="templates/tmpl_uni/images/buttons/submit.gif" width="77" height="30" alt="Submit" onclick="replyMsg(\''+userid+'\',\'icpl_my_reply_'+id+'\')"/></a>');
	}else{
		$('#'+id).html('');
	}
}

var gArr	= new Array();
function replyMsg(userid,bxid){
	var bxid_arr	= bxid.split('icpl_my_reply_');
	
	var divid	= bxid_arr[1];
	
	gArr['repDIV']	=  divid;
	 var postComment = $('#'+bxid).val();
      var toID = userid;
      var CmtParent = 0;
      if (postComment == '') {
          $('#'+bxid).focus();
          alert('Please write some reply!');
          return false;
      }
      
       $.ajax({
            type: "GET",
            data: {
                action: "CmtPost",
                CmtText: postComment,
                IsReply: 1,
                id: toID,
                CmtParent: CmtParent,
                time: Math.random()
            },
            url: "ICPLcmts.php",
           // dataType: "script",
            success: function(data){
                $('#'+gArr['repDIV']).html('');
                alert(data);
            }
        });
}


function manupulateHash(el){
	//alert(this.name);
	document.location.hash	= el;
}

function changeText(id){
    $('#'+id).html('<span class="icpl_loading">Saving..</span>')
}

