function adhocsubmit(submitType)
{
    if (submitType != -1)
    {
       // Not submit so either remove pen or change pen.
        if (document.getElementById)
        {
            document.getElementById("form1").action = "./adhoc.aspx" + BuildQueryString(submitType);
        }
        else
        {
            document.all["form1"].action = "./adhoc.aspx" + BuildQueryString(submitType);
        }
    }
    if (document.getElementById)
    {
        document.getElementById("form1").submit();
    }
    else
    {
        document.all["form1"].submit();
    }
}

function DisableEnterKey(event)
{
     var key;
     if(window.event)
     {
          key = window.event.keyCode;     //IE
     }
     else
     {
          key = event.which;     //firefox
     }
     
     if(key == 13)
     {
          adhocsubmit(-1);
          return false;
     }
     else
     {
          return true;
     }
}

function BuildQueryString(submitType)
{
    var returnValue = GetValue("ctl00_phDefault_hdQueryString");
    if (returnValue != null && returnValue.length > 0) 
    {
        returnValue += "&";
    }
    else 
    {
        returnValue += "?";
    }
    returnValue += "trendtype=" + GetValue("selTrendTypeList");
    var penCount = GetValue("ctl00_phDefault_hdPenCount");
    var i = 0;
    
    var index = "";
    for (i = 0; i < penCount; i++)
    {
        index = i.toString();
        returnValue += "&L" + index + "=" + HTMLEncode(GetValue("txtLookup" + index));
        returnValue += "&P" + index + "=" + GetValue("selPenColorList" + index);
        returnValue += "&A" + index + "=" + HTMLEncode(GetValue("ctl00_phDefault_hdPenAlias" + index));
        returnValue += "&I" + index + "=" + GetValue("selInterfaceList" + index);
        returnValue += "&S" + index + "=" + GetCheckedValue("chkPen" + index);
        returnValue += "&O" + index + "=" + HTMLEncode(GetValue("txtOffset" + index));
    }
    
    if (submitType >= 0)
    {
        if (submitType == 99)
        {
            returnValue += "&U=" + GetValue("selPenCountList");
        }
        else
        {
            returnValue += "&D=" + submitType.toString();
        }
    }

    returnValue += "&minY=" + HTMLEncode(GetValue("txtMinimumY"));
    returnValue += "&maxY=" + HTMLEncode(GetValue("txtMaximumY"));
    return returnValue;
}

function HTMLEncode(value)
{
	var SAFECHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.!~*'()";	
	var HEX = "0123456789ABCDEF";
	var encoded = "";
	for (var i = 0; i < value.length; i++ ) 
	{
		var ch = value.charAt(i);
	    if (ch == " ") 
	    {
		    encoded += "+";	
		} 
		else if (SAFECHARS.indexOf(ch) != -1) 
		{
		    encoded += ch;
		} 
		else 
		{
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) 
			{
				encoded += "+";
			} 
			else 
			{
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} 
	return encoded;
}

function GetValue(elementID)
{
    if (document.getElementById)
    {
        return document.getElementById(elementID).value;
    }
    else
    {
        return document.all[elementID].value;
    }
}

function GetCheckedValue(elementID, useDocumentAll)
{
    if (document.getElementById)
    {
        if (document.getElementById(elementID).checked)
        {
            return "1";
        }
        else
        {
            return "0";
        }
    }
    else
    {
        if (document.all[elementID].checked)
        {
            return "1";
        }
        else
        {
            return "0";
        }
    }
}

function cancel()
{
    var path = readCookie("vkpiadhoccancel");
    if (path.length > 0)
        window.location = readCookie("vkpiadhoccancel");
    else
        window.location = ".\default.aspx";
}