
/* shared/js/productpage/emailMeProductPage.js contents - Starts Here */

//Variable declarations moved from emailMeProductPage.js
var checkItemLevel = 0;
var catEntryId_1 = 0;
var isSoftlineOutStockEmail = 0;
//Flag for Product Page checking
var isProductPage = false;

function checkItemLevelCatEntry(num){	
	checkItemLevel = num;	
}

function checkSoftlinePage(changeNum){
	isSoftlineOutStockEmail = changeNum ;
}

function deCodePName(pName1){
	var temp1=replaceCharacters(pName1,'##12','\'');
	var temp2=replaceCharacters(temp1,'##13','\'\'');
	return temp2;
}

function replaceCharacters(origString,inChar,outChar) {				
	var newString = origString.split(inChar);  
	newString = newString.join(outChar);  
	return newString;
}

/*Following Methods are removed
function abcdabcd()
function setCookieEmail(cookieEmailAddress)
function sampleCallBack()
*/

/* shared/js/productpage/emailMeProductPage.js contents - Ends Here */


/* Store specific emailMe.js contents - Starts Here */

function storeLocatorPopup() {
	if(checkZipCode()){
		setCookie("zipCode",document.getElementById('zipCode').value);
		var zipCode=document.getElementById('zipCode').value;
		var catEntryId_1 = document.getElementById('zipCatEntryId').value;
		var URL = document.getElementById('zipProductURL').value;
		var storeId = document.getElementById('zipStoreId').value;	
		var catalogId = document.getElementById('zipCatalogId').value;
		var storeLocatorUrl = "StorePickupCommand?storeId="+storeId+"&langId=-1&catalogId="+catalogId+"&zipCode="+zipCode+"&catEntryId_1="+catEntryId_1+"&quantity_1=1"+"&URL="+escape(URL)+"&browseFlag=browseSearch";
		window.location = storeLocatorUrl;
		return false;
	}
	else{
		return false;
	}
}

function checkZipCode() {
	zip = document.getElementById('zipCode').value;
	var lv_pattern = /^\d{5}$|^\d{5}\-?\d{4}$/;
	var passed = lv_pattern.test(zip); 
	if (!passed) {
		alert("Please enter a valid 5-digit ZIP code (example: 60515).");
		return false;
	} else {
		return true;
	}
}

// Load events to 
addLoadEvent(function() {
		var a=document.getElementsByTagName('a');
		for (i=0;i<a.length;i++)
		{
			if (a[i].rel=='emailMe')
			{
				a[i].onclick = function (){
					mePop(this.getAttribute('info'),this.rel);
				};
			}
			if (a[i].rel=='storePickup')
			{
				a[i].onclick = function (){
					mePop(this.getAttribute('info'),this.rel);
				};
			}
		}
	}
);

/*Following Methods are removed
function getCookieValue(cookieName)
function setCookieValue(cookieName,cookieValue)
*/

/* Store specific emailMe.js contents - Ends Here */


/* Common methods of emailMe.js and emailMeProductPage.js - Starts Here */

//Moved from emailMe.js and Modified accordingly
function tempFunc(){
	var emailId1 = document.getElementById('emailMeAddress').value;
	var catId1= document.getElementById('emailCatEntryId').value;
	var partnumber= document.getElementById('emailPartNumber').value; 
	var storeId = document.getElementById('emailStoreId').value;
	
	if(isProductPage) {
		ajaxCall('EmailMeWhenInStockCmd?catEntryId='+catId1+'&userEmail='+emailId1+'&storeId='+getStoreId(),'emailCallBack');
	} else {
		ajaxCall('EmailMeWhenInStockCmd?partNumber='+partnumber+'&userEmail='+emailId1+'&storeId='+storeId,'emailCallBack');
	}
}

//Moved from emailMe.js
function emailCallBack() {
	return;
}

//Moved from emailMeProductPage.js
function validateEmailId() {	
	var emailentered = document.getElementById('emailMeAddress').value;
	if(emailentered == '') {
	    alert("Email address is required");
	    return false;
	}
	if (!validateEmailAddress(emailentered.toLowerCase(),emailentered)) {
        return false;
    }
    return true;
}

