﻿// JScript File

function StoreHour(dayOfWeek, openHour, openMinute, closeHour, closeMinute) {
    this.dayOfWeek = dayOfWeek;
    this.openHour = openHour;
    this.openMinute = openMinute;
    this.closeHour = closeHour;
    this.closeMinute = closeMinute;
    
    this.spanToNextDay = function() {
        return this.getCloseHour() < this.getOpenHour();

    }
    
    this.getDayOfWeek = function() {
        return parseInt(this.dayOfWeek);
    }
    
    this.getOpenHour = function() {
        return parseInt(this.openHour);
    }
    
    this.getOpenMinute = function() {
        return parseInt(this.openMinute);
    }
    
    this.getCloseHour = function() {
        return parseInt(this.closeHour);
    }
    
    this.getCloseMinute = function() {
        return parseInt(this.closeMinute);
    }
}

function jsDate(year, month, day) {
    this.year = year;
    this.month = month;
    this.day = day;
    
    this.getYear = function() {
        return parseInt(this.year);
    }
    
    this.getMonth = function() {
        return parseInt(this.month);
    }
    
    this.getDay = function() {
        return parseInt(this.day);
    }
    
    this.toString = function() {
        return this.month + "-" + this.day + "-" + this.year;
    }
    
    
    this.toStringOrder2 = function() {
        return this.day + "-" + this.month + "-" + this.year;
    }
    
    this.toDate = function() {
        var d = new Date();
        //d.setFullYear(this.year, this.month > 0 ? this.month -1 : this.month, this.day);
        d.setFullYear(this.getYear(), this.getMonth() > 0 ? this.getMonth() - 1 : this.getMonth(), this.getDay());
        return d;
    }
    
    this.equals = function(target) {
        //return (this.year == anotherJsDate.year && this.month == anotherJsDate.month && this.day == anotherJsDate.day);
        return (this.getYear() == target.getYear() && this.getMonth() == target.getMonth() && this.getDay() == target.getDay());
    }
}

