/*
 * Date: 2009-12-22
 * Revision: 6
 */






/*
 * @param {Object} i - the id of the element which active function (click)
 * @param {Object} o - id-ul div-ului care se creaza
 * @param {Object} p - the requested ajax page
 * @param {Object} s - the style of the div id="o" (position of the popup)
 * @param {Object} t - cand t=1 se incarca popupul direct
 */
function ajaxPopUp(i,o,p,s,t) {
$(i).click(function(event){
event.preventDefault();
if($(o).text().length==0){

$(o).addClass(s);
$(o).load(p, function(){
    /* close window */
    $("#close").click(function(){
    $(o).empty();
    });
});


}
})

}
/*
 * @param {Object} o - id-ul div-ului care se creaza
 * @param {Object} p - the requested ajax page
 * @param {Object} s - the new class of 0
 * @param {Object} c - the style of s (position of the popup)
*/
function ajaxPopUpDirect(o,p,s,c) {
// cand se incarca popupul direct, fara click
$(o).addClass(s);
$(".puv").css(c);
$(o).load(p, function(){
    /* close window */
    $("#close").click(function(){
    $(o).empty();
    });
});

}


/* 
 * ajaxPopUp2 - pagina ce se incarca dupa click (submit form sau orice altceva)
 * @param i - selectorul (id-ul) unde se face update-ul
 * @param i2 - selectorul (id-ul) de unde se da click
 * @param p - the requested page (usually the same)
 */
function ajaxPopUp2(i,i2,p) {
    $("form").submit(function(event){
    event.preventDefault();
        var str = $('form').serialize();
        page = p + str;
        $(i).load(page);
          });

    $(i2).click(function(event){
        event.preventDefault();
        var str = $('form').serialize();
        page = p+str;
        $(i).load(page);
    })
}




function userLogin() {
     var i = "#sf2";
     var p = "http://www.pressreleases.sg/a/a_bdtLoginLogin.php?";
     var d = "#loginStatus";
     var f = "#form2";
$("#fPasswordLogin").keyup(function(event){
        if(event.which == 13) {
            event.preventDefault();
            var p2 = $(f).serialize();
     $(d).empty().load(p+p2);
}

    })

 $(i).click(function(event){
     event.preventDefault();
     var p2 = $(f).serialize();
     $(d).empty().load(p+p2);

 })


}

/*
 * submit form
 * @param i: pe ce se da click
 * @param d: id-ul div-ului care se updateaza
 * @param p: pagina care se incarca
 * @param f: id-ul form-ului
 * */
function submitForm(i,d,p,f){    

 $(f).submit(function(event){
     event.preventDefault();
     var p2 = $(f).serialize();
     $(d).empty().load(p+p2);
 })

 $(i).click(function(event){
     event.preventDefault();
     var p2 = $(f).serialize();
     $(d).empty().load(p+p2); 
     
 })
}

function clickForm(i,d,p,f){

  $(f).submit(function(event){
     event.preventDefault();
  })

 $(i).click(function(event){
     event.preventDefault();
     var p2 = $(f).serialize(); 
     $(i).empty();
     $(d).load(p+p2);

 })
}

/*
 * @param {Object} i - id-ul pe care se da click
 * @param {Object} o - id-ul div-ului care se creaza
 * @param {Object} p - the requested ajax page
 * @param {Object} s - the style of the div id="o" (position of the popup)
 */
function ajaxPopUp(i,o,p,s) {
$(i).click(function(event){
event.preventDefault();
if($(o).text().length==0){

$(o).addClass(s);
$(o).load(p, function(){
    /* close window */
    $("#close").click(function(){
    $(o).empty();
    });
});

}
})
}

