//----------------------------[ Utility Functions ]----------------------------

//
// This line protects the entire website
//
//document.write('<script type="text/javascript" src="http://www.caragent.co.uk/javascript/protect.js"></script>');


	function show_props(obj, name)
	{
	 		 var result="";
			 var count=0;
			 for( i in obj )
			 {
			  	  result += name + "." + i + " = " + obj[i] + "\n";
				  
				  if( count++ > 20 )
				  {
				   	  alert(result);
					  count=0;
					  result="";
			      }
			 }
		  	 alert(result);
	}

	function show_frames(obj)
	{
		var result="";
		var count=0;

		for (var i=0;i<parent.frames.length;i++)
                {
			result += "Frame = " + parent.frames[i].name + "\n" ;

			if( count++ > 20 )
			{
                                          alert(result);
                                          count=0;
                                          result="";
			}
                }

		alert(result);

	}

	function statusbar_message(txt, timeout) {
		   window.status = txt;
		   setTimeout("clear_statusbar()",timeout);
	}

	function clear_statusbar() {
	   window.status="";
	}

/*	function frame_exists(what) 
	{
		for (var i=0;i<parent.frames.length;i++) 
		{
		if (parent.frames[i].name == what)
			return true;
		}
		return false;
	}
*/
	
        function frame_exists(what)
        {
                for (var i=0;i<top.frames.length;i++)
                {
	                if (top.frames[i].name == what)
	                        return true;

			if (top.frames[i].frames.length > 0 )
			{
				if( child_frame_exists(top.frames[i], what) == 1 )
					return true;
			}
                }
                return false;
        }

	function child_frame_exists(frame, what)
	{
//		alert("Checking Frame: " + frame + " for " + what );

		for (var i=0;i<frame.frames.length;i++)
                {
                        if (frame.frames[i].name == what)
                                return 1;

			//
			// If this frame has child frames then check them!
			//
                        if (frame.frames[i].frames.length > 0 )
                        {
                                if( child_frame_exists(frame.frames[i], what) == 1 )
                                        return 1;
                        }
                }

		return 0;
	}

        function variable_exists(what)
        {
//              alert( "Checking if variable [ " + what + " ] exists" );
                if ( eval("window." + what) )
                {
//                        alert("variable " + what + " exists");
                        return true;
                }
                else
                {
//                        alert("variable " + what + " doesnt exist");
                        return false;
                }
        }


     function getCurrentDate()
	 {
	  		  var dateStr;
	  		  currentTime = new Date();
			  
			  //
			  // Netscape stores the year since 1900
			  //
	  		  if( isIE() == true )
			  			  dateStr = currentTime.getDate() + "/" + (currentTime.getMonth()+1) + "/" + currentTime.getYear();
			  else
			  			  dateStr = currentTime.getDate() + "/" + (currentTime.getMonth()+1) + "/" + (currentTime.getYear()+1900);
			  
//			  alert("DAY: " + currentTime.getDate() + "\nMONTH: " + (currentTime.getMonth()+1) + "\nYEAR: " + currentTime.getYear() );
			  
			  return dateStr;  
	 }
	 
	 function getCurrentDateOffset(offset_day, offset_month, offset_year)
	 {
	  		  var dateStr = "NULL";
	  		  currentTime = new Date();
			  var year = currentTime.getYear() + offset_year;
			  var month = (currentTime.getMonth()+1) + offset_month; 
			  var day = currentTime.getDate() + offset_day;		  
			  
	  		  if( isIE() != true )
			  	  year+=1900;

			  if( year < 0 ) year = 0;
			  			  
			  if( month <= 0 )
			  { 
			  	  month += 12;
				  year--;
			  }
			  
			  if( month > 12 )
			  {
			   	  month-=12;
				  year++;
			  }
			  
		  	  switch( month )
		  	  {
				  case 1:
				  case 3:
				  case 5:
				  case 7:
				  case 8:
				  case 10:
				  case 12:
				  	   if( day <= 0 )
					   {
					   	   day+=31;
						   month --;
					   }
					   
					   if( day > 31 )
					   {
					        day-=31;
							month++;
					   }
					
					   break;
				  case 2:
				       if( day <= 0)
					   {
				  	   	   day+=28;
						   month --;
					   }
					   
					   if( day > 28 )
					   {
					       day-=28;
						   month++;
					   }
					   break;
				  case 4:
				  case 6:
				  case 9:
				  case 11:
				       if( day <= 0)
					   {
				  	   	   day+=30;
						   month --;
					   }
					   
					   if( day > 30 )
					   {
					       day-=30;
						   month++;
					   }
					   break			  	 
		      }			  
	   
			  dateStr = day + "/" + month + "/" + year;
			  return dateStr;
	 }
	 
	 function sendmail( address )
	 {
		   		   document.location = "mailto:" + address;
	 }	 
	 
	 function isIE()
	 {
//Your browser is Microsoft Internet Explorer
//Your browser version is 4.0 (compatible; MSIE 5.5; Windows NT 5.0; Supplied by blueyonder)
//Browser version number is 4
//Your browser code name is Mozilla
//Your user agent is Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0; Supplied by blueyonder)
//Your browser supports JavaScript version 1.3	 
 
	  	  //-- Putting browser info into global variables in the head of a page
	  	  //-- gives access to those variables to all scripts within the page.
		  var browserName = navigator.appName
		  var browserVersion = navigator.appVersion
		  var browserVersionNum = parseFloat(browserVersion)
		  var codeName=navigator.appCodeName
		  var userAgent=navigator.userAgent
		  
		  if (checkIt(userAgent.toLowerCase(),'msie') || checkIt(browserName.toLowerCase(),'microsoft') )
		  {
		   	 return true;		  
		  } 
		  
		  return false;
	 }  
	 
	 function checkIt(detect, string)
	 {
	  place = detect.indexOf(string) + 1;
	  
//	  alert("Searching For: [" + string + "] in [ " + detect + " ] - indexof = " + place );
	  
	  thestring = string;
	  return place;
	 }
	 



          function getRootDomainName(url)
          {
                                 var name = new String( url );
				 var protocol = "https://";

                                 name = name.toLowerCase();

                                 // 
				 // Try and find the https:// start protocol tag
				 //
                                 pos = name.indexOf(protocol);

				 //
				 // Could we find the protocol start tag?
				 //
                                 if( pos == -1 )
				 {
					//
					// Set the protocol to http
					//
					protocol = "http://";
					pos = name.indexOf(protocol);
				 }

				//
				// Dont know what the protocol is - return error
				//
				 if( pos == -1 )
					return "unknown";

                                 start_pos = pos + protocol.length;

                                 // Find the end char
                                 pos = name.indexOf("/",start_pos+1);

                                 if( pos == -1 )
                                         end_pos = name.length;
                                 else
                                 {
                                 	//
                                 	// Check to see if the url has a "~" if it does then that part of
                                 	// the domain/root node [ www.xyz.com/~spickett ] would be the root.
                                 	//
                                 	pos2 = name.indexOf("~",pos);
                                 	if( pos2 == -1 )                                 
	                                 	end_pos = pos;
	                                else
	                                {
	                                	// include the ~ element
	                                	pos2 = name.indexOf("/",pos+1);
	                                	
	                                	if( pos2 == -1 )
	                                		end_pos = name.length;
	                                	else
	                                		end_pos = pos2;
	                                
	                                }
                                 }

                                 // Grab the domain name
                                 var name = new String( url );
                                 domain = name.substr(start_pos,end_pos-start_pos);

                               alert("domain = " + domain + " - start = " + start_pos + " -  end = " + end_pos);

                                 return domain;
          }

	 function loadJavascriptFile(file)
	 {
//		alert( "<SCRIPT LANGUAGE=\"JavaScript1.2\" SRC=\""+file+"\"><\/SCRIPT>" );
//		alert( "Document Loaction: " + document.location );

//	 	file = "https://" + getRootDomainName(document.location) + "/javascript/" + file;
		file = getProtocol(document.location) +  "://" + getRootDomainName(document.location) + "/javascript/" + file;
//		alert(file);
		document.write("<SCRIPT LANGUAGE=\"JavaScript1.2\" SRC=\""+file+"\"><\/SCRIPT>");
	 }

	function disableMouseRightClick()
	{
//		document.writeln("<script language=\"javascript\">");
		document.writeln("function noRightClick() {");
		document.writeln("if (event.button==2) {");
		document.writeln("alert('You can NOT Right-Click on this page -- HTML Source Code Is Protected.');");
		document.writeln("}");
		document.writeln("}");
		document.writeln("document.onmousedown=noRightClick");
//		document.writeln("</script>");
	}

        //
        // This function return sthe protocol of the URL (i.e. HTTP, HTTPS, FTP, etc...
        //
        function getProtocol(domain_url)
        {

                var url = new String( domain_url );
                var pos = url.indexOf(":",1);

                if( pos == -1 )
                        return "unknown";

                var name = new String( url );
                domain = name.substr(0,pos);

                return domain;

        }



         //
         // NOTE: This function *must* be the same as the one defined in [ utils.js ]
         //
         function getRootDomainName(url)
         {
                                 var name = new String( url );
                                 name = name.toUpperCase();

                                 // Find the start char
                                pos = name.indexOf("HTTPS://");

                                if( pos == -1 )
                                        pos = name.indexOf("HTTP://");

                                if( pos == -1 )
                                        pos = name.indexOf("FTP://");

                                 if( pos == -1 )
                                         return "unknown";

                                 start_pos = pos + getProtocol(url).length + 3;

                                 // Find the end char
                                 pos = name.indexOf("/",start_pos+1);

                                 if( pos == -1 )
                                         end_pos = name.length;
                                 else
                                 {
                                        //
                                        // Check to see if the url has a "~" if it does then that part of
                                        // the domain/root node [ www.xyz.com/~spickett ] would be the root.
                                        //
                                        pos2 = name.indexOf("~",pos);
                                        if( pos2 == -1 )
                                                end_pos = pos;
                                        else
                                        {
                                                // include the ~ element
                                                pos2 = name.indexOf("/",pos+1);

                                                if( pos2 == -1 )
                                                        end_pos = name.length;
                                                else
                                                        end_pos = pos2;

                                        }
                                 }

                                 // Grab the domain name
                                 var name = new String( url );
                                 domain = name.substr(start_pos,end_pos-start_pos);

//                               alert("domain = " + domain + " - start = " + start_pos + " -  end = " + end_pos);

                                 return domain;
          }


         function random(max_val)
          {
                  // Generate a unique session ID [32 bit value]
                  var generatornum=Math.random();
                  var maxnumber=max_val;
                  var randomnum=Math.round(generatornum*maxnumber);
                  return randomnum;
          }


        <!-- Original:  Cyanide_7 (leo7278@hotmail.com) -->
        function formatCurrency(num)
        {
                num = num.toString().replace(/\$|\,/g,'');
                if(isNaN(num))
                        num = "0";

                sign = (num == (num = Math.abs(num)));
                num = Math.floor(num*100+0.50000000001);
                cents = num%100;
                num = Math.floor(num/100).toString();
                if(cents<10)
                        cents = "0" + cents;
                for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
                        num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
                return (((sign)?'':'-') + num + '.' + cents);
        }



