/*
 * Function used to display form fields which the user uses to enter information for saving dealer info to E-mail
 */
var privacy_popup_div;

function displaySaveDealerInfo(index){
	positionedDealer = dealerResultsGlobal[index];
	if(debugMode){
		alert("Entered function displaySaveDealerInfo()");
	}

	var inputBox = document.getElementById('inputBox');
	inputBox.innerHTML = "";
	//inputBox.className="inputBox";
	inputBox.style.display="block";

	var heading = document.createElement("h2");
	heading.innerHTML = dealerAssets["dealerLocator.saveInfo.heading"]+' <a href="javascript:void(null)" onclick="closePopUp(\''+('inputBox')+'\'); setDealerError(\'\'); return false;"><img src="/img/dealer_locator/close.jpg" width="12" height="12" id="close_image" /></a>';
	inputBox.appendChild(heading);

	var inputBoxInner = document.createElement("div");
	inputBox.appendChild(inputBoxInner);

	var errMsg = document.createElement("div");
	errMsg.innerHTML = "";
	errMsg.id = "save_dealer_err_msg";
	errMsg.className="errMsg";
	inputBoxInner.appendChild(errMsg);

	var fname = document.createElement("div");
	fname.innerHTML = dealerAssets["dealerLocator.saveInfo.firstName"]+"<br/>";
	var fnameInput = document.createElement("input");
	fnameInput.type="text";
	fnameInput.id="dFirstName";
	fnameInput.name="dFirstName";
	fnameInput.value=stateMgr.formfieldvalues["dFirstName"]?stateMgr.formfieldvalues["dFirstName"]:"";
	fnameInput.style.width="153px";
	fnameInput.style.marginRight="3px";
	fname.appendChild(fnameInput);
	jQuery(fname).css("float","left");
	inputBoxInner.appendChild(fname);

	var lname = document.createElement("div");
	lname.innerHTML = dealerAssets["dealerLocator.saveInfo.lastName"]+"<br/>";
	var lnameInput = document.createElement("input");
	lnameInput.type="text";
	lnameInput.id="dLastName";
	lnameInput.name="dLastName";
	lnameInput.value=stateMgr.formfieldvalues["dLastName"]?stateMgr.formfieldvalues["dLastName"]:"";
	lnameInput.style.width="153px";
	lname.appendChild(lnameInput);
	jQuery(lname).css("float","left");
	inputBoxInner.appendChild(lname);

	var email = document.createElement("div");
	email.innerHTML = dealerAssets["dealerLocator.saveInfo.emailAddress"]+"<br/>";
	var emailInput = document.createElement("input");
	emailInput.type="text";
	emailInput.id="dEmailAddress";
	emailInput.name="dEmailAddress";
	emailInput.value=stateMgr.formfieldvalues["dEmailAddress"]?stateMgr.formfieldvalues["dEmailAddress"]:"";
	emailInput.style.width="315px";
	email.appendChild(emailInput);
	jQuery(email).css("float","left");
	inputBoxInner.appendChild(email);

	var optin = document.createElement("div");
	optin.innerHTML='<input type="checkbox" style="border: 0px" value="sendPromotions" id="sendPromotions" name="sendPromotions"/> '+dealerAssets["dealerLocator.saveInfo.receiveAnnouncements"];

	jQuery(optin).css({"float":"left", width: "315px", fontWeight: "normal", "margin-top": "5px", "font-size": "9px"});
	inputBoxInner.appendChild(optin);

	var privacy_policy_div1 = document.createElement("div");
	var privacy_policy_data1 = document.createElement("div");
	privacy_policy_data1.innerHTML = '<u style="font-weight:bold;font-size:10px">'+dealerAssets["dealerLocator.saveInfo.privacy"]+'</u>';
	privacy_policy_div1.appendChild(privacy_policy_data1);

	privacy_policy_div1.id = "privacy_policy";
	privacy_policy_div1.className = "privacy_policy";
	privacy_policy_div1.onclick = privacy_policy_poup;
	inputBoxInner.appendChild(privacy_policy_div1);

	jQuery(privacy_policy_data1).css({"width":"150px", "height":"10px", "cursor":"pointer", "fontWeight": "normal"});

	var button = document.createElement("div");
	var buttonInput = document.createElement("input");
	buttonInput.type="image";
	buttonInput.name="saveDealerInfo";
	buttonInput.value="Send to Email";
	buttonInput.onclick=validateSaveDealerInfo;
	buttonInput.src="/img/dealer_locator/buttons/send_to_email_off.png"
	button.appendChild(buttonInput);
	jQuery(buttonInput).ifixpng().css({"padding-top":"15px", "top":"15px", border: "0px", height: "23px", "float": "right", "margin-right":"-12px"}).hover(imgSwitch,imgSwitch);
	jQuery(button).css({marginLeft: "158px", marginTop: jQuery(optin).height()>18?"-10px":"0px", "width":"150px" });
	inputBoxInner.appendChild(button);

	jQuery("#inputBox input,#inputBox select").change(function () {
		stateMgr.formfieldvalues[this.name]=jQuery(this).val();
	});

	if(debugMode){
		alert("Leaving function displaySaveDealerInfo()");
	}
}

