﻿var __largeImageDisplayWindow = null;

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g, '');
}

Date.prototype.addMilliseconds = function(milliseconds)
{
	this.setTime(
		new Date(this.getTime() + milliseconds).getTime()
	);
}

Date.prototype.addSeconds = function(seconds)
{
	this.addMilliseconds(seconds * 1000);
}

Date.prototype.addMinutes = function(minutes)
{
	this.addSeconds(minutes * 60);
}

Date.prototype.addHours = function(hours)
{
	this.addMinutes(hours * 60);
}

Date.prototype.addDays = function(days)
{
	this.addHours(days * 24);
}

var __popupWindows = new Array();

function openChromelessPopupWindow(url, width, height)
{
    var popupWindow = __popupWindows[url];
    if (popupWindow != null) {
        if (popupWindow.closed) {
           popupWindow = null;
        }
    }
    if (popupWindow == null) {
        popupWindow = window.open(url, '', 'scrollbars=no,width=' + width + ',height=' + height + ',status=no,toolbar=no,location=no', true);
        __popupWindows[url] = popupWindow;
    }
    if (window.focus) {
        popupWindow.focus();
    }
}

var __conciergeServiceWindow = null;

function openConciergeServiceWindow()
{
	if (__conciergeServiceWindow != null) {
		if (__conciergeServiceWindow.closed) {
			__conciergeServiceWindow = null;
		}
	}
	if (__conciergeServiceWindow == null) {
		__conciergeServiceWindow = window.open('ConciergeProcess.aspx', '', 'scrollbars=no,height=425,width=550,status=no,toolbar=no,location=no', replace = true);
	}
	if (window.focus) {
		__conciergeServiceWindow.focus();
	}
}

function enlargeImage(url, width, height)
{
	if (__largeImageDisplayWindow != null) {
		if (__largeImageDisplayWindow.closed) {
			__largeImageDisplayWindow = null;
		}
	}
	if (__largeImageDisplayWindow == null) {
		__largeImageDisplayWindow = window.open(url, 'LargeImageDisplayWindow', "width=" + width + ",height=" + height);
	} else {
		__largeImageDisplayWindow.location.href = url;
	}
	if (window.focus) {
		__largeImageDisplayWindow.focus();
	}
}

var vehicleOptionRows = new Array();
var vehicleOptions = new Array();

function showAllOptionRows(controllerId)
{
	var sibling = document.getElementById(controllerId);
	var parent  = document.getElementById("vehicleDetailsBody");

	for (var i = 0; i < vehicleOptionRows.length; i++) {
		parent.insertBefore(
			document.getElementById(vehicleOptionRows[i]),
			sibling);
	}

	parent.removeChild(sibling);
}

function showAllOptionRowsLanding(controllerId)
{
	var unorderedList = document.getElementById(controllerId);

	for (var i = 0; i < vehicleOptions.length; i++) {
        var itemNode = document.createElement("li");
        
        itemNode.appendChild(document.createTextNode(vehicleOptions[i]));

    	unorderedList.appendChild(itemNode);
	}

	unorderedList.parentNode.removeChild(document.getElementById('seeAllOptionsLinkDiv'));
}

function clearOnFocus(field, defaultValue)
{
	if (field.value == defaultValue) {
		field.value = "";
	}
}

function restoreOnBlur(field, defaultValue)
{
	if (field.value == '') {
		field.value = defaultValue;
	}
}

function moneyFormat(textBoxRef)
{
	textBoxRef.value = textBoxRef.value.replace(/[^\.0-9]/g, '').replace(/\..*$/, '');
}

function ssnFormat(textBoxRef)
{
	var tempValue = textBoxRef.value;
	
	tempValue = tempValue.replace(/[^0-9]/g, '');
	
	if (tempValue.length == 9) {
		tempValue =
			tempValue.substring(0,3) + "-" +
			tempValue.substring(3,5) + "-" +
			tempValue.substring(5,9);
	}

	textBoxRef.value = tempValue;
}

function phoneFormat(textBoxRef)
{
	var tempValue = textBoxRef.value;
	
	tempValue = tempValue.replace(/[^0-9]/g, '');
	
	if (tempValue.length == 10) {
		tempValue =
			tempValue.substring(0,3) + "-" +
			tempValue.substring(3,6) + "-" +
			tempValue.substring(6,10);
	}

	textBoxRef.value = tempValue;
}

function enabledFlags(dropDownRef, controlArray, validatorArray, flagsArray)
{
	var flags = flagsArray[dropDownRef.selectedIndex];

	for (var i = 0; i < controlArray.length; i++) {
		var enabled = (flags[i] == 1);
		var control = document.getElementById(controlArray[i]);
		if (!enabled) {
			if (control.tagName.toLowerCase() == "input") {
				switch (control.type.toLowerCase()) {
					case "text":
					case "hidden":
						control.value = '';
						break;
					case "radio":
					case "checkbox":
						control.checked = false;
						break;
				}
			} else {
				if (control.tagName.toLowerCase() == "select") {
					control.selectedIndex = 0;
				}
			}
		}

		control.disabled = !enabled;

		for (var j = 0; j < validatorArray[i].length; j++) {
			ValidatorEnable(
			    document.getElementById(validatorArray[i][j]), enabled);
		}
	}
}

