function GoToHttpHref(pageName)
{
    if(pageName.indexOf("AddParams") != -1)
    {
        pageName = pageName.replace("AddParams", params).replace("?","");
    }
    
    var nextLocation = location.href.replace("https","http");
    nextLocation = nextLocation.slice(0,nextLocation.indexOf(vRoot));
    location.href = nextLocation + vRoot + "/" + pageName;
    
}

function PostToCR(buttonURL, product)
{
    
    var errorPage = "";
    var controlPage = "";
    if(product == "vista")
    {
        controlPage = "downloads/Validate.aspx" + params + "&Page=VistaQualifyInfo";
        errorPage = "vista/PurchaseError.aspx" + params + "&msg=60";
    }
    else
    {
        controlPage = "purchase/BrowserAlternative.aspx" + params + "&page=validate";
        errorPage = "purchase/PurchaseError.aspx" + params + "&msg=40";
    }
    
    var details = $("genuineMachineDetail");
    if (details == null)
    {
        GoToPageHref(errorPage + "2");
    }
    else if (navigator.appName.indexOf("Microsoft") == -1 && navigator.appName.indexOf("Netscape") == -1 )
    {		
        GoToPageHref("purchase/BrowserAlternative.aspx" + params + "&page=validate");		        
    }
    else if ( details.value == "")
    {
        GoToPageHref(controlPage);
    }
    else
    {
        var decodedDetails = HtmlDecode(details.value);
        var osNumber = getXmlVal("OS", decodedDetails);

        if (null != osNumber && osNumber.length > 0)
        {
            var details = osNumber.split(".");
            var osType = details[7];
    	    
    	    if ((product == "vista" && details[0] != "6") || (product != "vista" && osType != "hom" && osType != "pro"))
    	    {
    	       GoToPageHref(errorPage + "3");
    	    }
            else
            {
                SubmitControlForm(buttonURL, false);
            }
        }
        else
        {
            GoToPageHref(errorPage + "3");
        }
    }
}

function GoToPageHref(pageName, newWindow)
{
    if(pageName.indexOf("AddParams") != -1)
    {
        pageName = pageName.replace("AddParams", params).replace("?","");
    }
    
    if(newWindow)
    {
        window.open(vRoot + "/" + pageName);
    }
    else
    {    
        location.href = vRoot + "/" + pageName;
    }
    
}

function GoToPageReplace(pageName)
{
    location.replace (vRoot + "/" + pageName);
}

/*********************************
Used for RC1 toggele message on nonGenuine page.
*********************************/
function nonGenuineToggleMessage()
{
    toggleMessage();
    
    if ($('failureMessage') && $('failureMessage').style.display != 'none')
    {
        pingOmniture('nonGenuine: View Details link clicked');
    }
    else
    {
        pingOmniture('nonGenuine: Hide Details link clicked');
    }
}

/****************************
 * Expand/collapse failure message (e.g. nonGenuine)
 */
function toggleMessage(){
    var expandImage = $("detailsImage");
    var messageDetails = $("failureMessage");
    var detailsLink = $("showDetailsLink");
    
    if(messageDetails.style.display == "none"){
        messageDetails.style.display = "block";
        
        expandImage.src = expandImage.src.replace("expand", "collapse");
        expandImage.alt = collapseText;
        
        detailsLink.innerHTML = collapseText;
        
    }else{
        messageDetails.style.display = "none";
        
        expandImage.src = expandImage.src.replace("collapse", "expand");
        expandImage.alt = expandText;        
             
        detailsLink.innerHTML = expandText;        
    }
}

/* toggle the hover for mousover/mouseout */
function buttonMouseOver(image){
    image.src = image.src.replace("normal", "hover");
    
    image.style.cursor = "pointer";
}

function buttonMouseOut(image){
    image.src = image.src.replace("hover", "normal");
    
    image.style.cursor = "default";
}

