

/********************************************************************
	 Store Locator Related Common Functions : Starts 
*********************************************************************/

/**********************************************************************
 * Function Name     : fnGetStoreHourHtml
 * Description       : This function will generate the HTML to be 
 *	displayed for each store as the Store Hour
 **********************************************************************/
function fnGetStoreHourHtml(strInfo){
	var html ="";
	var ohour, chour;
	ohour = strInfo.wkophr;
	chour = strInfo.wkclhr;
	if(ohour >= 0 && chour > 0){
		html += "<br><span>Mon-Fri: ";
		html += fnCreateStoreHours(ohour,chour);
		html += "</span>";
	}

	ohour = strInfo.satophr;
	chour = strInfo.satclhr;
	if(ohour >= 0 && chour >0){
		html += "<br><span>Sat: ";
		html += fnCreateStoreHours(ohour,chour);
		html += "</span>";
	}

	ohour = strInfo.sunophr;
	chour = strInfo.sunclhr;
	if(ohour >= 0 && chour >0){
		html += "<br><span>Sun: ";
		html += fnCreateStoreHours(ohour,chour);
		html += "</span>";
	}
	if(html != ""){
		html = "<p class=\"hours\"><strong>Hours:</strong>" + html + "</p>";
	}
	return html;
}

/**********************************************************************
 * Function Name     : fnGetHolidayHourHtml
 * Description       : This function will generate the HTML to be 
 *	displayed for each store as the Store Hour
 **********************************************************************/
function fnGetStoreServicesHtml(strInfo){
 
		var html = "";
		var attrArray = new Array();
		var name, rank;
		for(var i=0; i < strInfo.attrname.length; i++){
			try{
				name = strInfo.attrname[i];
				if(name == "" || name == null)
					break;
					
				rank = strInfo.attrrank[i];
				flag = strInfo.attrflag[i];
				if(flag != ""){
					attrArray.push(new SearsAttr(name, rank, flag));
				}
			}catch(ex){
				break;
			}

		}
		
		if(attrArray.length > 0){
			attrArray.sort(sortAttributes);
			
			var attr;
			for(var i=0; i < attrArray.length; i++){
				var storeType = attrArray[i].name;
				if(storeType == "STORE PICK UP"){
					html += "<br><span class=\"spu\">"+storeType.titleCase()+"</span><br>"
				}else{
					html += "<span>"+storeType.titleCase()+"</span>"
				}
			}
			
			if(html != ""){
				html = "<p class=\"services\"><strong>Store Services:</strong>" + html + "</p>";
			}
		}
		return html;

}
function SearsAttr(name, rank, flag){
	this.name = name;
	this.rank = rank;
	this.flag = flag;
}
function sortAttributes(a, b){
	var rank1 = parseInt(a.rank,10);
	var rank2 = parseInt(b.rank,10);
	return rank1 - rank2;
}
var monthnames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

/**********************************************************************
 * Function Name     : fnGetHolidayHourHtml
 * Description       : This function will generate the HTML to be 
 *	displayed for each store as the Store Hour
 **********************************************************************/
function fnGetHolidayHourHtml(strInfo){
	var html ="";
	var name, ohour, chour;
	for(var i=0; i < strInfo.hdt.length && i < 7 ; i++){
		name = getHolidayDate(strInfo.hdt[i]);
		if(name != ""){
			ohour = strInfo.hopntm[i];
			chour = strInfo.hclstm[i];
			if(chour >= 0){
				html += "<br><span>" + name + ": ";
				if(ohour == 0 && chour == 0){
					html += " CLOSED";
				}else if(ohour > 0 && chour > 0){
					html +=	fnCreateStoreHours(ohour,chour);
				};
				html += "</span>";					
			};
		};
	};
	
	if(html != ""){
		html = "<p class=\"hours\"><strong>Holiday Hours:</strong>" + html + "<br></p>";
	}
	return html;
}

function getHolidayDate(dt){
	if(dt == "0000-00-00"){
		return "";
	};
	
	var year = dt.substring(0, dt.indexOf("-"));
	var month = parseInt(dt.substring(dt.indexOf("-")+1, dt.lastIndexOf("-")))-1;
	var date = dt.substring(dt.lastIndexOf("-")+1);
	if((new Date(month + "/" + date + "/" + year))  > new Date()){
		return "";
	}
	else
		return monthnames[month] + " " + date + ", " + year;
}

/**********************************************************************
 * Function Name     : fnCreateStoreHours
 * Description       : This is an internal function used to Create HTML 
 *	for store hours
 **********************************************************************/
function fnCreateStoreHours(ohour,chour){
	ohour = getHoursFromSeconds(ohour);
	chour = getHoursFromSeconds(chour);
	
	var html="";
	if(ohour == 0){
		html += "12:00 am";
	}

	if(ohour> 0 && ohour < 1 ){
		html += fnDisplayTime(12+(ohour%12)) + " am";
	}else if(ohour >= 1 && ohour < 12){
		html += fnDisplayTime(ohour) + " am";
	}else if (ohour == 12) {
		html += fnDisplayTime(ohour) + " pm";
	}else if (ohour >12 && ohour<13) {
		html += fnDisplayTime(12+(ohour%12)) + " pm";
	}else if (ohour >=13 ){
		html += fnDisplayTime((ohour%12)) + " pm";
	};

	html += " - ";

	if(chour == 0){
		html += "12:00 am";
	}if(chour> 0 && chour < 1 ){
		html += fnDisplayTime(12+(chour%12)) + " am";
	}else if(chour >= 1 && chour < 12){
		html += fnDisplayTime(chour) + " am";
	}else if (chour == 12) {
		html += fnDisplayTime(chour) + " pm";
	}else if (chour >12 && chour<13) {
		html += fnDisplayTime(12+(chour%12)) + " pm";
	}else if (chour >=13 ) {
		html += fnDisplayTime((chour%12)) + " pm";
	};

	return html;
}

