//this script is used on both PUBLIC site and TURCK CONNECT

function Trim(s){
	return s == null ? '' : s.replace(/^\s*(.*?)\s*$/, "$1");
}



function NoInput(oField, sName)
{
	if (Trim(oField.value)=='') {alert('Please enter the '+ sName.toLowerCase() +'.'); oField.focus(); return true;} else return false;
}



function FilterDigits()
{
	event.returnValue = !String.fromCharCode(event.keyCode).match(/\D/);
}
function AlertAndSelect(sMessage, oField)
{
	if (sMessage > '') alert(sMessage);
	if (oField != null) {oField.focus(); oField.select();}
	return false;
}



function GoodEmail(addresses) //email address must have some@some.some format and consist of alphanumeric characters, dashes and dots only
{
	var a = addresses.split(';');
	for (var i=0; i<a.length; i++)
		if (!Trim(a[i]).match(/^(\w|\.|-)+@(\w|\.|-)+\.(\w|\.|-)+$/)) return false;
	return true;
}
function GetCookie(sName){
	var c = document.cookie;
	var i = c.indexOf(sName + '=');
	if (i<0) return '';

	i += sName.length + 1;
	var j = c.indexOf(';',i);
	if (j==-1) j = c.length;
	return unescape(c.substring(i,j));
}
function SetCookie(sName, sValue, nExpirationDays){
	if (nExpirationDays == null)
		sExpires = '';
	else
	{
		var d = new Date();
		d.setDate(d.getDate() + nExpirationDays);
		sExpires = 'expires='+ d.toUTCString() +';';
	}
	document.cookie = sName +'='+ escape(sValue) +'; path=/;'+ sExpires;
} 



function KillBackButton(sMagic, sRetreat) //clear the cookie set by the server; if the page is reloaded from cache by using back button, the user is kicked to the retreat page
{
	if (GetCookie('BackButtonKiller') == sMagic) SetCookie('BackButtonKiller', 'none'); else location = sRetreat;
}



function UpDown() //move up/down the screen cursor between the visible input fields of the same column on the search results page
{
	if (event.keyCode!=38 && event.keyCode!=40) return; //38=UP arrow, 40=DOWN arrow
	
	with (event.srcElement.parentElement) var i = parentElement.rowIndex, j = ActualCellIndex(cellIndex);
	var r = SearchResultTable.rows;
	var d = event.keyCode==38 ? -1 : 1;
	for (i+=d; 0<i && i<r.length; i+=d)
	{
		var c = r[i].cells[j].children;
		if (c.length && c[0].tagName.toUpperCase()=='INPUT' && !c[0].disabled && c[0].style.visibility!='hidden') {c[0].focus(); break;}
	}
}



function ActualCellIndex(cellIndex) //bug fix, see http://support.microsoft.com/kb/814506
{
	var cells = SearchResultTable.rows[0].cells, rules = document.styleSheets[0].rules;
	for (var i=n=0; i<cells.length; i++)
	{
		columnNumber = cells[i].className.substr(3);
		if (rules[columnNumber].style.display != 'none') n++;
		if (n > cellIndex) return i;
	}
}