/****************************
 * Expand/Collapse options list (e.g. GetGenuine and FailureScenarios)
 */
function ToggleOption(optionNum)
{
    var image = $("opt_" + optionNum + "_image");
    var description = $("opt_" + optionNum + "_expand");
    
    if(description.style.display == "none")
    {
        description.style.display = "block";
        
        image.src = image.src.replace("closed", "open");
        image.alt = collapseText;
    }
    else
    {
        description.style.display = "none";
    
        image.src = image.src.replace("open", "closed");
        image.alt = expandText;
    }
    
    UpdateExpansions();
}

// Adds an input form for Buy Now box component
function AddHiddenInputToForm(f, n, v)
{
    var el = document.createElement('INPUT');
    el.type = 'HIDDEN';
    el.name = n;
    el.id = n;
    el.value = v;
    f.appendChild(el);
}

/* change pointer style for mouseover/mouseout */
function expandMouseOver(object){
    object.style.cursor = "pointer";
}

function expandMouseOut(object){
    object.style.cursor = "default";
}

/************
 * Other Shared Functions
 */
function AllowEkeys(errorParam)
{
    if (errorParam == "1" || 
        errorParam == "3" || 
        errorParam == "8" || 
        errorParam == "9" ||
        errorParam == "11" ||
        errorParam == "13")
    {
        return true;
    }
    else
    {
        return false;
    }
}

function OpenWindow(popupUrl, popupHeight, popupWidth)
{	
	try
	{
	    if(popupHeight == null)
		    popupHeight = 415;
	    if(popupWidth == null)
		    popupWidth = 610;
    		
	    self.name='genuineMain';
	    var popupWindow = window.open(vRoot + "/" + popupUrl,null,'width=' + popupWidth + ',height=' + popupHeight + ',scrollbars=yes,resizable=yes'); 
	    popupWindow.focus();
	}
	catch(ex)
	{
	    //pop up blocker exists
	}
}

function LegitCheckOnLoad(paramsToAdd)
{
	var delimiter = "&";
	var lastErrorDiagnostic;
	var sresult = "-100";
	
	try
	{
		sresult = LegitCheck.LegitCheck();
	}
	catch(ex)
	{
		try
		{
			lastErrorDiagnostic = LegitCheck.GetLastErrors();
			
			if(lastErrorDiagnostic != "")
			{
				paramsToAdd = paramsToAdd + "&diag=" + lastErrorDiagnostic;
				if(pageName != "Offers")
				{

		            GoToPageHref("downloads/default.aspx" + paramsToAdd + "&LegitCheckError=-100");
				}
				// Allow execution to continue 
			}
			else
			{
				if(pageName != "Offers")
				{
		            GoToPageHref("downloads/default.aspx" + paramsToAdd + "&LegitCheckError=-100");
				}
				// Allow execution to continue 
			}
		}
		catch(ex2)
		{
			if(pageName != "Offers")
			{
		        GoToPageHref("downloads/default.aspx" + paramsToAdd + "&LegitCheckError=-100");
			}
			// Allow execution to continue 
		}
	}
	
	var result = Number(sresult);
	
	if(lastErrorDiagnostic == "")
	{
		try
		{
			lastErrorDiagnostic = LegitCheck.GetLastErrors();
		}
		catch(ex3)
		{
			lastErrorDiagnostic = "";
		}
	}
	
	if(lastErrorDiagnostic != "")
	{
		paramsToAdd = paramsToAdd + "&diag=" + lastErrorDiagnostic;
	}
	
	if(result == 0)
	{
		if (pageName != "SuccessfulActivation" && pageName != "Offers")
		{
			GoToPageHref("downloads/SuccessfulActivation.aspx" + paramsToAdd);
		}
		// Allow execution to continue
	}	
	else if((result >= 1 && result <= 13) || result == 90 || result == 99)
	{
		GoToPageHref("downloads/default.aspx" + paramsToAdd + "&LegitCheckError=" + result);
	}
	else 
	{
		if(pageName != "Offers")
		{
		    GoToPageHref("downloads/default.aspx" + paramsToAdd + "&LegitCheckError=-100");
		}
		// Allow execution to continue 
	}
	return result;
}