/*
*
*
* */
function autoTextarea(i,px) {
   $(i).keyup(function(){
    var strLength = $(i).val().length;
    var rowSize = $(i).attr("rows");
    var rowBreake = px * 0.165625;
    var positionRow = parseInt(strLength/rowBreake);

    // get line breaks
     var msgBody = $(i).val();
 var msgBodyBreaks = msgBody.split(/\n/g); var msgBodyBreaks2 = msgBodyBreaks.length;
    // end get line breaks
    var positionRow2 = positionRow + msgBodyBreaks2;

     if(rowSize == positionRow2) {
        var newRow = positionRow2;
        $(i).attr("rows",newRow);
    }

    if(rowSize > positionRow2 && rowSize>5) {
        var overRows = rowSize-positionRow2;
        var newRow2 = rowSize-overRows;
        if(newRow2<5) { var newRow2=5; }
        $(i).attr("rows",newRow2);
    }

    if(rowSize < positionRow2) {
        var newRow = positionRow2;
        $(i).attr("rows",newRow);
    }


    });
}




/* CALCULATE THE POSITION OF ID ELEMENT */
function topLeftPosition(i) {
var a = $(i);
var b = a.offset();
var bLeft = b.left;
var bTop = b.top;
// round a float
// left
var bLeft2 = bLeft + ' '; // making it string
var bLeft3 = bLeft2.split(".",2);
if(bLeft3[1]!=undefined) { var left = parseInt(bLeft3[0])+1; } else { left = bLeft; }
// right
var bTop2 = bTop + ' '; // making it string
var bTop3 = bTop2.split(".",2);
if(bTop3[1]!=undefined) { var top = parseInt(bTop3[0])+1; } else { top = bTop; }
// end round
var position = left + ',' + top;
return position;
}




/* FORGOT PASSWORD
 * i - pe ce se da click
 * a - input email
 * d - unde apare contentul (display:)
 * f - formul de recovery
 * p - submit form
 * u - where the update is made
 * */

function forgotPassword(i,a,d,f,p,u) {
$(i).click(function(event){
    event.preventDefault();
    $(d).css("display","block");
    $(p).click(function(event){
        event.preventDefault();
        if($(a).val().length>5) {
     var p2 = $(f).serialize();
     var p = 'http://www.pressreleases.sg/a/modules/aPage_forgotPassword.php?';
     $(u).load(p+p2);
    }
    else {
    $(u).html('<div class="error" style="margin:4px 0 4px 0">Invalid input!</div>');
}

    });
})
$(".c").click(function(event){ event.preventDefault(); $(d).css("display","none"); })


}



/* AUTOCOMPLETE
*  Functions paramaters
 * url - url-ul de unde se incarca rezultatele
 * f - formul
 * i - input-ul
 * ih - input hidden
 * ihc- input hidden idCountry (ac3)
 * d- divul unde se incarca rezultatele
 * Acronims
 * acf = form
 * acs = selected city
 * pos = daca calculam pozitia sau nu (0 - pentru da, 1 - pentru nu)
 * */
