// JavaScript Document
//------------------------------------------------------------------
// The following function will generate a timestamp number
//------------------------------------------------------------------
function generateTimestamp() {
	var dt = new Date().getTime();
	var tt = 't=' + dt;  //we concatenate minutes with seconds

	return tt;
}

var innerContent = '';

//------------------------------------------------------------------
//------------------------------------------------------------------
//-------------------------------------------------------------------------
// Due to some problems with FireFox we better create a new object
// every time we want to use it.
//-------------------------------------------------------------------------
function createRequestObject(){
	var xmlhttp = false;
	try
	{
		// Object creation for navigators different than IE
		xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		// o bien
		try
		{
			// Object creation for IE
			xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
		}
		catch (E)
		{
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	{
		xmlhttp = new XMLHttpRequest ();
	}
	return xmlhttp;
}


function registerClick(clientID) {

	var tt = '';
	var url = '';

	//Put the form data into a variable
	//var theQuery = document.getElementById('query').value;

	tt = generateTimestamp();
	url = 'lib/register_link_click.php?clientID=' + clientID + tt;

	var xmlhttp = createRequestObject();
	//Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
	xmlhttp.open('GET', url, true);
	//Check that the PHP script has finished sending us the result

	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {  // && xmlhttp.status == 200
			//Replace the content of the "result" DIV with the result returned by the PHP script
			//if (xmlhttp.responseText == '<html>0</html>')
			//document.getElementById('test').innerHTML = xmlhttp.responseText + ' ';
		} else {
			//If the PHP script fails to send a response, or sends back an error, display a
			//simple user-friendly notification
			//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			// The following line if enabled, will cause a jumping effect of the screen.
			// That's why it is commented out.
			//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			//eval("document.getElementById('" + divID + "').innerHTML = 'Searching'");
			//eval("document.getElementById('" + divID + "').innerHTML = 'Error: preSearch Failed!'");
		}
	};
	xmlhttp.send(null);

}

//------------------------------------------------------------------
//------------------------------------------------------------------

function showSendLink() {
	
	var code = '';
	
	code = '<table width="560" border="0" align="center" cellspacing="0" cellpadding="0">';
	code = code + '<tr><td>';
	
	code = code + '<table width="250" border="0" align="center" cellspacing="0" cellpadding="0">';
	code = code + '<tr>';
	code = code + '<td width="60" nowrap class="sendTab">Send link </td>';
	code = code + '<td width="85" class="sendTabBlank">&nbsp;</td>';
	code = code + '<td width="85" class="sendTabBlank">&nbsp;</td>';
	code = code + '<td width="20" class="sendTabBlank"><div align="right"><img src="images/stock-close.png" width="16" height="16" onClick="closeSendLink()" /></div></td>';
	code = code + '</tr>';
	code = code + '<tr>';
	code = code + '<td colspan="4" class="sendOuterFrame"><table width="250" border="0" cellspacing="2" cellpadding="0">';
	code = code + '<tr>';
	code = code + '<td class="sendLabels">your friend\'s email:<br>';
	code = code + '<input name="toEmail" type="text" class="sendInputFields" id="toEmail" size="25" maxlength="50"><img src="images/blue-question-mark.gif" alt="This email will not be stored by us anywhere in any form." title="This email will not be stored by us anywhere in any form." width="16" height="16"></td>';
	code = code + '</tr>';
	code = code + '<tr>';
	code = code + '<td class="sendLabels">your email:<br>';
	code = code + '<input name="fromEmail" type="text" class="sendInputFields" id="fromEmail" size="25" maxlength="50"><img src="images/blue-question-mark.gif" alt="This email will not be stored by us anywhere in any form." title="This email will not be stored by us anywhere in any form." width="16" height="16"></td>';
	code = code + '</tr>';
	code = code + '<tr>';
	code = code + '<td class="sendLabels">short message: (optional)<br>';
	code = code + '<textarea name="message" cols="25" rows="2" class="sendInputMultiLine" id="message"></textarea></td>';
	code = code + '</tr>';
	code = code + '<tr>';
	code = code + '<td><div align="center">';
	code = code + '<input name="Submit2" type="button" class="sendButton" value="Send" onClick="sendLink();">';
	code = code + '</div></td>';
	code = code + '</tr>';
	code = code + '</table></td>';
	code = code + '</tr>';
	code = code + '</table>';

	code = code + '</td></tr>';
	code = code + '</table>';

	innerContent = document.getElementById('searchResult').innerHTML;
	document.getElementById('searchResult').innerHTML = code;
	document.dataSending.toEmail.focus();
	
	return;
}

//------------------------------------------------------------------
//------------------------------------------------------------------
function closeSendLink()
{
	document.getElementById('searchResult').innerHTML = innerContent;
}
//------------------------------------------------------------------
//------------------------------------------------------------------

//==========================================================================================
// This function will send an Ajax request to send an email with a given link.
//==========================================================================================
function sendLink() {
	var frm = document.dataSending;
	var frm2 = document.formSearch;
	
	var toAddress = '';
	var fromAddress = '';
	var message = '';
	var pageLink = '';
	var cat1 = '';
	var cat2 = '';
	var cat3 = '';
	
	toAddress = frm.toEmail.value;
	fromAddress = frm.fromEmail.value;
	message = frm.message.value;
	
	if (toAddress == '' || fromAddress == '')
	{
		alert('Please enter both email addresses.');
		return;
	}
	
	pageLink = frm2.scriptName.value;
	cat1 = frm2.cat1.value;
	cat2 = frm2.cat2.value;
	cat3 = frm2.cat3.value;
	
	var tt = '';
	var url = '';
	var str = "";
	//Put the form data into a variable
	//var theQuery = document.getElementById('query').value;

	
	url = 'lib/send_to_friend.php';
	str = "&link=" + pageLink + "&cat1=" + cat1 + "&cat2=" + cat2 + "&cat3=" + cat3 + "&to=" + toAddress + "&from=" + fromAddress + "&message=" + message;
	//alert(str);
	//return;
	var xmlhttp = createRequestObject();
	//Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
	xmlhttp.open('POST', url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
	xmlhttp.send(str);
	
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {  // && xmlhttp.status == 200
			//Replace the content of the "result" DIV with the result returned by the PHP script
			//if (xmlhttp.responseText == '<html>0</html>')
			document.getElementById('searchResult').innerHTML = innerContent;
			//document.getElementById('sendToFriend').innerHTML = xmlhttp.responseText;
		} else {
			//If the PHP script fails to send a response, or sends back an error, display a
			//simple user-friendly notification
			//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			// The following line if enabled, will cause a jumping effect of the screen.
			// That's why it is commented out.
			//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			//eval("document.getElementById('" + divID + "').innerHTML = 'Searching'");
			//eval("document.getElementById('" + divID + "').innerHTML = 'Error: preSearch Failed!'");
		}
	};
	xmlhttp.send(null);

}

//==========================================================================================
// A function will use an Ajax to bring a page that will give a layout to choose search
// options by category.
//==========================================================================================
function showSearch(searchType, divID) 
{
	var frm = document.formSearch;
	
	frm.searchType.value = searchType;

	switch (searchType)
	{
		case ('byCategory'):
			url = 'inc_topsearch_category.php';
			break;
		case ('byKeyword'):
			url = 'inc_topsearch_keyword.php';
			break;
	}
	
	var xmlhttp = createRequestObject();
	//Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
	xmlhttp.open('GET', url, true);
	//Check that the PHP script has finished sending us the result

	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {  // && xmlhttp.status == 200
			//Replace the content of the "result" DIV with the result returned by the PHP script
			//if (xmlhttp.responseText == '<html>0</html>')
			document.getElementById(divID).innerHTML = xmlhttp.responseText + ' ';
			switch (searchType)
			{
				case ('byCategory'):
					document.formSearch.searchList.focus();
					break;
				case ('byKeyword'):
					document.formSearch.searchText.focus();
					break;
			}
		} else {
			//If the PHP script fails to send a response, or sends back an error, display a
			//simple user-friendly notification
			//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			// The following line if enabled, will cause a jumping effect of the screen.
			// That's why it is commented out.
			//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			//eval("document.getElementById('" + divID + "').innerHTML = 'Searching'");
			//eval("document.getElementById('" + divID + "').innerHTML = 'Error: preSearch Failed!'");
		}
	};
	xmlhttp.send(null);

	frm.searchText.focus();
}
//==========================================================================================
// This function will use an Ajax to display a page that will give the search results.
//==========================================================================================
function runMainSearch(divID)
{
	// Read from a hidden field a type of search that should be performed then 
	// execute it during submit event (when user precess Enter button).
	var frm = document.formSearch;
	var whatSearch = frm.searchType.value;
	
	runSearch(whatSearch, divID);
	
	return false;
}

