﻿/*
 * Common.js Version 1.0
 * Copyright (c) 2007-2008 SIRVINA corp. (www.sirvina.com)
 */
// ---- Global String Object Util Function ---------------------------------
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

// ---- SIRVINA Common Functions --------------------------------------------
function viewPopup(url)
{
    var winName = getRandomWindowName();
    var win = window.open(url, winName, 'toolbar=no,scrollbars=yes,resizable=yes' );
    win.focus();
}

function previewQuestion(questionID, questionTypeID, createdDate, surveyID)
{
    var winName = getRandomWindowName();
    var win = window.open('../SurveyAnswer/QuestionPreview.aspx?qID=' + questionID + '&qtype=' + questionTypeID + '&dtSv=' + createdDate + '&sId=' + surveyID, 
            winName, 'toolbar=no,scrollbars=yes,resizable=yes' );
    win.focus();
}

function previewSurvey(surveyID, secretSurveyCode)
{
    var winName = getRandomWindowName();
    var win = window.open('/WebSurvey/' + secretSurveyCode + '/' + surveyID + 'T.aspx', winName,
        'toolbar=no,scrollbars=yes,resizable=yes' );
    win.focus();
}

function previewSurveySiteRoot(surveyID, secretSurveyCode, siteRoot)
{
    var winName = getRandomWindowName();
    var win = window.open(siteRoot + '/' + secretSurveyCode + '/' + surveyID + 'T.aspx', winName,
        'toolbar=no,scrollbars=yes,resizable=yes' );
    win.focus();
}

function viewSAP(surveyID, secretSurveyCode)
{
    var winName = getRandomWindowName();
    var win = window.open('/WebSurvey/' + secretSurveyCode + '/' + surveyID + 'F.aspx', winName,
        'toolbar=no,scrollbars=yes,resizable=yes' );
    win.focus();
}

function viewSAPSiteRoot(surveyID, secretSurveyCode, siteRoot)
{
    var winName = getRandomWindowName();
    var win = window.open(siteRoot + '/' + secretSurveyCode + '/' + surveyID + 'F.aspx', winName,
        'toolbar=no,scrollbars=yes,resizable=yes' );
    win.focus();
}

function viewQuestionSample(questionTypeID, languageCode)
{
     var winName = getRandomWindowName();
     var url;
     if (languageCode == "vi-VN")
     {
        url = "../QuestionSample/QuestionType_" + questionTypeID + ".htm";
     } 
     else
     {
        url = "../QuestionSample/QuestionType_" + questionTypeID + "." + languageCode + ".htm";
     }  
     var win = window.open(url, winName, 'toolbar=no,scrollbars=yes,resizable=yes' );   
     win.focus();      
}

function getRandomWindowName()
{
    return "sirvina_"+(Math.round(Math.random()*1000000));
}

function showHideAdvancedArea(divAdvancedArea, imgSwitch)
{
    img =  document.getElementById(imgSwitch);
    divAdvArea = document.getElementById(divAdvancedArea); 

    if (divAdvArea.style.display == 'none')
    {
        img.src = img.src.replace("icoPlus.gif", "icoMinus.gif");
        divAdvArea.style.display = 'block';
    } 
    else
    {
        img.src = img.src.replace("icoMinus.gif", "icoPlus.gif");
        divAdvArea.style.display = 'none';
    }
}

function swapShowHide2Control(control_1, control_2)
{
    cnt_1=  document.getElementById(control_1);
    cnt_2 = document.getElementById(control_2); 
    if ((cnt_1.style.display == 'block') || (cnt_1.style.display == ''))
    {
        cnt_1.style.display = 'none';
        cnt_2.style.display = 'block'; 
    } 
    else
    {
        cnt_1.style.display = 'block';
        cnt_2.style.display = 'none'; 
    }
}

// If checkbox is checked, show control; otherwise hide the control
function showHideControlByCheckbox(checkbox, control)
{
    chk = document.getElementById(checkbox);
    cnt = document.getElementById(control); 
    if (chk.checked)
    {
        cnt.style.display = 'block'; 
    } 
    else
    {
        cnt.style.display = 'none'; 
    }
}

function getValue(clientID)
{
    var control = document.getElementById(clientID);
    if (control != null) { return control.value; } 
    else { return "";}
}

function setValue(clientID, value)
{
    var control = document.getElementById(clientID);
    if (control != null) { control.value = value; }
}

function getChecked(clientID)
{
   var chk = document.getElementById(clientID);
   if (chk) { return chk.checked; }
   return false;
}

function setChecked(clientID, checked)
{
    var chk = document.getElementById(clientID);
    if (chk) { chk.checked = checked; }
}

