﻿// JScript File

var deleteAnswerMessage = 'Are you sure you want to delete this answer'; //'Are you sure? You will not be able to submit a new answer for this question.\n\nTo edit your current answer:\n1. Click on \'Cancel\'.\n2. Go to your answer and click on the \'Edit\' link below it.';
var deleteQuestionMessage = 'Are you sure you want to delete this question?';

function getParam(param)
{
    var query = window.location.search.substring(1); 
    var vars = query.split("&");
    var val= new Array();
    for (var i=0; i< vars.length; i++) 
    {
        var pair = vars[i].split("=");
        if (pair[0] == param.trim()) 
        {
            val.push(unescape(pair[1]).replace(/\+/g, ' ')); 
        }
    }
    
    if(val.length == 0)
    {    
        return '';
    }
    else
    {
        return val.join(',');
    }
}

function onFailure(error)
{
    try {
        var message = error.get_message();   
        
        if(message == 'Authentication failed.'){
            location.href = 'login.aspx?ReturnUrl=' + location.href;
        } else {   
            alert(message);
        }
    } catch(e) { alert(e.message); }
    

}

function onTimeOut(error)
{
    
    try {
        var message = error.get_message();   

        if(message == 'Authentication failed.'){
            location.href = 'login.aspx?ReturnUrl=' + location.href;
        } else {   
            alert(message);
        }
    } catch(e) { alert(e.message); }
}

function ValidateKeywordSearch(input)
{
    if(input.value.trim().length == 0 || input.value.trim().toLowerCase() == 'please enter keyword(s) here.')
    {
        input.value = 'Please enter keyword(s) here.';
        input.select();
        return false;
    }
    
    return true;
}


function CheckLimit(field, maxLimit, displayContainerId) 
{
	var fieldLength = field.value.length;
	
	if (fieldLength > maxLimit) 
	{
		field.value = field.value.substr(0, maxLimit);		
	} 
	
	if(displayContainerId && displayContainerId != '')
	{
	    document.getElementById(displayContainerId).innerHTML = (maxLimit - fieldLength > 0) ? maxLimit - fieldLength : 0;
	}	
}


function LineWrap(word, length, separator)
{ 
    var newWord = '';
    word = word.replace(/\n|&lt;br(\/)?&gt;|<br(\/)?>/ig, ' <br/> ');   
    var tmp =  word.split(' ');  
    var chunks = tmp; //new Array();
    
    /*
    for(i = 0; i < tmp.length; i++)
    {
        var btmp = tmp[i].split('<br/>');
        
        for(j = 0 ; j < btmp.length; j++)
        {
            chunks.push(btmp[j]);
        }        
    }
    */    

    for (i = 0; i < chunks.length; i++)
    {
        chunks[i] = chunks[i].replace(/href[\s]*=[\s]*/ig, 'href=');
        if (chunks[i].length > length && chunks[i].indexOf('href=') == -1)
        {
            var num_wraps = Math.ceil(chunks[i].length / length);                    
            var start;
            
            for (var j = 0; j < num_wraps; j++)
            {                       
                start = j * length;                   
                newWord += chunks[i].substr(start, length) + separator;                
            }   
        }
        else
        {
            newWord += chunks[i] + ' ';            
        }
    }
    
    return newWord;
}

function SetLink(text, param)
{ 
    if(!param) param = ''; 
    text = text.replace(/\n/g, '').replace(/\r/g, '').replace(/&amp;/ig, '&');
    return text.replace(/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/ig, '<a href="$&" ' + param + '> $& </a>');    
}

function SubmitKeyword(field, e)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
    {
        if(ValidateKeywordSearch(field)) location.href='searchresults.aspx?t=q&k=' + field.value;
        return false;
    }
    else
        return true;
}

function SubmitLogin(field, e, btn)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
    {
        field.value = field.value;
        WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(btn, "", true, "", "", false, true));
        return false;
    }
    else   
        return true;
}

function FocusPassword(e, pwd)
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
    {
        $get(pwd).focus();
        return false;
    }
    else
        return true;
}

function OnAuthenticate(result) 
{
    if (result) {
        
    } else {
        window.location.reload(); // just reload the page so that the ASP.Net Authentication would redirect the page to the login page
    }
}