function mePop(info,id) 
{	
	if (id != "" && id != null && !document.getElementById(id) && info != null )
	{
		if(!document.getElementById('grayDiv')){
			var grayDiv=document.createElement('div');
			grayDiv.id="grayDiv";
			grayDiv.className="transparent";
			grayDiv.style.zIndex = '55';
			document.body.appendChild(grayDiv);
			var gray=document.getElementById('grayDiv');
			gray.style.height=document.body.scrollHeight+200+"px";
			gray.style.width=document.body.scrollWidth+"px";
		} else if (document.getElementById('grayDiv').style.display=="none"){
			document.getElementById('grayDiv').style.display="";
		}
		
		var containerDiv=document.createElement('div');
		containerDiv.id=id;
		containerDiv.className='floatWindow';
		containerDiv.style.zIndex = '60';
		document.body.appendChild(containerDiv);

		//presell changes
		var releaseDate = '';
		var status ='delivery';
		if(info.indexOf("`") >= 0) {
			releaseDate=info.split("`")[0];
			info=info.split("`")[1];
		}
		if(releaseDate != '') {
			status = 'pre-order';
		}

		//Changes for emailMe.js and emailMeProductPage.js merging
		var infoArray=info.split("^ ");
		var pName='';
		var pId='';
		var pMfr='';
		var storename='';
		var URL='';
		var jspStoreImgDir='';
		var kmartButtonsDir='';
		var storeId='';
		var catalogId='';
		var currentPage = $('body').attr('id');
		
		//Set isProductPage flag, if it is product page
		if(currentPage == 'product' || currentPage == 'hardline' || currentPage == 'softline' || currentPage == 'collection') {
			isProductPage = true;
		}
		
		if(isProductPage) {
			var pName1 = infoArray[0];
			pName = deCodePName(pName1);
			pId = infoArray[1];
			pMfr = infoArray[2];
			storename =infoArray[3];
			catEntryId_1 = infoArray[4];
			URL = infoArray[5]; 
			jspStoreImgDir = infoArray[6];
			kmartButtonsDir = infoArray[7];
			storeId = infoArray[8];
			catalogId = infoArray[9];
		} else {
			pName=infoArray[0];
			pId=infoArray[1];
			pMfr=infoArray[2];
			catEntryId_1 = infoArray[3];
			URL = infoArray[4];
			jspStoreImgDir = infoArray[5];
			kmartButtonsDir = infoArray[6];
			storeId = infoArray[7];
			catalogId = infoArray[8];
		}		
		
		if(checkItemLevel == 0 ){
			if(isSoftlineOutStockEmail == 1){
				catEntryId_1 = _Pd.prod.prodList[emailPIndex].sProd;			
			}
		}
		
		//This block is moved from emailMe.js and modified for product page changes.
		switch (id)
		{
		case 'emailMe':
		var container=document.getElementById('emailMe');
		container.innerHTML="<p onclick='remove(this.parentNode);hideBox(\"grayDiv\");enableSelect();'><a href='javascript:;' class='closeWindow'>Close</a></p>\n"
			+ "<br clear='all'>\n"			
			+ "<div id='sorryMsg'>"
			+ "<p>We're sorry but the <b>"+pName+"</b> is temporarily unavailable for "+status+". Please enter your e-mail address below and we will notify you when it is available.</p>\n"
			+ "\t<input type='hidden'id='emailStoreId' name='storeId' value='"+storeId+"'>\n"
			+ "\t<input type='hidden' id='emailCatEntryId' name='catEntryId' value='"+catEntryId_1+"'>\n"			
			+ "\t<input type='hidden' id='emailPartNumber' name='partNumber' value='"+pId+"'>\n"
			+ "\t<p><b>Email address:</b> <input class='text'id='emailMeAddress' type='text' name='userEmail' size='20' value='"+getCookie('userEmail')+"'>\n"
			+ "\t<a href='javascript:;' onClick='if(validateEmailId()){showBox(\"thankYou\");hideBox(\"sorryMsg\");document.getElementById(\"emailAddress\").innerHTML=document.getElementById(\"emailMeAddress\").value;setCookie(\"userEmail\",document.getElementById(\"emailMeAddress\").value);tempFunc();}'><img id='emailMeSubmitBtn' width='65' height='21' alt='submit' align='top' style='margin-top: -1px; _margin-top:1px;'></a></p><br>\n"		
			+ "</div>\n"
			+ "<div id='thankYou' style='display:none;'>"
			+ "\t<p>Thank you. You will be notified at <b id='emailAddress'></b>&nbsp;when the following item is available:</p>"
			+ "\t<p><b>"+pName+"</b><br><small>"+storename+" Item #"+pId+" Mfr. model #"+pMfr+"</small></p>"
			+ "\t<p>Pricing is subject to change.</p>"
			+ "</div>";
			setTimeout(function(){
				if(typeof jspStoreImgDir != 'undefined' && typeof kmartButtonsDir != 'undefined') {					
					document.getElementById('emailMeSubmitBtn').src=jspStoreImgDir+kmartButtonsDir+"submit_blue.gif";					
				} else if(typeof imagePath != 'undefined') {
					document.getElementById('emailMeSubmitBtn').src=imagePath+'img/buttons/submit_blue.gif';
				}
			}, 0);
			break;

		case 'storePickup':
		var container=document.getElementById('storePickup');
		container.innerHTML="<p onclick='remove(this.parentNode);hideBox(\"grayDiv\");enableSelect();'><a href='javascript:;' class='closeWindow'>Close</a></p>\n"
			+ "<br clear='all'>\n"			
			+ "\t<input type='hidden' id='zipCatEntryId' name='catEntryId_1' value='"+catEntryId_1+"'>\n"
			+ "\t<input type='hidden' id='zipProductURL' name='URL' value='"+URL+"'>\n"			
			+ "\t<input type='hidden' id='zipPartNumber' name='partNumber' value='"+pId+"'>\n"
			+ "\t<input type='hidden' id='zipStoreId' name='storeId' value='"+storeId+"'>\n"
			+ "\t<input type='hidden' id='zipCatalogId' name='catalogId' value='"+catalogId+"'>\n"
			+ "\t<p><b>Enter Zip Code:</b> <input type='text' name='zipCode' id='zipCode' size='15' maxlength='5' class='text' value='"+getCookie('zipCode')+"' /> "
			+ "\t<a href='javascript:;' onClick='return storeLocatorPopup();'><img id='storePickupGoBtn' alt='go button' align='absmiddle'></p></a>";
			setTimeout(function(){document.getElementById('storePickupGoBtn').src=jspStoreImgDir+kmartButtonsDir+"go_blue.gif";},0);
			break;
		}

		// Set position of this div //
		var thisDIV=document.getElementById(containerDiv.id);
		/*
		if (this.innerHeight) // all except Explorer
		{
			xPos = (this.innerWidth/2);
			yPos = (this.innerHeight/2)+window.pageYOffset;
		}
		else if (document.body) // other Explorers
		{
			xPos = (document.body.clientWidth/2);
			yPos = (document.body.clientHeight/2)+document.body.scrollTop;
		}
		thisDIV.style.top=(yPos-(thisDIV.offsetHeight/2))+"px";
		thisDIV.style.left=(xPos-(thisDIV.offsetWidth/2))+"px";
		*/
		$(thisDIV).centerOnScreen();
		disableSelect();
		
	}
}