function ac(url,f,i,ih,d,ic,pos) {

if(!pos) {
// get position of input
var position = topLeftPosition(i);
var p = position.split(",",2);
var pHeight = $(i).height();
var inputPadding = $(i).css("padding-top"); // este necesar pentru a il lua in calcul pentru ca se pare ca offsetul nu calculeaza si padding-ul la inaltimee
inputPadding = parseInt(inputPadding);
var ppTop = parseInt(p[1])+pHeight+inputPadding+1;
var leftPos = p[0]+"px";var topPos = ppTop+"px";
// end get position of input


var posCSS = {left:leftPos,top:topPos};
$(d).css(posCSS);
}

var i2 = i.replace(/#/g,'');
var ic2 = ic.replace(/#/g,'');

$(f).submit(function(event){
            event.preventDefault();
        })

$(i).keyup(function(event){


    var cityVal = $(i).val();
    cityVal = cityVal.replace(/ /g,'_s_');

    var countryVal = $(ic).val();

    if(cityVal && event.which != 40 && event.which != 38) {

    $(d).load(""+url+"?"+i2+"="+ cityVal +"&"+ ic2 +"="+ countryVal +"");
    }


    if(cityVal=='') {$(d).css("display","none");} else {$(d).css({display:"inline",border:"1px solid #EEF4F9",background:"#EEF4F9"});}


    if(event.which == 40) {
        var results = $("#results").text();
       rowSel = $(".acs .nc").text();rowSel = parseInt(rowSel);
       var nextSel = rowSel+1;var nextSel = "#c"+nextSel;
       if(rowSel==results) {var nextSel = "#c1";}
       $(""+d+" .acs").removeClass();
       $(nextSel).addClass("acs");
}

    if(event.which == 38) {
        var results = $("#results").text();
       rowSel = $(".acs .nc").text();rowSel = parseInt(rowSel);
       var nextSel = rowSel-1;var nextSel = "#c"+nextSel;
       if(rowSel==1) {var nextSel = "#c"+results;}
       $(""+d+" .acs").removeClass();
       $(nextSel).addClass("acs");
}





// enter
    if(event.which == 13) {
// $(i).val($(".acs #cc").text());
// $(ih).val($(".acs #cval").html());

$(i).val($(".acs .cc").text());
$(ih).val($(".acs .cval").html());

$(d).css("display","none");
}

// escape
if(event.which == 27) {
$(d).css("display","none");
}
})
}



/* SEARCH ACTIONS FOR CHECKED PROFILES */

function searchActions() {
/*
*  UPDATE BACKGROUND COLORS FOR CHECKED MESSAGES
*/
for(j=1;j<=10;++j) { checkedbox(j); }

function checkedbox(j) {
var p = "#cb" + j;
var p2 = "#sId" + j;
$(p).click(function(){
    if($(p+":checked").length>0) {$(p2).css("background","#F8FAFA");} else { $(p2).css("background","#ffffff"); }
});
}

/*
*  ACTIONS FOR DELETE/MARK AS READ/UNREAD MESSAGES
*/


/* activate submit link only where need */
$("#sAction").change(function(){
    $('#submitUpdate').css('display','none'); /* in cazul in care inainte s-a mai realizat un submit */
    var sNum = $('#scb').serialize();
    var checkSelected = $("#sAction option:selected").val();
    if(checkSelected!="Select" && sNum.length>0) { $("#submitActive").css("display","inline").html('<a href="#" class="small">Submit</a>');
        $("#submitInactive").css("display","none"); } else { $("#submitInactive").css("display","inline"); $("#submitActive").css("display","none"); }
})

$("#scb input:checkbox").click(function(){
    $('#submitUpdate').css('display','none'); /* in cazul in care inainte s-a mai realizat un submit */
    var sNum = $('#scb').serialize();
    var checkSelected = $("#sAction option:selected").val();
    if(checkSelected!="Select" && sNum.length>0) { $("#submitActive").css("display","inline").html('<a href="#" class="small">Submit</a>');
        $("#submitInactive").css("display","none"); }
    else { $("#submitInactive").css("display","inline"); $("#submitActive").css("display","none"); }
})
/* DO SUBMIT ACTIONS */
$('#submitActive').click(function(){
    /* make actions first */

        for(i = 1; i < 10; i++) {
            cb="#cb" + i + ":checkbox:checked"; cbValue=$(cb).val();
            sTitle="#sTitle" + i;
            sId="#sId" + i;
            if($('#sAction').val()=='Delete Listing' || $('#sAction').val()=='Remove Listing') { if(cbValue!=null) { $(sId).css("background","#F3F6F6").fadeOut(500); } }

        }

    /* serialiaze and load */
var sNum = $('#scb').serialize();
var sAction = $('#sActionForm').serialize();
var pageVar = sNum + '&' + sAction;
var page = 'http://www.pressreleases.sg/a/search/aForm_cSearch_search_actions.php?';
$('#submitActive').load(page+pageVar,function(){
    $('#submitInactive').css('display','none');
});
});

}

/* END SEARCH ACTIONS FOR CHECKED PROFILES */


/* let only alphanumeric characters to pass */
function alphaOnly(string,type){
string = string.replace(/[^a-zA-Z 0-9]+/g,'');
// for links - trim and remove spaces
if(type==1) {
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(string)) { string = string.replace(obj, '$2'); }
   var obj = / +/g;
   string = string.replace(obj, " ");
   if (string == " ") { string = ""; }
   var obj2 = / /g;
   string = string.replace(obj2, "-");
}
return string;

}




/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};




/* END NEW */

/* 
* HERE ARE EXECUTED GENERAL JAVASCRIPT FUNCTIOns
*/

/*

$(document).ready(function(){

$("#gadh").css("display","inline");


});

*/





