// array to store the original TR class name in updateRowStyle()
var originalTrClass = new Array;
/*
+--------------------------------------------------------------------------+
| Fn name:     updateRowStyle                                              |
+--------------------------------------------------------------------------+
| Purpose:     performs table row highlighting on mouseover, mouseout, and |
|              onclick events and also checks checkboxes if passed as a    |
|              parameter.                                                  |
+--------------------------------------------------------------------------+
| Inputs:      docElement tr - id of tr tag                                |
|              string myClassName - CSS classname to change tr to          |
+--------------------------------------------------------------------------+
| Returns:     nothing                                                     |
+--------------------------------------------------------------------------+
| Notes:       when using the checkbox functionality, we have to add an    |
|              onclick handler to the checkbox itself to make it work.     |
|              e.g. onclick="(this.checked == true) ? this.checked=false   |
|                                                   : this.checked=true"   |
|              it's a bit of an ugly hack, but c'est la vie.               |
+--------------------------------------------------------------------------+
*/
function updateRowStyle(tr, myClassName, checkbox)
{
   // keep track of the grey shaded cells
   if (tr.className == 'cellShading') { originalTrClass[tr.id] = tr.className; }
   
   // row has not been clicked to "active" state, so toggle class name
   if (tr.className != 'rowActive')
   {
      // if the original class name is a shaded grey cell when trying to change
      // class name to the "off row" class, restore it back to shaded grey
      if (originalTrClass[tr.id] == 'cellShading' && myClassName == 'rowOff')
         tr.className = 'cellShading';
      // set tr cell to specified class name
      else
         tr.className = myClassName;
   }
   // if row is already "active" and clicked again, then turn of the active class name
   else if (tr.className == 'rowActive' && myClassName == 'rowActive')
   {
      tr.className = 'rowOn';
   }
   
   // if a checkbox was passed and is checked, then uncheck it
   if (checkbox && checkbox.checked == true)
      checkbox.checked = false;
   // if a checkbox was passed and is NOT checked, check it
   else if (checkbox && checkbox.checked == false)
      checkbox.checked = true;
}

/*
Filename:         get_hint.js
Author:           Jenny Yuan
Date Created:     06/06/2006
Purpose:          All javascript functions used for fabu
*/

var xmlHttp;
/*
+-----------------------------------------------------------------+
| Fn name:    showClientHint , clientStateChanged                 |
+-----------------------------------------------------------------+
| Purpose:    get similiar merchant list by merchant name         |
+-----------------------------------------------------------------+
| Inputs:     merchant_name                                       |
+-----------------------------------------------------------------+
| Result:    change the content of object 'client_hint'           |
|            with the result got from get_client_hint.php         |
+-----------------------------------------------------------------+
*/

function showClientHint(str)
{ 
	if (str.length > 0)
	{ 
		str = str.replace(/&/,"%26");
		var url = "get_client_hint.php?client_name=" + str;
		xmlHttp = GetXmlHttpObject(clientStateChanged);
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	} 
	else
	{ 
		document.getElementById("client_hint").innerHTML = ""
	} 
} 

function clientStateChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		document.getElementById("client_hint").innerHTML = xmlHttp.responseText 
	} 
} 

/*
+-----------------------------------------------------------------+
| Fn name:    showBrandHint, brandStateChanged                    |
+-----------------------------------------------------------------+
| Purpose:    get similiar brand list by merchant name            |
+-----------------------------------------------------------------+
| Inputs:     brand_name                                          |
+-----------------------------------------------------------------+
| Result:    change the content of object 'brand_hint'            |
|            with the result got from get_brand_hint.php          |
+-----------------------------------------------------------------+
*/
function showBrandHint(str)
{ 
	if (str.length > 0)
	{ 
		str = str.replace(/&/,"%26")
		var url = "get_brand_hint.php?brand_name=" + str
		xmlHttp = GetXmlHttpObject(brandStateChanged);
		xmlHttp.open("GET", url , true)
		xmlHttp.send(null)
	} 
	else
	{ 
		document.getElementById("brand_hint").innerHTML = ""
	} 
} 

function brandStateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		document.getElementById("brand_hint").innerHTML=xmlHttp.responseText 
	} 
} 

