//---------- required field colors
function setRequiredColors(fieldName) {
	var fieldText = fieldName + "Label";
	if (document.getElementById(fieldName).value == "") {
		document.getElementById(fieldText).style.color="red";
	} else {
		document.getElementById(fieldText).style.color="black";
	}
}

//---------- format currency
function formatCurrency(amount){
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

//---------- valid email
function validEmail(email) {
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) {
		return true;
	} else {
		return false;
	}
}


/* 
* newWin(URL,w,h,scroll) 
*	   URL = destination URL
*	     w = width 
*       h = height
*  scroll = scrollbar switch; 0=off, 1=on
*
* Opens a new browser window with specified size and destination
*
*/
function newWin(URL, w, h, scroll, toolbar) {
    if (scroll == '') {
        scroll = 0;
    }
    if (toolbar == '') {
        toolbar = 0;
    }
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar='+ toolbar +',scrollbars='+scroll+',location=0,statusbar=0,menubar=0,resizable=1,width=" + w + ",height=" + h + ",left = 0,top = 0');");
}


/*
* newPositionedWindow(URL,w,h,scroll,left,top)
*	   URL = destination URL
*	     w = width 
*       h = height
*  scroll = scrollbar switch; 0=off, 1=on
*    left = position of window relative to left edge of screen
*     top = position of window relative to top edge of screen
* 
* Opens a new browser window with specified size and destination at a specific location on screen
*/
function newPositionedWindow(URL, w, h, scroll, left, top) {
    if (scroll == '') {
        scroll = 0;
    }
    day = new Date();
    id = day.getTime();
    eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars='+scroll+',location=0,statusbar=0,menubar=0,resizeable=1,width=" + w + ",height=" + h + ",left=" + left + ",top=" + top + "');");
}


/* 
* openDelayedWindow(thisDelayInSeconds, thisFile, thisHeight, thisWidth, thisTop, thisLeft, thisScroll)
*	   thisDelayInSeconds = delay time in seconds
*	             thisFile = url or file to open 
*                     
*						      odw_H = height of new window
*							    odw_W = width of new window
*							    odw_Y = position of window relative to top edge of screen
*							    odw_X = position of window relative to left edge of screen
*							    odw_S = scrollbar switch; 0=off, 1=on
* Opens a new browser window with specified destination after a specified number of seconds
*
*/
var odw_H = 200;
var odw_W = 200;
var odw_Y = 200;
var odw_X = 200;
var odw_S = 0;
var odw_File = "";
function openDelayedWindow(thisDelayInSeconds, thisFile, thisHeight, thisWidth, thisTop, thisLeft, thisScroll) {
    odw_H = thisHeight;
    odw_W = thisWidth;
    odw_Y = thisTop;
    odw_X = thisLeft;
    odw_S = thisScroll;
    odw_File = thisFile;
    var w = setTimeout("newPositionedWindow(odw_File,odw_W,odw_H,odw_S,odw_X,odw_Y)", thisDelayInSeconds * 1000);
}


/*
* redirectParent(thisUrl)
*       thisUrl = destination URL for the the parent window
*
* Redirects the parent window to a specified URL, then close this window
*
*/
function redirectParent(thisUrl) {
    var myParent = window.opener;
    myParent.location = thisUrl;
    window.close();
}


//customer list banner functions

function loadList(listUrl) {
   $.ajax({ url: listUrl, cache: false, success: drawList });
}

function drawList(html) {
    $('#customer-list').append(html);
    $('#customer-list-carousel').append('<li></li>').jcarousel({
        auto:3,
        visible: 4,
        animation: 0,
        scroll: 4,
        initCallback: customerList_init,
        itemLastInCallback: customerList_itemLastIn
    });
}

function customerList_itemLastIn(carousel, listObj, itemIndex, actionState) {
    if (itemIndex == carousel.options.size) {
        customerList_restart(carousel);
    }
    return false;
}

function customerList_restart(carousel) {
    carousel.scroll(1);
    carousel.startAuto();
    return false;
}

function customerList_init(carousel) {

    if ($('#customer-list').hasClass('debug')) 
    {
        carousel.options.auto = 0;
        $('#customer-list').append('<div id=\"customer-list-controls\"><button id=\"prevBtn\">< Prev </button><button id=\"startBtn\"> Start </button><button id=\"nextBtn\"> Next ></button></div>');
        $('#customer-list-controls').css({ position: 'absolute', top: '50px', left: '50px', display: 'block', border: '1px solid black', 'background-color': 'white'});
        $('#nextBtn').click(function() { carousel.stopAuto(); carousel.next(); return false; });
        $('#prevBtn').click(function() { carousel.stopAuto(); carousel.prev(); return false; });
        $('#startBtn').click(function() { carousel.options.auto = 3; customerList_restart(carousel); return false; });
        
    }
    return false;
}