function SetSpreadsheetStyle(oTableOrRow, oSpreadsheetHeading, sPaleGray) //used throughout the site for [submittable] spreadsheet formatting
{
   if (!sPaleGray || sPaleGray=='') sPaleGray = 'gray';

   //oTableOrRow can be either the entire table or just one row (added by AppendNewRow)
   var oSpreadsheet, i, n;
   if (oTableOrRow.tagName.toLowerCase() == 'table') oSpreadsheet = oTableOrRow, oTableOrRow.width = oSpreadsheetHeading.width, i = 0,  n = oSpreadsheet.rows.length;
   else                                              oSpreadsheet = oTableOrRow.parentElement.parentElement,                    i = oTableOrRow.rowIndex,  n = i + 1;

 	for (; i<n; i++)
		for (var j=0; j<oSpreadsheet.rows[i].cells.length; j++)
			with (oSpreadsheet.rows[i].cells[j])
			{
				var c = oSpreadsheetHeading.rows[0].cells[j];
				c.style.borderTop  = '1 solid';
				c.style.borderLeft  = '1 solid';
				c.style.borderRight  = c.cellIndex == oSpreadsheetHeading.rows[0].cells.length-1 ? '1 solid' : '';

				var width = c.width;
				style.cursor = 'default';
				style.border = '1 solid';
				if (i) style.borderTopWidth  = 0; //for non-top rows, remove the top border, and for non-leftmost - the left one
            if (j) style.borderLeftWidth = 0;

				c = children.length ? children[0] : null;
				if (c && c.tagName.toLowerCase() != 'br' && c.tagName.toLowerCase() != 'table')
				{
					if (c.readOnly) c.style.cursor = 'default';
					if (c.tagName.toLowerCase() == 'a'     ) c.style.font = 'xx-small verdana', c.style.color = sPaleGray;
					if (c.tagName.toLowerCase() == 'select') c.style.font = 'xx-small verdana', c.style.width = oSpreadsheetHeading.rows[0].cells[j].clientWidth==''? '99%' : oSpreadsheetHeading.rows[0].cells[j].clientWidth-1;
					if (c.tagName.toLowerCase() == 'input' )
					{
						c.style.borderWidth = 0;
						if (c.readOnly && c.style.color=='') c.style.color = sPaleGray;
						if (c.type.toLowerCase() == 'checkbox') style.textAlign = 'center';
						if (c.type.toLowerCase() == 'text'    ) c.style.font = 'xx-small verdana',c.style.width = oSpreadsheetHeading.rows[0].cells[j].clientWidth==''? '99%' : oSpreadsheetHeading.rows[0].cells[j].clientWidth-1;
					}
				}
				else
				{
					style.font = 'xx-small verdana';
					if (style.color == '') style.color = sPaleGray;
				}
			}

   if (oTableOrRow.tagName.toLowerCase() == 'table')
		oSpreadsheetHeading.cellPadding = oSpreadsheetHeading.cellSpacing = oSpreadsheet.cellPadding = oSpreadsheet.cellSpacing = 0,
		oSpreadsheet.style.display = '';
}



function AppendNewRow(oSpreadsheet, oSpreadsheetHeading, sPaleGray) //append an empty row to the end of the spreadsheet and structure it accordingly
{
	var r = oSpreadsheet.insertRow();
	var n = r.rowIndex;
	for (var i=0; i < oSpreadsheet.rows[n-1].cells.length; i++)
	{
		var c = r.insertCell();
		c.insertAdjacentHTML('BeforeEnd', oSpreadsheet.rows[n-1].cells[i].innerHTML);
      c = c.children.length ? c.children[0] : null;
		if (c && c.tagName.toLowerCase() != 'br' && c.tagName.toLowerCase() != 'table')
		{
			if (c.type.toLowerCase() == 'checkbox') c.checked = false;
			if (c.type.toLowerCase() == 'text'    ) c.value = '';
		}
	}
	SetSpreadsheetStyle(r, oSpreadsheetHeading, sPaleGray)
	return r;
}



//Asynchronous JavaScript And XML (AJAX) technology is used for dynamic client/server interactions without submission or use of hidden frames
var oAjax;
function AjaxInstantiate(url)
{
	if (oAjax && oAjax.readyState != 0) oAjax.abort();
	oAjax = null;

	try {
		oAjax = new ActiveXObject("Msxml2.XMLHTTP"); //for IE
	}
	catch(e) { //non-IE clients
		try {
			oAjax = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(f) {
			if (typeof XMLHttpRequest != 'undefined') oAjax = new XMLHttpRequest();
		}
	}

	if (oAjax)
	{
		oAjax.onreadystatechange = function() {
			if (oAjax.readyState == 4 && oAjax.status == 200 && typeof AjaxCallBack == 'function') AjaxCallBack(oAjax.responseText);
		};
		oAjax.open('get', url);
		oAjax.send(null);
	}
}