/*
#**************************************************************#
# #### © OYSTOR (Your World, Simplified.) :: ADMIN PORTAL #### #
#**************************************************************#
*/
/*
    Document    : general
    Created on  : 24 Feb, 2011
    Author      : Subha
    Description : common js used for all pages
*/
var popupStatus = 0;
var id = "";
var confirmClose = 1;
// initialise plugins
$(function(){
    initMenu();
//    centerPopup();
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup();
        }
    });

    $('#table tr').click(function(){
        $('.activeTr').removeClass('activeTr');
        $(this).addClass('activeTr');
    });
});

function initMenu() {
    $('#menu li a').click(
        function() {
            var checkElement = $(this).next();
            if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
            checkElement.slideUp('normal');            
            return false;
            }
            if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
            $('#menu ul:visible').slideUp('normal');
            checkElement.slideDown('normal');
            return false;
            }
            }
        );
}

/*
 * PopUpId = popup elements id
 * func = example("param1","param2") //Call back function162635
 */

function loadPopup(PopUpId,Param){
    //loads popup only if it is disabled
    if(popupStatus==0){
        id=PopUpId;
        $("body").prepend("<div id='backgroundPopup' style='display:none;position:absolute;width:100%;height:100%;background-color:#000000;z-index:999;'>sadfsd</div>");
        
        $("#"+id).append("<div id='PopUpClose' onclick='disablePopup()' style='cursor:pointer;position:absolute;right:2px;top:2px;'><img src='"+imagesLocation+"layout/close1.png' alt='X' /></div>");
        
        $("#"+id).css({
                "z-index": "1000"
        });
        $("#backgroundPopup").css({
                "opacity":"0.7","filter":"alpha(opacity=70)"
        });
        $("#backgroundPopup").fadeIn("slow");
        
        $("#"+id).fadeIn("slow");

        centerPopup();
        
        popupStatus = 1;
        if(Param['func'])
        {
            setTimeout(Param['func'],200);
//            window[func]();
        }

        //Click out event!
        $("#backgroundPopup").click(function(){
            disablePopup();
        });
        }
}

function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
            $("#backgroundPopup").fadeOut("slow");
            $("#"+id).fadeOut("slow");
            popupStatus = 0;
//            setTimeout("checkpagechange()",500);
        }
}

function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
        var popupHeight,popupWidth;
        $('.PopUpCenter').each(function(i){
            popupHeight = $(this).height();
            popupWidth = $(this).width();
            //centering
            $(this).css({
                    "position": "absolute",
                    "top": windowHeight/2-popupHeight/2,
                    "left": windowWidth/2-popupWidth/2
            });
            //only need force for IE6
        });
        
	$("#backgroundPopup").css({
		"height": windowHeight
	});

}

function reload()
{
    location.reload();
}


function validateEmail(email)
{
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(email);
}



function IsNumericNoDot(sText)
{
   var reg = /^[0-9]+$/;
   if(reg.test(sText) == false)
   {
       return false;
   }
   else
   {
       return true;
   }
}

function IsNumeric(sText)
{
   var reg = /^[0-9,.]+$/;
   if(reg.test(sText) == false)
   {
       return false;
   }
   else
   {
       return true;
   }
}

//function IsNumeric(sText)
//{
//   var ValidChars = "0123456789.";
//   var IsNumber=true;
//   var Char;
//
//
//   for (i = 0; i < sText.length && IsNumber == true; i++)
//      {
//      Char = sText.charAt(i);
//      if (ValidChars.indexOf(Char) == -1)
//         {
//         IsNumber = false;
//         }
//      }
//   return IsNumber;
//}

function IsAlphabets(sText)
{
    var alphaExp = /^[a-zA-Z,]+$/;  
   if(alphaExp.test(sText) == false)
   {
      return false;
   }
   else
   {
       return true;
   }
	
}

function IsTwoComma(obj)
{
    var count=obj.length;
    var i;
    var check="false";
    var findval;
    for(i=0;i<count;i++)
    {
        if(obj.charAt(i)==",")
        {
        check="true";
        }
        
        if(check=="true")
        {
            findval=parseInt(i)+1;
            if(obj.charAt(findval)==",")
            {                
                return false;
            }
        }
        check="false";
    }
     return true;
}

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}

// ************* bof :: display status **************** //
function updatedSucc()
{
    $(function(){
        $("#success_msg").fadeIn();
        setTimeout('$("#success_msg").fadeOut("slow")',2000);
    });
}

function updatedFail()
{
    $(function(){
        $("#error_msg").fadeIn();
        $("#updParamError").html('Error while Updating');
        setTimeout('$("#error_msg").fadeOut("slow")',2000);
    });
}

