// Yapta is hiring!
// Interested in javascript/AJAX, HTML, CSS and good web programming?
// Yapta is looking to hire a web developer to help us put together
// simple yet effective and dynamic web interfaces.  Check out
// our job listings at http://www.yapta.com/jobs/.
// Let us know what you think of our code below.

function textCounter(field, countfield, maxlimit) {
    if (field.value.length > maxlimit) // if too long...trim it!
    field.value = field.value.substring(0, maxlimit);
    // otherwise, update 'characters left' counter
    else
    countfield.value = maxlimit - field.value.length;
}
function toggleView(obj) {
	var el = document.getElementById(obj);
	if (el.style.display != 'none') {
		el.style.display = 'none';
	} else {
		el.style.display = '';
	}
}

function showhideDiv(obj) {
    var el = document.getElementById(obj);
    if (el.style.display == 'none') {
        el.style.display = '';
    }
    else if (el.style.display == '') {
        el.style.display = 'none';
    }

}

function showTheForm() {
    if (document.refundForm) {
        if (document.refundForm.gotRefund.checked == true) {
            showhideDiv('newPrice');
        }
    }
    if (document.boughtForm) {
        if (document.boughtForm.boughtIt.checked == true) {
            showhideDiv('boughtIt');
        }
    }
} 

function showDiv(obj) {
	var el = document.getElementById(obj);
	if (el.style.display == 'none') {
		el.style.display = '';
	}
}


function hideDiv(obj) {
	var el = document.getElementById(obj);
	if (el.style.display == '') {
		el.style.display = 'none';
	}
}


function onlyConfCode(e)
{
	var key = window.event ? e.keyCode : e.which;
	if (key < 32)
		return true;
	var keychar = String.fromCharCode(key);
	reg = /[a-zA-Z0-9]/;
	return reg.test(keychar);
}

function onlyDollarAmount(e)
{
	var key = window.event ? e.keyCode : e.which;
	if (key < 32)
		return true;
	var keychar = String.fromCharCode(key);
	reg = /[0-9\.v]/;
	return reg.test(keychar);
}

function onlyNumbers(e)
{
	var key = window.event ? e.keyCode : e.which;
	if (key < 32)
		return true;
	var keychar = String.fromCharCode(key);
	reg = /[0-9v]/;
	return reg.test(keychar);
}

