// JavaScript Document

function MM_openBrWindow(theURL,winName,features) { 
  window.open(theURL,winName,features);
}
function checkMakeModelStateCity(objForm)
{
	//alert("script called!");
	//alert(objForm.cMake.value);
	//alert(objForm.cModel.value);
	//alert(objForm.cCity.value);
	//objForm = document.formNewCarSearch;
	var isValid=true;
	if(objForm.cMake.selectedIndex == 0){
		alert("Please Select a Make");
		objForm.cMake.focus();
		isValid=false;
		return false;
	}
	if(objForm.cModel.selectedIndex == 0){
		alert("Please Select a Model");
		objForm.cModel.focus();
		isValid=false;
		return false;
	}
	
	if(objForm.cCity.selectedIndex == 0){
		alert("Please Select a City");
		isValid=false;
		objForm.cCity.focus();
		return false;
	}
	if(isValid==true)
		submitCheckMakeModelStateCity();

}//end of method



function bookmarkMe(myUrl, myTitle)
{
        if(window.sidebar)
        {
               //******************************//
               // Firefox is used
               // We can just prompt the user
               // to add a regular bookmark...
                //******************************//

               alert('Please press CTRL+D to bookmark this page!');

               //******************************//
               // ... or we can add a 'sidebar' bookmark.
               // To do it, use the line below,
               // instead of the line above:
               //******************************//

               // window.sidebar.addPanel(myTitle, myUrl, '');

               //******************************//
               // In the second case, you can
               // give the visitor additional information.
               // to do it, uncomment the lines below
               //******************************//

               // var msg = "Firefox has just added the bookmark ";
               // msg += "that will open in the sidebar by default.\n";
               // msg += "If you want it to open in the main window instead, ";
               // msg += "please go to Bookmarks, right-click the recently ";
               // msg += "added bookmark, choose 'Properties' and uncheck the ";
               // msg += "'load this bookmark in the sidebar' option.\n\n";
               // msg += "Sorry, this in unavoidable with Firefox so far.";
               // alert(msg);
        }
        else if(window.opera && window.print)
        {
               //******************************//
               // Opera is used
               //******************************//

               var bkmark = document.createElement('a');
               bkmark.setAttribute('rel','sidebar');
               bkmark.setAttribute('href', myUrl);
               bkmark.setAttribute('title', myTitle);
               bkmark.click();
        }
        else if(document.all)
        {
               //******************************//
               // Most probably IE, but even if it's not,
               // we don't want an error to pop up
               // so we use the try-catch structure
               //******************************//

               try
               {
                       window.external.AddFavorite(myUrl, myTitle);
               }
               catch(e)
               {
                       //******************************//
                       // oops! not IE. Prompt user for action.
                       //******************************//

                       var msg = "Sorry, your browser doesn't allow scripts ";
                       msg += "to add bookmarks.\n";
                       msg += "Please use your browser's keyboard shortcut ";
                       msg += "(most commonly CTRL+D) to add a bookmark.";
                       alert(msg);
               }
        }
}

function setAsHome(myLink)
{
        if(navigator.appName.indexOf('Microsoft')!=-1)
        {
               // IE
               myLink.style.behavior='url(#default#homepage)';
               myLink.setHomePage(location.href);
        }
        else if(navigator.appName.indexOf('Netscape')!=-1)
        {
               // Netscape / firefox browsers
               var msg = "Just drag this link onto your 'home' ";
               msg += "button to set this page as your ";
               msg += "default homepage!";
               alert(msg);
        }
        else if(navigator.appName.indexOf('Opera')!=-1)
        {
               // Opera
               var msg = "Please go to Tools - Preferences ";
               msg += "- General and click 'Use current' ";
               msg += "to set this as your homepage! ";
               alert(msg);
        }
        else
        {
               // Other browsers
               var msg = "Sorry, your browser doesn't allow ";
               msg += "scripts to set the default homepage. ";
               msg += "Please use your browser's 'options' dialog ";
               msg += "to do it! Thank you for your patience. ";
               alert(msg);
        }
}


function showThank(obj)
{
	
	if(obj == 'thank')
	{
		document.getElementById('indRef').style.display='none'
		document.getElementById('indRefTnk').style.display='block'
	}
	else if(obj == 'usrForm')
	{
		document.getElementById('indRef').style.display='block'
		document.getElementById('YouEmId').value = "";
		document.getElementById('FriEmId').value = "";
		document.getElementById('indRefTnk').style.display='none'
		document.getElementById('YouEmId').focus();
	}
	
}

function valid_email(mailStr){
	var matchStr=mailStr;
	var isValid = (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(matchStr));
	
	if(isValid){
		return true;
	}
	else{
		return false;
	}
}

