﻿function FirstStep(stepName, gpSessionId, gpName)
{
   jQuery.post(
      '/support/ajax/guidedPath/FirstStep.aspx',
      {
         stepId: stepName,
         gpSessionId: gpSessionId,
         gpName: gpName
      },
      function(data)
      {
         jQuery('#gpTarget').html(data.toString());
      }
   );
}

function NextStep(ruleId, gpReply, currentStepId, gpSessionId, svcTag, gpName)
{
	jQuery.post(
		'/support/ajax/guidedPath/NextStep.aspx?svcTag=' + svcTag,
		{
			ruleId: ruleId,
			gpReply: gpReply,
			currentStepId: currentStepId,
			gpSessionId: gpSessionId,
			svcTag: svcTag,
			gpName: gpName			
		},
		function(data)
		{
		    procDataForOmniature(data,svcTag,gpReply,gpName);
			procNextStep(data);
		}
	);		    
}

function procNextStep(data)
{
	var dataString = data.toString();	
	if (dataString.indexOf('$redirect$') > -1)
	{
		var url = dataString.substr(dataString.indexOf(':') + 1);
		window.location.href = url;
	}
	else
	{
	    jQuery('#gpTarget').html(data.toString());

	    if (window.procESC)
	    {
	        procESC();
	    }	
	}
}

function BackStep(gpSessionId, historyId, svcTag, gpName, historyDisplayName)
{
	jQuery.post(
		'/support/ajax/guidedPath/BackStep.aspx?svcTag=' + svcTag,
		{
			gpSessionId: gpSessionId,
			historyId: historyId,
			svcTag: svcTag,
			gpName: gpName
		},
		function(data)
		{
		    procDataForOmniature(data,svcTag,historyDisplayName,gpName);
		    procNextStep(data);
		}
	);
}

function ReRender(gpSessionId, historyId, svcTag, gpName)
{
    jQuery.post(
		'/support/ajax/guidedPath/ReRender.aspx?svcTag=' + svcTag,
		{
		    gpSessionId: gpSessionId,
		    historyId: historyId,
		    svcTag: svcTag,
		    gpName: gpName
		},
		function(data) {
		    procDataForOmniature(data,svcTag,'ReRender',gpName);
	        procNextStep(data);
		}
	);
}

function SubmitSearchTerms(ruleId, gpReply, currentStepId, gpSessionId, svcTag, gpName)
{
    var searchParams = jQuery('#searchParams').val();

    jQuery.post(
		'/support/ajax/guidedPath/NextStep.aspx?svcTag=' + svcTag,
		{
		    ruleId: ruleId,
		    gpReply: gpReply,
		    currentStepId: currentStepId,
		    gpSessionId: gpSessionId,
		    svcTag: svcTag,
		    searchParams: searchParams,
		    gpName: gpName
		},
		function(data) {
		    procDataForOmniature(data,svcTag,gpReply,gpName);
		    procNextStep(data);
		}
	);
}

function SubmitSvctag(ruleId, gpReply, currentStepId, gpSessionId, required, requiredMsg, gpName)
{
    var svcTag = jQuery('#serviceTag').val();
    var chkSaveAs = jQuery('#chkSaveAs').val(); // Flag to add the service tag to permamnent cookie
    createCookie('tempSvcTag', svcTag);
	
	jQuery.post(
		'/support/ajax/guidedPath/SubmitSvcTag.aspx?svcTag=' + svcTag + '&required=' + required,
		{
			svcTag: svcTag,
			currentStepId: currentStepId,
			required: required,
			requiredMsg: requiredMsg,
			chkSaveAs: chkSaveAs
		},
		function(data)
		{
			if (data.toString().length > 0)
			{
				jQuery('#errorStrip').html(data.toString());
			}
			else
			{
				NextStep(ruleId, gpReply, currentStepId, gpSessionId, svcTag, gpName);
			}
		}
	);
}

function SubmitNoSvctag(ruleId, gpReply, currentStepId, gpSessionId, required, requiredMsg, gpName)
{
    var svcTag = '';
    jQuery.post(        
		'/support/ajax/guidedPath/SubmitSvcTag.aspx?svcTag=&required=' + required,
		{
		    svcTag: svcTag,
		    currentStepId: currentStepId,
		    required: required,
		    requiredMsg: requiredMsg
		},
		function(data) {
		    if (data.toString().length > 0) {
		        jQuery('#errorStrip').html(data.toString());
		    }
		    else {
		        NextStep(ruleId, gpReply, currentStepId, gpSessionId, svcTag, gpName);
		    }
		}
	);
}

function myOpenWindow()
{
	myWindowHandle = window.open('','WinName','WIDTH=800,HEIGHT=600,RESIZABLE=YES,SCROLLBARS=YES,TOOLBAR=NO,LEFT=0,TOP=20');
	document.formSupportRequestEnterTag.submit();
}

function procESC()
{
    var sTag = readCookie('tempSvcTag');

    var esc = parseInt(sTag, 36) + ' ';
    
	jQuery('#escSpan').text(esc);
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

//Functions related to Omniature javascript call
function procDataForOmniature(data,svcTag,gpReply,gpName)
{
	var dataString = data.toString();
	var dsnNumber='',lendocId;
	var gpCookie=readCookie("eSupportGuidedPath_" + gpName);
	var arrgpCookie;
	if (dataString.indexOf('docid=') > -1 && dataString.indexOf('&~ck') > -1)
	{
	    lendocId=dataString.indexOf('&~ck') - (dataString.indexOf('docid=') + 6)
		dsnNumber = dataString.substr(dataString.indexOf('docid=') + 6,lendocId);
	}	
	else if(dataString.indexOf('treeid=') > -1 && dataString.indexOf('&~ck') > -1)
	{
		lendocId=dataString.indexOf('&~ck') - (dataString.indexOf('treeid=') + 7)
		dsnNumber = dataString.substr(dataString.indexOf('treeid=') + 7,lendocId);
	}
    s_dell.prop17=svcTag;
    if(gpReply=='Go'&& svcTag=='') gpReply="svcTag:";
    if(gpReply=='Go'&& svcTag!='') gpReply="svcTag:" + svcTag;
    if(dsnNumber != '')
    {
        s_dell.prop24=dsnNumber;
    }
    else
    {
        s_dell.prop24=gpReply;
    }
    if(gpCookie != null)
    {
        arrgpCookie=gpCookie.split("&");
        for(var i=0;i< arrgpCookie.length;i++)
        {
            if(arrgpCookie[i] != null)
            {
                if (arrgpCookie[i].indexOf('cnlnsegcs') == 0)
                    gpName= arrgpCookie[i].substring(10, arrgpCookie[i].length)+ gpName;
            }
        }
    }
    s_dell.pageName=gpName;
    s_dell.t();
}
//End Functions related to Omniature javascript call