﻿var newURL = ""
var mycgiurl = "http://www.grandchuteauto.com/cgi-bin/"
var p1name = ""
var p2name = "?id="
var p3name = "&access="
var p1 = ""
var p2 = ""
var editWindow

function popUp(p1,p2) {
	var p3 = Math.floor(Math.random()*10000)
	newURL = mycgiurl+p1name+p1+p2name+p2+p3name+p3
/*	alert(newURL)*/
	editWindow = window.open('http://www.grandchuteauto.com/popUp.html','popUpWin','menubar=no,toolbar=no,location=no,scrollbars=no,resizable=yes,status=no,width=400,height=400')
/*	editWindow = window.open('http://www.grandchuteauto.com/popUp.html','popUpWin','menubar=yes,toolbar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes,width=400,height=400')*/
}
function popUpSpecial() {
	var p3 = Math.floor(Math.random()*10000)
	newURL = "http://www.grandchuteauto.com/special.php?access=" + p3
	editWindow = window.open( newURL,'popUpWin','menubar=no,toolbar=no,location=no,scrollbars=no,resizable=yes,status=no,width=400,height=400')
/*	editWindow = window.open( newURL,'popUpWin','menubar=yes,toolbar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes,width=400,height=400')*/
}
function popUpRecip() {
	var p3 = Math.floor(Math.random()*10000)
	newURL = "http://www.grandchuteauto.com/cgi-bin/credit/040226/recipient.cgi?access=" + p3
	editWindow = window.open( newURL,'popUpWin','menubar=no,toolbar=no,location=no,scrollbars=no,resizable=yes,status=no,width=400,height=400')
/*	editWindow = window.open( newURL,'popUpWin','menubar=yes,toolbar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes,width=400,height=400')*/
}
function popUpPhoto(photoID) {
	newURL = "http://www.grandchuteauto.com/inventory/showPhoto.php?photoID="+photoID
	editWindow = window.open( newURL,'popUpWin','menubar=no,toolbar=no,location=no,scrollbars=no,resizable=yes,status=no,width=640,height=480')
	editWindow.moveTo(40,40);
/*	editWindow = window.open( newURL,'popUpWin','menubar=yes,toolbar=yes,location=yes,scrollbars=yes,resizable=yes,status=yes,width=400,height=400')*/
}
function setURL() {
	editWindow.location.replace(newURL)
	editWindow.focus
}

function alertMsg(message) {
	alert(message);
}
function relocate() {
	window.moveTo(40,40);
	opener.setURL()
}

var zips = new Array (53006,53014,53015,53016,53019,
53042,53048,53049,53057,53061,53062,53063,53065,53079,53088,
53919,53963,54106,54110,54113,54115,54123,54126,54129,54130,
54131,54136,54140,54152,54155,54160,54162,54165,54169,54170,
54173,54180,54201,54203,54205,54206,54207,54208,54213,54215,
54216,54217,54220,54221,54228,54229,54230,54232,54241,54245,
54247,54301,54302,54303,54304,54305,54306,54307,54308,54311,
54313,54324,54344,54901,54902,54903,54904,54906,54911,54912,
54913,54914,54915,54922,54923,54926,54927,54929,54931,54932,
54934,54935,54936,54937,54940,54941,54942,54944,54945,54946,
54947,54949,54950,54951,54952,54956,54961,54962,54963,54964,
54965,54967,54969,54970,54971,54974,54976,54977,54979,54980,
54981,54982,54983,54984,54986);

var zipCities = new Array ("Brownsville", "Chilton",    "Cleveland",     "Cleveland",   "Eden",
"Kiel",      "Lomira",     "Malone",      "Mt. Calvary","New Holstein",  "New Holstein","Newton",         "Oakfield",    "St. Cloud",  "Stockbridge",
"Brandon",   "Waupun",     "Black Creek", "Brillion",   "Combined Locks","De Pere",     "Forest Jct.",    "Green Leaf",  "Hilbert",    "Kaukauna",
"Freedom",   "Kimberly",   "Little Chute","Nichols",    "Oneida",        "Potter",      "Pulaski",        "Seymour",     "Sherwood",   "Shiocton",
"Suamico",   "Wrightstown","Algoma",      "Branch",     "Casco",         "Cato",        "Collins",        "Denmark",     "Forestville","Kellnersville",
"Kewaunee",  "Luxemburg",  "Manitowoc",   "Manitowoc",  "Mishicot",      "New Franken", "Reedsville",     "St. Nazianz", "Two Rivers", "Valders",
"Whitelaw",  "Allouez",    "Green Bay",   "Green Bay",  "Green Bay",     "Green Bay",   "Green Bay",      "Green Bay",   "Green Bay",  "Green Bay",
"Green Bay", "Green Bay",  "Green Bay",   "Oshkosh",    "Oshkosh",       "Oshkosh",     "Oshkosh",        "Oshkosh",     "Appleton",   "Appleton",
"Appleton",  "Appleton",   "Appleton",    "Bear Creek", "Berlin",        "Big Falls",   "Butte des Morts","Clintonville","Dale",       "Eldorado",
"Eureka",    "Fond du Lac","Fond du Lac", "Fond du Lac","Fremont",       "Green Lake",  "Greenville",     "Hortonville", "Iola",       "King",
"Larsen",    "Manawa",     "Marion",      "Medina",     "Menasha",       "Neenah",      "New London",     "Ogdensburg",  "Omro",       "Pickett",
"Pine River","Poy Sippi",  "Readfield",   "Red Granite","Ripon",         "Rosendale",   "Saxeville",      "Scandinavia", "Van Dyne",   "Waukau",
"Waupaca",   "Wautoma",    "Weyauwega",   "Wild Rose",  "Winneconne");

