// ###############################################################################
//
//  COMMON FUNCTIONS
//
//    replaceSubstring(inputString, badString, goodString, caseSensitive)
//		- Replaces one string with another, within a string
//
//    leftString(fullString, subString)
//		- returns a string matching that to the left of the substring in the full string
//
//    rightString(fullString, subString)
//		- returns a string matching that to the right of the substring in the full string
//  
//    LeftBack(fullString, startString)
//  
//    trim(inputstringTrim)
//
// ###############################################################################
function setCookieDD(){
var dropdown = document.getElementById('countries')
var myindex  = dropdown.selectedIndex
var SelValue = dropdown.options[myindex].value
var expiration = new Date( 2099, 11, 31, 12, 0, 0 );
setCookie('skipintro', SelValue , expiration ,  '/' );
var expiration = new Date( 1954, 11, 31, 12, 0, 0 );
setCookie('SampleRequestCookie', '', expiration, "/" );
location.reload(true);
}
function cookieCountrySet(){
	s = getCookie('skipintro');
	if(s){
	}
	else{
	//var expiration = new Date( 2099, 11, 31, 12, 0, 0 );
	//setCookie('skipintro', 'United Kingdom' , expiration ,  '/' );
	//location.replace("http://"+domain+path+"intro")
	}
}
addLoadEvent(function (){
//cookieCountrySet();
 });
