//========================================================================================//
// Copyright 2008 Coastal Carolina Web Services Inc.                                      //
// File: utils.js                                                                         //
//========================================================================================//
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;
		}	
	}
}
// 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;
	oidList.options.add(oid);
}
// Delete 1 or all items in a list box
function deleteFromList(oidList, index)
{
	if (index < 0) {		
		var items = oidList.length;
		for (i=0; i<items; i++)			// Delete all items in the list box
			oidList.remove(oidList.i);
	}
	else
		oidList.remove(oidList.index); // Remove 1 item in the list box
}
myWin = null;
function showImage(url,w,h)
{
	var width=w+20;
	var height=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;
	hideImage();
	myWin = window.open(url, 'myWin', atts);
}
function hideImage()
{
	if (myWin && myWin.open)
		myWin.close();
}
// 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;
}
