// cookie functions

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// create cookies

// create list holding input values



// read cookie
var contact_form_value_cookie_exists = readCookie('contact_input_value_list_cookie');

if(contact_form_value_cookie_exists) {
	contact_form_value_cookie_exists               = contact_form_value_cookie_exists.split("|");
	document.contact_form.sub_email.value          = contact_form_value_cookie_exists[0];
    document.contact_form.sub_name.value           = contact_form_value_cookie_exists[1];
    document.contact_form.sub_address.value        = contact_form_value_cookie_exists[2];
    document.contact_form.sub_city.value           = contact_form_value_cookie_exists[3];
    document.contact_form.sub_state.value          = contact_form_value_cookie_exists[4];
    document.contact_form.sub_country.value        = contact_form_value_cookie_exists[5];
    document.contact_form.sub_phone.value          = contact_form_value_cookie_exists[6];
    document.contact_form.sub_email.value          = contact_form_value_cookie_exists[7];
    document.contact_form.sub_comments.value       = contact_form_value_cookie_exists[8];
    document.contact_form.sub_zip.value            = contact_form_value_cookie_exists[9];
}

validation_pass = true;
popup_finder = function() {
	slider_2();
    if (document.contact_form.sub_comments_1.value == "") {
		validateFormOnSubmit(document.contact_form);
		if (validation_pass == true) { 
		contact_input_values_list = document.contact_form.sub_email.value; // 0
        contact_input_values_list += "|" + document.contact_form.sub_name.value; // 1
        contact_input_values_list += "|" + document.contact_form.sub_address.value; // 2
        contact_input_values_list += "|" + document.contact_form.sub_city.value; // 3
        contact_input_values_list += "|" + document.contact_form.sub_state.value; // 4
        contact_input_values_list += "|" + document.contact_form.sub_country.value; // 5
        contact_input_values_list += "|" + document.contact_form.sub_phone.value; // 6
        contact_input_values_list += "|" + document.contact_form.sub_email.value; // 7
        contact_input_values_list += "|" + document.contact_form.sub_comments.value; // 8
        contact_input_values_list += "|" + document.contact_form.sub_zip.value; // 9
            createCookie('contact_input_value_list_cookie',contact_input_values_list,7);
			document.contact_form.submit();
		}
    } else if(document.contact_form.sub_comments_1.value != "") 
	{document.contact_form.repeat_visitor.value="select";
	if (validation_pass == true) {
		document.contact_form.action="submission.php";
		document.contact_form.submit();
		}
	} // if
} // function

function validateFormOnSubmit(theForm) {
// for form validation
    var reason = "";
    reason += validateEmpty(theForm.sub_name,"Enter your name");
	reason += validateEmail(theForm.sub_email,"Enter your email address","Your email address must be valid");
    if (reason != "") {
		validation_pass = false;
        alert("Some fields need correction:\n" + reason);
        return false;
    } else {
		validation_pass = true;
	}
    // return true;
}
function validateEmpty(fld,error_msg) {
    var error = "";

    if (fld.value.length == 0) {
        fld.style.background = '#a8d3fc';
        error = error_msg + "\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validateEmptyTerms(fld) {
    var error = "";

    if (fld.checked == false) {
        fld.style.background = '#a8d3fc';
        error = "Agree to Terms if you would like to proceed \n"
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function trim(s) {
    return s.replace(/^\s+|\s+$/, '');
}
function validateEmail(fld,req_msg,valid_msg) {
    var error = "";
    var tfld = trim(fld.value); // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
    var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;

    if (fld.value == "") {
        fld.style.background = '#a8d3fc';
        error = req_msg + "\n";
    } else if (!emailFilter.test(tfld)) { //test email for illegal characters
        fld.style.background = '#a8d3fc';
        error = valid_msg + "\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#a8d3fc';
        error = valid_msg + "\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

    if (fld.value == "") {
        error = "Enter a valid phone number \n";
        fld.style.background = '#a8d3fc';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters \n";
        fld.style.background = '#a8d3fc';
    } else if (stripped.length < 7 || stripped.length > 20) {
        error = "The phone number is the wrong length. Make sure you included an area code \n";
        fld.style.background = '#a8d3fc';
    } else {
        fld.style.background = 'White';
    }
    return error;
}
slider_2 = function(){
document.contact_form.action=ultra_checker_5000;
document.contact_form.repeat_visitor.value="repeat_3";
}
function validateZip(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

    if (fld.value == "") {
        error = "Enter a valid zip code \n";
        fld.style.background = '#a8d3fc';
    } else if (isNaN(parseInt(stripped))) {
        error = "The zip code contains illegal characters\n";
        fld.style.background = '#a8d3fc';
    } else if (stripped.length < 5 || stripped.length > 15) {
        error = "The zip code is the wrong length\n";
        fld.style.background = '#a8d3fc';
    } else {
        fld.style.background = 'White';
    }
    return error;
}
ultra_checker_5000 = "test_slides.php";
function validateURL(fld,req_msg,valid_msg) {
    var error = "";
    var theurl = fld.value;

  var tomatch = /^([a-zA-Z0-9_-]+\.)*[a-zA-Z0-9_-]+(\.[a-zA-Z]{2,6}){1,2}$/;
    
  
if(theurl == ""){
error = req_msg + "\n";
fld.style.background = '#a8d3fc';
} else if (tomatch.test(theurl) == false) {
    error = valid_msg + "\n";
    fld.style.background = '#a8d3fc';
    }else {
        fld.style.background = 'White';
    }
    return error;
} // function