function privacy_policy_poup(){
	//alert("Privacy policy popup");

	createPrivacyPopup();

}

function createPrivacyPopup(){

	//alert("create Privacy Popup");
	if(privacy_popup_div==null){
		privacy_popup_div = document.createElement("div");
		privacy_popup_div.id="privacy_popup";
		privacy_popup_div.className="privacy_popup";
		privacy_popup_div.style.display="block";


	var heading = document.createElement("h2");
	heading.innerHTML = dealerAssets["dealerLocator.saveInfo.privacy"]+' <a href="javascript:void(null)" onclick="closePopUp(\''+('privacy_popup')+'\');setDealerError(\'\'); return false;"><img src="/img/dealer_locator/close.jpg" width="12" height="12" id="close_image" /></a>';
	privacy_popup_div.appendChild(heading);

	var privacy_data = document.createElement("div");
	privacy_data.innerHTML = '<div style="padding:15px;">'+" We value your privacy. Your personal information is used for Nissan communications purposes only. <br/>Providing your e-mail address allows Nissan and its dealers to contact you with marketing information about our products and services. This permission supersedes any national Do Not Call lists.</div>";
	privacy_data.id = "privacy_data";
	privacy_data.className = "privacy_data";
	privacy_popup_div.appendChild(privacy_data);


	var pageContent = document.getElementById('cdpageContent');
	pageContent.appendChild(privacy_popup_div);

	}else{
		//privacy_popup_div.className="privacy_popup";
		privacy_popup_div.style.display="block";
	}
	if(document.getElementById('save_dealer_err_msg').style.display=='block')
		{
			//alert("in if loop");
			document.getElementById('privacy_popup').className="privacy_popup_error";
			document.getElementById('privacy_data').className="privacy_data_error";
			//jQuery("#privacy_policy").css({"top":"143px","left":"15px"});
		}
	else
		{
			//alert("in else loop");
			document.getElementById('privacy_popup').className="privacy_popup";
			document.getElementById('privacy_data').className="privacy_data";
			//jQuery("#privacy_policy").css({"top":"115px","left":"15px"});
		}

	//jQuery(privacy_policy_div).css({"width": "315px", "fontWeight": "normal", "padding-top": "9px", "font-size": "9px"});

}


/*
 * Function to validate the information provided to the save dealer info
 */
