﻿
function EmailResults(vin, zip)
{       document.getElementById("hdnData").value = GetHtml(vin, zip);
   
}

/////////////////////////////////////////////////////////////////

function GetHtml(vin, zip)
{
    var objHF = document.getElementById("DivHighFeatures");    
   // var objSF = document.getElementById("DivStandFeatures"); 
    
    //var objRV = document.getElementById("DivRelatedVehicles");
     style="font-weight: bold"
    var objDt = document.getElementById("DivCarDetail");

    var objImg = window.frames["ifImage"].document; //.forms["form1"];
    //var objRVL = window.frames["ifRelatedCars"].document.forms["form2"];
   // var objRVL = document.getElementById("DivRelatedCars");    
    
    
    var hf = '<div style="border: 1px none #000000; padding: 0px; background: #FFFFFF;">'+
            '<table border="0" cellspacing="0" cellpadding="2" width="100%" style="background: #CCCCCC;'+
            '    color: #000000;">'+
            '    <tr>'+
            '        <td width="4%" bgcolor="#FFCC00">'+
            '            <div align="center" class="style24">'+
            '                <strong>[<a title="show/hide" id="exp1176132896_link" href="javascript: void(0);"'+
            '                     style="text-decoration: none; color: #000000;">-</a>]</strong></div>'+
            '        </td>'+
            '        <td width="92%">'+
            '            <span class="style20"><strong>HIGHLIGHTED FEATURES</strong></span></td>'+
            '        <td width="4%" align="right">'+
            '            &nbsp;</td>'+
            '    </tr>'+
            '</table>'+
            '<div id="exp1176132896" style="padding: 3px;">'+
            '     <asp:Literal ID="HtmlLiteral" runat="server"></asp:Literal>'+
            '</div>'+
            '</div>';
            
    var sf = '<div style="border: 1px none #000000; padding: 0px; background: #FFFFFF;">'+
        '<table border="0" cellspacing="0" cellpadding="2" width="750px" style="background: #CCCCCC;'+
        '    color: #000000;">'+
        '    <tr>'+
        '        <td width="4%" bgcolor="#FFCC00">'+
        '            <div align="center" class="style24">'+
        '                <strong>[<a title="show/hide" id="exp1176132896_link" href="javascript: void(0);"'+
        '                     style="text-decoration: none; color: #000000;">-</a>]</strong></div>'+
        '        </td>'+
        '        <td width="92%">'+
        '            <span class="style20"><strong>STANDARD FEATURES</strong></span></td>'+
        '        <td width="4%" align="right">'+
        '            &nbsp;</td>'+
        '    </tr>'+
        '</table>'+
        '<div id="exp1176132896" style="padding: 3px;">'+
        '     <asp:Literal ID="HtmlLiteral" runat="server"></asp:Literal>'+
        '</div>'+
        '</div>';
    
    var header = "<html><head><style type=\"text/css\">"+
            ".style1 {"+
            "font-size: 14px;"+
            "font-family: Arial, Helvetica, sans-serif;"+
            "}.style17 {font-size: 12px; font-family: Arial, Helvetica, sans-serif;}"+
            ".style20{font-family: Arial, Helvetica, sans-serif}"+
            ".style27{font-size: 24px; font-weight: bold; font-family: Arial, Helvetica, sans-serif;}"+
          ".model8-right-sub-left{ float: left; width: 260px; position: relative; margin: 0 auto; margin-top:6px;line-height:100%;}" + 
            
           "</style></head><body>";
            
    var img = "<table width='810px' cellpadding=0 cellspacing=0 border=0>";
    img += "<tr><td align=\"center\" colSpan=\"2\"><strong><a href=\"" + document.getElementById("hdnUrl").value + document.getElementById("hdnDirectory").value + "/more-details.aspx?vin="+ vin + "&zip=" + zip + "\">" + document.getElementById("SpanCar").innerHTML + "</a></strong><br/></td></tr>";
    img += "<tr><td align=\"center\" colSpan=\"2\">&nbsp;</td></tr>";
    img += "<tr><td><span style='font-size:24px; line-height: 30px;  color: #0081ff;'>" + document.getElementById("SpanCar").innerHTML + "</span></td><td class=\"style27\" align='left'><h2>" + document.getElementById("DivPrice").innerHTML + "</h2></td></tr>";
    img += "<tr><td>" + objImg.getElementById('dynloadarea').innerHTML + "</td><td valign='top'> <div class=model8-right-sub-left>" 
    + objDt.innerHTML + "</div></td></tr></table>";
    //+ '<div id="DivCarDetail" name="DivCarDetail" class="model8-right-sub-left">' + 
    

    var html = header;
    html += img;
    html += "<br/>";    
    html += hf;
    html += objHF.innerHTML;
   //html += sf;
    //html += objSF.innerHTML;
    html += "<br/><br/>";    
   // html += objRV.innerHTML;
    html += "<br/>";
    try
    {
       // html += objRVL.innerHTML;
    }
    catch(e){}
    
    html += "</body></html>";
    
    return html;
}

