// *****************************************************************************//
// Browser Checker
// type the following into the browsers address field - javascript:document.write(navigator.userAgent)
// below is some sample output
// Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Aim Package version 1.07.8; (R1 1.1))
// Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)
var minVer = 5.5
var site   = "error.html"
var ver    = navigator.userAgent
var ms     = ver.indexOf('MSIE')
var split1 = ver.split(';')
var split2 = split1[1].split('MSIE ')
function browserCheck(){
 if(ms<0||split2[1]<minVer){
  document.location.href = site
 }
}
browserCheck()

// *****************************************************************************//
// Distance from Mouse
// Returns the average distance between two specified points (usually for pixels)
// The first point (x1,y1) specifies the origin of the distance.
function getDistance(x1,y1,x2,y2){
  var d =  Math.round(Math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))));
  if (isNaN(d)) d = 0;
  return d;
}


// *****************************************************************************//
// Angle of mouse against object
// Gives the angle of the radian described by the two specified points
// The first point (x1,y1) specifies the origin of the radian.
function getAngle(x1,y1,x2,y2){
  var diffH = (x1-x2);
  var diffV = (y2-y1);
  if (diffH){
    var slope = diffV / diffH ;
    var angle = Math.atan(slope);
    var dgrs = (angle * 180) / Math.PI;
  	if (diffH < 0){dgrs += 180;}
  }
  else if (diffV < 0){dgrs = 270;}
  else if (diffV > 0){dgrs = 90;}
  else {dgrs = 0;}
  if (dgrs < 0) dgrs = 360 + dgrs;
  return Math.round(dgrs);
}


// *****************************************************************************//
// Decimal to Hex converter
// Covert decimal to hex number, return hex num as string
function toHex(hex) {
  var hex_val = hex.toString(16).toUpperCase();
    if (hex_val.length == 1){
	  var temp = "0" + hex_val
	  hex_val = temp;
	 }
  return hex_val
 }


// *****************************************************************************//
// Hex to Decimal converter
// Covert decimal to hex number, return dec num as string
function toDec(dec){
  dec_val = parseInt(dec.toUpperCase(),16);
  return dec_val
 }


// *****************************************************************************//
// Hex to RGB color converter
// Covert hex number to RGB color code, return rgb as string
function toRGB(val){
 if(val.substr(0,1)=='#'){
  r=val.substr(1,2)
  g=val.substr(3,2)
  b=val.substr(5,2)
 }else{
  r=val.substr(0,2)
  g=val.substr(2,2)
  b=val.substr(4,2)
 }
 return toDec(r)+','+toDec(g)+','+toDec(b)
}


// *****************************************************************************//
// Drag and Drop
// add type='drag' to any absolutely positioned element
var dragObj,dragx,dragy,dragz

function moveObj(){
  if (event.button==1){
    if(dragObj!=null){
	  if(dragObj.type=='drag'){
        dragObj.style.pixelLeft=temp1+event.clientX-dragx
        dragObj.style.pixelTop=temp2+event.clientY-dragy
        return false
	   }
     }
   }
 }

function dragObj(){
  dragObj=event.srcElement
    if(dragObj.style.zIndex<=dragz){
      dragObj.style.zIndex=dragz+1
     }
  temp1=dragObj.style.pixelLeft
  temp2=dragObj.style.pixelTop
  dragx=event.clientX
  dragy=event.clientY
 }

function dropObj(){
  dragz=parseInt(dragObj.style.zIndex)
  dragObj=null
 }


// *****************************************************************************//
// Easy Sliders
var slideObj,slideParent,slideHmin,slideHmax,slideVmin,slideVmax

function slideDo(){
  if (event.button==1){
    if(slideObj!=null){
	  if(slideObj.type=='drag'){
        if(slideParent.type=='horizontal'){
          if ((event.clientX  - slider_x >= slideHmin) && (event.clientX - slider_x <= slideHmax)){
            slideObj.style.left=event.clientX - slider_x;
			slideVal(parseInt(eval('slideObj.style.left')))
           }
		 }
        if(slideParent.type=='vertical'){
          if ((event.clientY  - slider_y >= slideVmin) && (event.clientY - slider_y <= slideVmax)){
            slideObj.style.top=event.clientY - slider_y;
			slideVal(parseInt(eval('slideObj.style.top')))
           }
		 }
        return false
	   }
     }
   }
 }