function Schedule() {
    this.storeEntryArray = new Array();
    
    this.addStoreHour = function(storeHour) {
        this.storeEntryArray.push(storeHour);
    }
    
    this.getStoreEntryByDayOfWeek = function(dayOfWeek) {

	
	//window.alert("array size: " + this.storeEntryArray.length);
	var currentIndex = 0;
	var currentOpenHour = 0;
 	var currentCloseHour = 0;
	var currentOpenMin = 0;
	var currentCloseMin = 0;
	var storeHourArrayToReturn = new Array();

	while(currentIndex < this.storeEntryArray.length){
		
		var dow = parseInt(dayOfWeek);
		if(dayOfWeek == -1){
		     	dow = 6;
		}
		else if(dayOfWeek == 7) {
			dow = 0;
		}

		if(this.storeEntryArray[currentIndex].dayOfWeek == dow){

			//if(storeHourToReturn == null){
				
				storeHourArrayToReturn.push(new StoreHour(this.storeEntryArray[currentIndex].dayOfWeek, this.storeEntryArray[currentIndex].openHour, this.storeEntryArray[currentIndex].openMinute, this.storeEntryArray[currentIndex].closeHour, this.storeEntryArray[currentIndex].closeMinute));
				
			//}
			//else{

				
			//}
		}

		currentIndex++;
	}
	
	
	return storeHourArrayToReturn;

	/*
	
	
        for(x in this.storeEntryArray) {
            var dow = parseInt(dayOfWeek);
            if(dayOfWeek == -1) {
                dow = 6;
            } else if(dayOfWeek == 7) {
                dow = 0;
            }
            
	    
            if(this.storeEntryArray[x].dayOfWeek == dow) {
		//window.alert("return: " + this.storeEntryArray[x].dayOfWeek + " Open Hour: " + this.storeEntryArray[x].openHour);
                return this.storeEntryArray[x];
		
            }
        }
        
        return null;
	*/
    }
    
    this.setSchedule = function(selectedDayOfWeek, selectedDate, today, currentHour, currentMinute) {
        var scheduleDate = document.getElementById('scheduleDate');
        if(scheduleDate != null) {
            if(jMessage['dateFormat'] == "2"){
                
                scheduleDate.value = selectedDate.toStringOrder2();
            }
            else{
                scheduleDate.value = selectedDate.toString();
            }
        }
        
        var scheduleTime = document.getElementById('scheduleTime');
        if(scheduleTime != null) {
            scheduleTime.options.length = 0;
            scheduleTime.options[0] = new Option(jMessage['selectATime']);

            this.renderTime(scheduleTime, selectedDayOfWeek, selectedDate, today, currentHour, currentMinute);
        }
    }


    this.renderTime = function(objSelect, selectedDayOfWeek, selectedDate, today, currentHour, currentMinute) {
        
	
	//var selectedDayHourEntry = this.getStoreEntryByDayOfWeek(selectedDayOfWeek); 

	
        var selectedDayHourEntryArray = this.getStoreEntryByDayOfWeek(selectedDayOfWeek);
        for(x in selectedDayHourEntryArray){
		
		if(selectedDate.equals(today)) {
            
                	this.renderOption(objSelect, currentHour, currentMinute,
                    		selectedDayHourEntryArray[x].getOpenHour(), selectedDayHourEntryArray[x].getOpenMinute(),
                    		selectedDayHourEntryArray[x].getCloseHour(), selectedDayHourEntryArray[x].getCloseMinute(),
                    		selectedDate, today);
            
        	} else {
            		var closeHour = selectedDayHourEntryArray[x].getCloseHour();
            		var closeMinute = selectedDayHourEntryArray[x].getCloseMinute();
            
            	if(closeHour < selectedDayHourEntryArray[x].getOpenHour()) {
                	closeHour = 23;
                	closeMinute = 60;
            	}
            
            	this.renderOption(objSelect, 0, 0,
                	selectedDayHourEntryArray[x].getOpenHour(), selectedDayHourEntryArray[x].getOpenMinute(),
                	closeHour, closeMinute,
                	selectedDate, today);
                
        	}
	}
        
        
        
        
        
    }
    
/*
    
    this.renderTime = function(objSelect, selectedDayOfWeek, selectedDate, today, currentHour, currentMinute) {
        
	//window.alert("selected day of the week " + selectedDayOfWeek);
	var selectedDayHourEntry = this.getStoreEntryByDayOfWeek(selectedDayOfWeek); 

	//window.alert("previous day of the week ");
	           
        var previousDayHourEntry = this.getStoreEntryByDayOfWeek(selectedDayOfWeek - 1);
        
        
        if(previousDayHourEntry.spanToNextDay()) {
            
            this.renderOption(objSelect, currentHour, currentMinute,
                0, 0,
                previousDayHourEntry.getCloseHour(), previousDayHourEntry.getCloseMinute(),
                selectedDate, today);
        }
        
        if(selectedDate.equals(today)) {
            if(selectedDayHourEntry.spanToNextDay()) {
                this.renderOption(objSelect, currentHour, currentMinute,
                    selectedDayHourEntry.getOpenHour(), selectedDayHourEntry.getOpenMinute(),
                    23, 59,
                    selectedDate, today);
            } else {
                this.renderOption(objSelect, currentHour, currentMinute,
                    selectedDayHourEntry.getOpenHour(), selectedDayHourEntry.getOpenMinute(),
                    selectedDayHourEntry.getCloseHour(), selectedDayHourEntry.getCloseMinute(),
                    selectedDate, today);
            }
        } else {
            var closeHour = selectedDayHourEntry.getCloseHour();
            var closeMinute = selectedDayHourEntry.getCloseMinute();
            
            if(closeHour < selectedDayHourEntry.getOpenHour()) {
                closeHour = 23;
                closeMinute = 60;
            }
            
            this.renderOption(objSelect, 0, 0,
                selectedDayHourEntry.getOpenHour(), selectedDayHourEntry.getOpenMinute(),
                closeHour, closeMinute,
                selectedDate, today);
                
        }
        
        
    }
*/
    
    this.renderOption = function(objSelect, currentHour, currentMinute, openHour, openMinute, closeHour, closeMinute, selectedDay, today) {
        var hh = 0;
        var mm = 0;
       
        if(! selectedDay.equals(today) && openHour == 0 && openMinute == 0) {
            hh = openHour;
            hh = openMinute;
        } else {
            if(openHour > currentHour) {
                hh = openHour;
                mm = openMinute;
            } else if(openHour < currentHour) {
                hh = currentHour;
                while(mm <= currentMinute) {
                    mm+= 15;
                }
                
                if(mm >= 60) {
                    mm = 0;
                    hh++;
                }
                
                if((mm - currentMinute) < 15) {
                    mm += 15;
                    if(mm >= 60) {
                        mm = 0;
                        hh++;
                    }
                }
            } else {
                hh = openHour;
                
                if(openMinute > currentMinute) {
                    mm = openMinute;
                } else {
                    while(mm <= currentMinute) {
                        mm+= 15;
                    }
                    
                    if(mm >= 60) {
                        mm = 0;
                        hh++;
                    }
                    
                    if((mm - currentMinute) < 15) {
                        mm += 15;
                        if(mm >= 60) {
                            mm = 0;
                            hh++;
                        }
                    }
                }
            }
        }
        
        var tt;
        var optionValue;
        var absHour;
        

	
	
        while(hh <= closeHour) {
            if(hh >= 12) {
                tt = "PM";
            } else {
                tt = "AM";
            }
            
            
            
            while(mm < 60) {
            
                if(hh == closeHour && mm > closeMinute) {
                    break;
                }
            
                if(hh == 0 || hh > 12) {
                    absHour = Math.abs(hh - 12);
                } else {
                    absHour = hh;
                }
                
                optionValue = absHour < 10 ? "0" + absHour : absHour;
                optionValue += ":" + (mm < 10 ? "0" + mm : mm) + " " + tt;
                
		
                objSelect.options[objSelect.options.length] = new Option(optionValue, optionValue);
                
                mm+= 15;
            }
            
            mm = 0;
            hh++;
        }

	
    }
    
}








