var currentExt = "ext2";
var currentInt = "int1";
var shimUrl = "/dyv3/dyv3/common/empty.jsp";

//Lineup Script
function showContent(newContent, defaultContent) {
	document.getElementById(defaultContent).style.display='none';
	document.getElementById(newContent).style.display='block';
}
function hideContent(newContent, defaultContent) {
	document.getElementById(defaultContent).style.display='block';
	document.getElementById(newContent).style.display='none';
}

//Models Script
function selectModel(id)
{
	document.location.href = "/dyv3/config/models?selectPart=" + id + "&selectMode=select&OmnitureFlag=true";
}

//Styling Script
function selectPart(id)
{
	var scrollPos = 350;
	document.location.href = "/dyv3/config/styling?selectPart=" + id + "&selectMode=select&OmnitureFlag=true" + "&scrollPos=" + scrollPos;
}

function openTirePopup(){
	document.getElementById('informationPopup').style.display="block";
}
function closeTirePopup(){
	document.getElementById('informationPopup').style.display="none";
}

function changeView(type, view,image) {
	if (type == "exterior") {
		document.getElementById('largeExterior').className=view;
		document.largeExt.src = image;
		currentExt = view;
	}
	if (type == "interior") {
		document.getElementById('largeInterior').className=view;
		document.largeInt.src = image;
		currentInt = view;
	}
}

function revert(flag,exteriorOn,wheelOn,interiorOn,image1,image2, image3){
	if (flag == "ext"){
		if(currentExt == "ext1")
			document.largeExt.src =image1;
		if(currentExt == "ext2")
			document.largeExt.src =image2;
		if(currentExt == "ext3")
			document.largeExt.src =image3;
	}
	if(flag == "int"){
		if(currentInt == "int1")
			document.largeInt.src = image1;
		if(currentInt == "int2")
			document.largeInt.src = image2;
		if(currentInt == "int3")
			document.largeInt.src = image3;
	}
	
	var lInitiator = document.getElementById("styleSelected").getElementsByTagName("li");
	for (var i=0; i<lInitiator.length; i++) {
		lInitiator[i].style.display = 'none';
	}
	document.getElementById("ext" + exteriorOn).style.display = 'block';
	document.getElementById("int" + interiorOn).style.display = 'block';
	document.getElementById("wheeld" + wheelOn).style.display = 'block';

}


function extPreview (extOn,wheelOn,intOn,image1,image2,image3) {

	var lInitiator = document.getElementById("styleSelected").getElementsByTagName("li");
	for (var i=0; i<lInitiator.length; i++) {
		lInitiator[i].style.display = 'none';
	}
	
	if(currentExt == "ext1")
		document.largeExt.src =image1;
	if(currentExt == "ext2")
		document.largeExt.src =image2;
	if(currentExt == "ext3")
		document.largeExt.src =image3;

	document.getElementById("ext" + extOn).style.display = 'block';
	document.getElementById("int" + intOn).style.display = 'block';
	document.getElementById("wheeld" + wheelOn).style.display = 'block';	

}

function intPreview (extOn,wheelOn,intOn,image1,image2,image3) {
	var lInitiator = document.getElementById("styleSelected").getElementsByTagName("li");
	for (var i=0; i<lInitiator.length; i++) {
		lInitiator[i].style.display = 'none';
	}
	if(currentInt == "int1")
		document.largeInt.src = image1;
	if(currentInt == "int2")
		document.largeInt.src = image2;
	if(currentInt == "int3")
		document.largeInt.src = image3;

	document.getElementById("int" + intOn).style.display = 'block';
	document.getElementById("ext" + extOn).style.display = 'block';
	document.getElementById("wheeld" + wheelOn).style.display = 'block';
}
//notalbes
function onCancelNotable(page){
    window.onunload=null;
	document.location.href = "/dyv3/config/" + page + "?rejectNotable=reject" ;
}
function hideNotablePopup(){
    window.onunload=null;
	document.getElementById('notablePopup').style.visibility="hidden";
	document.getElementById('alertCover').style.visibility="hidden";		
}	
	
