var IE = navigator.appName.indexOf("Microsoft") != -1;
var DOM = document.getElementById;
var is_mac = navigator.userAgent.toLowerCase().indexOf("mac") != -1;
var jigsawWindow = true;
var helpWindow;
var pWindow;
var timeoutId = null;
var darkblue = "#002D60";
//var darkblue = "#666666";

var lightblue = "#DFEAF4";

function setPopupTimeout() {
    setTimeout("testPopup()",700);
}

function testPopup () {
    timeoutId = null;
    if ((pWindow==null) ||(typeof(pWindow)=="undefined") || (typeof(pWindow.location.hash)!="string"))
        alert("You must disable your popup killer to use this feature of Jigsaw.");
}



function makeLink (innerText, href) {
	var link = document.createElement("a");
	var textNode = document.createTextNode(innerText);
	link.style.whiteSpace = "nowrap";
	if (href != null && trim(href) != "")
	    link.href = href;
	
	link.appendChild (textNode);
	return link;	
}

function removeChildren (elem) {
    if (elem == null || elem.childNodes == null || !elem.childNodes.length) return;
    var len = elem.childNodes.length - 1;
    for (var i = len; i >= 0; i--) {
        elem.removeChild(elem.childNodes[i]);
    }
}

function showObject (object, show, visibilityOnly) {
	if (object == null) return;
	if (visibilityOnly == null) visibilityOnly = false;
	if (show) {
		object.style.visibility = "visible";
		if (!visibilityOnly) object.style.display = "block";
	} else {
		object.style.visibility = "hidden";
		if (!visibilityOnly) object.style.display = "none";
	}
}

function getInnerText (elem) {
	if (IE) return elem.innerText;
	var children = elem.childNodes;
	for (var i = 0; i < children.length; i++) {
		var child = children[i];
		if (child.nodeType == 3) return child.data;
	}
		
}

function setInnerText (elem, text) {
	if (elem == null) return;
	if (IE) elem.innerText = text;
	else {
	    var hasTextNode = false;
		var children = elem.childNodes;
		for (var i = 0; i < children.length; i++) {
			var child = children[i];
			if (child.nodeType == 3) {
			    child.data = text;
			    hasTextNode = true;
			    break;
			}
		}
		if (!hasTextNode) {
		    var textNode = document.createTextNode(text);
		    elem.appendChild (textNode);  
		}
	}	
}

function $get (id) {
    var elem = null
    if (DOM) {
           elem = document.getElementById (id);
    } else if (IE) {
        elem = document.all[id];
    }
    return elem;
}

function addEventHandler (obj, ev, handler) {
	if (IE) 
		obj.attachEvent (ev, handler);
	else {
		obj.addEventListener (ev, handler, false);
	}
}

function getArgs() {
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split("&");
	for (var i = 0; i < pairs.length; i++) {
		var pos = pairs[i].indexOf('=');
		if (pos == -1) continue;
		var argname = pairs[i].substring (0,pos);
		var value = pairs[i].substring (pos+1);
		args[argname] = unescape(value);
	}
	return args;
}

//override this within a page to show context-sensitive help
var helpAnchor = 'top';

function showWebsite(url) {
    if (url.indexOf("http://") == -1) url = "http://" + url;
    window.open (url);
}

function showHelp (fileName) {
    if (fileName == null || fileName == "") fileName = "Help.html";
    helpWindow = window.open (serverURL + "help/" + fileName, 'Help', "left=400,top=0,toolbar=yes,scrollbars=yes,menubar=yes,resizable=yes,location=yes,status=yes,height=575,width=775");
    try {
        helpWindow.focus();
    } catch (e) {
    
    }  
}

function showHelpPopup(url, width, height) {
    url = serverURL + url;
    if (width == null) width = 500;
    if (height == null) height = 400;
    window.open (url, 'Help', "left=400,top=0,toolbar=no,scrollbars=yes,menubar=no,resizable=yes,location=no,status=yes,height="+height+",width="+width);
}


var problemWindow;

function reportCompanyProblem (url, id) {
    url = url + (url.indexOf('?') == -1 ? "?" : "&" ) + "id=" + id;
    var args = "toolbar=no,scrollbars=yes,menubar=no,resizable=yes,location=no,status=no,height=800,width=800";
    var left =  150;
    var top = 150;
    args += ",top=" + top + ",left=" + left;
    problemWindow = window.open (url, 'Problem', args);
    try {
        problemWindow.focus();
    } catch (e) {
    
    }  
}

