var Site = {
	start: function() {
		Site.textSize.load({'id':'txtSize', 'cookieName':'textSize'});
		Site.tips.init();

	},
	textSize: {
		cName: null,
		opts: { duration: 60 },
		scale: ['100%','110%','120%', '130%'],
		myScale: null,
		els: null,
		curr: 0,
		load: function(obj) {
			if (!$(obj.id)) return
			
			this.cName = obj.cookieName
			//if doesnt exist, set to 0
			if (!$chk(Cookie.read(this.cName))) Cookie.write(this.cName, 0, this.opts)
			
			this.myScale = parseInt(Cookie.read(this.cName))
			
			this. els = $(obj.id).getElements('a')
						
			this.els.each(function(el, i) {
				el.set('href', 'javascript:void(0);')
				el.store('size', i)
				el.addEvent('click', function() {
					this.processScale(el)
				}.bind(this))
			}, this)
			this.targeted = $("siteBody") || $(document.body)
			this.targeted.setStyle('font-size', this.scale[this.myScale])			
		},
		setCookie: function() {
			Cookie.write(this.cName, this.myScale, this.opts)
		},
		processScale: function(el) {
			var size = el.retrieve('size')
			size = (size == 0) ? 'plus' : 'minus'
			switch (size) {
				case 'plus':
					this.myScale = (this.myScale + 1 > this.scale.length - 1) ? this.scale.length - 1 : this.myScale + 1				
					break;
				case 'minus':
					this.myScale = (this.myScale - 1 < 0) ? 0 : this.myScale - 1
					break;
			}
			
			this.setCookie()
			this.targeted.setStyle('font-size', this.scale[this.myScale])	
		}		
	},
	
	tips: {
		init: function() {
			var anchors = $$(".term");
			anchors.each(function(el, i) {
				var t = el.get("title") || "";
				var title = (t.indexOf("::") != -1) ? t.split("::") : ["", t];
				
				var link = el.get("href");
				var endlink = "<a href='" + link + "'>More&raquo;</a>";
				//el.store("tip:title", title[0] + "&nbsp;-&nbsp");
				el.store("tip:title", "");
				el.store("tip:text", title[1] + " " + endlink);				
			});
			
			this.myTips = new Tips(anchors, {
				className: "tip-dialog",
				hideDelay: 700,
				offset: {x: 16, y: 16}
			});
		}
	}
		
}

Tips.implement("toElement", function(){		
		if (this.tip) return this.tip;		
		return this.tip = new Element('div', {
			'class': this.options.className,
			styles: {
				position: 'absolute',
				top: 0,
				left: 0
			}
		}).addEvents({
			"mouseenter": function() {
				$clear(this.timer);		
			}.bind(this),
			"mouseleave": function() {
				$clear(this.timer);
				this.timer = this.hide.delay(this.options.hideDelay, this, this.tip);			
			}.bind(this)
		}).adopt(
			new Element('div', {'class': 'tip-top'}),
			this.container,
			new Element('div', {'class': 'tip-bottom'})
		).inject(document.body);
	}
);



window.addEvent('domready', function() {
	Site.start();	
});



function submitSearchForm() {
    var searchTerm = "ctl00_Top_SearchText";
    if (validateSearchForm(searchTerm)) {
        window.location = window.root + 'Search_Results.aspx?q=' + document.getElementById(searchTerm).value;
    }
    return false;
}
function validateSearchForm(searchTerm) {
    var valid = true;
    if (document.getElementById(searchTerm).value == "Search" ||
    document.getElementById(searchTerm).value.length == 0) {
        window.alert("Please enter a search text.");
        valid = false;
    }
    else {
        var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";
        for (var i = 0; i < document.getElementById(searchTerm).value.length; i++) {
            if (iChars.indexOf(document.getElementById(searchTerm).value.charAt(i)) != -1) {
                window.alert("Your search text has special characters. \nThese are not allowed.\nPlease remove them and try again.");
                valid = false;
                break;
            }
        }
    }
    return valid;
}

function setRightColumnDisplay()
	{
		var rightCol = document.getElementById('GreenWrapCol');		
		if(rightCol.childNodes.length < 1)
		{
			rightCol.style.display = 'none';
		}
		else if(rightCol.childNodes.length == 1)
		{
			if(rightCol.childNodes[0] == "Text" && rightCol.childNodes[0].value == 'undefined');
				rightCol.style.display = 'none';
		}
	}
	
