function GoToPageHref(pageName)
{
    location.href = vRoot + "/" + pageName;
}

function GoToPageReplace(pageName)
{
    location.replace (vRoot + "/" + pageName);
}

/****************************
 * 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();
}

/* 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(" " + paramsToAdd + "&LegitCheckError=-100");
				}
				// Allow execution to continue 
			}
			else
			{
				if(pageName != "Offers")
				{
		            GoToPageHref(" " + paramsToAdd + "&LegitCheckError=-100");
				}
				// Allow execution to continue 
			}
		}
		catch(ex2)
		{
			if(pageName != "Offers")
			{
		        GoToPageHref(" " + 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(" " + paramsToAdd);
		}
		// Allow execution to continue
	}	
	else if((result >= 1 && result <= 13) || result == 90 || result == 99)
	{
		GoToPageHref(" " + paramsToAdd + "&LegitCheckError=" + result);
	}
	else 
	{
		if(pageName != "Offers")
		{
		    GoToPageHref(" " + 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(" " + paramsToAdd + "&LegitCheckError=-100");
				}
				// Allow execution to continue 
			}
			else
			{
				if(pageName != "Offers")
				{
		            GoToPageHref(" " + paramsToAdd + "&LegitCheckError=-100");
				}
				// Allow execution to continue 
			}
		}
		catch(ex2)
		{
			if(pageName != "Offers")
			{
		        GoToPageHref(" " + 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("" + paramsToAdd);
		}
		// Allow execution to continue
	}	
	else if((result >= 1 && result <= 13) || result == 90 || result == 99)
	{
		GoToPageHref("" + paramsToAdd + "&LegitCheckError=" + result);
	}
	else 
	{
		if(pageName != "Offers")
		{
		    GoToPageHref("" + 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 + "http://www.guiabcn.cat/imatges/banesppeq.gif";		
	}
	else
	{
		changeThisButton.alt = "Expand";
		changeThisButton.src =  vRoot + "http://www.guiabcn.cat/imatges/bancatpeq.gif";	
	}
}

// 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 = "http://www.guiabcn.cat/imatges/banesppeq.gif";		
	}
	else
	{
		changeThisButton.src = "http://www.guiabcn.cat/imatges/banengpeq.gif";		
	}
}

// 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 = "http://www.guiabcn.cat/imatges/bancatpeq.gif";		
	}
	else
	{
		changeThisButton.src = "http://www.guiabcn.cat/imatges/banesppeq.gif";	
	}
}

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 = " "+InString;				
//	}
//	else
//	{
//		var sresult = "-100";
//		try
//		{
//			sresult = OGACheck.LegitCheck();
//			var result = Number(sresult);
//			
//			switch(sresult)
//			{
//				case "100":
//					document.location.href = " "+InString;
//					break;
//				default:
//					document.location.href = " "+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);
    }        
}