function isNumberKey( evt )
{
	var charCode = (evt.which) ? evt.which : event.keyCode;
	if( charCode > 31 && (charCode <48 || charCode > 57 ))
	  return false;
	  
	return true;
}

function showFAQ( faqId, categoryId, articleId )
{
	if( ( articleId != null ) && ( articleId != -1 ) )
		showFAQArticle( faqId, articleId );
	else
		showFAQCategory( faqId, categoryId );
}


function showFAQCategory( faqId, categoryId )
{
	loadPage( contextPath + "/faq/faqManager.do?cmd=showFAQCategory&faqId=" + faqId + "&categoryId=" + categoryId + "&noLog=true&ajax=true" )
}


function showFAQArticle( faqId, articleId )
{
	loadPage( contextPath + "/faq/faqManager.do?cmd=showFAQArticle&faqId=" + faqId + "&articleId=" + articleId + "&noLog=true&ajax=true" )
}


function giveFeedbackGuide( contextPath, userName, section, language, caseTitle, caseAction, sessionId )
{
	if( section == null ) section = "";
	if( caseTitle == null ) caseTitle = "";
	if( caseAction == null ) caseaction = "";
	if( language == null ) language = "English";
	if( contextPath == null ) contextPath = "/staff";

	caseTitle = Url.encode( caseTitle );
	caseAction = Url.encode( caseAction );

	var feedURL = contextPath + "/feedback/createfeedback.jsp?fromUser=" + userName;
	feedURL += "&withTitle=" + caseTitle;
	feedURL += "&withAction=" + caseAction;
	feedURL += "&userLanguage=" + language;
	feedURL += "&section=" + section;
	feedURL += "&type=Guide";

	if( ( sessionId != null ) && ( sessionId != undefined ) )
	{
		feedURL += "&sessionId=" + sessionId;
	}

	var myFeedback = window.open( '', '_feedback', 'height=470,width=530' );
	myFeedback.location = feedURL;
	if( window.focus )
		myFeedback.focus();
}

function giveFeedbackFAQ( contextPath, userName, section, language, caseTitle, caseAction )
{
	if( section == null ) section = "";
	if( caseTitle == null ) caseTitle = "";
	if( caseAction == null ) caseaction = "";
	if( language == null ) language = "English";
	if( contextPath == null ) contextPath = "/staff";

	caseTitle = Url.encode( caseTitle );
	caseAction = Url.encode( caseAction );


	var feedURL = contextPath + "/feedback/createfeedback.jsp?fromUser=" + userName;
	feedURL += "&withTitle=" + caseTitle;
	feedURL += "&withAction=" + caseAction;
	feedURL += "&userLanguage=" + language;
	feedURL += "&section=" + section;
	feedURL += "&type=FAQ";

	//alert(feedURL);

	var myFeedback = window.open( '', '_feedback', 'height=470,width=530' );
	myFeedback.location = feedURL;
	if( window.focus )
		myFeedback.focus();
}


function escalateToRemedy( contextPath, sessionId, customerNumber, userName, section, language, modelName, stepName )
{
	// param stepName not currently used

	var escalateURL = contextPath + "/escalation/escalationManager.do?cmd=createEscalation&sessionId=" + sessionId + "&customerNumber=" + customerNumber + "&userName=" + userName + "&caseSection=" + section + "&caseLanguage=" + language + "&caseType=" + section + "&caseTitle=" + modelName;

	/*
	var myEscalation = window.open( '', '_escalate', 'height=570,width=600,scrollbars=yes' );
	myEscalation.location = escalateURL;
	if( window.focus )
		myEscalation.focus();

		*/

	document.location = escalateURL;
}


var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }
}


function submitRequestFaqCategory( categoryId )
{
	document.forms['faqManagerForm'].categoryId.value = categoryId;
	document.forms['faqManagerForm'].cmd.value = "getCategory";
	document.forms['faqManagerForm'].submit();
}


function getCustomerId()
{
	var customerid = "";

	var customerNumberField = document.getElementById("customerNumberField");
	var callerNumberField = document.getElementById("callerNumberField");

	if( customerNumberField != null )
		customerid = customerNumberField.value;

	if( customerid == null || customerid.length == 0 )
	{
	 	// if no customer id, then use callernumber
	 	if( callerNumberField != null )
	 		customerid = callerNumberField.value
	}

	return customerid;
}


