﻿/* Validate Text Fields on Homepage and Modify your Search */
function ValidateEntryFields(boardErrorMessage, processorErrorMessage)
{
//	var input = document.getElementsByTagName('input');

	var textBoxes = $(":text");
	
	if (textBoxes != null && textBoxes.length > 0)
	{
		var errorMessage = "";
	
		for (var ctr = 0; ctr < textBoxes.length; ctr++)
		{
			var tb = textBoxes[ctr];

			if (!$(tb).hasClass("search-motherboard") && !$(tb).hasClass("search-processor"))
			{
				continue;
			}
			
			if (tb.value != null && jQuery.trim(tb.value) != "")
			{
				/// Need to remove disallowed characters in {', ", <, >, *}
				tb.value = tb.value.replace(/[']/g, "").replace(/["]/g, "").replace(/</g, "").replace(/>/g, "").replace(/\*/g, "");
			}

			var text = jQuery.trim(tb.value);
			var isMotherboard = !$(tb).hasClass("search-motherboard");
			var displayType = "none";
			
			if (text == null || text == "")
			{
				displayType = "inline";
				///
				/// Searching for boards ? Then we're entering a processor number (and vice versa). Display
				/// the appropriate message.
				var message = isMotherboard ? processorErrorMessage : boardErrorMessage;
				errorMessage += (errorMessage == "" ? "" : "\n") + message;
				tb.focus(); /// there might be more than one, but we'll just ignore that for now.
			}
			
			try
			{
				///
				/// HACK: Should pass in as a parameter to this JavaScript function
				/// where to select the "label". =(
				///
				$(tb).parent().parent("tr").children("td > span.ErrorMessageStyle").contents().eq(0).css("display", displayType);
			}
			catch (e)
			{
				$(tb).parent().find(".ErrorMessageStyle").css("display", displayType);
			}

		}
		
		if (errorMessage != "")
		{
			alert(errorMessage);
			
			// Based on Browser Type, we need to cancel the Submit event
			CancelSubmitEvent();
		}
	}
}
	
// Cancels the Submit Button event based on Browser Type
function CancelSubmitEvent()
{
	// Based on Browser Type, we need to cancel the Submit event
	if(navigator.userAgent.indexOf("Firefox") != -1)
	{
		SetFirefoxHiddenField();
		return false;
	}
	else
		event.returnValue = false; // Works fine on IE, Opera and Safari
}

/* Display Div when user clicks "Where can i find" link on homepage (for board) */
function showBoardInfoDiv(controlID)
{
	// Close Processor Info Div 
	var divProcessorID = document.getElementById(ProcessorInfodivID);
	divProcessorID.style.visibility = "hidden";
	divProcessorID.style.display = "none";
	
	if(controlID != null)
	{
		var linkID = document.getElementById(controlID);
		var divBoardID = document.getElementById(BoardInfodivID);
			
		var XCordinate = findPosX(linkID);
		var YCordinate = findPosY(linkID);
		
		divBoardID.style.visibility = "visible";
		divBoardID.style.position = "absolute";
		divBoardID.style.display = "block";
		divBoardID.style.left = XCordinate - 30 + "px";
		divBoardID.style.top = YCordinate + 15 + "px"; 
	}
	
	return false;
}

/* Display Div when user clicks "Where can i find" link on homepage (for processor) */
function showProcessorInfoDiv(controlID)
{
	// Close Board Info Div 
	var divBoardID = document.getElementById(BoardInfodivID);
	divBoardID.style.visibility = "hidden";
	divBoardID.style.display = "none";
	
	if(controlID != null)
	{
		var linkID = document.getElementById(controlID);
		var divProcessorID = document.getElementById(ProcessorInfodivID);
		
		var XCordinate = findPosX(linkID);
		var YCordinate = findPosY(linkID);
		
		divProcessorID.style.visibility = "visible";
		divProcessorID.style.position = "absolute";
		divProcessorID.style.display = "block";
		divProcessorID.style.left = XCordinate - 30 + "px";
		divProcessorID.style.top = YCordinate + 15 + "px"; 
	}
	
	return false;
}

/* Display DIV for Partially Compatible products based on Min-Bios Check on Compatibility Matrix Table */
function ShowDivOnGridCellClick(boardName, boardAAName, processorNumber, specName, 
											biosComments, rowBackColor, gridCell)
{
	var compareString = null;

	// Get Current CellID object
	var cellID = document.getElementById(gridCell);

	// Check whether there is a DIV already created with the given cellID 
	var divID = document.getElementById("Div" + gridCell);
	if(divID != null)
		compareString = divID.id.substring(3);		// DIV ID's format: DIV+CellID -- here, we need CellID

	// First make Invisible all the other existing DIV(s)
	var visibleCellID = document.getElementById(GridViewHiddenFieldIDCellID);
	var rowBackColorID = document.getElementById(CellRowBackColorHiddenFieldID);

	if(typeof(visibleCellID) != "undefined")
	{
		if(visibleCellID.value != "0")
		{
			if(compareString != null)
			{
				// Check whether the DivID is NOT same as Current CellID's DivID (check to see whether user clicked same cell)
				if(compareString != visibleCellID.value)
				{
					// Hide the existing Cell Div
					HideMe(visibleCellID.value, rowBackColorID.value);
				}
				else
				{
					// Make it invisible only if it's display = "inline"
					if(divID.style.display == "inline")
						HideMe(visibleCellID.value, rowBackColorID.value);
					else
					{
						divID.style.visibility = "visible";
						divID.style.display = "inline"; 
			
						// Set the Current Grid Table Cell color
						cellID.style.backgroundColor='#FBFFA2';
					}
				}
			}
			else
				HideMe(visibleCellID.value, rowBackColorID.value);
		}
	}

	// Calculate Grid Cell co-ordinates
	var xCordinate = findPosX(cellID);
	var yCordinate = findPosY(cellID);

	// If Div is NULL, create a new DIV Element
	if(divID == null)
	{
		this.div = document.createElement("div");
		document.body.appendChild(this.div);

		// Div settings
		this.div.id = "Div" + gridCell;
		this.div.style.width = 190 + "px";
		this.div.style.position = "absolute";
		this.div.style.visibility = "visible";
		this.div.style.display = "inline"; 
		this.div.style.border = "1px solid black";
		this.div.style.backgroundColor = '#fefefe';				
		this.div.style.top = yCordinate + "px";
		this.div.style.left = xCordinate + 50 + "px";
		this.div.style.height = 100 + "px"
		this.div.style.fontFamily = "verdana";
		this.div.style.fontSize = 10 + "px";

		// Build the Text to be shown in DIV
		var displayText = "Board Name: " + "<br>";
			displayText += "<b>" + boardName + " Revision " + boardAAName + "</b>" + "<br>" + "<br>";
			
			displayText += "Processor: " + "<br>";
			displayText += "<b>" + processorNumber + " Spec #" + specName + "</b>" + "<br>" + "<br>";
			
			displayText += "Minimum BIOS Version: " + "<br>";
			displayText += "<b>" + biosComments + "</b>" + "<br>";

		this.div.innerHTML = displayText;	
		
		// Set the Current Grid Table Cell color
		cellID.style.backgroundColor='#FBFFA2';
	}
	else
	{	
		// Show it only if current CellID is not same as DivID (same IDs are handled in the beginning of the logic)
		if(compareString != visibleCellID.value)
		{
			divID.style.visibility = "visible";
			divID.style.display = "inline"; 
			
			// Set the Current Grid Table Cell color
			cellID.style.backgroundColor='#FBFFA2';
		}
	}
	
	// Set Hidden fields with new CellID and RowBack Color
	visibleCellID.value = gridCell;
	rowBackColorID.value = rowBackColor;
}

// Used to Hide the DIV which is shown on Compatibility Matrix table
function HideMe(gridCell, originalRowBackColor)
{
	// Get TableCell ID
	var cellID = document.getElementById(gridCell);
	
	// If NOT null, restore to its original rowback color
	if(cellID != null)
		cellID.style.backgroundColor="#" + originalRowBackColor;
		
	// Get DIV ID
	var divID = document.getElementById("Div" + gridCell);
	
	if(divID != null)
	{
		divID.style.display = "none";
	}
}

// Hidden Field is set to "1" when user clicks radio buttons in Modify your search panel
function SetHiddenFeildValue()
{
	var hiddenFieldID = document.getElementById(hfRadioSelectionID);
	hiddenFieldID.value = "1";
}

// Calculate X-Cordinate
function findPosX(obj)
{
   var curleft = 0;
   if(obj.offsetParent)
       while(1) 
       {
         curleft += obj.offsetLeft;
         if(!obj.offsetParent)
           break;
         obj = obj.offsetParent;
       }
   else if(obj.x)
       curleft += obj.x;
   return curleft;
}

// Calculate Y-Cordinate
function findPosY(obj)
{
   var curtop = 0;
   if(obj.offsetParent)
       while(1)
       {
         curtop += obj.offsetTop;
         if(!obj.offsetParent)
           break;
         obj = obj.offsetParent;
       }
   else if(obj.y)
       curtop += obj.y;
   return curtop;
}

// Used to handle UltraWebGrid Copy & Paste Events
function UltraWebGrid1_KeyDownHandler(gridName, cellId, key)
{
	var ctrlPressed = false;

	if (key == 17)
		ctrlPressed = true;
	else if (ctrlPressed && key == 67)
	{
		var rowString = "";
		var isFirstRow = true;
		var grid = igtbl_getGridById(gridName);

		for (var rowId in grid.SelectedRows)
		{
			var row = igtbl_getRowById(rowId);

			if (!isFirstRow)
				rowString += '\n';

			for (var i=0; i < row.cells.length; i++)
			{
				if (i != 0)
					rowString += '\t';

				var cell = row.getCell(i);
				rowString += cell.getValue();
			}

		if (isFirstRow)
			isFirstRow = false;
		}

		window.clipboardData.setData('Text',rowString);
	}
}

function UltraWebGrid1_KeyUpHandler(gridName, cellId, key)
{
	if (key == 17)
		ctrlPressed = false;
}


function ShowWindow(url, windowID, toolbar, addressbar, resizable, width, height)
{
    var size = '';

    if(typeof(windowID) == 'undefined')
        windowID = 'UnKnown';

    
    if(typeof(toolbar) == 'undefined')
        toolbar = 1;

    var menubar = toolbar;
  
    if(typeof(addressbar) == 'undefined')
    {
        addressbar = 1;
    }
    
    if(typeof(resizable) == 'undefined')
    {
        resizable = 1;
    }
    
    var scrollbars = resizable;
    

    if(typeof(width) == 'undefined' || typeof(height) == 'undefined')
    {
        width = 750;
        height = 550;
    }
    
    size = 'width=' + width + ',height=' + height;
         
    var attributes = 'scrollbars=' + scrollbars + ',resizable=' + resizable + ',menubar=' + menubar + ',toolbar=' + toolbar + ',location=' + addressbar + ',' + size;

    var newWindow = window.open(url, windowID, attributes); 
    newWindow.focus();
}

function AutoCompleteSelected( source, eventArgs ) {
	///
	/// There is a key/value pair in eventArgs - shove the value in the input element pointed to by the source ACE.
	///
	$get(source.get_element().id).value = eventArgs.get_value();
}