//////////////////////////////////////////////////////////////////

function ValidateZIP(browser,zipcodeID,url) 
{
 var zip = document.getElementById(zipcodeID);
var rfvZip = document.getElementById('rfvZip');
var regExp = document.getElementById('regExpZipCode');


if(zip)
{
   var zipCode = zip.value; 
   
   
  if(zipCode.length > 0 && regExp.style.visibility=='hidden')
  {

	    var http_request = false;	
	    if (window.XMLHttpRequest) 
	    {
		    http_request = new XMLHttpRequest(); 
		    if (http_request.overrideMimeType) 
		    { 
			    http_request.overrideMimeType('text/xml'); 		
		    } 
    		
	    } 
	    else if (window.ActiveXObject) 
	    { 
		    // IE 
	        try 
	        { 
		        http_request = new ActiveXObject("Msxml2.XMLHTTP");
	        } 
	        catch (e) 
	        { 
	            try 
	            { 
		            http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
	            }
	            catch (e) 
	            {} 
	        } 
	    } 
	    
		if (!http_request) 
		{ 			
		}
		
		
		
		var urls = url + "Ajax/Server.aspx?request=validatezip&zipcode="+zipCode;
				
		if(browser == "IE")
		    http_request.open('POST', urls, false);
		else
		    http_request.open('GET', urls, false);
		    
		http_request.send(null); 
		
	 if (http_request.readyState == 4) 
	  {
		if (http_request.status == 200) 
		{
				if(http_request.responseText=="false")
				{				    
				    alert("INVALID ZIPCODE!");
				    return false;		    				    
				}
				else
				{
				   return true;
				}
		}
	 }
		
  }
}	
	
}


//////////////////////////////////////////////////////////////////////

function IsLoggedIn(http_request, userCarId, zip)
{
    if (http_request.readyState == 4) 
	{ 
		if (http_request.status == 200) 
		{
			if(http_request.responseText=="False")
			{
			    window.location = document.getElementById("hdnUrl").value + document.getElementById("hdnDirectory").value + "used/log-in.aspx";
			    return false;
			}
			else
			{
                var url = document.getElementById("hdnUrl").value + document.getElementById("hdnDirectory").value + "used/Ajax/UsedAjax.aspx?save=1&cid=" + userCarId +"&zip=" + zip;
                var result = CallServerToSave(url, Save, false);    
            
                url = document.getElementById("hdnUrl").value + document.getElementById("hdnDirectory").value + "used/Ajax/UsedAjax.aspx?vehcount=1";
                CallServerToSave(url , getCountResponse, true);
        
			    return result;
			}
		}
	}
}

//////////////////////////////////////////////////////////////////////