function insertFail()
{
      $(function(){
        $("#error_msg").fadeIn();
        $("#statusError").html('Error while Inserting');
        setTimeout('$("#error_msg").fadeOut("slow")',2000);
    });
}
// ************* eof :: display status **************** //


function fadeOutDiv(divId)
{
    $('#'+divId).fadeOut('slow');
}

function showDiv(divId)
{
    $('#'+divId).fadeIn('fast');
}

function clearMessages()
{
    if(document.getElementById('error_msg'))
    {
        var errDisplay = document.getElementById('error_msg').style.display;
        if(errDisplay != 'none')
        {
            $('#error_msg').fadeOut('slow');
        }

    }
    if(document.getElementById('success_msg'))
    {
        var succDisplay = document.getElementById('success_msg').style.display;
        if(succDisplay != 'none')
        {
            $('#success_msg').fadeOut('slow');
        }

    }

}

function locationHref(url)
{
    location.href = url;
}

function dateComparator(startDate, endDate)
{
    var startDateArr = startDate.split('-');
    var endDateArr = endDate.split('-');

    if(endDateArr[2] >= startDateArr[2])
    {
        if(endDateArr[2] == startDateArr[2])
        {
            if(endDateArr[1] >= startDateArr[1])
            {
                if(endDateArr[1] == startDateArr[1])
                {
                    if(endDateArr[0] >= startDateArr[0])
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
        else
        {
            return true;
        }
    }
    else
    {
        return false;
    }

}


function loginUser()
{
    var username = $('#username').val();
    var password = $('#password').val();
    $('#username').css('border', 'none');
    $('#password').css('border', 'none');
    if(username == 0)
    {
        $('#username').css('border', 'thin solid #ff0000');
        $('#username').focus();
    }
    else if(password == 0)
    {
        $('#password').css('border', 'thin solid #ff0000');
        $('#password').focus();
    }
    else
    {
        $.post(portalLocation+'users/loginCheck.php',{'username':username,'password':password}, function(data){
            //alert(data);
            if(data == '0' || data == '-1')
            {
                alert('Invalid Username or Password');
            }
            else
            {
                window.location = portalLocation+'story/index.php';
            }
            
        });
    }
}


function getNotificationsCount()
{
    $.post(portalLocation+'users/notificationsCount.php',{'count':1}, function(data){
        //alert(portalLocation+'users/notificationsCount.php');
        //alert(data);
        var splittedData = data.split('---');
        $('#notificationsCount').html('&nbsp;&nbsp;'+splittedData[0]+'&nbsp;&nbsp;');
        $('#unreadMessagesCount').html('&nbsp;&nbsp;'+splittedData[1]+'&nbsp;&nbsp;');

    });
}

function redirectLogin(pageType, positionId, articleId)

{

    if(pageType == 'story')

    {

        var voteType = $('#'+positionId).val();

        if(voteType == '')

        {

            voteType = "yes";

        }

        positionId = voteType;

    }

    

    var url = portalLocation+'users/redirectLogin.php?type='+pageType+'&posId='+positionId+'&artId='+articleId;

    //alert(url);

    $.get(url,function(data){

        if(data != '-1')

        {

            window.location = portalLocation+'users/login.php';

        }

    });

}

function searchRedirect()
{
    word=document.getElementById("search").value;
    document.location.href = portalLocation + "story/ac-search_results.php?searchterm="+word;
	
}
function isNumberKey(evt,textbox){
   	message = ("Sorry, you are not allowed to enter any spaces");
	var charCode = (evt.which) ? evt.which : evt.keyCode
    	if (charCode == 32) {
		alert (message);
		textbox.value = substring(0,textbox.value.length-1);
	}
    return true;
}
function checksubmit(evt)
{
evt = (evt) ? evt : ((event) ? event : null);
var evver = (evt.target) ? evt.target : ((evt.srcElement)
?evt.srcElement : null );
var keynumber = evt.keyCode;
if(keynumber==13)
{
	searchRedirect();
}
}

function hidediv(hid)
{
    //alert(hid);
    $('#'+hid).fadeOut('slow');
    //animatedcollapse.toggle(hid);
}

function openArgumentMonitor(roomId)
{
   
        var redirectUrl = '';
        var encUser = $('#user'+roomId).val();
        var host = $('#host'+roomId).val();
        //alert(data);
        
        redirectUrl = tomcatUrl + '/enter_arg_pop.jsp?user='+encUser+'&roomid='+roomId+'&host='+host;
        openWindowMonitor(redirectUrl);
        
        //alert(redirectUrl);
        //frames['loadArgumentPage'+roomId].location.href = redirectUrl;
        
}

function openWindowMonitor(redirectUrl)
{
	window.open(redirectUrl, "Chat Window", "width=950, height=850,menubar=yes, location=yes, resizable=yes,scrollbars=yes,status=yes");
}