function getHoursFromSeconds(sec){
	var sec = trimSPUStr(sec);
	var disptime;
	if(sec != ""){
		sec = parseInt(sec) / 60 / 60;
	 
		//correct min display  sec = parseInt(sec);	
	}
	else {
		sec = -1
	};
	return sec;	
}

function trimSPUStr(st) {
	if(st == undefined || st == null){
		return "";
	}
	st += "";
	var len = st.length
	var begin = 0, end = len - 1;
	while ((st.charAt(begin) == " " || st.charAt(begin) == "\n") && begin < len) {
		begin++;
	}
	while ((st.charAt(end) == " " || st.charAt(end) == "\n") && begin < end) {
		end--;
	}
	return st.substring(begin, end+1);
}

/**********************************************************************
 * Function Name     : fnDisplayTime
 * Description       : This is an internal function used to correct the 
 *	min display time.
 **********************************************************************/
function fnDisplayTime(time) {
	var hrs = Math.floor(time);
	var min = time - hrs;
	min = min * 60;

	if(min.toString().length>2){
	min=parseInt(min)
	};
	if (min == 0) {
		min = "00";
	}
	return (hrs+":"+min);
}

/* Code to display result : Ends */


/* Code for titleCase() function Starts */
String.noLC = new Object({
	the:1, a:1, an:1, and:1, or:1, but:1, aboard:1,
	about:1, above:1, across:1, after:1, against:1,
	along:1, amid:1, among:1, around:1, as:1, at:1,
	before:1, behind:1, below:1, beneath:1, beside:1,
	besides:1, between:1, beyond:1, but:1, by:1, 'for':1,
	from:1, 'in':1, inside:1, into:1, like:1, minus:1,
	near:1, of:1, off:1, on:1, onto:1, opposite:1,
	outside:1, over:1, past:1, per:1, plus:1,
	regarding:1, since:1, than:1, through:1, to:1,
	toward:1, towards:1, under:1, underneath:1, unlike:1,
	until:1, up:1, upon:1, versus:1, via:1, 'with':1,
	within:1, without:1
});

String.acronyms = new Object({
    le: 1
});

String.map = new Object({
    le: 'Lands\' End',
    rd: 'Rd'
});

String.prototype.titleCase = function() {
    var parts = this.toLowerCase().split(' ');
    if (parts.length == 0) return '';

    var fixed = new Array();
    var i =0;
    for (var i=0; i<parts.length; i++) {
        var fix = '';
        if (String.map[parts[i]]) {
            fix = String.map[parts[i]];
        }else if (String.noLC[parts[i]]) {
            fix = parts[i].toLowerCase();
        }else if (parts[i].match(/^([A-Z]\.)+$/i)) {
        	// will mess up "i.e." and like
            fix = parts[i].toUpperCase();
        }else if (String.acronyms[parts[i]]) {
        	// acronyms
            fix = parts[i].toUpperCase();
        }else if (parts[i].match(/^[^aeiouy]+$/i)) {
        	// voweless words are almost always acronyms
            fix = parts[i].toUpperCase();
        }else {
            fix = parts[i].substr(0, 1).toUpperCase() + parts[i].substr(1, parts[i].length);
        }
        if(fix.name != undefined){
        	fixed.push(fix.name);
        }else{
        	fixed.push(fix);
        }
    }
    fixed[0] = fixed[0].substr(0, 1).toUpperCase() + fixed[0].substr(1, fixed[0].length);
    return fixed.join(' ');
}
/* Code for titleCase() function Ends */


// Function to format the phone number
function fnFormatPhoneNo(phoneNo){
	var retPhoneNo="";
	if(!phoneNotValid(phoneNo)){
	
	if (phoneNo.length == 10){
		retPhoneNo = "(";
		for (i = 0; i < 3 ; i++){
		retPhoneNo += phoneNo.charAt(i);
		}
		retPhoneNo += ") ";
		for ( i = 3; i<6 ; i++){
		retPhoneNo += phoneNo.charAt(i);
		}
		retPhoneNo += "-";
		for ( i = 6; i<10 ; i++){	
		retPhoneNo += phoneNo.charAt(i);
		}
	}else{
		retPhoneNo = phoneNo;
	}
	}	
	return retPhoneNo;
}

 //****************************************************************************************************
// Is Blank Function
//****************************************************************************************************
function isBlank(str){
	//Pre: str != null
	//Post: if str = '' return true, else false
	str = trim(str);
	if(str == ''|| str=='(000)000-0000'|| str=='(#00)000-0000') return true;
	return false;
}
//****************************************************************************************************
// phoneNotValid Function
//****************************************************************************************************
function phoneNotValid(str){
	 str = trim(str);
	if(str=='(000)000-0000'|| str=='(#00)000-0000' || str=='#') return true;
	return false;
}

//****************************************************************************************************
// trim Function
//****************************************************************************************************

function trim(st) {
	var len = st.length
	var begin = 0, end = len - 1;
	while ((st.charAt(begin) == " " || st.charAt(begin) == "\n") && begin < len) {
		begin++;
	}
	while ((st.charAt(end) == " " || st.charAt(end) == "\n") && begin < end) {
		end--;
	}
	return st.substring(begin, end+1);
}
/********************************************************************
	 Store Locator Related Common Functions : Ends 
*********************************************************************/