// Check field is not empty

function validateField(name) {
    
    var field 		= document.getElementById(name);

    
    if (field.value.length === 0) {
        
		//alert("starting class " + field.className);
		
		if (field.type == 'text') field.className = 'error';
		if (field.type == 'select-one') 
		{
			if (field.className == "mediumWidth") field.className = 'selectMediumError';
			if (field.className == "smallWidth") field.className = 'selectSmallError';
		}
		
		if (!focussed)
		{
		
			focussed = true;
			$("#"+name).focus();
		
		}
       
	   showError(name);
		
		//if (name == 'lblCardType') 
		//alert(field + " " + field.type + " " + field.className);
		
        return false;
		
		
    } else {
        //field.className = '';
        hideError(name);
        return true;
    }
}

// Make sure is integer
function validateNumber(name) 
{
    
    var field        = document.getElementById(name);
    var checkVal     = field.value;
    var val          = Number(field.value);
    var decimalCheck = (/\.0+$|\.$/.test(checkVal));
    checkVal         = parseInt(val, 10);
    
    if (val !== checkVal || decimalCheck === true) {
        
        field.className = 'error';
        showError(name);
        var req = document.getElementById(name+'-req');
        
        if (!focussed)
		{
		
			focussed = true;
			$("#"+name).focus();
		
		}
       
        return false;
        
    } else {
        
        return true;
    }
    
}

function validateNumericExistence(name) {
	
	var field        = document.getElementById(name);
    var checkVal     = field.value;
	
	var myRegxp = /(^[a-zA-Z]+)$/;
	
	if(myRegxp.test(checkVal) == true)
	{
		//alert( "1 FAIL for " + checkVal );
		field.className = 'error';
        showError(name);
		
		var req = document.getElementById(name+'-req');	
		
		if (req) {
            req.innerHTML = '(invalid)';
        }
        
        if (!focussed)
		{
		
			focussed = true;
			$("#"+name).focus();
		
		}
        
		return true;
		
	}  else
	{
	
		//alert( "1 PASS for "  + checkVal);
		field.className = '';
        hideError(name);
		return true;
		
	}
	
}

// Check email format.
function validateEmail(name) 
{
    
	var email = document.getElementById(name);
	var str   = email.value;
	
	var error = false;
	var at   = "@";
	var dot  = ".";
	var lat  = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);

    if (str.indexOf(at)==-1){
         error = true;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
         error = true;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
         error = true;
    }
    if (str.indexOf(at,(lat+1))!=-1){
         error = true;
    }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
         error = true;
    }
    if (str.indexOf(dot,(lat+2))==-1){
        error = true;
    }
    if (str.indexOf(" ")!=-1){
        error = true;
    }
    
	if (!focussed && error == true)
	{
	
		email.className = 'error';
		showError(name);
		
		var req = document.getElementById(name+'-req');
		
		if (req) {
		req.innerHTML = '(invalid)';
		}
		
		focussed = true;
		$("#"+name).focus();
		
		return false;
	
	}
	else
	{
	
	    email.className = '';
	    hideError(name);
	    return true;
    
    }
    
    
}

// Show and hide the (required) red label.
function showError(name) {
    var req = document.getElementById(name+'-req');
    if (req) {
        req.innerHTML = '(required)';
        req.style.visibility = 'visible';
        req.style.display    = 'inline';
    }
}
function hideError(name) {
    var req = document.getElementById(name+'-req');
    if (req) {
        req.style.visibility = 'hidden';
        req.style.display    = 'none';
    }
}

// Format currency, add ',' and make it 2 place decimal.
function formatCurrency(num) {
    num   = Math.floor(num*100+0.50000000001);
    cents = num%100;
    if(cents<10) {
        cents = "0" + cents;
    }
    num   = Math.floor(num/100).toString();
    for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++) {
        num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
    }
    return (num + '.' + cents);
}