/*
+-----------------------------------------------------------------+
| Fn name:    GetXmlHttpObject                                    |
+-----------------------------------------------------------------+
| Purpose:    check browser type, alert when the browser          |
|             doesn't support the method                          |
+-----------------------------------------------------------------+
| Inputs:     handler                                             |
+-----------------------------------------------------------------+
| Result:    if browser supports, return objXmlHttp               |
|            Or return nothing                                    |
+-----------------------------------------------------------------+
*/
function GetXmlHttpObject(handler)
{ 
	var objXmlHttp = null;

	if (navigator.userAgent.indexOf("Opera") >= 0)
	{
		alert("This example doesn't work in Opera") 
		return 
	}
	if (navigator.userAgent.indexOf("MSIE") >= 0)
	{ 
		var strName = "Msxml2.XMLHTTP";
		
      if (navigator.appVersion.indexOf("MSIE 5.5") >= 0)
		{
			strName = "Microsoft.XMLHTTP";
		} 
		try
		{ 
			objXmlHttp = new ActiveXObject(strName)
			objXmlHttp.onreadystatechange = handler;
			return objXmlHttp;
		} 
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled");
			return;
		} 
	} 

	if (navigator.userAgent.indexOf("Mozilla") >= 0)
	{
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload = handler;
		objXmlHttp.onerror = handler;
		return objXmlHttp;
	}
} 

/*
+-----------------------------------------------------------------+
| Fn name:    deleteBrandConfirm                                  |
+-----------------------------------------------------------------+
| Purpose:    popup confirm window for brand deleting             |
+-----------------------------------------------------------------+
| Inputs:     delete_no, brand_name                               |
+-----------------------------------------------------------------+
| Result:     Alert windows for no brand choosed, return false    |
|				  If confirm is true, return true,                    |
|				  otherwise, return false.                            |
+-----------------------------------------------------------------+
*/
function deleteBrandConfirm($delete_no, $name)
{
	if ($delete_no == 0) 
	{
		$str = "Please select a brand.";
		alert($str);
		return false;
	}
	else if ($delete_no == 1)
	{
		$str = "Do you really want to delete brand: " + $name;
		var name = confirm($str);
		return name;
	}
	else 
	{
		$str = "Do you really want to delete all the selected brand?";
		var name = confirm($str);
		return name;
	}
}

/*
+-----------------------------------------------------------------+
| Fn name:    deleteMerchantConfirm                                  |
+-----------------------------------------------------------------+
| Purpose:    popup confirm window for merchant deleting             |
+-----------------------------------------------------------------+
| Inputs:     merchant_id, merchant_name                          |
+-----------------------------------------------------------------+
| Result:     If confirm is true, return true,                    |
|				  otherwise, return false.                            |
+-----------------------------------------------------------------+
*/
function deleteMerchantConfirm($id, $name)
{
	$str = "Do you really want to delete Merchant: " + $name
	var name = confirm($str);
	return name;
}

/*
+-----------------------------------------------------------------+
| Fn name:    deleteCatConfirm                                    |
+-----------------------------------------------------------------+
| Purpose:    popup confirm window for category deleting          |
+-----------------------------------------------------------------+
| Inputs:     cat_id, cat_name                               |
+-----------------------------------------------------------------+
| Result:     If confirm is true, return true,                    |
|				  otherwise, return false.                            |
+-----------------------------------------------------------------+
*/
function deleteCatConfirm($id, $name)
{
	$str = "Do you really want to delete Category: " + $name;
	var name = confirm($str);
	return name;
}

/*
+-----------------------------------------------------------------+
| Fn name:    checkAll                                            |
+-----------------------------------------------------------------+
| Purpose:    group check/uncheck for checkboxes on brand_list    |
+-----------------------------------------------------------------+
| Inputs:                                                         |
+-----------------------------------------------------------------+
| Result:     If check_all is checked, 								   |
|				    all checkboxes set to be checked 						|
|					 and delete_no set to be 10								|
|				  If check_all is unchecked,								   |
|				    all checkboxes set to be unchecked and 			   |
|					 and delete_no set to be 0									|
+-----------------------------------------------------------------+
*/
function checkAll() 
{
	if (document.getElementById("check_all").checked == true) 
	{
		var t = parseInt(document.getElementById("delete_no").value);
		t = 10;
		document.getElementById("delete_no").value = t;
		document.getElementById("check1").checked = true;
		document.getElementById("check2").checked = true;
		document.getElementById("check3").checked = true;
		document.getElementById("check4").checked = true;
		document.getElementById("check5").checked = true;
		document.getElementById("check6").checked = true;
		document.getElementById("check7").checked = true;
		document.getElementById("check8").checked = true;
		document.getElementById("check9").checked = true;
		document.getElementById("check10").checked = true;
	}
	else 
	{
		var t = parseInt(document.getElementById("delete_no").value);
		t = 0;
		document.getElementById("delete_no").value = t;
		document.getElementById("check1").checked = false;
		document.getElementById("check2").checked = false;
		document.getElementById("check3").checked = false;
		document.getElementById("check4").checked = false;
		document.getElementById("check5").checked = false;
		document.getElementById("check6").checked = false;
		document.getElementById("check7").checked = false;
		document.getElementById("check8").checked = false;
		document.getElementById("check9").checked = false;
		document.getElementById("check10").checked = false;
	}
}