function SaveVehicle(userCarId, zip)
{   
    var url = document.getElementById("hdnUrl").value + document.getElementById("hdnDirectory").value + "used/Ajax/UsedAjax.aspx?login_status=1";
    
    var result = CallServerToSave(url, IsLoggedIn, true, userCarId, zip);
    return result;
}

function SaveRelatedVehicle(userCarId, zip, obj)
{
    if(!obj.checked)
    {
        return;
    }  
    var url = document.getElementById("hdnUrl").value + document.getElementById("hdnDirectory").value + "used/Ajax/UsedAjax.aspx?login_status=1";    
    var result = CallServerToSave(url, IsLoggedIn, true, userCarId, zip);
    return result;
}


//////////////////////////////////////////////////////////////////////

function CallServerToSave(url, functionName, asyn, userCarId, zip)
{
    var browser = document.getElementById("hdnBrowser").value;
    var reqObj = false;
	if (window.XMLHttpRequest) 
	{
		reqObj = new XMLHttpRequest();
		if (reqObj.overrideMimeType)
		{
			reqObj.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject)
	{
		// IE 
	    try 
	    { 
		    reqObj = new ActiveXObject("Msxml2.XMLHTTP");
	    } 
	    catch (e) 
	    { 
	        try 
	        { 
		        reqObj = new ActiveXObject("Microsoft.XMLHTTP"); 
	        } 
	        catch (e) 
	        {} 
	    } 
	}						    
	
	if(asyn)
	    reqObj.onreadystatechange = function() { return functionName(reqObj, userCarId, zip); };
	
	if(browser == "IE")
	    reqObj.open('POST', url, true);
	else
	    reqObj.open('GET', url, true);
	    
	reqObj.send(null); 	
	
	if(!asyn)
	    return functionName(reqObj, userCarId, zip);
}
	
//////////////////////////////////////////////////////////////////////
	  
function Save(reqObj) 
{ 
	if (reqObj.readyState == 4) 
	{ 
		if (reqObj.status == 200) 
		{
			if(reqObj.responseText=="True")
			{
			    ShowSavedMessage("<strong>You have successfully saved this vehicle.</strong>");
			    return true;
			}
			else
			{
			    ShowSavedMessage("<strong>Sorry, could not save the vehicle.</strong>");
			    return false;
			}
		}
	} 
} 

//////////////////////////////////////////////////////////////////////

function getCountResponse(objReq) 
{ 
	if (objReq.readyState == 4) 
	{
		if (objReq.status == 200) 
		{
		    try
		    {
		        if(objReq.responseText != "")		    
			        window.parent.document.getElementById("spanTotalVehicles").innerHTML = objReq.responseText;
			    else
			        window.parent.document.getElementById("spanTotalVehicles").innerHTML = "0";
            }
            catch(e) { }
		}
	} 
} 

/////////////////////////////////////////////////////////////////////

function ShowSavedMessage(msg)
{
    var objDiv = document.getElementById("divSavedMessage");
    
    objDiv.innerHTML = msg;
    objDiv.style.visibility = "visible";
    
    setTimeout( function() { objDiv.style.visibility = "hidden"; }, 5000);
}

//////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////

function PrintPage(cid, zip)
{
    var objWin = window.open("Print");
    objWin.document.open();    
    objWin.document.writeln(GetHtml(cid, zip));
    objWin.document.close();
    objWin.focus();
    objWin.print();
}

///////////////////////////////////////////////////////////////////

var retStatus = false; 

function Validate()
{

    var url = document.getElementById("hdnUrl").value;
    var brs= document.getElementById("hdnBrowser").value;
    var frm = document.getElementById("Form1");
   
    // empty the email data so that "potentially dangrous" exception could be avoided.
    document.getElementById("hdnData").value = "";
    
	document.getElementById("btnSubmit").disabled = true;

	var http_request = false; 
	
	if (window.XMLHttpRequest) 
	{
		http_request = new XMLHttpRequest(); 
		if (http_request.overrideMimeType) 
		{ 
			http_request.overrideMimeType('text/xml'); 
		
		} 		
	} 
	else if (window.ActiveXObject) 
	{ 
		// IE 
	    try 
	    { 
		    http_request = new ActiveXObject("Msxml2.XMLHTTP"); 
	    } 
	    catch (e) 
	    { 
	        try 
	        { 
		        http_request = new ActiveXObject("Microsoft.XMLHTTP"); 
	        } 
	        catch (e) 
	        {} 
	    } 
	} 
	
	if (!http_request) 
	{ 
		//return false;
	}

	if(!ValidateZIPCode(brs, url))
    {
        document.getElementById("btnSubmit").disabled = false;
        return false;
    };	    
	
	url = url + "Ajax/Server.aspx?request=validateform&fname="+ htmlEncode(document.getElementById("Name_last").value)+"&lname="+ htmlEncode(document.getElementById("Name_first").value)+"&areacode="+ document.getElementById("Home_Phone_area_ch").value+"&prefix="+ document.getElementById("Home_Phone_prefix_ch").value+"&email="+ htmlEncode(document.getElementById("Email").value);
	
	if(brs=="IE")
	    http_request.open('POST', url, false);
	else
	    http_request.open('GET', url, false);
	    
	http_request.send(null); 
	var ret = alertContents_(http_request);
	
	if(!(ret && retStatus))
	{
	     document.getElementById("btnSubmit").disabled = false;
	}

	return (ret && retStatus);
}

function htmlEncode(html)
{
    var index = html.indexOf("<");
    
    while(index != -1)
    {
        html = html.replace("<", "&lt;");
        
        index = html.indexOf("<");
    }
    
    index = html.indexOf("<");
    
    while(index != -1)
    {
        html = html.replace(">", "&gt;");
        
        index = html.indexOf(">");
    }
    
    return html;
}

function alertContents_(http_request) 
{ 
	if (http_request.readyState == 4) 
	{ 
		if (http_request.status == 200) 
		{
			var arr=http_request.responseText.split("|");
			
			var email_=true;
			
			try
			{
			    if(arr[1]=="True")
			        email_=true;
			    else if(arr[1]=="False")
	    		    email_=false;
	        }
	        catch(e){email_=true;}

            
			if(arr[0]=="11")
			{			
				if(ValidateForm(document.getElementById("Form1"),true,true,email_))
				    document.getElementById("Form1").submit();
				    
				submitonce_inverse(document.getElementById("Form1"));
			}
			else if(arr[0]=="00") //-> both invalid
			{
				if(ValidateForm(document.getElementById("Form1"),false,false,email_))
				{
				    document.getElementById("Form1").submit();				    
				}
				submitonce_inverse(document.getElementById("Form1"));
			}
			
			else if(arr[0]=="10") //-> name is good phone is invalid
			{
				if(ValidateForm(document.getElementById("Form1"),true,false,email_))
				{
				    document.getElementById("Form1").submit();				    
				}
				submitonce_inverse(document.getElementById("Form1"));
				    
			}
			else if(arr[0]=="01") //-> name is invalid phone is good
			{
				if(ValidateForm(document.getElementById("Form1"),false,true,email_))
				{
				    document.getElementById("Form1").submit();				    
				}
				submitonce_inverse(document.getElementById("Form1"));
			}
			else //-> every thing is true than submit form to the next step.
			{
				if(ValidateForm(document.getElementById("Form1"),true,true,email_))
				{
				    document.getElementById("Form1").submit();				    
				}
			}

            document.getElementById("Form1").disabled = false; 
            return email_;  
		}
	} 
} 
/////////////////////////////////////////////////////////////////////////////////////////////////////

function ValidateForm(frm,Names,Phones,Emails)
{
	message = "In order to buy car, your CONTACT information must be VALID.\n";
	message += "Please correct the following:\n";
	message2 = "Following fields must match with each other:-\n";

	valid = true;
	match = true;
	focused = false;
	
	if(frm.Name_last.value=="" || frm.Name_last.value.length<2) // first and last names are reversed ;)
	{
		message += "- First Name\n";
		if( ! focused )
		{
			frm.Name_last.focus();
			focused = true;
		}
		valid = false;
	}
	if(frm.Name_first.value=="" || frm.Name_first.value.length<2) // first and last names are reversed ;)
	{
		message += "- Last Name\n";
		if( ! focused )
		{
			frm.Name_first.focus();
			focused = true;
		}
		valid = false;
	}
	
	if(!Names)
	{
		message += "- First / Last Name!\n";
		if( ! focused )
		{
			frm.Name_first.focus();
			focused = true;
		}
		valid = false;
	}
	
	try
	{
	    if(frm.Address != 'undefined' && frm.Address.value=="" || frm.Address.value.length<2)
	    {
		    message += "- Address\n";
		    if( ! focused )
		    {
			    frm.Address.focus();
			    focused = true;
		    }
		    valid = false;
	    }
	}
	catch(e) { }
	
	if(frm.Home_Phone_area_ch.value=="" || frm.Home_Phone_prefix_ch.value == "" || frm.Home_Phone_ch.value == "" ||
	   isNaN(frm.Home_Phone_area_ch.value) || isNaN(frm.Home_Phone_prefix_ch.value) || isNaN(frm.Home_Phone_ch.value) ||
	   frm.Home_Phone_area_ch.value.length!=3 || frm.Home_Phone_prefix_ch.value.length != 3 || frm.Home_Phone_ch.value.length != 4 ||
	   (frm.Home_Phone_area_ch.value.charCodeAt(0) == frm.Home_Phone_area_ch.value.charCodeAt(1) && frm.Home_Phone_area_ch.value.charCodeAt(1) == frm.Home_Phone_area_ch.value.charCodeAt(2)) )
	{
		message += "- Phone Number\n";
		if( ! focused )
		{
			if(frm.Home_Phone_area_ch.value=="" || isNaN(frm.Home_Phone_area_ch.value) || frm.Home_Phone_area_ch.value.length!=3 || (frm.Home_Phone_area_ch.value.charCodeAt(0) == frm.Home_Phone_area_ch.value.charCodeAt(1) && frm.Home_Phone_area_ch.value.charCodeAt(1) == frm.Home_Phone_area_ch.value.charCodeAt(2)))
				frm.Home_Phone_area_ch.focus();
			else
			if(frm.Home_Phone_prefix_ch.value == "" || isNaN(frm.Home_Phone_prefix_ch.value) || frm.Home_Phone_prefix_ch.value.length != 3)
				frm.Home_Phone_prefix_ch.focus();
			else
				frm.Home_Phone_ch.focus();
			focused = true;
		}
		valid = false;
	}
	if(frm.Email.value=="" || !(frm.Email.value.indexOf("@")>1 && frm.Email.value.lastIndexOf(".") > frm.Email.value.indexOf("@")+1) )
	{
		message += "- Email\n";
		if( ! focused )
		{
			frm.Email.focus();
			focused = true;
		}
		valid = false;
	}
	
	if(frm.Email.value.indexOf(" ")!=-1 && frm.Email.value.indexOf(" ")!=0 && frm.Email.value.indexOf(" ")!= frm.Email.value.length-1)
	{
		
		message += "- Email: Spaces are not Allowed\n";
		if( ! focused )
		{
			frm.Email.focus();
			focused = true;
		}
		valid = false;
	}
	
	if(!Emails)
	{
		message += "- Please check your email - We can only accept United States Based emails.\n";
		if( ! focused )
		{
			frm.Email.focus();
			focused = true;
		}
		valid = false;
	}
	
	if(frm.Email.value.indexOf("www")!=-1 || frm.Email.value.indexOf("WWW")!=-1)
	{
		message += "-Email: (www) not Allowed\n";
		if( ! focused )
		{
			frm.Email.focus();
			focused = true;
		}
		valid = false;
	}
	
	//->Any phones with the following area codes:  900, 911, 411, 456, 500, 555, 222, 333, 444, invalid ones.
	if(frm.Home_Phone_area_ch.value=="900" || frm.Home_Phone_area_ch.value=="911" || frm.Home_Phone_area_ch.value=="441" || frm.Home_Phone_area_ch.value=="456" || frm.Home_Phone_area_ch.value=="500")
	{
		message += "- Invalid Phone Area\n";
		valid=false;
	}
	
	//->Any area code with the first digit of 0 or 1 is an invalid phone number.
	if(frm.Home_Phone_area_ch.value.charAt(0)=="1" || frm.Home_Phone_area_ch.value.charAt(0)=="0")
	{
		message += "- Invalid Phone Area\n";
		valid=false;
	}
	 
     //->800, 888, 877, or 876 (toll free numbers) are considered valid phone numbers.
    if(frm.Home_Phone_area_ch.value=="800" || frm.Home_Phone_area_ch.value=="888" || frm.Home_Phone_area_ch.value=="877" || frm.Home_Phone_area_ch.value=="876")
	{
		//message += "- Invalid Phone Area\n";
		valid=true;
	}
	
	 //-> Any exchange that starts with a 0 or 1 is considered an invalid phone number.
	 
	 if(frm.Home_Phone_prefix_ch.value.charAt(0)=="0" || frm.Home_Phone_prefix_ch.value.charAt(0)=="1")
	 {
		message += "- Invalid Phone Prefix\n";
		valid=false;
	 }
	 
     //-> 411, 555, 611, 911 is considered an invalid phone number.
     
     if(frm.Home_Phone_prefix_ch.value=="411" || frm.Home_Phone_prefix_ch.value=="555" ||  frm.Home_Phone_prefix_ch.value=="611" ||  frm.Home_Phone_prefix_ch.value=="911" )
	 {
		message += "- Invalid Phone Prefix\n";
		valid=false;
	 }
     
    //->  Phone numbers with repeating sequences of 10 digits are considered invalid phone numbers. 
    //-> For example, 305-777-7777 would be considered valid:
    
    if(!Phones)
	{
		message += "- Invalid Area Code / Prefix Combination!\n";
		if( ! focused )
		{
			frm.Home_Phone_area_ch.focus();
			focused = true;
		}
		valid = false;
	}

	if(! valid)
	{
		alert(message);
		retStatus = false;
		return false;
	}

	if(! match)
	{
		alert(message2);
		retStatus = false;
		return false;
	}

    retStatus = true;		
	return true;	
}

function submitonce_inverse(frm)
{
    document.getElementById("btnSubmit").disabled = false;
}

function ValidateZIPCode(browser, url)
{

var http_request = false;
if (window.XMLHttpRequest)
{
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType)
{
http_request.overrideMimeType('text/xml');
}

}
else if (window.ActiveXObject)
{
// IE
try
{
http_request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e)
{
try
{
http_request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{}
}
}

if (!http_request)
{
}

var zipCode = document.getElementById('zipCode').value;
var zip=null;
if(zipCode == "" || zipCode == null)
{
alert("Please enter zip code.");
return false;
}
if(zipCode.length>0)
zip = zipCode;

var urls = url + "Ajax/Server.aspx?request=validatezip&zipcode="+zip;


if(browser == "IE")
http_request.open('POST', urls, false);
else
http_request.open('GET', urls, false);

http_request.send(null);
return ValidationResponse(http_request);
}

function ValidationResponse(http_request)
{
if (http_request.readyState == 4)
{
if (http_request.status == 200)
{
if(http_request.responseText=="false")
{
alert("INVALID ZIPCODE!");
document.getElementById("btnSubmit").disabled = false;
return false;
}
else
{
return true;
}
}
}
}