function LegitCheckOnLoadOGA(paramsToAdd)
{
	var delimiter = "&";
	var lastErrorDiagnostic;
	var sresult = "-100";
	
	try
	{
		sresult = LegitCheck.LegitCheck();
	}
	catch(ex)
	{
		try
		{
			lastErrorDiagnostic = LegitCheck.GetLastErrors();
			
			if(lastErrorDiagnostic != "")
			{
				paramsToAdd = paramsToAdd + "&diag=" + lastErrorDiagnostic;
				if(pageName != "Offers")
				{
		            GoToPageHref("downloads/default.aspx" + paramsToAdd + "&LegitCheckError=-100");
				}
				// Allow execution to continue 
			}
			else
			{
				if(pageName != "Offers")
				{
		            GoToPageHref("downloads/default.aspx" + paramsToAdd + "&LegitCheckError=-100");
				}
				// Allow execution to continue 
			}
		}
		catch(ex2)
		{
			if(pageName != "Offers")
			{
		        GoToPageHref("downloads/default.aspx" + paramsToAdd + "&LegitCheckError=-100");
			}
			// Allow execution to continue 
		}
	}
	
	var result = Number(sresult);
	
	if(lastErrorDiagnostic == "")
	{
		try
		{
			lastErrorDiagnostic = LegitCheck.GetLastErrors();
		}
		catch(ex3)
		{
			lastErrorDiagnostic = "";
		}
	}
	
	if(lastErrorDiagnostic != "")
	{
		paramsToAdd = paramsToAdd + "&diag=" + lastErrorDiagnostic;
	}
	
	if(result == 0)
	{
		if (pageName != "SuccessfulActivation" && pageName != "Offers")
		{
			GoToPageHref("downloads/SuccessfulActivation.aspx" + paramsToAdd);
		}
		// Allow execution to continue
	}	
	else if((result >= 1 && result <= 13) || result == 90 || result == 99)
	{
		GoToPageHref("downloads/default.aspx" + paramsToAdd + "&LegitCheckError=" + result);
	}
	else 
	{
		if(pageName != "Offers")
		{
		    GoToPageHref("downloads/default.aspx" + paramsToAdd + "&LegitCheckError=-100");
		}
		// Allow execution to continue 
	}
	return result;
}

function LTrim(InString){
	OutString=InString;
	for(Count=0; Count < InString.length; Count++)  {
		TempChar=InString.substring(Count, Count+1);
		if(TempChar!=" ") {
			OutString=InString.substring(Count, InString.length);
			break;
		}
	}
	return(OutString);
}

function RTrim(argvalue) {
	while (1) {
		if (argvalue.substring(argvalue.length - 1, argvalue.length) != " ")
			break;
		argvalue = argvalue.substring(0, argvalue.length - 1);
	}
	return argvalue;
}

//remove left line breaks
function RemoveLBreaks(InString)
{
	OutString=InString;
	for(Count=0; Count < InString.length; Count++)  {
		TempChar=InString.substring(Count, Count+1);
		if(TempChar!="\r" && TempChar!="\n") {
			OutString=InString.substring(Count, InString.length);
			break;
		}
	}
	return(OutString);
}

//remove right line breaks
function RemoveRBreaks(InString)
{
	while (1) {
		if (InString.substring(InString.length - 1, InString.length) != "\r" && InString.substring(InString.length - 1, InString.length) != "\n")
			break;
		InString = InString.substring(0, InString.length - 1);
	}
	return InString;
}

function translate_errnum(num) 
{
	var msg = '' + num.toString(16).toUpperCase();
	while (msg.length < 6) 
	{
		msg = '0' + msg;
	}
	return '0x80' + msg;
}

