//Library

function confirmLink(url,msg)
{
	if (confirm(msg)) 
		window.location.href=url;
	else
		return false;
}
function Get(id){
	return document.getElementById(id);
}
function getQSVal(key) 
{
	qs = window.location.search.substring(1);
	aQs = qs.split("&");
	for (i=0;i<aQs.length;i++) 
	{
		aKv = aQs[i].split("=");
		if (aKv[0] == key) return aKv[1];
	}
}
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


function setCookie( name, value, expires, path, domain, secure )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );	
	
	if (parseInt(expires) == 0) expires = 60;
	expires = (expires * 1000 * 60 * 60 * 24);
	
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++)
		if (this[i] === value)return true;
	return false;
};
function isEmail(argvalue) {
  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("@") == -1)
    return false;
  else if (argvalue.indexOf("@") == 0)
    return false;
  else if (argvalue.indexOf("@") == (argvalue.length-1))
    return false;

  // arrayString = argvalue.split("@"); (works only in netscape3 and above.)
  var retSize = customSplit(argvalue, "@", "arrayString");

  if (arrayString[1].indexOf(".") == -1)
    return false;
  else if (arrayString[1].indexOf(".") == 0)
    return false;
  else if (arrayString[1].charAt(arrayString[1].length-1) == ".") {
    return false;
  }
  return true;
}
function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;	
	for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
			{
			IsNumber = false;
			}
		}
	return IsNumber;
}
function valRadio(btn) {
var cnt = -1;
for (var i=0; i < btn.length; i++) {
   if (btn[i].checked) {cnt = i; i = btn.length;}
   }
if (cnt > -1) return btn[cnt].value;
else return null;
}
function valRadChk(obj) {
var cnt = -1;
for (var i=0; i < obj.length; i++) {
   if (obj[i].checked) {cnt = cnt + 1}
   }
if (cnt > -1) return true;
else return false;
}
function getFormVal(frmElmtId){
	return $('#'+ frmElmtId).val();
}
function doAjaxUpdate(url, destObjId, showLoading, loadClass, loadContainerId) {	
	if (showLoading) {
		var loadObjId = (loadContainerId == null) ? destObjId : loadContainerId;
		$('#'+loadObjId).prepend('<img class="'+ loadClass +'" src="/_images/ajax-loader.gif" />');
	}
	$('#'+destObjId).load(url,function(){
		if (loadObjId == loadContainerId) $('#'+loadObjId).html('');
	});
	
}
//Search
function Search(container)
{	
	this.init = function()
	{
		$('#chkAdv').livequery('click', this.toggleAdv);
		$('#ddlCo').livequery('change', this.toggleAdv);		

		var co = getQSVal('co');
		if (co=='') co = getCookie('searchCo');
		if (co) $('#ddlCo option[value=\''+ co +'\']').attr('selected', 'selected');
		
		var adv = getQSVal('adv');
		if (adv == 'on') setCookie("advSearch",'1',60,'/');
		if (getCookie('advSearch')=='1') {
			$('#chkAdv').attr('checked',true);
			this.toggleAdv();
		}
		var co = $('#ddlCo').val();
		doAjaxUpdate('/search/aGetRegoPrefix/?co=' + co, 'txtRegoPrefix');
	};			
	this.toggleAdv = function()
	{	
		var co = $('#ddlCo').val();
		setCookie("searchCo",co,60,'/');		
		if ($('#chkAdv').attr('checked')){
			doAjaxUpdate('/search/aGetAdv/?co=' + co +'&'+ window.location.search.substring(7), container, true, 'imgLoad', 'srchLoading');
			setCookie("advSearch",1,60,"/");
		}else{
			doAjaxUpdate('/search/aGetBasic/?co=' + co +'&'+ window.location.search.substring(7), container, true, 'imgLoad', 'srchLoading');
			setCookie("advSearch",0,60,'/');
		}		
	};
}