function BaseValidator(controlName, errorMessage) {
	this.controlName = controlName;
	this.errorMessage = errorMessage;
	
	this.init = function(controlName, errorMessage) {
		this.controlName = controlName;
		this.errorMessage = errorMessage;
	}
	
	this.trim = function(str) {
		if(str != null) {
			var newStr = str.replace(/^\s*/, '').replace(/\s*$/, ''); 
			if(newStr != '') {
				return newStr;
			}
		}
		
		return null;
	}
	
	this.displayErrorMessage = function() {
		alert(this.errorMessage);
	}
}



function UserNameValidator(controlName, nullMessage, invalidMessage) {
	this.init(controlName, nullMessage);
	this.nullMessage = nullMessage;
	this.invalidMessage = invalidMessage;
	
	
	this.isValid = function() {
		var control = document.getElementsByName(this.controlName);
		if(control[0].disabled) {
			return true;
		}
		
		var userName = this.trim(control[0].value);
		if(userName != null) {
		    var pattern = '^\\w+$';
		    var regex = new RegExp(pattern);
		    if(regex.test(userName)) {
		        return true;
		    } else {
		        this.errorMessage = this.invalidMessage;
		    }
		} else {
		    this.errorMessage = this.nullMessage;
		}

		control[0].focus();
		return false;
	}
}
UserNameValidator.prototype = new BaseValidator();
UserNameValidator.prototype.constructor = UserNameValidator;
UserNameValidator.superclass = BaseValidator.prototype;





function TextValidator(controlName, errorMessage) {
	this.init(controlName, errorMessage);
	
	this.isValid = function() {
		var control = document.getElementsByName(this.controlName);
		if(control[0].disabled) {
			return true;
		}
		
		if(this.trim(control[0].value) != null) {
			return true;
		}

		control[0].focus();
		return false;
	}
}
TextValidator.prototype = new BaseValidator();
TextValidator.prototype.constructor = TextValidator;
TextValidator.superclass = BaseValidator.prototype;


function EmailValidator(controlName, nullMessage, invalidMessage) {
	this.init(controlName, nullMessage);
	this.nullMessage = nullMessage;
	this.invalidMessage = invalidMessage
	
	this.isValid = function() {
		var control = document.getElementsByName(this.controlName);
		if(control[0].disabled) {
			return true;
		}
		
		if(control[0].value != null) {
		    var controlValue = this.trim(control[0].value);
			if(controlValue != null) {
			    if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(controlValue)) {
			        return true;
			    } else {
			        this.errorMessage = this.invalidMessage;
			    }
			    
			    /*
			    var pattern = '^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$';
			    
			    var regex = new RegExp(pattern);
			    if(regex.test(controlValue)) {
				    return true;
				} else {
				    this.errorMessage = this.invalidMessage;
				}
				*/
			}
		} else {
		    this.errorMessage = this.nullMessage;
		}

		control[0].focus();
		return false;
	}
}
EmailValidator.prototype = new BaseValidator();
EmailValidator.prototype.constructor = EmailValidator;
EmailValidator.superclass = BaseValidator.prototype;