function changeBg(color){	//sets bg color to new value
window.document.bgColor= color
}

function changeText(color){	//sets text color to new value
window.document.fgColor= color
}

function changeLink(color){	//sets link color to new value
window.document.linkColor= color;
}

function changeVlink(color){	//sets vlink color to new value
window.document.vlinkColor= color
}

        //
        // This function will search for the specified IFRAME and will return an
        // object reference that allows JavaScripts to be invoked within that
        // page
        //
        function getScriptFrame(iframe_name)
        {
                return recursiveFrameSearch( iframe_name, top.frames[0] )
        }

        //
        // This function will search for the specified frame object - and if found
        // will return it.
        //
        function getFrame(iframe_name)
        {
                if( top.frames.length == 0 )
                {
                        if( top.frames.name == iframe_name )
                                return top;

                        return null;
                }

                try
                {
                        if( top.frames[iframe_name] != null )
                                return top.frames[iframe_name];

                } catch( e )
                {
                        // Do nothing
                }

                var frame_object = recursiveFrameSearch( iframe_name, top.frames[0] );
                if( frame_object != null )
                {
                        //
                        // We need the correct object
                        //
//alert("Found Frame: " + iframe_name);
//show_props( frame_object, "frame_object" );

                        return frame_object.parent.document.getElementById(iframe_name);
                }

                return frame_object;
        }


        function recursiveFrameSearch( iframe_name, frames )
        {
                //
                // Check to see if the current frame is the frame we are looking
                // for.
                //
                if( frames.name == iframe_name )
                {
                        return frames;
                }

                //
                // Are there any more child frames?
                //
                if( frames.length == 0 )
                        return null;

                //
                // Check to see if this frame has any child frames.  If it does then
                // recursively visit each of these child frames and search for the
                // requested frame.
                //
                var max_frames = frames.length;
                var loop=0;

                while(loop<max_frames)
                {
//                      alert("Searching For Frame Match [" + iframe_name + "] = [" + frames[loop].name + "]");

                        frame_object = recursiveFrameSearch( iframe_name, frames[loop] );

                        //
                        // If the frame object is not null then we have a match!
                        //
                        if( frame_object != null )
                        {
                                return frame_object;
                        }

                        loop++;
                }

                return null;
        }