function submitSearchFormHCP() {
    var searchTerm = "ctl00_Top_SearchText";
    if (validateSearchForm(searchTerm)) {
        window.location = window.root + 'hcp/Search_Results.aspx?q=' + document.getElementById(searchTerm).value;
    }
    return false;
}

function confirmHCP()
{
	var bln = confirm('Please certify that you are a healthcare professional licensed and practicing in the United States.');
	if (bln)
	{
		window.location.href= window.root+'hcp/Default.aspx';
	}
}

function printGraph()
{
    var printWin = window.open(window.root+"images/hcpgraph_2.1_large.gif", "_blank");
    printWin.print();
}

function openWin(windowURL, windowName, windowFeatures) {
    window.open(windowURL, windowName, windowFeatures);
}

function validateAge(source, args) {
    var yearObj = document.getElementById('ctl00_MainContent_UserDateOfBirthYear');
    var monthObj = document.getElementById('ctl00_MainContent_UserDateOfBirthMonth');
    var dayObj = document.getElementById('ctl00_MainContent_UserDateOfBirthDay');
    var now = new Date();
    try {
        var ourdate = new Date(monthObj.value + "/" + dayObj.value + "/" + yearObj.value);
        var diff = days_between(now, ourdate)
        args.IsValid = diff >= 6576;
    } catch (x) {
        args.IsValid = true;
    }
}

function days_between(date1, date2) {
    var ONE_DAY = 1000 * 60 * 60 * 24// The number of milliseconds in one day
    var date1_ms = date1.getTime()// Convert both dates to milliseconds
    var date2_ms = date2.getTime()
    var difference_ms = Math.abs(date1_ms - date2_ms)// Calculate the difference in milliseconds
    return Math.round(difference_ms / ONE_DAY)// Convert back to days and return
}

function validateFirstName(source, args) {
    var firstNameObj = document.getElementById('ctl00_MainContent_UserFirstName');
    if (firstNameObj.value == "") {
        args.IsValid = true;
        return;
    }
    try {
        var lg = firstNameObj.value.length;
        args.IsValid = parseInt(lg) < 21;
    } catch (x) {
        args.IsValid = true;
    }
}

function validateLastName(source, args) {
    var lastNameObj = document.getElementById('ctl00_MainContent_UserLastName');
    if (lastNameObj.value == "") {
        args.IsValid = true;
        return;
    }
    try {
        var lg = lastNameObj.value.length;
        args.IsValid = parseInt(lg) < 21;
    } catch (x) {
        args.IsValid = true;
    }
}

function validateLastName1(source, args) {
    var lastNameObj = document.getElementById('ctl00_MainContent_UserLastName');
    if (lastNameObj.value == "") {
        args.IsValid = true;
        return;
    }
    try {
        if (lastNameObj.value.match(" "))
            lastNameObj.value = lastNameObj.value.replace(" ", "");
        var lg = lastNameObj.value.length;
        args.IsValid = parseInt(lg) > 1;
    } catch (x) {
        args.IsValid = true;
    }
}
function validateAddress1(source, args) {
    var address1Obj = document.getElementById('ctl00_MainContent_UserAddress1');
    if (address1Obj.value == "") {
        args.IsValid = true;
        return;
    }
    try {
        var lg = address1Obj.value.length;
        args.IsValid = parseInt(lg) < 21;
    } catch (x) {
        args.IsValid = true;
    }
}

function validateAddress2(source, args) {
    var address2Obj = document.getElementById('ctl00_MainContent_UserAddress2');
    if (address2Obj.value == "") {
        args.IsValid = true;
        return;
    }
    try {
        var lg = address2Obj.value.length;
        args.IsValid = parseInt(lg) < 21;
    } catch (x) {
        args.IsValid = true;
    }
}

function validateCity(source, args) {
    
    var cityObj = document.getElementById('ctl00_MainContent_UserCity');
    if (cityObj.value == "") {
        args.IsValid = true;
        return;
    }
    try {
        var lg = cityObj.value.length;
        args.IsValid = parseInt(lg) < 21;
    } catch (x) {
        args.IsValid = true;
    }
}

function validateYearDiagnosed(source, args) {
    var yearObj = document.getElementById('ctl00_MainContent_UserYear');
    var monthObj = document.getElementById('ctl00_MainContent_UserMonth');
    if (yearObj.value == "0") {
        if (monthObj.value != "0")
            args.IsValid = false;
            return;
    }
    else
    { 
        args.IsValid = true;
        return;
    } 
}



function onlyNumbers(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;

}
    