// ==============================================================================
//					SET GLOBAL VARIABLES
// ==============================================================================
validateAgencyURL = "http://www2.ku.edu/~pfspilot/cgi-bin/lists/AgencyChk.php"

State = ""

// ==============================================================================
//					TEXT SIZE
// ==============================================================================
function TextSize(s){
	document.getElementById("main_content").className = "main_content_"+s
}

// ==============================================================================
//					GET MOUSE POSITION
// ==============================================================================
mY = 0
mX = 0
document.onmousemove = getMouseXY;
IE = document.all?true:false
if (!IE) document.captureEvents(Event.MOUSEMOVE)

function getMouseXY(e){
	if (IE){
		mY = event.clientY + document.body.scrollTop
		mX = event.clientX + document.body.scrollLeft}
	else{
	mY = e.pageY
	mX = e.pageX}
}

// ==============================================================================
//					POPUP
// ==============================================================================
function showPopUp(msg,w){
	if(!w) w=300
	if(document.getElementById('popup').style.visibility == 'visible'){
		document.getElementById('popup').style.visibility='hidden'
		return false
	}
	document.getElementById('popup').style.visibility='visible'
	if(IE) document.getElementById('popup').style.top = (mY - 0) + "px"
	else  document.getElementById('popup').style.top = (mY - 0) + "px"
	document.getElementById('popup').style.left = (mX - 0) + "px"
	document.getElementById('popup').style.width = w+"px"
	document.getElementById('message').innerHTML = msg
}

// ==============================================================================
//					DROP DOWN MENUE
// ==============================================================================
lastName = ''
function navOver(name){
	menuPosY = document.getElementById("mainMenu").offsetTop
	bntPosY = document.getElementById(name).offsetTop
	menuPosX = document.getElementById("mainMenu").offsetLeft
	bntPosX = document.getElementById(name).offsetLeft

	if (name == lastName) return false
	if (lastName != '') navOut(lastName)

	if(document.getElementById(name+"_dd")){
		document.getElementById(name+"_dd").style.display = "block"
		document.getElementById(name+"_dd").style.top = (bntPosY + menuPosY + 20) + "px"
		document.getElementById(name+"_dd").style.left = (bntPosX + menuPosX + 0) + "px"
		}
	lastName = name
}

function navOut(name){
	if(name == 'all') name = lastName
	if(name == '') return

	if(document.getElementById(name+"_dd")) document.getElementById(name+"_dd").style.display = "none"
	lastName = ''
}

// ==============================================================================
//					HELP POPUP POSITION
// ==============================================================================
function HelpPopup(n){
	if(document.getElementById(n).style.display != "block"){
		document.getElementById(n).style.display = "block"
		document.getElementById(n).style.top = (mY-80)+'px'
		document.getElementById(n).style.left = (mX-300)+'px'}
	else{
		document.getElementById(n).style.display = "none"}
}


// ==============================================================================
// 					CHARACTER COUNT
// ==============================================================================
function charCount(v,lng) {
  l = document.getElementById(v).value.length +1
  if(l > lng){
	  document.getElementById('length').value = "maximum 256 characters exceeded"
	  document.getElementById('length').className = "exceeded"
  }
  else{
	  document.getElementById('length').className = "length"
	  document.getElementById('length').value = l + " characters"
  }
}

// ==============================================================================
// 					CONVERT TO DOLLAR FORMAT
// ==============================================================================
function currency( num )
{
   prefix = "$"
   suffix = ""
   if ( num < 0 )
   {
       prefix = "($"
       suffix = ")"
       num = - num
   }
       temp = Math.round( num * 100.0 );
       if ( temp < 10 ) return prefix + "0.0" + temp + suffix
       if ( temp < 100 ) return prefix + "0." + temp + suffix
       temp = prefix + temp
       return temp.substring(0,temp.length-2) + "." + temp.substring(temp.length-2) + suffix
}

// ==============================================================================
//					VALIDATION
// ==============================================================================
// ------------------  EMAIL   --------------------------------------------------
function checkEmail(v,n) {
error=""

if(v.length == 0)return error
emailFilter=/^.+@.+\..{2,3}$/
    if (!(emailFilter.test(v))) {
       error = "Please enter a valid email address for the "+n+".\n"
    }
    else {
//test email for illegal characters
   illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
         if (v.match(illegalChars)) {
          error = "The "+n+" email address contains illegal characters.\n"
       }
    }
return error
}

// ------------------  PHONE   --------------------------------------------------
// phone number - strip out delimiters and check for 10 digits
function checkPhone (v,n) {
error = ""
	if(v.length == 0)return error

	stripped = v.replace(/[\(\)\.\-\ ]/g, '') //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The "+n+" contains illegal characters.\n"

    }
    if (!(stripped.length == 10)) {
	error = "The "+n+" is the wrong length.\n"
    }
return error
}