function getFrameObject( iframe_name)
{

        if( document.getElementById(iframe_name) != null )
        {
                return document.getElementById(iframe_name);
        }

        if( parent.document.getElementById(iframe_name) != null )
        {
                return parent.document.getElementById(iframe_name);
        }

        if( top.frames['ROOT'].document.getElementById(iframe_name) != null )
        {
                return top.frames['ROOT'].document.getElementById(iframe_name);
        }

        if( top.document.getElementById(iframe_name) != null )
        {
                return top.document.getElementById(iframe_name);
        }

        //
        // Last try to resolve frame.
        //
        return getFrame(iframe_name);
}

function random(max_val)
{

                                  // Generate a unique session ID [32 bit value]

                                  var generatornum=Math.random();

                                  var maxnumber=max_val;

                                  var randomnum=Math.round(generatornum*maxnumber);

                                  return randomnum;           

}

        

function counter( site, page )
{

                url = "http://www.simpleanswers.co.uk/cgi-bin/counter.cgi?site=" + site + "&name=" + page+ "&rand=" + random(999999);

                document.write("<IMG SRC=\"" + url + "\">\n");

}


var isDOM = (document.getElementById ? true : false);
var isIE4 = ((document.all && !isDOM) ? true : false);
var isNS4 = (document.layers ? true : false);
var isDyn = (isDOM || isIE4 || isNS4);

function getElementById(id) {
   if (isDOM) return document.getElementById(id);
   if (isIE4) return document.all[id];
   if (isNS4) return document.layers[id];
}


function open_new_page(url, w, h)
{
	var x,y;
	var height, width;
	height=h;
	width=w+20;  // 20 + for scrollbar
						 
	x = (screen.width - width) / 2; 
	y = (screen.height - height) / 2; 
				
	var mywin = window.open(url,'mywin','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width='
+ width + ',height=' + height + ',top=' + y + ',left=' + x);	
  	mywin.focus();   			 																			
}				

