normalStyle = {
    'border-color':'blue',
    'background-color':'#FFFFFF'
};
errorStyle = {
    'border-color':'red',
    'background-color':'#FFFFA7'
};
//JQuery.noConflict();
$(document).ready(function(){
    $('#submit_button').click(formSubmit);
    $('fieldset.jcalendar').jcalendar();
	
	/*form_warranty - show and hide ...purchase experience? field*/	 
	$("#purchase_experience_no").click(function () {
      $('#hide_option').hide();
    }); 
	$("#purchase_experience_yes").click(function () {
      $('#hide_option').show();
    }); 
	/*end*/
	
    $('#set-date').click(function(){
        text = 'Close';
        if($(this).html() == text){
            $(this).html('Select date');
        }else $(this).html(text);
        $('div.jcalendar').slideToggle('slow');
    });
	
	$('#country_sel').change(function(){
		var HTML = 	'<input type="text" name="state_sel" class="selector_states">';						  
		if(this.value == 'US'){
			HTML = '<select name="state_sel" class="selector_states">'+$('#st').html()+'</select>';
		}
		$('#state_options').fadeOut('slow', function(){
			$('#state_options').html(HTML);
			$('#state_options').fadeIn('slow');
		});
	});
});


function formSubmit(){
    err = true;
    o = $('input[type=text]');
    o.css(normalStyle);
    $.each(o, function(k,e){
        e.value = trim(e.value, '');
            if(empty($(e).val())){
            err = false;
            setError($(e));
            }
    })

    if(!is_valid_email($('#email').val())){
        err = false;
        setError($('#email'));
    }

//    if(empty($('#comment').val())){
//        err = false;
//        setError($('#comment'));
//    }

    if (!isZip($('#zipcode').val())){
        err = false;
        setError($('#zipcode'));
    }
    
    n = $("input:checked").length;
    if(n == 0){
        err = false;
        alert('Please check the program that best fit your needs');
    }

    return err;
}

function empty (mixed_var) {
    var key;
    if (mixed_var === "" ||
        mixed_var === 0 ||
        mixed_var === "0" ||
        mixed_var === null ||
        mixed_var === false ||
        mixed_var === undefined
        ){
        return true;
    }

    if (typeof mixed_var == 'object') {
        for (key in mixed_var) {
            return false;
        }
        return true;
    }

    return false;
}

function setError(e){
    e.focus(function(){
        $(this).css(normalStyle);
    });
    e.css(errorStyle);
}

function is_valid_email (email)
{
    return /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email);
}


function isZip(s){
    reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
    if (!reZip.test(s)) {
        return false;
    }
    return true;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