// ------------------  PHONE   --------------------------------------------------
// phone number - strip out delimiters and check for 10 digits
function stripPhone (phone) {
	phone =  phone.replace(/[\(\)\.\-\ ]/g, '')
	return phone
}

// ------------------------------------------------------------------------------
// IS NUMERIC
function isNumeric(v,n) {
error = ""

	if(v.length == 0)return error
	stripped = v.replace(/[\(\)\.\-\ ]/g, '') //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The "+n+" must be numeric.\n"
    }
return error
}

// ------------------------------------------------------------------------------
// 						IS EMPTY
function isEmpty(v, n) {
error = ""
  if (v.length == 0) {
     error = "Please enter "+n+".\n"
  }
return error
}

// ------------------------------------------------------------------------------
//						DROP DOWN BOX
function checkDropdown(v,n) {
error = ""
    if (v == 0) {
    error = "Please choose an option from the "+n+" drop-down list.\n"
    }
return error
}

// ------------------------------------------------------------------------------
//						USERNAME
// username - 4-10 chars, uc, lc, and underscore only.
function checkUsername (v) {
error = ""
if (v == "") {
   error = "Please enter a username.\n"
}

illegalChars = /\W/ // allow letters, numbers, and underscores
    if ((v.length < 4) || (v.length > 10)) {
       error = "The username is the wrong length.\n"
    }
    else if (illegalChars.test(v)) {
    error = "The username contains illegal characters.\n"
    }
return error
}

// ------------------------------------------------------------------------------
//						MAXIMUM/MINIMUM LENGTH
function checkLength(v,l,m,n) {
error = ""
    if (v.length > l) {
    	error = n+" can not be more than "+l+" characters long.\n"
	}
	else if(v.length < m) {
		error = n+" must be at least "+m+" characters long. \n"
    }
return error
}


// ------------------------------------------------------------------------------
// Declaring valid date character, minimum year and maximum year
dtCh = "/"
minYear = 1900
maxYear = 2100

function isInteger(s){
    for (x = 0; x < s.length; x++){
        // Check that current character is number.
        c = s.charAt(x)
        if (((c < "0") || (c > "9"))) return false
    }
    // All characters are numbers.
    return true
}

function stripCharsInBag(s, bag){
    returnString = ""
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (x = 0; x < s.length; x++){
        c = s.charAt(x)
        if (bag.indexOf(c) == -1) returnString += c
    }
    return returnString
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 )
}

function DaysArray(n) {
	for (x = 1; x <= n; x++) {
		this[x] = 31
		if (x==4 || x==6 || x==9 || x==11) {this[x] = 30}
		if (x==2) {this[x] = 29}
   }
   return this
}

function isDate(dtStr,m){
	error = ""
	if(dtStr == ''){
		return error
	}
	daysInMonth = DaysArray(12)
	pos1=dtStr.indexOf(dtCh)
	pos2=dtStr.indexOf(dtCh,pos1+1)
	strMonth=dtStr.substring(0,pos1)
	strDay=dtStr.substring(pos1+1,pos2)
	strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (x = 1; x <= 3; x++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		error = "The date format should be : mm/dd/yyyy"+m+"\n"
	}
	if (strMonth.length<1 || month<1 || month>12){
		error = "Please enter a valid month"+m+"\n"
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		error = "Please enter a valid day"+m+"\n"
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		error = "Please enter a valid 4 digit year between "+minYear+" and "+maxYear+" "+m+"\n"
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		error = "Please enter a valid date"+m+"\n"
	}
return error
}

// ==============================================================================
//					FORMAT A DATE FOR DATABASE
function stripDate(d){
	if(d == '') return ''
	d_arr = d.split('/')
	month = d_arr[0]
	day = d_arr[1]
	year = d_arr[2]
	return d = year+"-"+month+"-"+day
}


// ==============================================================================
//					FORMAT A DATE
// ==============================================================================
function date_Format(v,n) {
	char = ""
	if(v == '') return
	validChars = "0123456789/"
	l = v.length
	for(i=0; i<l && char!="x"; i++){
		char = v.charAt(i)
		if(validChars.indexOf(char) == -1){
			document.getElementById(n).value = String(v).substring(i,1)
			return
		}
	}

switch (l){
	case 2:
		v += "/"
	document.getElementById(n).value = v
		break
	case 5:
		v += "/"
	document.getElementById(n).value = v
	}
//	if(l == 10) v = String(v).substring(0,9)
//	if(l == 9) isDate(v)
}


// ==============================================================================
//					AJAX
// ==============================================================================
function getHTTPObject() {

var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {

      try {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (E) {

        xmlhttp = false;

      }

    }

  @else

  xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

    try {

      xmlhttp = new XMLHttpRequest();

    } catch (e) {

      xmlhttp = false;

    }

  }

  return xmlhttp;

}

http = getHTTPObject(); // We create the HTTP Object