/*
+-----------------------------------------------------------------+
| Fn name:    checkOne                                            |
+-----------------------------------------------------------------+
| Purpose:     change delete_no based on the checkbox status      |
+-----------------------------------------------------------------+
| Inputs:      checked status of the checkbox                     |
+-----------------------------------------------------------------+
| Result:     If checkbox is checked,  delete_no + 1				   |
|				  If checkbox is not checked,	delete_no - 1			   |
+-----------------------------------------------------------------+
*/
function checkOne(checked) 
{
	if (checked == true) 
	{
		var t = parseInt(document.getElementById("delete_no").value);
		t = t + 1;
		document.getElementById("delete_no").value = t;
	}
	else 
	{
		var t = parseInt(document.getElementById("delete_no").value );
		t = t - 1;
		document.getElementById("delete_no").value = t;
	}
}

/*
+-----------------------------------------------------------------+
| Fn name:    sendBrandName                                       |
+-----------------------------------------------------------------+
| Purpose:    Send the brand name to brand_name					      |
+-----------------------------------------------------------------+
| Inputs:     checked status of the checkbox, brand name string   |
+-----------------------------------------------------------------+
| Result:     If checkbox is checked,  delete_no + 1				   |
|				  If checkbox is not checked,	delete_no - 1			   |
+-----------------------------------------------------------------+
*/
function sendBrandName(checked,str) 
{
	if (checked == true) {
	   document.getElementById("brand_name").value = str;
	}
}

/*
+-----------------------------------------------------------------+
| Fn name:    searchCheck                                         |
+-----------------------------------------------------------------+
| Purpose:    check the content of search box 					      |
+-----------------------------------------------------------------+
| Inputs:     str																   |
+-----------------------------------------------------------------+
| Result:     If str is empty,  alert error, return false		   |
+-----------------------------------------------------------------+
*/
function searchCheck() 
{
	$str = document.getElementById("search_con").value;
	
   if ($str == "")
	{
		alert('Please enter you search query');
		return false;
	}
}

/*
+-----------------------------------------------------------------+
| Fn name:    prevBanner	                                       |
+-----------------------------------------------------------------+
| Purpose:    display the previous B1			 					      |
+-----------------------------------------------------------------+
| Inputs:     cur_id contains current b1 id,								|
|				  str contains id array information						   |
+-----------------------------------------------------------------+
| Result:     showBanner, change value of banner1_id				   |
+-----------------------------------------------------------------+
*/
function prevBanner(cur_id, str)
{
	id_array = str;
   
	for(i = 0; i < (id_array.length - 1); i ++)
	{
		if (cur_id == id_array[i])
		{
			if (i == 0)
			{
				id = id_array[id_array.length - 2];
			}
			else
			{
				id = id_array[i-1];
			}
         
			document.getElementById("banner1_id").value = id;
			showBanner(id);
			break;
		}
	}
}

/*
+-----------------------------------------------------------------+
| Fn name:    nextBanner	                                       |
+-----------------------------------------------------------------+
| Purpose:    display the next B1 				 					      |
+-----------------------------------------------------------------+
| Inputs:     cur_id contains current b1 id,								|
|				  str contains id array information						   |
+-----------------------------------------------------------------+
| Result:     showBanner, change value of banner1_id				   |
+-----------------------------------------------------------------+
*/
function nextBanner(cur_id, str)
{
	id_array = str;
   
	for(i = 0; i < (id_array.length - 1); i ++)
	{
		if (cur_id == id_array[i])
		{
			if (i == (id_array.length - 2))
			{
				id = id_array[0];
			}
			else
			{
				id = id_array[i+1]
			}
         
			document.getElementById("banner1_id").value = id;
			showBanner(id);
			break;
		}
	}
}

function showBanner(id)
{
	if (id != null)
	{ 
		var url = "b1_cycle.php?banner1_id=" + id;
		xmlHttp = GetXmlHttpObject(bannerChanged);
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
	} 
} 

function bannerChanged() 
{ 
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
	{ 
		document.getElementById("bannerleft").innerHTML = xmlHttp.responseText;
      
      // update the alt and title contents of the image map from the img tag
      document.getElementById('b1_map_mainarea').alt = document.getElementById('b1_img').alt;
      document.getElementById('b1_map_mainarea').title = document.getElementById('b1_img').alt;
      document.getElementById('b1_map_mainarea').href = document.getElementById('b1_ahref').href;
	} 
} 