function slideGet(){
  slideObj=event.srcElement
  slideParent=eval(slideObj.parentElement)
  slider_x = (event.clientX-slideObj.style.pixelLeft);
  slider_y = (event.clientY-slideObj.style.pixelTop);
  slideHmin = 0
  slideVmin = 0
  if(slideObj.className=='pic'){
    slideHmax = parseInt(slideParent.style.width)-(parseInt(slideObj.style.width))
	slideVmax = parseInt(slideParent.style.height)-(parseInt(slideObj.style.height))
   }else{
    slideHmax = parseInt(slideParent.style.width)-(parseInt(slideObj.style.width)+(parseInt(document.styleSheets[0].rules[1].style.borderWidth)*2))
    slideVmax = parseInt(slideParent.style.height)-(parseInt(slideObj.style.height)+(parseInt(document.styleSheets[0].rules[1].style.borderWidth)*2))
   }
 }

function slideDrop(){
  slideObj=null
 }


// *****************************************************************************//
// Set Cookie
function setCookie(NameOfCookie,value,expiredays){
  var ExpireDate = new Date ();
  ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
  document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
 }


// *****************************************************************************//
// Get Cookie
function getCookie(NameOfCookie){
  if (document.cookie.length > 0){              
    begin = document.cookie.indexOf(NameOfCookie+"=");       
    if (begin != -1){           
      begin += NameOfCookie.length+1;       
      end = document.cookie.indexOf(";", begin);
      if (end == -1) end = document.cookie.length;
       return unescape(document.cookie.substring(begin, end))
      } 
    }
  return false;
 }


// *****************************************************************************//
// Add Style Sheets to document
function addStyleSheet(name,style){
 if(!document.styleSheets.length){document.createStyleSheet()}
 document.styleSheets[document.styleSheets.length-1].addRule(name, style)
}

// *****************************************************************************//
// Get or Set an attribute of a style within the styles collection
function styleAttribute(where,what,val){
 for(a=0;a<document.styleSheets.length;a++){
  for(b=0;b<document.styleSheets[a].rules.length;b++){
   sr=document.styleSheets[a].rules[b]
   if(sr.selectorText==where){
    if(val){
     eval('sr.style.'+what+'="'+val+'"')
	}else{
     return eval('sr.style.'+what)
	}
   }
  }
 }
}

// *****************************************************************************//
// Date Functions

// create date string in US date format (m/d/yy)
function USdate(){
 d = currdate.getDate()
 m = currdate.getMonth()+1;
 y = currdate.getYear()
 return m+"/"+d+"/"+y
}

// convert US date format (m/d/yy) to UK date format (dd/mm/yyyy)
function US2UK(format){
 tmp=format.split('/')
 d = tmp[1]
 m = tmp[0]
 y = tmp[2]
 if(d<10){d="0"+d}else{d=""+d}
 if(m<10){m="0"+m}else{m=""+m}
 return d+'/'+m+'/'+y
}

// calculte new date for n days previous (str is in US date format)
function daysPrev(str,days){
 var dateArray = str.split('/');
 sdate = new Date(dateArray[2],dateArray[0]-1,dateArray[1]);
 var odate = new Date(sdate.getTime() - (days * 86400000));
 return (odate.getMonth()+1) + '/' + odate.getDate() + '/' + odate.getYear();
}

// EXAMPLE
// compile yesterday's date from date functions
//dateStr=US2UK(daysPrev(USdate(),1))

// *****************************************************************************//
// String Functions

function allCapital(what){
 return what.toUpperCase()
}

function allLower(what){
 return what.toLowerCase()
}

function firstCapital(what){
 return allCapital(what.substring(0,1))+allLower(what.substring(1))
}

