/* Function for testing textarea limit validation */

function createObject(objId) {
    if (document.getElementById) return document.getElementById(objId);
    else if (document.layers) return eval("document." + objId);
    else if (document.all) return eval("document.all." + objId);
    else return eval("document." + objId);
}


function limitText(limitField, limitCount, limitNum, eve) {

    var unicode=eve.charCode? eve.charCode : eve.keyCode ;
    limitCount = document.getElementById(limitCount);
    limitCount.value = limitNum - limitField.value.length+" characters remaining";

    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
    else if (limitField.value.length == limitNum) {
        if (unicode!=8 && unicode!=46 && (unicode <37 || unicode>40)){
            return false;
        }
    }
}

function resetCounter(limitField, limitCount, limitNum){
    document.getElementById(limitCount).value = limitNum - document.getElementById(limitField).value.length+" characters remaining";

}

/* Function for limiting the input characters in textarea
limitField - the id of the textarea for which we will count the characters
showCount - the id of the element which will show how many characters we still have to use
limitNum - the number of characters maximum allowed
eve - the event on the textarea
 */
function limitTextarea(limitField, showCount, limitNum, eve) {
    var unicode=eve.charCode? eve.charCode : eve.keyCode ;
    showCount = document.getElementById(showCount);
    var countChar = limitNum - limitField.value.length;
    
    if(eve.ctrlKey && (eve.charCode == 97 || eve.charCode == 99 ||  eve.charCode == 120 || eve.charCode == 65 || eve.charCode == 67 ||eve.charCode == 88  )){
        //console.debug('eve.charCode : '+ eve.charCode );
        return true;
    }else if(eve.ctrlKey && (eve.charCode == 86 || eve.charCode == 118)){
        
        if(countChar == 0){
            //console.debug('Paste not allowed');
            return false;
        }else{
            if (limitField.value.length > limitNum) {
                limitField.value = limitField.value.substring(0, limitNum);
                return;
            }
        }
    }
    
    if(countChar >= limitNum) {
        showCount.innerHTML =  countChar+" characters maximum";
    } else {
        showCount.innerHTML =  countChar+" characters remaining";
    }
   
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    }
    else if (limitField.value.length == limitNum) {
        if (unicode!=8 && unicode!=46 && (unicode <37 || unicode>40)){
            return false;
        }
    }
}


function textCounter(field,cntfield,maxlimit) {
    showCount = document.getElementById(cntfield);
    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else{
        //cntfield.value = maxlimit - field.value.length;
        if(countChar == limitNum) {
            showCount.innerHTML =  countChar+" characters maximum";
        }
        else {
            showCount.innerHTML =  countChar+" characters remaining";
        }
    }
}

/* Function for setting the value onload
limitField - the id of the textarea for which we will count the characters
showCount - the id of the element which will show how many characters we still have to use
limitNum - the number of characters maximum allowed
 */
function resetCounterText(limitField, showCount, limitNum){
    showCount = document.getElementById(showCount);
    var countChar = limitNum - document.getElementById(limitField).value.length;
    if(countChar == limitNum) {
        showCount.innerHTML =  countChar+" characters maximum";
    }
    else {
        showCount.innerHTML =  countChar+" characters remaining";
    }
   
}

/*This function can be used to limit the maximum characters in a textarea or h:inputTextarea
 maxlength is not an attribute of these components so the solution is to do it with javascript*/
function textAreaMaxLength(field, maxLimit){
    if(field.value.length > maxLimit){
        field.value = field.value.substring(0,maxLimit);
    }
}

//Dynamically changes the height of the element by adding the amount of pixels specified.
function changeElementHeight(el, pixels){
    var panelHeight = el.getHeight();
    panelHeight = panelHeight + pixels;
    el.style.height = panelHeight+'px';
}

//
function showLoadingIndicator(spinnerDivElement, replacementDivElement) {
    spinnerDivElement.style.display = "block";
    replacementDivElement.style.display = "none";
}

function hideLoadingIndicator(spinnerDivElement, replacementDivElement) {
    spinnerDivElement.style.display = "none";
    replacementDivElement.style.display = "block";
}


function tipsetup(orgContainer, dummyContainer){

    var appearFunc = function(){
        //dummyContainer.style.opacity = '0.1';
        try{
            var tmp = dummyContainer.firstChild.nextSibling.nextSibling.nextSibling.firstChild.nextSibling.nextSibling.nextSibling;
            if(tmp){
                tmp.style.opacity='0.1';
            }
        }catch(err){ }
        orgContainer.innerHTML = dummyContainer.innerHTML;

        
        $('tips-data-div').appear({
            duration: 0.5,
            from: 0.1,
            to: 1
        });
    }
    $('tips-data-div').fade({
        duration: 0.5,
        from: 1,
        to: 0.1,
        afterFinish: appearFunc
    });    
}

function sleepMillis(millis)
{
    var date = new Date();
    var curDate = null;

    do {
        curDate = new Date();
    }
    while(curDate-date < millis);
}
var nextLinkOnClick = function(){
    
    var reqUrl = requestContext + '/content/components/support_center/try_this.jsf';

    var dummyContainer = document.createElement('div');
    new Ajax.Updater(dummyContainer, reqUrl,
    { 
        method: "get",
        onComplete: function(req){
            if(req.responseText != null &&  req.responseText != ''){
                var responseText =  req.responseText;
                if(responseText.indexOf('homePage()', 0) > 0 ){
                    //alert('Your session expired...');
                    location.href=requestContext;
                    return;
                }else{
                    tipsetup($('tips-portlet-div'),dummyContainer);
                }
            }
        },
        onFailure: function(){
            alert('Something gone wrong...');
        }
    });
    
    return false;
}

var setTitleVal = function(id,val){
    if(!val){
        val = $(id).value;
    }
    if($(id)){
        ($(id)).setAttribute('title',val);
    }
}


function showWaitCursor(){
    document.body.style.cursor='wait';
}

function showDefaultCursor(){
    document.body.style.cursor='default';
}