function runSearch(searchType, divID) 
{
	var frm = document.formSearch;
	var cat3 = 0;
	var searchText = '';
	url = 'inc_topsearch_result.php?type=' + searchType;
	
	switch (searchType)
	{
		case ('byCategory'):
			cat3 = frm.searchList.value;
			locationID = frm.location.value;
			url = url + '&cat3=' + cat3 + '&locationID=' + locationID;
			break;
		case ('byKeyword'):
			searchText = frm.searchText.value;
			locationID = frm.location.value;
			url = url + '&searchText=' + searchText + '&locationID=' + locationID;
			break;
		case ('byLocation'):
			locationID = frm.location.value;
			if (locationID == 0)
			{
				alert('Location is not selected.');
				return;
			}
			url = url + '&locationID=' + locationID;
			break;
	}
	
	var xmlhttp = createRequestObject();
	//Open the URL above "asynchronously" (that's what the "true" is for) using the GET method
	xmlhttp.open('GET', url, true);
	//Check that the PHP script has finished sending us the result

	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {  // && xmlhttp.status == 200
			//Replace the content of the "result" DIV with the result returned by the PHP script
			//if (xmlhttp.responseText == '<html>0</html>')
			if (xmlhttp.responseText != '')
			{
				document.getElementById(divID).innerHTML = ' ';
				document.getElementById(divID).innerHTML = xmlhttp.responseText + ' ';
			}
		} else {
			//If the PHP script fails to send a response, or sends back an error, display a
			//simple user-friendly notification
			//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			// The following line if enabled, will cause a jumping effect of the screen.
			// That's why it is commented out.
			//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
			//eval("document.getElementById('" + divID + "').innerHTML = 'Searching'");
			//eval("document.getElementById('" + divID + "').innerHTML = 'Error: preSearch Failed!'");
		}
	};
	xmlhttp.send(null);
	
}
//==========================================================================================
//==========================================================================================
function replaceCode(string,text,by) {
	// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replaceCode(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}
//==========================================================================================
//==========================================================================================
// This function will open up a new window with a google map displayed in it.
// Parameters are:
//		windowName - unique identifier of the window so we can have multiple windows opened
//		address - an address that we want to dispaly
//
function showMap(mypage, windowName, address, company, w, h, scroll)
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	mypage = mypage + '?adrs=' + address + '&w=' + w + '&h=' + h + '&company=' + company;
	win = window.open(mypage, windowName, winprops)
}
//==========================================================================================
//==========================================================================================