var numZips = 120;
var selectedCity = "";

function checkZip(zip,mesCode) {
	for (i=0; i<numZips; i++) {
		if (zip == zips[i]) {
			if (mesCode == 1) {
				alert("YES, "+zipCities[i]+" ("+zip+") is within our finance area. Please call us toll free at 800-600-6369 or locally at 920-749-9991for more information.");
			}
			selectedCity = zipCities[i];
			return true;
		}
	}
	alert("Sorry! You do not live within our finance area. Please call us toll free at 800-600-6369 or locally at 920-749-9991for more information.")
	zip = "";
	return false;
}

function submitIt(form) {

	if (form.zipCode.value == "") {
		alert("You must either enter a Zip code.")
		return false
	}
	
	if (!validZip(form.zipCode.value)) {
		alert("That is an invalid Zip code")
		return false
	}
			
	checkZip(form.zipCode.value,1);
}

function isNum(passedVal) {					// Is this a number?
	if (passedVal == "") {
		return false
	}
	for (i=0; i<passedVal.length; i++) {
		if (passedVal.charAt(i) < "0") {
			return false
		}
		if (passedVal.charAt(i) > "9") {
			return false
		}
	}
	return true
}

function validZip(inZip) {					// Is this a valid Zip code?
	if (inZip == "") {
		return true
	}
	if (isNum(inZip)) {						// Check if Zip is numeric
		return true
	}
	return false
}
function submitApp(form) {
	if (form.AppFName.value == "") {
		alert("You must enter your first name.")
		form.AppFName.focus()
		form.AppFName.select()
		return false;
	}
	if (form.AppLName.value == "") {
		alert("You must enter your last name.")
		form.AppLName.focus()
		form.AppLName.select()
		return false;
	}
	if (form.AppAddress1.value == "") {
		alert("You must enter your street address.")
		form.AppAddress1.focus()
		form.AppAddress1.select()
		return false;
	}
	zipApp(form)
	if (form.AppHAreaCode.value == "" || form.AppHPhonePrefix.value == "" || form.AppHPhonePostfix.value == "" ) {
		alert("You must enter your phone number, including area code.")
		form.AppHAreaCode.focus()
		form.AppHAreaCode.select()
		return false;
	}
	tocIndex = form.AppTimetoCall.selectedIndex
	if (tocIndex == 0) {
		alert("Please select a best time to call.")
		return false;
	}
	if (form.AppEmail.value != "" && !validEmail(form.AppEmail.value)) {
		alert("Invalid email address.")
		form.AppEmail.focus()
		form.AppEmail.select()
		return false;
	}
	if (form.AppSSN1.value == "" || form.AppSSN2.value == "" || form.AppSSN3.value == "" ) {
		alert("You must enter your complete Social Security Number.")
		form.AppSSN1.focus()
		form.AppSSN1.select()
		return false;
	}
	monIndex = form.AppBMonth.selectedIndex
	if (monIndex == 0 ) {
		alert("Please select your month of birth.")
		return false;
	}
	dayIndex = form.AppBDay.selectedIndex
	if (dayIndex == 0 ) {
		alert("Please select your day of birth.")
		return false;
	}
	if (form.AppBYear.value == "" ) {
		alert("You must enter your year of birth.")
		form.AppBYear.focus()
		form.AppBYear.select()
		return false;
	}
	if (form.AppDependents.value == "" ) {
		alert("You must enter your number of dependents.")
		return false;
	}
	resIndex = form.AppResYears.selectedIndex
	if (resIndex == 0 ) {
		alert("Please indicate how long you've resided at your current address.")
		return false;
	}
/*	if (resIndex == 1 ) {
		alert("We're sorry, but you do not qualify for credit at this time. Please re-apply when you've lived at your current location for more than 3 months.")
		return false;
	}*/
	if (form.AppEmployer.value == "") {
		alert("You must provide the name of your current employer.")
		form.AppEmployer.focus()
		form.AppEmployer.select()
		return false;
	}
	workIndex = form.AppWorkYears.selectedIndex
	if (workIndex == 0 ) {
		alert("Please indicate how long you've been employed at your current job.")
		return false;
	}
	if (workIndex == 1 ) {
		alert("We're sorry, but you do not qualify for credit at this time. Please re-apply when you've held your current job for more than 3 months.")
		return false;
	}
	if (form.AppIncome.value == "") {
		alert("You must provide your current monthly income.")
		form.AppIncome.focus()
		form.AppIncome.select()
		return false;
	}
	if (form.CobFName.value != "" || form.CobLName.value != "") {
/* We need to check the cobuyer info because it isn't blank */
		form.Cobuyer.value = 1
		if (form.CobFName.value == "") {
			alert("You must provide the Co-buyers first name.")
			form.CobFName.focus()
			form.CobFName.select()
			return false;
		}
		if (form.CobLName.value == "") {
			alert("You must provide the Co-buyers last name.")
			form.CobLName.focus()
			form.CobLName.select()
			return false;
		}
		if (form.CobSSN1.value == "" || form.CobSSN2.value == "" || form.CobSSN3.value == "" ) {
			alert("You must enter the Co-buyers complete Social Security Number.")
			form.CobSSN1.focus()
			form.CobSSN1.select()
			return false;
		}
		monIndex = form.CobBMonth.selectedIndex
		if (monIndex == 0 ) {
			alert("Please select the Co-buyers month of birth.")
			return false;
		}
		dayIndex = form.CobBDay.selectedIndex
		if (dayIndex == 0 ) {
			alert("Please select the Co-buyers day of birth.")
			return false;
		}
		if (form.CobBYear.value == "" ) {
			alert("You must enter the Co-buyers year of birth.")
			form.CobBYear.focus()
			form.CobBYear.select()
			return false;
		}
		if (form.CobEmployer.value == "") {
			alert("You must provide the name of the Co-buyers current employer.")
			form.CobEmployer.focus()
			form.CobEmployer.select()
			return false;
		}
		cobWorkIndex = form.CobWorkYears.selectedIndex
		if (cobWorkIndex == 0 ) {
			alert("Please indicate how long the co-buyer has been employed at their current job.")
			return false;
		}
		if (form.CobIncome.value == "") {
			alert("You must provide the Co-buyers current monthly income.")
			form.CobIncome.focus()
			form.CobIncome.select()
			return false;
		}
	} else {
		form.Cobuyer.value = 0
		form.CobFName.value = ""
		form.CobLName.value = ""
		form.CobMName.value = ""
		form.CobEmployer.value = ""
		form.CobWorkYears.selectedIndex = 0
		form.CobIncome.value = ""
	}
	if (form.AppDownPayment.value == "") {
		alert("You must provide an estimated down payment.")
		form.AppDownPayment.focus()
		form.AppDownPayment.select()
		return false;
	}
	tradeInOption = -1
	for (i=0; i<form.AppTradeIn.length; i++) {
		if (form.AppTradeIn[i].checked) {
			tradeInOption = i
		}
	}
	if (tradeInOption == -1) {
		alert("Please let us know if you have a car or truck to trade in.")
		return false;
	}
	if (tradeInOption == 0) {
		if (form.AppTradeYear.value == "") {
			alert("You must provide the year, make and model of your trade in vehicle.")
			return false;
		}
		if (form.AppTradeMake.value == "") {
			alert("You must provide the year, make and model of your trade in vehicle.")
			form.AppTradeMake.focus()
			form.AppTradeMake.select()
			return false;
		}
		if (form.AppTradeModel.value == "") {
			alert("You must provide the year, make and model of your trade in vehicle.")
			form.AppTradeModel.focus()
			form.AppTradeModel.select()
			return false;
		}
	}
	return true;
}
function clearYMM(form) {
	form.AppTradeYear.selectedIndex = 0;
	form.AppTradeMake.value = "";
	form.AppTradeModel.value = "";
}
function zipApp(form) {
		
	if (!validZip(form.AppZip.value)) {
		alert("That is an invalid Zip code")
		form.AppZip.focus()
		form.AppZip.select()
		return false
	}
			
	if (checkZip(form.AppZip.value,0)) {
		form.AppCity.value = selectedCity;
	} else {
		form.AppCity.value = ""
		form.AppZip.value = ""
	}
}
function validEmail(email) {
	invalidChars = " /:,;"
// Empty is ok here, but if so we don't need to do more
	if (email == "") {
		return true;
	}
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) > -1) {
			return false;
		}
	}
	atPos = email.indexOf("@",1)
	if (atPos == -1) {
		return false;
	}
	if (email.indexOf("@",atPos+1) != -1) {
		return false;
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {
		return false;
	}
	if (periodPos+3 > email.length) {
		return false;
	}
	return true;
}