// Changes the row expanding button to a row collapsing button or vice versa.
function changeImageSet()
{	
	var changeThisButton = document.getElementById("togglebutton");
			
	if(changeThisButton.alt == "Expand")
	{
		changeThisButton.alt = "Collapse";
		changeThisButton.src =  vRoot + "/static/images/collapse.jpg";		
	}
	else
	{
		changeThisButton.alt = "Expand";
		changeThisButton.src =  vRoot + "/static/images/expand.jpg";	
	}
}

// On mouse over, changes the expand or collapse button to an inverted color version.
function invertOnMouseOver()
{
	var changeThisButton = document.getElementById("togglebutton");
	
	if(changeThisButton.alt == "Expand")
	{
		changeThisButton.src = "../static/images/expand.jpg";		
	}
	else
	{
		changeThisButton.src = "../static/images/collapse.jpg";		
	}
}

// On mouse out, returns the expand or collapse button to the regular image.
function revertOnMouseOut()
{
	var changeThisButton = document.getElementById("togglebutton");
	
	if(changeThisButton.alt == "Expand")
	{
		changeThisButton.src = "../static/images/expand.jpg";		
	}
	else
	{
		changeThisButton.src = "../static/images/collapse.jpg";	
	}
}

function pingOmniture(prop10Value, urlToRedirect, formName)
{
	try
	{
		//Omniture tracking
		s.prop10=prop10Value;
		s.t();
	}
	catch (e) { }	
	
	if (null != urlToRedirect && urlToRedirect.length > 0)
	{
		if (null != formName && formName.length > 0)
		{
			document.getElementById(formName).action = urlToRedirect;
			document.getElementById(formName).submit();
		}
		else
		{
			document.location.href = urlToRedirect;
		}
	}
}

//function runValidation(InString)
//{
//	if(IsPluginSupportedBrowser()) 
//	{
//		document.location.href = "InstallPlugin.aspx"+InString;				
//	}
//	else
//	{
//		var sresult = "-100";
//		try
//		{
//			sresult = OGACheck.LegitCheck();
//			var result = Number(sresult);
//			
//			switch(sresult)
//			{
//				case "100":
//					document.location.href = "SuccessfulValidation.aspx"+InString;
//					break;
//				default:
//					document.location.href = "default.aspx"+InString+ "&LegitCheckError=" + sresult;
//					break;		
//			}
//		}
//		catch(ex)
//		{
//			// Allow user to try Alternate Method
//		}
//	}
//}
function Machine(legitcontrol) {
    this.os = "";
    this.sysPKey = "";
    this.pid = "";
    this.sysMod = "";
    this.sysMan = "";
    this.legitcontrol = legitcontrol;
    
    this.getMachineDetails();
}
function getMachineDetails() {
    with (this) {
        var machineInfo;
        try {
            machineInfo = legitcontrol.GetMachineDetails();
        } catch(e){}
     
        if(machineInfo != "") {
            sysPKey = getXmlVal("PKey", machineInfo);
	        sysPid = getXmlVal("PID", machineInfo);
	        var sysSid = getXmlVal("SID", machineInfo);
	        sysMod = getXmlVal("SYSTEM/Model", machineInfo);
	        sysMan = getXmlVal("SYSTEM/Manufacturer", machineInfo);

	        var osNumber = getXmlVal("OS", machineInfo);
	        var details = osNumber.split(".");
        		
	        var osMajor = details[0];
	        var osMinor = details[1];
	        var osBuild = details[2];
	        var osSPmaj = details[5];
	        var osSPmin = details[6];
	        var osType = details[7];
    	
		    if(osMajor == "5" && osMinor == "1") {
			    os = "Windows XP";
			    if (osType == "tab") {
			        //if(isTablet == "yes")
		            os = os + " Tablet PC Edition";
			    } else if(osType == "med") {
			        //else if(isMedia == "yes")
				    os = os + " Media Center Edition";
			    } else if (osType == "sta") {
		    	    //else if(isStarter == "yes")
				    os = os + " Starter Edition";
			    } else if (osType == "pro") {
				    //else if(osSuite == "256")
				    os = os + " Professional";
			    } else {
				    os = os + " Home";
			    }
		    } else if(osMajor == "5" && osMinor == "2") {
			    os = "Windows Server 2003";
		    } else if(osMajor == "5" && osMinor == "0") {
			    os = "Windows 2000";
		    } else if(osMajor == "6" && osMinor == "0") {
    			os = "Windows Vista";
    		}
		    if(osSPmaj != 0) {
			    os = os + ", Service Pack " + osSPmaj;
		    }
        }
	}
}
//This statement defines the function 'getMachineDetails' as a  method for the class 'Machine'
Machine.prototype.getMachineDetails = getMachineDetails;