//Options script
function toggleDetail(detail) {
    window.onunload=null;
	var status;
	status = (document.getElementById(detail).className);
	if (status == 'detailedContentShow') {
		document.getElementById(detail).className='detailedContentHidden';
	}
	else if (status == 'detailedContentHidden') {
		document.getElementById(detail).className='detailedContentShow';
	}
}
function selectOptionsPart(id,selectMode,e){
    window.onunload=null;	
    var scrollPos = getMouseY(e) - 300;
    document.location.href = "/dyv3/config/options?selectPart=" + id + "&selectMode=" + selectMode +"&scrollPos=" + scrollPos+"&OmnitureFlag=true";
}
function openLargeImgPopup(image,desc,e){
    window.onunload=null;
	document.getElementById("image").src = image;
	document.getElementById("popupDesc").firstChild.data = desc;
	var topPos = getMouseY(e) -200;
	document.getElementById("largepopup").style.top=topPos + "px";
	//smartPositionElement('largepopup', e);
	document.getElementById("largepopup").style.display = "block";
}
function closeLargeImgPopup(){
    window.onunload=null;
	document.getElementById("largepopup").style.display = "none";
}

//Accessories Script
function selectAccessory(id,selectMode,e){
    window.onunload=null;
 	var scrollPos = getMouseY(e) - 300;
	document.location.href = "/dyv3/config/accessories?selectPart=" + id + "&selectMode=" + selectMode +"&scrollPos="+ scrollPos+"&OmnitureFlag=true";	
 	
}

//Summary Script
function goPrint(){
	window.open("/dyv3/config/summary?print=true");		
}
function openEmailPopup(top){	
	document.getElementById("alertCover").style.display="block";
	document.getElementById("emailPopup").style.display="block";
	document.getElementById("emailFrame").style.display="block";	
	if (top)	document.getElementById("emailPopup").style.top=top+"px";	
}
function closeEmailPopup(){
	document.getElementById("emailFrame").style.display="none";
	document.getElementById("emailPopup").style.display="none";
	document.getElementById("alertCover").style.display="none";
	s.prop18=""
	s.eVar28 = ""
	var s_code=s.t();if(s_code)document.write(s_code)
}

//Tally
function popupAdjustable(url, width, height) {
	msgbox = window.open( url,"msgbox_00","scrollbars=1,width="+width+",height="+height+",menubar=0,scrollbars=yes,status=0,titlebar=0,toolbar=0,resizable=1");
}
function openTestDrivePopupSelectFix() {
	ifrm = document.createElement("IFRAME");
	ifrm.setAttribute('id', 'shimTestDrivePopup');
	ifrm.setAttribute('class', 'shimTestDrivePopup');
	ifrm.setAttribute('src', shimUrl);
	ifrm.setAttribute('scrolling', 'no');
	ifrm.setAttribute('frameborder', '0');
	document.body.appendChild(ifrm);
	shimIframe = document.getElementById('shimTestDrivePopup');
	shimmedPopup = document.getElementById('testDrivePopup');
	shimIframe.style.height = shimmedPopup.offsetHeight;
}
function closeTestDrivePopupSelectFix() {
	shimIframe = document.getElementById('shimTestDrivePopup');
	shimIframe.parentNode.removeChild(shimIframe);
	s.prop18=""
	s.eVar28 = ""
	var s_code=s.t();if(s_code)document.write(s_code)
}
function openTestDrivePopup(){	
	
	document.getElementById("alertCover").style.display="block";
	document.getElementById("testDrivePopup").style.display="block";
	document.getElementById("testdriveFrame").style.display="block";
	if (window.attachEvent) { 
		openTestDrivePopupSelectFix();
	}
}
function closeTestDrivePopup(){
	if (window.attachEvent) { 
		closeTestDrivePopupSelectFix();
	}
	document.getElementById("testdriveFrame").style.display="none";
	document.getElementById("testDrivePopup").style.display="none";
	document.getElementById("alertCover").style.display="none";	
}