/* Common methods of emailMe.js and emailMeProductPage.js - Ends Here */


/* Methods for validating email in validateEmailAddress() - Starts Here */

// Function to trim the white spaces from the right side of a string 
function rtrim (s){	
   return s.replace( /\s*$/, "" );
}

// Function to trim the white spaces from the left side of a string
function ltrim ( s ){	
  return s.replace( /^\s*/, "" );
}

/* Methods for validating email in validateEmailAddress() - Ends Here */

/* This Code validates the Email entered*/

function validateEmailAddress(tempemail,displayEmail){
var email = $.trim(tempemail); //jquery to trim whitespace, replacing the rtrim and ltrim function, 2-13-09

if(email == '' ){
 alert("Email address is required");
 return false;
}

var regex1 = /^[^\s@]+@([A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]\.|[A-Za-z0-9]\.)+([A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9]|[A-Za-z0-9])$/; //legacy regex variables
var regex2 = /^(root@|abuse@|spam@)/; //legacy regex variables
var failedAlert = "The e-mail address you entered appears incorrect. (Example of a correct address: sears@sears.com.) Please check your information and try again.";

 if(!email.match(regex1)){
 alert(failedAlert);
 return false;
 } else if(email.match(regex2)){
 alert(displayEmail + " is not allowed");
 return false;
 }else if(!checkEmailChar(email)){
 alert(failedAlert);
 return false;
 }
return true;
}