function chkdata(url)
{
	if (document.getElementById("YouEmId").value == "" )
	{ 
		alert("Please enter your e-mail id");
		document.getElementById("YouEmId").focus();
		return false;
	}

	else if (document.getElementById("YouEmId").value !="" && !valid_email(document.getElementById("YouEmId").value)){ 
		alert("Please enter valid e-mail id");
		document.getElementById("YouEmId").focus();
		return false;
	}
	else if (document.getElementById("FriEmId").value == "" )
	{ 
		alert("Please enter friend's e-mail id");
		document.getElementById("FriEmId").focus();
		return false;
	}

	else if (document.getElementById("FriEmId").value !="" && !valid_email(document.getElementById("FriEmId").value)){ 
		alert("Please enter valid e-mail id");
		document.getElementById("FriEmId").focus();
		return false;
	}
	else if((document.getElementById("YouEmId").value) == (document.getElementById("FriEmId").value))
	{
		alert("Please enter different e-mail id");
		document.getElementById("FriEmId").select();
		return false;
	}
	/*else
	{
		document.getElementById('indRef').style.display='none'
		document.getElementById('indRefTnk').style.display='block'
		return true;
	}*/

		//Ajax code

	     var yourId=document.getElementById("YouEmId").value;
	 
     var friendId=document.getElementById("FriEmId").value;
	 //added for showing submitting..............
//alert('before ');
		document.getElementById('indRef').style.display='none';
		document.getElementById('indRefImd').style.display='block';
	//end of adding submitting.....


	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
  		http_request = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		http_request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (!http_request) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	if(http_request){
			url=url +"&YouEmId="+yourId + "&FriEmId=" +friendId ;
			http_request.onreadystatechange = function() { 

				if (http_request.readyState == 4){
		         // document.getElementById("indRefTnk").innerHTML = http_request.responseText;
				//  document.getElementById('indRef').style.display='none';
				
				document.getElementById('indRefImd').style.display='none';
		          document.getElementById('indRefTnk').style.display='block';
				 
				  
		         
				  
				  
				  return true;
		         } 
			} 
			http_request.open ("POST", url, true);			
			http_request.send(null);
}
	
}

function validNCar(){
	var isValid=true;
	objForm = document.nCar;
	if (document.getElementById('nMake').selectedIndex == 0)
	{
		alert("Please select a make");
		document.getElementById('nMake').focus();
		isValid=false;
		return false;
	}
	if (document.getElementById('nModel').selectedIndex == 0)
	{
		alert("Please select a model");
		document.getElementById('nModel').focus();
		isValid=false;
		return false;
	}
	if (document.getElementById('nCity').selectedIndex == 0)
	{
		alert("Please select a city");
		document.getElementById('nCity').focus();
		isValid=false;
		return false;
	}
	if(isValid==true)
		submitCheckMakeModelStateCity();

	
}
/* added for new car filter purpose */
function submitCheckMakeModelStateCity()
{
		var obj =document;
        var seoQuery ="/newcars/showroom/";
		seoQuery= seoQuery + replaceSpaceWithUnderscore(obj.getElementById('nMake').value) + "/";
		seoQuery= seoQuery + replaceSpaceWithUnderscore(obj.getElementById('nModel').value)  + "/";
      /*  seoQuery= seoQuery + replaceSpaceWithDash(obj.getElementById('cState').value)  + "-";*/
		seoQuery= seoQuery + replaceSpaceWithUnderscore(obj.getElementById('nCity').value) ;
		document.nCar.action=seoQuery;
		//document.nCar.method="post";
	//	document.URL=seoQuery;
	//	document.nCar.submit();
		
}
function replaceSpaceWithDashed(StringToReplace){
	var a=StringToReplace;
	a = a.replace(" "," ");
	return a;
}
function replaceSpaceWithDashed1(StringToReplace){
	var a=StringToReplace;
	a = a.replace(" "," ");
	return a;
}
function replaceSpaceWithUnderscore(StringToReplace){
	var a=StringToReplace;
	//a = a.replace(" ","_");
	a = a.replace(/ /g,"_");
	return a;
}
/* end  for adding new car filter purpose */

function validUCar(){
	var isValid="true";
	if (document.getElementById('uMake').selectedIndex == 0)
	{
		alert("Please select a make");
		document.getElementById('uMake').focus();
		return false;
	}
	if (document.getElementById('uModel').selectedIndex == 0)
	{
		alert("Please select a model");
		document.getElementById('uModel').focus();
		return false;
	}
	if (document.getElementById('uCity').selectedIndex == 0)
	{
		alert("Please select a city");
		document.getElementById('uCity').focus();
		return false;
	}
submitCheckHomeUsedCarResearch();
}

function submitCheckHomeUsedCarResearch(){
		var obj =document;
		var seoQuery ="/usedcars/search/";
		seoQuery= seoQuery + obj.getElementById('uMake').value+ "/";
		seoQuery= seoQuery + obj.getElementById('uModel').value  + "/";
		seoQuery= seoQuery + obj.getElementById('uCity').value;
		document.cCar.action=seoQuery;
		document.cCar.method="post";
}