function openDealerQuotePopup(dealerCode) {
	document.getElementById("alertCover").style.display="block";
	document.getElementById("dealerQuotePopup").style.display="block";	
	
	var oldsrc = document.getElementById("dealerQuoteFrame").src;	
	var index = oldsrc.indexOf("&selectedDealer=");	
	if (index > 0) oldsrc = oldsrc.substring(0, index);	
	var newsrc = oldsrc + "&selectedDealer=" + dealerCode;	
	document.getElementById("dealerQuoteFrame").src = newsrc; 
	
	
	document.getElementById("dealerQuoteFrame").style.display="block";
	
	if (window.attachEvent) { 
		openDealerQuotePopupSelectFix();
	} 
}


function openDealerQuotePopupSelectFix() {
	ifrm = document.createElement("IFRAME");
	ifrm.setAttribute('id', 'shimDealerQuotePopup');
	ifrm.setAttribute('class', 'shimDealerQuotePopup');
	ifrm.setAttribute('src', shimUrl);
	ifrm.setAttribute('scrolling', 'no');
	ifrm.setAttribute('frameborder', '0');
	document.body.appendChild(ifrm);
	shimIframe = document.getElementById('shimDealerQuotePopup');
	shimmedPopup = document.getElementById('dealerQuotePopup');
	shimIframe.style.height = shimmedPopup.offsetHeight;
}

function closeDealerQuotePopup(){
	/*if (window.attachEvent) { 
		closeDealerQuotePopupSelectFix();
	}*/
	//alert(parent.s.pageName);
	document.getElementById("dealerQuoteFrame").style.display="none";
	document.getElementById("dealerQuotePopup").style.display="none";
	document.getElementById("alertCover").style.display="none";	
}
function intelliPricePopup(){
	window.open("http://www.intelliprice.com/intellipricedealer/start.htm?dealerid=905004", "test","scrollbars=1,width=800,height=710,menubar=0,status=0,titlebar=0,toolbar=1,resizable=1");		
}

function volvoFinancePopup(appType,zipcode)
{
	if(zipcode == null || zipcode.length == 0){
		document.forms['zipcodeForm'].appType.value = appType;
		if(appType == "CA"){
			document.getElementById("zipcodeHeading").firstChild.data="Apply for Credit";
			document.getElementById("label").firstChild.data= "apply for credit:";
		}else{
			document.getElementById("zipcodeHeading").firstChild.data="Estimate Payments";
			document.getElementById("label").firstChild.data = "estimate payment:";
		}
		document.getElementById("alertCover").style.display="block";
		document.getElementById("zipcodePopup").style.display="block";
	}
	else{
		window.open("/dyv3/config/paymentEstimate?appType=" + appType, "PaymentEstimate","scrollbars=1,width=800,height=710,menubar=0,status=0,titlebar=0,toolbar=1,resizable=1");
	}
}
function openDealerZipCodePopup (zipcode, noThanks) {
	if((noThanks == null || noThanks != "noThanks") && (zipcode == null || zipcode.length == 0 )){
		document.getElementById("alertCover").style.display="block";
		document.getElementById("dealerZipcodePopup").style.display="block";
	}
}
function openZipcodePopup(){
	document.getElementById("alertCover").style.display="block";
	document.getElementById("zipcodePopup").style.display="block";
}
function submitZipcode(){
	var zipcode = document.forms['zipcodeForm'].zipcode.value;
	var flag = validateZIP(zipcode);
	if(flag == true){
		var appType = document.forms['zipcodeForm'].appType.value;
		document.getElementById("alertCover").style.display="none";
		document.getElementById("zipcodePopup").style.display="none";
		window.open("/dyv3/config/paymentEstimate?zipcode="+zipcode+"&appType=" + appType, "PaymentEstimate","scrollbars=1,width=800,height=710,menubar=0,status=0,titlebar=0,toolbar=1,resizable=1");
	}
}
/*
function submitDealerZipcode() {
	
	var form = document.getElementById("zipcodeFormId");
	form.submit();
	
}
function submitDealerName() {
	
	var form = document.getElementById("dealerNameFormId");
	form.submit();
	
}
*/
function validateZIP(field) {
	var valid = "0123456789";
	
	if (field.length!=5) {
		return false;
	}
	for (var i=0; i < field.length; i++) {
		temp = "" + field.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			return false;
		}
	}
	return true;
}
function validateDlrName(field) {
	return true;
}