function reportContactProblem (url, id) {
    url = url + (url.indexOf('?') == -1 ? "?" : "&" ) + "id=" + id;
    var args = "toolbar=no,scrollbars=no,menubar=no,resizable=yes,location=no,status=no,height=475,width=575";
    var left =  (screen.width - 475) / 2;
    var top =  (screen.height - 575) / 2;
    args += ",top=" + top + ",left=" + left;
    problemWindow = window.open (url, 'Problem', args);
    try {
        problemWindow.focus();
    } catch (e) {
    
    }  
}


function showUploadHelp (anchor) {
    helpWindow = window.open ("help/UploadHelp.html#" + anchor, 'Help', "toolbar=yes,scrollbars=yes,menubar=yes,resizable=yes,location=yes,status=yes,height=500,width=700");
    try {
        helpWindow.focus();
    } catch (e) {
    
    }    
}

function doClear (form) {
    if (form == null) form = document.forms[0];
    if (form == null) return;
    for (var i = 0; i < form.elements.length; i++) {
        var field = form.elements[i];
        if (field.type != 'hidden') {
            if (field.type == 'select-one') {
                field.selectedIndex = 0;
            } else if (field.type == 'text') {
                field.value = "";
            } else if (field.type == 'checkbox') {
                field.checked = false;
            }
        }
    }
}

function validateField (field, fieldname) {
    var msg = "";
    if (field.value == null || trim(field.value) == "") {
        firstField = firstField == null ? field : firstField;
        msg += "\n-" + fieldname;
    }
    return msg;
}

function trim(s) {
    if (s == null || s == "") return "";
    return ltrim(rtrim(s));
}

function ltrim(s) {
	var re = /^\s */;
	return s.replace(re, '');
}

function rtrim(s) {
	var re = /\s *$/;
	return s.replace(re, '');
}

function setSelectValue(select, value) {
    if (select == null) return;
    if (!is_mac) {
        select.value = value;
        return;
    }
    for (var i = 0; i < select.options.length; i++) {
        if (select.options[i].value == value)
              select.options[i].selected = true;
    }
    
}

function showLeaderboard (url) {
    window.location.href = url;
}

function isValid (s) {
    if (s != null && trim(s) != "")
        return true;
}

function js_encodeURIComponent (s) {
    if (typeof encodeURIComponent == "function") {
		return encodeURIComponent(s);
	}
	else {
		return escape(s);
	}
}

function isValidDate(s) {
    if (s == "") return true;
    var format = /\d{1,2}\/\d{1,2}\/\d{4}/;
    if (s.length > 10 || s.length < 8 || s.match(format) == null) return false;  
    return true; 
}

function dayExists (s) {
    if (s == "") return true;
    var nums = s.split('/');
    if (nums.length != 3) return false;
    var month = nums[0] * 1;
    if (month < 1 || month > 12) return false;
    var year = nums[2] * 1;
    //if (year < 2004 || year > 2005) return false;
    var day = nums[1] * 1;
    if (!isValidDay (day, month, year)) return false;
    return true;
}

function isValidDay (day, month, year) {
	if( year < 1970 || year > 2030) return false;
    if (day < 1) return false;
    var numDays = 31;
    if (month == 2) {
        numDays = isLeapYear(year) ? 29 :28;
    }
    if (month == 4 || month == 6 || month == 9 || month == 11) {
        numDays = 30;
    }
    return day <= numDays;
}

function isLeapYear(year) {
    return (year - 2000) % 4 == 0;
}

function isAfter(d1, d2) {
    //returns true if day 1 comes after day 2
    var nums1 = d1.split('/');
    if (nums1.length != 3) return false;
    var nums2 = d2.split('/');
    if (nums2.length != 3) return false;
    var month1 = nums1[0] * 1;
    var year1 = nums1[2] * 1;
    var day1 = nums1[1] * 1;
    
    var month2 = nums2[0] * 1;
    var year2 = nums2[2] * 1;
    var day2 = nums2[1] * 1;
    
    if (year1 > year2) {
        return true;
    } else if (year1 < year2) {
        return false;
    } else if (month1 > month2) {
       return true;
    } else if (month1 < month2) {
        return false;
    } else if (day1 > day2) {
        return true;
    } else return false;
    
}

function showVerisign() {
    var url = "https://seal.verisign.com/splash?form_file=fdf/splash.fdf&dn=WWW.JIGSAW.COM&lang=en";
    var width = 650;
    var height = 450;
    var args = "toolbar=yes,scrollbars=yes,menubar=yes,resizable=yes,location=yes,status=yes";
    var left =  (screen.width - width) / 2;
    var top =  35;
    args += ",height=" + height + ",width=" + width + ",top=" + top + ",left=" + left;
    var vwin = window.open(url, 'VeriSign', args);
    try {
        vwin.focus();
    } catch (e) {
    
    }  
}

function goURL(newUrl){
	document.location.href = newUrl;
}