function CheckboxValidator(controlName, minChecked, errorMessage) {
    this.init(controlName, errorMessage);
    this.minChecked = minChecked;
    
    this.isValid = function() {
        var checkbox = document.getElementsByName(this.controlName);
        var totalChecked = 0;
        
        for(var i = 0; i < checkbox.length; i++) {
            if(checkbox[i].checked && (!checkbox[i].disabled)) {
                totalChecked++;
             }
        }
        
        return (totalChecked >= this.minChecked);
    }
}
CheckboxValidator.prototype = new BaseValidator();
CheckboxValidator.prototype.constructor = CheckboxValidator;
CheckboxValidator.superclass = BaseValidator.prototype;





function RadioValidator(controlName, errorMessage) {
    this.init(controlName, errorMessage);
    
    this.isValid = function() {
        var checkbox = document.getElementsByName(this.controlName);
        for(var i = 0; i < checkbox.length; i++) {
            if(checkbox[i].checked && (!checkbox[i].disabled)) {
                return true;
             }
        }
        
        return false;
    }
    
    this.getValue = function() {
        var checkbox = document.getElementsByName(this.controlName);
        for(var i = 0; i < checkbox.length; i++) {
            if(checkbox[i].checked && (!checkbox[i].disabled)) {
                return checkbox[i].value;
             }
        }
        
        return '';
    }
}
RadioValidator.prototype = new BaseValidator();
RadioValidator.prototype.constructor = RadioValidator;
RadioValidator.superclass = BaseValidator.prototype;



function PasswordValidator(password1, password2, nullMessage, mismatchedMessage) {
	//this.init(null, nullMessage);
	this.nullMessage = nullMessage;
	this.mismatchedMessage = mismatchedMessage;
	this.password1 = password1;
	this.password2 = password2;
	
	this.isValid = function() {
	    var p1Control = document.getElementsByName(this.password1)[0];
	    var p2Control = document.getElementsByName(this.password2)[0];
	    
		var p1 = this.trim(p1Control.value);

		if(p1 == null) {
		    this.errorMessage = this.nullMessage;
		    p1Control.focus();
		} else {
		    var p2 = this.trim(p2Control.value);
		    if(p2 != null && p1 == p2) {
			    return true;
		    } else {
		        this.errorMessage = this.mismatchedMessage;
		        p2Control.focus();
		    }
		}
		
		return false;
	}
}
PasswordValidator.prototype = new BaseValidator();
PasswordValidator.prototype.constructor = PasswordValidator;
PasswordValidator.superclass = BaseValidator.prototype;




function FormValidator() {
	this.validatorArray = new Array();
	
	this.addValidator = function(validator) {
		this.validatorArray.push(validator);
	}
	
	this.validate = function() {
		for(var i = 0; i < this.validatorArray.length; i++) {
			var obj = this.validatorArray[i];
			if(! obj.isValid()) {
				obj.displayErrorMessage();
				return false;
			}
		}
		return true;
	}
}


function StateDeliveryRate(stateAbbr, stateName, rate, rateLabel, cartTotalWithRate){

    this.stateAbbr = stateAbbr;
    this.stateName = stateName;
    this.rate = rate;
    this.rateLabel = rateLabel;
    this.cartTotalWithRate = cartTotalWithRate;

}

function DeliveryZipCodeRate(zipCode, rate, rateLabel, cartTotalWithRate){

    this.zipCode = zipCode;
    this.rate = rate;
    this.rateLabel = rateLabel;
    this.cartTotalWithRate = cartTotalWithRate;

}

function DeliveryCityRate(cityName, rate, rateLabel, cartTotalWithRate){
    this.cityName = cityName;
    this.rate = rate;
    this.rateLabel = rateLabel;
    this.cartTotalWithRate = cartTotalWithRate;
}

function CreditCardInfoReq(cardName, requireInfo){
    this.cardName = cardName;
    this.requireInfo = requireInfo;
}