function closeZipcodePopup(){
	document.getElementById("zipcodePopup").style.display="none";
	document.getElementById("alertCover").style.display="none";
}
function closeDealerZipcodePopup(){
	document.getElementById("dealerZipcodePopup").style.display="none";
	document.getElementById("alertCover").style.display="none";
}
function toggleTabs(tab) {
	document.getElementById('tallySection').className=tab;
}

function opentrickitoutPopup(){
	document.getElementById('alertCover').style.display="block";
	document.getElementById('trickitoutPopup').style.display="block";
}

function closetrickitoutPopup(){
	document.getElementById('trickitoutPopup').style.display="none";
	document.getElementById('alertCover').style.display="none";
}

//Common

//for positioning floatables
function smartPositionElement(elementName, e)
{
    var obj = getElementRef(elementName)
    var objHeight = 200; //how to get it dynamically?
    var objWidth = 310; //how to get it dynamically?
    var winHeight = getWindowHeight();
    var winWidth = getWindowWidth();
    var scrollX = getScrollPosX();
    var scrollY = getScrollPosY();
    var winTop = scrollY;
    var winBottom = winTop + winHeight;
    var winLeft = scrollX;
    var winRight = winLeft + winWidth;
    var mouseX = getMouseX(e);
    var mouseY = getMouseY(e);
    
    //the top-left corner will be positioned at the
    //current mouse location, the object will not
    //fit in the window
    var objLeft = mouseX;
    var objTop = mouseY;
    
    //if the object will extend past the right of the
    //window, anchor to the right of the object, not the left
    if ((mouseX + objWidth) > winRight)
    {
    	objLeft = mouseX - objWidth;
    	if (objLeft < winLeft) objLeft = winLeft;
    }
    
    //if the object will extend blow the bottom of the
    //current window, anchor to the bottom of the obj, not the top
    if ((mouseY + objHeight) > winBottom)
    {
    	objTop = mouseY - objHeight;
    	if (objTop < winTop) objTop = winTop;
    }
    
    //position the object
    obj.style.left = objLeft + "px";
    obj.style.top = objTop + "px";
    
    
    //debug info
    alert("winHeight: " + winHeight + "\n"
    	+ "winWidth: " + winWidth + "\n"
    	+ "winTop: " + winTop + "\n"
    	+ "winBottom: " + winBottom + "\n"
    	+ "winLeft: " + winLeft + "\n"
    	+ "winRight: " + winRight + "\n"
    	+ "mouseY: " + mouseY + "\n"
    	+ "mouseX: " + mouseX + "\n"
    	+ "objTop: " + objTop + "\n"
    	+ "objLeft: " + objLeft);
    
}

// Get the mouse X position for IE and non-IE browsers
function getMouseX(e) {
  if (document.all?true:false) {
    var tempX = event.clientX + document.body.scrollLeft;
  } else {
    var tempX = e.pageX;
  }
  if (tempX < 0){tempX = 0;}
  return tempX;
  
}

// Get the mouse Y position for IE and non-IE browsers
function getMouseY(e) {
  if (document.all?true:false) {
    var tempY = e.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) ;
   } else {
    var tempY = e.pageY;
  }
  if (tempY < 0) { tempY = 0;}
  return tempY;
}


function getWindowHeight() {
	if (window.innerHeight != null)
		return window.innerHeight;
	if (document.body.clientHeight != null)
		return document.body.clientHeight;
	return(null);
}

function getWindowWidth() {
	if (window.innerWidth != null)
		return window.innerWidth;
	if (document.body.clientWidth != null)
		return document.body.clientWidth;
	return(null);
}

function getScrollPosY() {
	if (document.layers) { // or ns6
		return window.pageYOffset;
	} else {
		return self.document.body.scrollTop;
	}
}

function getScrollPosX() {
	if (document.layers) { // or ns6
		return window.pageXOffset;
	} else {
		return self.document.body.scrollLeft;
	}
}

function getElementRef(id) {
    if (document.all) {
        return document.all[id];
    }
    if (document.getElementById) {
        return document.getElementById(id);
    }
    return null;
}
function disableUnload() {
	return null;
}