// This method takes the elementId and returns the element
function $(elementId) {
    if (document.getElementById(elementId))
    {
        return document.getElementById(elementId);
    }
    else if (document.getElementById(masterID + elementId))
    {
        return document.getElementById(masterID + elementId);   
    }
    else if (document.getElementById(otherFormID + elementId))
    {
        return document.getElementById(otherFormID + elementId);
    }        
}

function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

//Cookie Functions.
function ReadCookie(name) 
{
	var nameEQ = name + "=";
	var cookies = document.cookie.split(';');
	for(var count = 0; count < cookies.length; count++) 
	{
		var cookie = cookies[count];
		while (cookie.charAt(0)==' ') 
		    cookie = cookie.substring(1, cookie.length);
		if (cookie.indexOf(nameEQ) == 0) 
		    return cookie.substring(nameEQ.length, cookie.length);
	}
	return null;
}

function GetCookieCount(product)
{
    var cookieValue = ReadCookie(countCookieName);
    
    if(cookieValue != null)
    {
        cookieValue = trim(cookieValue);
        if(cookieValue.length != 0)
        {
            var index = cookieValue.indexOf(product);
            if(index != -1)
            {
                return cookieValue.substring(index + 4, index + 5);
            }
        }    
    }
    return "-1";
}

function SubmitAspNetForm(nextPage)
{
    //submit the form
    var webForm = $("aspnetForm");
    webForm.action = nextPage;                
    webForm.submit();
}

// Common accross OMPost.js, Docuprint.js and DigitalRiver.js
function addField(name, value)
{
    var newfield = btpField.cloneNode(true);
	
    newfield.name = name;
    newfield.value = value; 

    var form1 = document.getElementById('omform');
    form1.insertBefore(newfield, btpField);
}

//Creates XMLHTTPRequest object
function createAsynHttpRequest() 
{
    try
    {
	    request = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch(e)
    {
	    try
	    {
		    request = new ActiveXObject("Microsoft.XMLHTTP");
	    }
	    catch(oc)
	    {
		    request = null;
	    }
    }

    if(!request && typeof XMLHttpRequest != "undefined")
    {
        request = new XMLHttpRequest();
    }

    return request;
}

function HtmlDecode(text)
{
    while(text.indexOf("&gt;") > 0)
    {
        text = text.replace("&gt;", ">").replace("&lt;", "<");
    }
    return text;
}

function CookieCheck(name, value)
{
	return document.cookie.search(name + "=" + value) >=0;
}

function ShowCalendar(e, server, vRoot) 
{
	var posx = 0;
	var posy = 0;
	
	if(!e) var e = window.event || window.Event;
	 
    if (e.clientX || e.clientY) 	
	{
		posx = e.clientX;
		posy = e.clientY;
	}
	
	// posx and posy contain the mouse position relative to the document
	calendar_window = window.open(server + vRoot + '/common/datepicker.aspx?ctrlId=btnCal','calendar_window','width=325,height=170,status=no,toolbar=no,titlebar=no,left=' + (posx+125) + ',top=' + posy);
	calendar_window.focus();
}