//========================================================================================//
// Copyright 2008 Coastal Carolina Web Services Inc.                                      //                                                                     //
//========================================================================================//
var i;

function validateEmail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1)
		return false
		
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false

	if (str.indexOf(at,(lat+1))!=-1)
		return false

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false

	if (str.indexOf(dot,(lat+2))==-1)
		return false
		
	if (str.indexOf(" ")!=-1)
		return false
			
	return true;				
}

// Returns the index of an item in a listbox or -1 if not found
function getIndexByValue(oid, value)
{
	for (i=0; i<oid.options.length; i++) {
       if (oid.options[i].value == value) {
			return i;
		}
    }
	return -1;
}
// Returns the index of an item in a listbox or -1 if not found
function getListIndex(oid, str)
{
	for (i=0; i<oid.options.length; i++) {	
		if (oid.options[i].text == str) {
			return i;
		}
    }
	return -1;
}
// Returns the value, index or text from a list box
function getListSelection(oid, id)
{
	if (oid != null) {
		var index = oid.options.selectedIndex;
		if (index < 0)
			return -1;
		if (id == 'value')
			return oid.value;
		if (id == 'index')
			return index;
		if (id == 'text')
			return oid.options[index].text;
	}
	return -1;
}
function setListSelection (oid, val)
{
	for (i=0; i < oid.length; i++) {
		if (oid.options[i].value == val) {
			oid.selectedIndex = i;
			return;
		}	
	}
	return -1;
}
// Check if a string (arg) exists in another string (searchList)
// Returns 0 if not found or 1 if found
function findInList(searchList, arg, delim) 
{     
	var arrayList = new Array();      
	arrayList = searchList.split(delim);
	
	for(i=0; i < arrayList.length; i++) {     
		if (arrayList[i].toLowerCase() == arg.toLowerCase()) {
			return 1;
		}
	}
	return 0;
}
// Add an entry to a list box
function addToList (oidList, text, value) {
	var oid = document.createElement("option");
	oid.text = text;
	oid.value = value;
	// Add only if it does not exist in the list
	if (!inList(oidList, text))
		oidList.options.add(oid);
}
// Check if a string exists in listbox
function inList(oid, text)
{
	var i;
	for (i = oid.length - 1; i >= 0; i--) {		
		if (oid.options[i].text == text)
			return true;
	}
	return false;
}
// Delete selected items in list box
function deleteSelectedInList (oid)
{
	var i;
	for (i = oid.length - 1; i >= 0; i--) {
		if (oid.options[i].selected)
			oid.remove(i);
	}
}
// Delete all items in a list box
function deleteAllInList (oid)
{
	var i;
	for (i = oid.length - 1; i >= 0; i--) {
		oid.remove(i);
	}
}
myWin = null;
function showImage(url,w,h)
{
	var width=parseInt(w)+20;
	var height=parseInt(h)+20;
	var from_top=50;
	var from_left=50;
	var toolbar='no';
	var location='no';
	var directories='no';
	var status='no';
	var menubar='no';
	var scrollbars='yes';
	var resizable='yes';
	var atts='width='+width+'show,height='+height+',top='+from_top+',screenY=';
	atts+= from_top+',left='+from_left+',screenX='+from_left+',toolbar='+toolbar;
	atts+=',location='+location+',directories='+directories+',status='+status;
	atts+=',menubar='+menubar+',scrollbars='+scrollbars+',resizable='+resizable;
	if (myWin)
		closeWindow();
	myWin = window.open(url, 'myWin', atts, true);
}
function closeWindow()
{
	if (myWin && myWin.open)
		myWin.close();
}
function initImage(photo) {
  image = document.getElementById(photo);
  setOpacity(image, 0);
  image.style.visibility = 'visible';
  fadeIn(photo,0);
}
// This function sets the opacity of the supplied object using four proprietary ways. It also prevents a
// flicker in Firefox caused when opacity is set to 100%, by setting the value to 99.999% instead.
function setOpacity (obj, opacity) {
  opacity = (opacity == 100) ? 99.999 : opacity;
  obj.style.filter = "alpha(opacity:"+opacity+")";   // IE/Win  
  obj.style.KHTMLOpacity = opacity/100;				 // Safari<1.2, Konqueror 
  obj.style.MozOpacity = opacity/100;				 // Older Mozilla and Firefox  
  obj.style.opacity = opacity/100;					 // Safari 1.2, newer Firefox and Mozilla, CSS3
}

// This function calls itself every 100ms. The opacity is specified as a percentage and is increased by 10%
// each time it is called. The loop terminates when opacity is 100%. This technique allows the image to be faded in.
function fadeIn (photo,opacity) {
  if (document.getElementById) {
    photoObj = document.getElementById(photo);
    if (opacity <= 100) {
      setOpacity (photoObj, opacity);
      opacity += 10;
      window.setTimeout("fadeIn('"+photo+"',"+opacity+")", 100);
    }
  }
}
function populateMonths(objList)
{
	var months ={"0":"January","1":"February","2":"March","3":"April","4":"May","5":"June","6":"July","7":"August","8":"September","9":"October","10":"November","11":"December"};

	var today = new Date();
	for(var i=0;i<12;i++) {
		var val = (today.getMonth() + i)%12;		
		objList.options[objList.options.length] = new Option(months[val],i);
	}
}
// start (int) - starting year from current year. Ex. if current year is 2009 a value of -3 will start from 2006
// or a value of 2 will start from 2011.
// numYears (int) - The number of years to insert into the list based on the starting year
function populateYears(objList, start, numYears)
{
	var today = new Date();
	var year = today.getFullYear()+start;	
	for(var i=0;i<numYears;i++) {
		objList.options[objList.options.length] = new Option(year+i,i);
	}
}
// Create XML object to handle the update request
var xmlHttp;
function GetXmlHttpObject()
    {
        var xmlHttp=null;
        try
            {
            // Firefox, Opera 8.0+, Safari
            xmlHttp = new XMLHttpRequest();
        }
        catch (e)
        {
            //Internet Explorer
            try
                {
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch (e)
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        }
        return xmlHttp;
}

// Check if an item exists in an array. Return true if it exist or false if not.
function in_array(needle, haystack) {
	for (i=0; i<haystack.length; i++) {
		if (needle == haystack[i]) {
			return true;
		}
	}
	return false;
}