function onlyName(e)
{
	var key = window.event ? e.keyCode : e.which;
	if (key < 32)
		return true;
	var keychar = String.fromCharCode(key);
	reg = /[a-zA-Z \.\'\-]/;
	return reg.test(keychar);
}

function nCheck(n) {
    if (n == null){
        return false;
    }
    //var illegalChars = /[\w\s\-\/]+/;
    // allow only letters, numbers, minuses and underscores and  parens()
    //alert(n);
    
    if (!n.match(/^[\w\d\s\-\/i\(\)]+$/)) {
       return false;
    }
    return true;
}

function aCheck(a) {
    if (a==null) {
        return false;
    }

    if (!a.match(/^\w{3}/)){
        return false;
    }
    return true;
}

function dateCheck(d) {
    if (d==null) {
        return false;
    }

    if (!d.match(/\d{2}\/\d{2}\/\d{4}/)){
        return false;
    }
    return true;
}


function addCommas(nStr)
{
    nStr += '';
    x = nStr.split('.');
    x1 = x[0];
    x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}


function isNumeric(string)
{
	var numbers = "0123456789.";
	var isnum=true;
	var num;

	for (i = 0; i < string.length && isnum == true; i++){ 
		num = string.charAt(i); 
		if (numbers.indexOf(num) == -1) {
			isnum = false;
		}
	}

	return isnum;
}

function findLow(arrNums){
	var low = 0;
	for (i = 0; i < arrNums.length; i++)
	{
		num = parseInt(arrNums[i]);
		if(!low && num != 0)
		{
			low = num;
		}else if((num < low) && num != 0){
			low = num;
		}
	}
	return low;
}

function findHigh(arrNums){
	var high = 0;
	for (i = 0; i < arrNums.length; i++)
	{
		num = parseInt(arrNums[i]);
		if(!high && num != 0)
		{
			high = num;
		}else if((num > high) && num != 0){
			high = num;
		}
	}
	return high;
}

function findAverage(arrNums){
	var avg = 0;
	var total = 0;
	var count = 0;

	for (i = 0; i < arrNums.length; i++)
	{
		num = parseInt(arrNums[i]);
		if (num > 0)
		{
			total += num;
			count += 1;
		}
	}
	avg = (total / count);

	return avg;
}

function showDropDown(el, container, menuAlign, horizontalOffset){
	$(container).toggle();	
	$(".menuDropDown").not(container).css("display","none");

	$(".menuDropDown").mouseleave(function(){
		$("body").click(function() {
		    $(".menuDropDown").css("display","none");
			$("body").unbind();
		});    	
	});
	
	$(".multiSelect").mouseenter(function(){
			$("body").unbind();
	});

	
	if (!menuAlign == 'undefined'){
		menuAlign = 'left';
	}

	if(!horizontalOffset == 'undefined'){
		horizontalOffset = 0;
	}

	if (menuAlign=='left')
	{
		offsetTop = 20;
		posTop = $(el).position().top + offsetTop;
		posLeft = $(el).position().left + horizontalOffset;
		$(container).css({ left:posLeft, top:posTop });
	
	}else{
		offsetTop = 20;
		posTop = $(el).position().top + offsetTop;
		posRight = $(el).position().left + $(el).width() - $(container).width() - horizontalOffset;
		$(container).css({ left:posRight, top:posTop });

	}

	return false;
}

function hideDropDown(container){
	$(container).addClass('hidden');
	$(container).css("display","none");
}


function showHidePopup(el, container, wrapperId, wrapperClass){
	var wrapperClass = (wrapperClass == null) ? false : wrapperClass;
	var wrapperId = (wrapperId == null) ? false : wrapperId;

	//hack to fix IE6/7 not supporting z-indexes with absolutely positioned elements inside a relative positioned element.
	if ($.browser.msie){
		$("#airResults .module").css('zIndex','0');
		$(el).closest(".module").css('zIndex','1');
	}
	//end hack

	if(!$(container).hasClass('hidden')){
		hidePopup(container);
		return false;
	}

	offsetTop = $(el).outerHeight() + 17;
	offsetLeft = -10;

	posTop = $(el).position().top + offsetTop;
	posLeft = $(el).position().left + offsetLeft;
	
	// checks for a close button, if <img class="close"/> doesn't exist, we create one.
	if(!$(container + ' img.close').length){
		$(container).append('<img src="/images/close.gif" alt="close" title="close" class="close" onclick="hidePopup(\'' + container + '\');" />');
	}

	// checks for rounded corners, if they don't exist, we create them.
	var addCorners = '';
	if(!$(container + ' span.tl').length){
		addCorners += '<span class="tl"></span>';
	}
	if(!$(container + ' span.tr').length){
		addCorners += '<span class="tr"></span>';
	}
	if(!$(container + ' span.bl').length){
		addCorners += '<span class="bl"></span>';
	}
	if(!$(container + ' span.br').length){
		addCorners += '<span class="br"></span>';
	}

	if (addCorners){
		$(container).append(addCorners);
	}


	
	$(container).css({ left:posLeft, top:posTop });
	$(container).removeClass('hidden');
	$(el).blur(function(e){
		hidePopup('.popup');
	});

	return false;
}

function hidePopup(container){
	$(container).addClass('hidden');
}

function showCal(e){
		$(e).datepicker('show');
}


function roundDown(val,mod) {
    return val - val % mod;
}

function roundUp(val,mod) {
    var m = val % mod;
    return (m>0) ? (val + mod - m) : (val);
}