function checkDate(monthSelectRef, daySelectRef, yearSelectRef)
{
	var day   = daySelectRef.options[daySelectRef.selectedIndex].value;
	var month = monthSelectRef.options[monthSelectRef.selectedIndex].value - 1;
	var year  = yearSelectRef.options[yearSelectRef.selectedIndex].value;

	var dateObj = null;

	dateObj = new Date();
	dateObj.setFullYear(year, month, day);

	return (dateObj.getMonth() == month);
}

function __makeDate(monthRef, dayRef, yearRef)
{
	var month = monthRef.options[monthRef.selectedIndex].value - 1;
	var day   =   dayRef.options[dayRef.selectedIndex  ].value;
	var year  =  yearRef.options[yearRef.selectedIndex ].value;
	
	var date = null;
	
	date = new Date();
	date.setFullYear(year, month, day);
	date.setHours(0, 0, 0, 0);

    return (date.getMonth() == month ? date : null);
}

function __validateNonEmptyPositiveInteger(sender, args)
{
	args.IsValid = /^\d+$/.test(args.Value.trim());
}

function __validateDate(monthRef, dayRef, yearRef, args)
{
	args.IsValid = __makeDate(monthRef, dayRef, yearRef) == null ? false : true;
}

function __validateFutureDate(monthRef, dayRef, yearRef, dayOffset, args)
{
	var dateObject = __makeDate(monthRef, dayRef, yearRef);

	if (dateObject == null) {
		args.IsValid = false;
	} else {
		var compareDate = new Date();

		compareDate.addDays(dayOffset);

		if (dateObject >= compareDate) {
			args.IsValid = true;
		} else {
			args.IsValid = false;
		}
	}
}

function __validateZipCode(sender, args)
{
	if (args.Value.trim() == "Your ZIP") {
		args.IsValid = false;
	} else {
		args.IsValid = /^\d{5}$/.test(args.Value.trim());
	}
}

function __validateEmailAddress(sender, args)
{
	____validateEmailAddressInternal(sender, args, false);
}

function __validateNonEmptyEmailAddress(sender, args)
{
	____validateEmailAddressInternal(sender, args, true);
}

function ____validateEmailAddressInternal(sender, args, validateEmptyText)
{
	var content = args.Value.trim();
	
	if (content.length == 0) {
		args.IsValid = !validateEmptyText;
	} else {
		args.IsValid = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w{2,}$/.test(content);
	}
}

function __validateNonEmptyPhoneNumber(sender, args)
{
	____validatePhoneNumberInternal(sender, args, true);
}

function __validatePhoneNumber(sender, args)
{
	____validatePhoneNumberInternal(sender, args, false);
}

function ____validatePhoneNumberInternal(sender, args, validateEmptyText)
{
	var content = args.Value.trim();
	
	// We explicitly ignore empty content, since the required field validator
	// would pick up on this
	if (content.length == 0) {
		args.IsValid = !validateEmptyText;
	} else {
		content = content.replace(/[^0-9]/g, '');
		
		if (content.length == 10 || (content.length == 11 && content.charAt(0) == '1')) {
			args.IsValid = (/^1?[2-9][0-8][0-9][2-9][0-9]{6}$/.test(content));
		} else {
			args.IsValid = false;
		}
	}

	return;
}

function cleanVIN(str) {
    return str.replace(/[^A-Z0-9]+/ig, '');
}

var carfaxSharedPrefix = 'https://secure.carfax.com/creditCard.cfx?vin=';
var carfaxSharedSuffix = '&partnerCode=CCX'

function openCARFAX(vin, unlimited) {
    if (unlimited) {
        window.open(carfaxSharedPrefix + carfaxSharedSuffix);
    } else {
        if (vin)
            vin = cleanVIN(vin);
        else
            vin = '';

        window.open(carfaxSharedPrefix + (vin.length == 17 ? vin : '') + carfaxSharedSuffix);
    }
    return false;
}

var autoCheckSharedPrefix    = 'http://www.autocheck.com/consumers/history-reports/';
var autoCheckSharedSuffix    = '-buy-now.do?siteID=';
var autoCheckSharedVINSuffix = '&vin='

function openAutoCheck(vin, unlimited, sid) {
    if (!sid)
        sid = '1798';
    if (unlimited) {
        window.open(autoCheckSharedPrefix + 'unlimited' + autoCheckSharedSuffix + sid);
    } else {
        if (vin)
            vin = cleanVIN(vin);
        else
            vin = '';

        window.open(autoCheckSharedPrefix + 'single' + autoCheckSharedSuffix + sid + autoCheckSharedVINSuffix + (vin.length == 17 ? vin : ''));
    }
    return false;
}

function showDirectScreenshot() {
    openChromelessPopupWindow('/protect/Enlarged.html', 600, 325);
    return false;
}

function openWhatToExpect() {
    var link = document.getElementById('whatToExpect');
    
    if (link) {
        link.onclick = function() {
            openChromelessPopupWindow(this.href, 500, 550);
            return false;
        }
    }
}

var __pw = new Array();
function opu(u,w,h){var pw = __pw[u];if(pw!=null){if(pw.closed){pw = null;}}if(pw==null){pw=window.open(u,'','scrollbars=no,width='+w+',height='+h+',status=no,toolbar=no,location=no',true);__pw[u] = pw;}if(window.focus){pw.focus();}}

runOnLoad(openWhatToExpect);