function validateSaveDealerInfo(){
	if(debugMode){
		alert("Entered function validateSaveDealerInfo()");
	}
	//crmEvent7();
	var firstName = DWRUtil.getValue('dFirstName');
	var lastName = DWRUtil.getValue('dLastName');
	var email = DWRUtil.getValue('dEmailAddress');

	document.getElementById('dwrErrorRegion').innerHTML = '';
	var errorMessage='Please enter your ';
	var errorFlag = false;
	var formatFlag = false;

	if(debugMode) {
		alert("firstName:"+DWRUtil.getValue('dFirstName'));
		alert("lastName:"+DWRUtil.getValue('dLastName'));
		alert("email:"+DWRUtil.getValue('dEmailAddress'));
	}

	// First name validation
	if(firstName == null || firstName == ''){
		errorMessage += 'First Name';
		formatFlag = true;
		errorFlag = true;
	}

	// Last name validation
	if(lastName == null || lastName == ''){
		if(formatFlag){
			errorMessage +=', Last Name';
		}else{
			errorMessage +='Last Name';
			formatFlag = true;
		}
		errorFlag = true;
	}

	// E-mail validation
	if(email == null || email == ''){
		if(formatFlag){
			errorMessage +=' and valid Email Address';
		}else{
			errorMessage ='Please enter a valid Email Address';
		}
		errorFlag = true;
	}
	errorMessage +='.';

	if(!errorFlag){
		// If all the required information is provided then this loop is executed
		// Regular expression to validate the first name and last name
		errorMessage = "";

		var validName = /^[a-zA-Z]*$/;
		if(!validName.test(firstName) || !validName.test(lastName)) {
			errorMessage += 'Only Alphabets are available in both First Name and Last Name fields. Please enter proper information';
			formatFlag = true;
			errorFlag = true;
		}
		//alert("Check mail status: "+checkEmail(email));

		// E-mail validation. The function is part of utility file
		if(!checkEmail(email)){
			if(formatFlag){
				errorMessage += '<br>';
			}
			errorMessage += 'Please enter a valid Email Address.';
			errorFlag = true;
		}
	}

    if(!errorFlag){
		// if there are no errors and email Optin is true then fire the crmEvent.
		var emailOptin = DWRUtil.getValue('sendPromotions');
		if(emailOptin){
			crmEvent7({'optin':emailOptin});
		} else {
			crmEvent7({'optin':false});
		}
		// If all validations are successfull then the following method call send the dealer information to the  given E-mail id
		saveDealerInfo();
	}
	setDealerError(errorMessage);

	if(debugMode){
		alert("Leaving function validateSaveDealerInfo()");
	}
}

function setDealerError(err) {
	if (err.length > 0) {

		jQuery("#inputBox").css({"height":"199px","top": "195px"});
		jQuery("#save_dealer_err_msg").css({"display":"block","height":"25px","margin-top":"8px"}).html(err);
		jQuery("#privacy_policy").css({"top":"143px","left":"15px"});
		//jQuery("#privacy_popup").css({"height":"199px","top":"195px"});
	} else {

		jQuery("#inputBox").css({"height":"169px","top": "225px"});
		jQuery("#save_dealer_err_msg").css({"display":"none"}).html("");
		jQuery("#privacy_policy").css({"top":"115px","left":"15px"});
		//jQuery("#privacy_popup").css({"height":"169px","top":"225px"});
	}
}

/*
	This function sends the selected dealer information to the given E-mail id
*/
function saveDealerInfo(){
	if(debugMode){
		alert("Entered function saveDealerInfo()");
	}

	document.getElementById('dwrErrorRegion').innerHTML = '';
	var firstName = DWRUtil.getValue('dFirstName');
	var lastName = DWRUtil.getValue('dLastName');
	var emailAddress =DWRUtil.getValue('dEmailAddress');
	var dealerId = positionedDealer["id"];
	var localeLanguage = document.dealerLocatorForm.hdnLocaleLanguage.value;
	var localeCountry = document.dealerLocatorForm.hdnLocaleCountry.value;
	var emailOptin = DWRUtil.getValue('sendPromotions');

	if(debugMode){
		alert("localeLanguage"+localeLanguage);
		alert("localeCountry"+localeCountry);
		alert("firstName"+firstName);
		alert("lastName"+lastName);
		alert("emailAddress"+emailAddress);
		alert("positionedDealer"+positionedDealer["id"]);
	}

	// 'SENDING_EMAIL' variable is part of display.js
	// The following line has been commented as loading message is not required
    // DWRUtil.useLoadingMessage(SENDING_EMAIL+' '+firstName+" "+lastName);
	// Ajax/DWR call to send E-mail to the given address
	DealerLocatorHelper.sendEmail(firstName,lastName,emailAddress,emailOptin,dealerId,localeLanguage,localeCountry);

	document.getElementById('inputBox').innerHTML="";
	closePopUp("inputBox");

	if(debugMode){
		alert("Leaving function saveDealerInfo()");
	}
}