function submitRequestFaqArticle( articleId, language, faqId, userId )
{
	document.forms['faqManagerForm'].articleId.value = articleId;
	document.forms['faqManagerForm'].cmd.value = "getArticle";
	document.forms['faqManagerForm'].language.value = language;
	document.forms['faqManagerForm'].faqId.value = faqId;
	document.forms['faqManagerForm'].userId.value = userId;
	document.forms['faqManagerForm'].customerId.value = getCustomerId();

	document.forms['faqManagerForm'].submit();
}


function submitRequestFaqArticleAnswer( clickId, answer, returnCategoryId )
{
	document.forms['faqManagerForm'].cmd.value = "clickArticle";
	document.forms['faqManagerForm'].clickId.value = clickId;
	document.forms['faqManagerForm'].clickAnswer.value = answer;
	document.forms['faqManagerForm'].categoryId.value = returnCategoryId;

	document.forms['faqManagerForm'].submit();
}

function submitGetFAQArticleAnswer( contextPath, faqId, faqLanguage, faqArticleClickId, faqAnswer )
{
	var faqClickURL = contextPath + "/faq/faqManager.do?cmd=showClickArticle&faqId="+faqId+"&language="+faqLanguage+"&clickId="+faqArticleClickId+"&clickAnswer="+faqAnswer;

	document.location = faqClickURL ;
}


// Standard versions
function submitRequestSwitchLanguage( sessionId, language )
{
	document.forms['requestNextStepForm'].sessionId.value = sessionId;
	document.forms['requestNextStepForm'].language.value = language	;
	document.forms['requestNextStepForm'].switchLanguage.value = true;
	document.forms['requestNextStepForm'].language.value = language;
	document.forms['requestNextStepForm'].submit();
}

function submitRequestNextStep( sessionId, stepKey, responseKey )
{
	document.forms['requestNextStepForm'].sessionId.value = sessionId;
	document.forms['requestNextStepForm'].stepKey.value = stepKey;
	document.forms['requestNextStepForm'].responseKey.value = responseKey;
	document.forms['requestNextStepForm'].submit();
}

function submitRequestPerformedStep( sessionId, stepKey )
{
	document.forms['requestNextStepForm'].sessionId.value = sessionId;
	document.forms['requestNextStepForm'].stepKey.value = stepKey;
	document.forms['requestNextStepForm'].performedStep.value = true;
	document.forms['requestNextStepForm'].submit();
}

function submitRequestExpertStep( sessionId, stepKey, expertStepKey )
{
	document.forms['requestNextStepForm'].sessionId.value = sessionId;
	document.forms['requestNextStepForm'].stepKey.value = stepKey;
	document.forms['requestNextStepForm'].expertStepKey.value = expertStepKey;
	document.forms['requestNextStepForm'].expertStep.value = true;
	document.forms['requestNextStepForm'].submit();
}

function submitRequestAssistance( sessionId )
{
	document.forms['requestAssistanceForm'].sessionId.value = sessionId;
	document.forms['requestAssistanceForm'].submit();
}




// test for inspector mode
function submitRequestExpertStepAnswerSolutionDirectly( sessionId, stepKey, expertStepKey, answer )
{
	document.forms['requestNextStepForm'].sessionId.value = sessionId;
	document.forms['requestNextStepForm'].stepKey.value = stepKey;
	document.forms['requestNextStepForm'].expertStepKey.value = expertStepKey;
	document.forms['requestNextStepForm'].inspectorStep.value = true;
	document.forms['requestNextStepForm'].expertStepSolutionAnswer.value = answer;
	document.forms['requestNextStepForm'].submit();
}

function submitRequestExpertStepAnswerQuestionDirectly( composite )
{
	var sessionId;
	var stepKey;
	var expertStepKey;
	var answer;

	document.forms['requestNextStepForm'].sessionId.value = sessionId;
	document.forms['requestNextStepForm'].stepKey.value = stepKey;
	document.forms['requestNextStepForm'].expertStepKey.value = expertStepKey;
	document.forms['requestNextStepForm'].inspectorStep.value = true;
	document.forms['requestNextStepForm'].expertStepSolutionAnswer.value = answer;
	document.forms['requestNextStepForm'].submit();
}




function test(sessionId, stepKey)
{
	alert(sessionId + " hund " + stepKey);
}