// For DropDownList
function setSelectedValue(clientID, value)
{
    var selectCnt = document.getElementById(clientID);
    if (selectCnt) 
    {   
        for (var i=0; i < selectCnt.length; i++) 
        {
            if (selectCnt.options[i].value == value) 
            {
                selectCnt.options[i].selected = true;
                return;
            }
        }
    } 
}

// For DropDownList
function getSelectedValue(clientID)
{
    ddl = document.getElementById(clientID);
    if (ddl) { return ddl.options[ddl.selectedIndex].value; }
    return "";
}

function setStyleDisplay(clientID, value)
{
    var control = document.getElementById(clientID);
    if (control != null) { control.style.display = value; }
}

function setFocus(clientID)
{
    var control = document.getElementById(clientID);
    if (control != null) { control.focus(); }
}

// Hide Ajax Popup
function hidePopup(id)
{
    var popup = $find(id);
    if (popup != null) { popup.hide(); }
}

// Show Ajax Popup
function showPopup(id)
{
    var popup = $find(id);
    if (popup != null) { popup.show(); }
}

// Validate Max Size of Textbox
function validateTextboxMaxSize(clientID, maxSize)
{
    var value = getValue(clientID);
    if (value.length > maxSize)
    {
        setValue(clientID, value.substr(0, maxSize));
    }
}

function setPasswordWaterMark(txtPasswordClientID, defaultMessage)
{
    var txtPassword = document.getElementById(txtPasswordClientID);
    if (txtPassword)
    {
        changeInputType(txtPassword, 'text', defaultMessage, false, true);    
    }
}

function changeInputType(
    oldElm, // a reference to the input element
    iType, // value of the type property: 'text' or 'password'
    iValue, // the default value, set to 'password' in the demo
    blankValue, // true if the value should be empty, false otherwise
    noFocus // set to true if the element should not be given focus
)
{    
    if(!oldElm || !oldElm.parentNode || (iType.length<4) || 
    !document.getElementById || !document.createElement) return;
    
    var newElm = document.createElement('input');
    newElm.type = iType;
    if(oldElm.name) newElm.name = oldElm.name;
    if(oldElm.id) newElm.id = oldElm.id;
    if(oldElm.className) newElm.className = oldElm.className;
    if(oldElm.size) newElm.size = oldElm.size;
    if(oldElm.tabIndex) newElm.tabIndex = oldElm.tabIndex;
    if(oldElm.accessKey) newElm.accessKey = oldElm.accessKey;
    if(oldElm.style.width) newElm.style.width = oldElm.style.width;
    
    newElm.onfocus = function()
    {   return function()
        {
            if(this.hasFocus) return;
            var newElm = changeInputType(this,'password',iValue,
                (this.value.toLowerCase()==iValue.toLowerCase())?true:false);
            if(newElm) newElm.hasFocus=true;
        }
    }();
    
    newElm.onblur = function()
    {
        return function(){
            if(this.hasFocus)
            if(this.value=='' || (this.value.toLowerCase()==iValue.toLowerCase())) 
            {
                changeInputType(this,'text',iValue,false,true);
            }
        }
    }();
    
    // hasFocus is to prevent a loop where onfocus is triggered over and over again
    newElm.hasFocus = false;
    oldElm.parentNode.replaceChild(newElm, oldElm);
    
    if(!blankValue) newElm.value = iValue;
    if(!noFocus || typeof(noFocus)=='undefined') 
    {
        window.tempElm = newElm;
        setTimeout("tempElm.hasFocus=true;tempElm.focus();", 1);
    }
    return newElm;
}

// Tctuong - 11Dec2008 - 00139 - Edit Chart
function showHideAreaByRadioButton(radioClientID, areaClientID)
{
    radio = document.getElementById(radioClientID); 
    area = document.getElementById(areaClientID); 
    if (radio.checked)
    { area.style.display = 'block'; }
    else { area.style.display = 'none'; }
}

// 00152 - Tctuong - VAT in Survey Fee - The same as in SurveyFee.cs
function calculateSurveyCreationFee(numberOfRespondent, creationFeeRate, vatRate)
{
    var creationFee = numberOfRespondent * creationFeeRate;
    creationFee = creationFee + (creationFee * vatRate) / 100;
    return creationFee;
}
//---------------- END ----------------------------------//

//function checkURL(idText, error)
//{
//    var val = document.getElementById(idText);
//    if(val)
//    {
//        try 
//        {
//            if (val.value != null && val.value.length > 0 && !new RegExp("([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?").test(val.value))
//            {
//                alert(error);
//                window.event.returnValue = false;
//            }
//        } catch(ex) {}
//    }
//}