function isObject( r ) {
return( r && (( 'object' == typeof r ) ||
( 'function' == typeof r )));
}
function replaceSubstring(inputString, badString, goodString, caseSensitive)
{
	var fixedReplace = "";
	var UI = inputString;
	var UB = badString;
	if ((caseSensitive != 1) && (caseSensitive != true))
	{
		UI = inputString.toUpperCase();
		UB = badString.toUpperCase();
	}
	var badEnd = -1;
	var badLoc = UI.indexOf(UB);
	if (badLoc != -1)
	{
		for (x = 1; (badLoc != -1); x++)
		{
			fixedReplace = fixedReplace + inputString.substring((badEnd + 1), badLoc) + goodString;
			badEnd = badLoc + UB.length -1;
			badLoc = UI.indexOf(UB, (badLoc + 1));
		}
		fixedReplace = fixedReplace + inputString.substring((badEnd + 1), inputString.length);
	}
	else
	{
		fixedReplace = inputString;
	}
	return fixedReplace;
}
// ##############################################################################################
function leftString(fullString, subString)
{
	return (fullString.substring (0, fullString.indexOf(subString)))
}
// ##############################################################################################
function rightString(fullString, subString)
{
	// fullString += " ";
	// subString += " ";
	if (subString != "" && fullString != "" && fullString.indexOf (subString) > -1)
	{
		return(fullString.substring(fullString.indexOf(subString) + subString.length, fullString.length))
	}
	else
	{
		return(fullString)
	}
}
// ##############################################################################################
function LeftBack(fullString, startString) {
	var storeString = fullString;
	var position = fullString.indexOf(startString);
	var tmp = position;
	while (position > -1) {
		fullString = fullString.substring(position + 1, fullString.length);
		position = fullString.indexOf(startString);
		tmp = tmp + position + 1;
	}
	return (storeString.substring(0, tmp))
}
function trim(inputstringTrim) {
	fixedTrim = "";
	lastCh = " ";
	for (x = 0; x < inputstringTrim.length; x++) {
		ch = inputstringTrim.charAt(x)
		if ((ch != " ") || (lastCh != " ")) {
			fixedTrim += ch;
		}
		lastCh = ch;
	}
	if (fixedTrim.charAt(fixedTrim.length - 1) == " ") {
		fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1);
	}
	return fixedTrim
}
// ###############################################################################
//
//	GetFieldValue(fieldobject)	Returns the field value(s) based on a field object that is passed to it.
// 
//	Multi-Checkbox field content is separated by '##'
// ###############################################################################
function GetFieldValue (theField) {
	theValue = ""; 
	sep = ""; 
	hits = 0; 
	
	//Assumes that a field with a type==hidden is an ordinary text field
	//If the field is manually set to hidden this will overwrite the real type
	//Better to use style="display:none" to hide fields
	if (theField.type == "text" || theField.type == "textarea" || theField.type=="hidden") {
		
		//simple text field
		return (theField.value) 
	}
	
	if (theField.type=="checkbox" || theField.type=="radio") {
	
		//one element checkbox or radio
		if (theField.checked) {
			return(theField.value)
		}
		else {
			return("")
		}
	}
	//radio, checkbox or select list field
	if (theField(0).type == "checkbox" || theField(0).type == "radio") {
		//if we're here, we are validating a radio button or a nn multi-element checkbox 
		for ( i = 0; i <  theField.length; i++ ) 
			{ 
				if ( theField[i].checked ) 
				{
 				theValue += theField[i].value + "#Y##" ; 					
				} 	
				else
				{	
				theValue += theField[i].value + "N##"; 
				} 
			}
		 
		return ( theValue );
	}
	
	if ( theField.type == "select-multiple" || theField.type == "select-one") {
					
		for ( i = 0; i < theField.options.length; i++ ) 
		{
			if ( theField.options[i].selected ){
				hits++; 
				if ( hits > 1 ) 
				{
					sep = "; ";
				} 
				 theValue += sep + theField.options[i].text 
			}
		} 
	return ( theValue );
			
	}
	
	else 
	{
		//oh dear, I don't know what field type we're dealing with
		return ("error")
		
	}
}
function popUpWin(url,name,param) {
        var newWindow = window.open(url,name,param);
}
// Check strV for the presence of any of the characters listed in chars
function charChk(strV,chars) {
  var res=false;
  for ( i=0; i<chars.length; i++) {
    for ( c=0; c<strV.length; c++) {
      if(strV.charAt(c)==chars.charAt(i)) { res=true; }
   }
  }
  return (res);
}
// Count the number of char characters in strVal
function countChar(s,c) {
  var charCount=0;
  for ( i=0; i<s.length; i++) {
    if (s.charAt(i)==c) {charCount++};
  }
  return charCount;
}
// Return how many checkboxes in field fd are set
function checkboxCount(fd) {
	counter=0
	for ( i=0; i<fd.length; i++) {
		if(fd[i].checked) { counter++ }
	}
	return counter;
}
// Check form fm for presence of field fd, returns true/false
function findField(fm,fd) {
  var found=false;
  for ( i=0; i<fm.elements.length; i++) {
    if (fm.elements[i].name==fd) {return true};
  }
  return false;
}
function getCookie(name){
  var cname = name + "=";               
  var dc = document.cookie;             
  if (dc.length > 0) {              
    begin = dc.indexOf(cname);       
    if (begin != -1) {           
      begin += cname.length;       
      end = dc.indexOf(";", begin);
      if (end == -1) end = dc.length;
        return dc.substring(begin, end);
    } 
  }
  return null;
}
function setCookie(name, value, expires, path, domain, secure) {
  document.cookie = name + "=" + value + 
  ((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
  ((path == null) ? "" : "; path=" + path) +
  ((domain == null) ? "" : "; domain=" + domain) +
  ((secure == null) ? "" : "; secure");
}
function delCookie (name,path,domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path == null) ? "" : "; path=" + path) +
    ((domain == null) ? "" : "; domain=" + domain) + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}
addLoadEvent(function (){
if(document.getElementById("searchflyout"))
{
	var strcook = getCookie('flyout');
	if(strcook == null)
	{
	return false;
	}
	if (strcook == 'visible'){
	document.getElementById("searchflyout").style.visibility = "visible"
	return false;
	}
	else{
	document.getElementById("searchflyout").style.visibility = "hidden"
	return false;
	}
	}
}
);
function setVisible(obj )
{
	whichclicked = obj
	obj = document.getElementById(obj);
	
	obj.style.visibility  = ( obj.style.visibility  == 'visible') ? 'hidden' : 'visible';
	var now = new Date();
	now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
	if(whichclicked  == "searchflyout"){
	setCookie("flyout", obj.style.visibility , now, '/');
	}
	
}
