// Print function

function printit(){
    if (window.print) {
        window.print() ;
    } else {
        alert('Please use the print feature of your browser.');
    }
} 


//Empty form fields

function clearText(objField){
    if (objField.defaultValue==objField.value)
    objField.value = ""
}
function resetText(objField){
    if (objField.value=="")
    objField.value = objField.defaultValue
}


//JS for Suckerfish dropdown nav

sfHover = function() {
var sfEls = document.getElementById("nav").getElementsByTagName("LI");
for (var i=0; i<sfEls.length; i++) {
	sfEls[i].onmouseover=function() {
		this.className+=" sfhover";
	}
	sfEls[i].onmouseout=function() {
		this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
	}
}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);



/// ACCESSIBLE POPUP SCRIPT ///////////////////////////////////////////////////////////
// ARRAY EXTENSIONS

if (!Array.prototype.push) Array.prototype.push = function() {
    for (var i=0; i<arguments.length; i++) this[this.length] = arguments[i];
    return this.length;
}

Array.prototype.find = function(value, start) {
    start = start || 0;
    for (var i=start; i<this.length; i++)
        if (this[i]==value)
            return i;
    return -1;
}

Array.prototype.has = function(value) {
    return this.find(value)!==-1;
}

// FUNCTIONAL

function map(list, func) {
    var result = [];
    func = func || function(v) {return v};
    for (var i=0; i < list.length; i++) result.push(func(list[i], i, list));
    return result;
}

function filter(list, func) {
    var result = [];
    func = func || function(v) {return v};
    map(list, function(v) { if (func(v)) result.push(v) } );
    return result;
}


// DOM

function getElem(elem) {
    if (document.getElementById) {
        if (typeof elem == "string") {
            elem = document.getElementById(elem);
            if (elem===null) throw 'cannot get element: element does not exist';
        } else if (typeof elem != "object") {
            throw 'cannot get element: invalid datatype';
        }
    } else throw 'cannot get element: unsupported DOM';
    return elem;
}

function hasClass(elem, className) {
    return getElem(elem).className.split(' ').has(className);
}

function getElementsByClass(className, tagName, parentNode) {
    parentNode = !isUndefined(parentNode)? getElem(parentNode) : document;
    if (isUndefined(tagName)) tagName = '*';
    return filter(parentNode.getElementsByTagName(tagName),
        function(elem) { return hasClass(elem, className) });
}

// MISC CLEANING-AFTER-MICROSOFT STUFF

function isUndefined(v) {
    var undef;
    return v===undef;
}

// These defaults should be changed the way it best fits your site
var _POPUP_FEATURES = '';

function raw_popup(url, target, features) {
    // pops up a window containing url optionally named target, optionally having features
    if (isUndefined(features)) features = _POPUP_FEATURES;
    if (isUndefined(target  )) target   = '_blank';
    var theWindow = window.open(url, target, features);
    theWindow.focus();
	theWindow.moveTo(0,0);
    return theWindow;
}

function link_popup(src, features) {
    // to be used in an html event handler as in: <a href="..." onclick="link_popup(this,...)" ...
    // pops up a window grabbing the url from the event source's href
    return raw_popup(src.getAttribute('href'), src.getAttribute('target') || '_blank', features);
}

/// END ACCESSIBLE POPUP SCRIPT ////////////////////////////////////////////////////////



/// Functions needed for Dry Eye Syndrome Test Flash movie  ///////////////////////
function fullScreenWindow() {
	window.moveTo(0,0);
	window.resizeTo(screen.width,screen.height);
	
}
function resizeWindow() {
	var	winl = ((screen.width - 760) / 2) - 10;
	var	wint = ((screen.height - 500) / 2) - 25;
	window.moveTo(wint,winl);
	window.resizeTo(760,500);
}

///// Functions for Coupon Form /////////////////////

// Patanol.com Form Validation
function validateForm(theForm)
{
    if (theForm.FirstName.value == "") 
	{
	   	alert("Please provide your first name.");
    	theForm.FirstName.focus();
       	return false; 
	} 
    if (theForm.LastName.value == "")  
	{
	   	alert("Please provide your last name.");
    	theForm.LastName.focus();
       	return false; 
	} 
	if (theForm.Email.value == "" )
	{
		if (strMessage != null)
		{
			strMessage += "Please enter your email address.";
		}
		else
		{
			var strMessage = "Please enter your email address.";
		}
		alert(strMessage);
		theForm.Email.focus();
		return false; 
	}
	// check validity of email
	var validEmail = checkEmail(theForm.Email);
	if (!validEmail)
	{
		alert("Please enter a valid email address.");
		theForm.Email.focus();
		return false;
	} 				
    if (theForm.Address1.value == "")  
	{
	  	alert("Please provide your address.");
    	theForm.Address1.focus();
       	return false; 
	} 
    if (theForm.City.value == "")  
	{
	  	alert("Please provide your city.");
    	theForm.City.focus();
       	return false; 
	}  
	if (theForm.Zip.value == "") 
	{
	   	alert("Please provide your ZIP Code.");
    	theForm.Zip.focus();
		return (false);
	}
	if ((theForm.Zip.value.length!=5) && (theForm.Zip.value.length!=10))
	{
	   	alert("Please provide a valid ZIP Code.");
    	theForm.Zip.focus();
		return (false);
	}
	RefString="1234567890-";
	for (Count=0; Count < theForm.Zip.value.length; Count++)  
	{
		TempChar= theForm.Zip.value.substring (Count, Count+1);
		if (RefString.indexOf (TempChar, 0)==-1) 
			{
	   			alert("Please provide a valid ZIP Code.");
    			theForm.Zip.focus();
				return false; 
			}
	}			
    theForm.submit();
} 
function checkEmail(email)  		
{
	var goodEmail = email.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	if (goodEmail)
	{
    	 return true;
	} 
	else
	{
		return false;
	}		
}