var isInternetExplorer = (navigator.appName.indexOf("Microsoft") != -1);

/*
future references:
var keycode = isInternetExplorer ? event.keyCode : event.which;
*/

function countSpaces(obj){
	var iLength = obj.value.length;
	var strSpaces = obj.value.match(new RegExp("( )", "g"));
	var countSpaces = strSpaces ? strSpaces.length : 0;
	return countSpaces;
}

function countLineBreaks(obj){
	var iLength = obj.value.length;
	var strLineBreaks = obj.value.match(new RegExp("(\\n)", "g"));
	var countLineBreaks = strLineBreaks ? strLineBreaks.length : 0;
	return countLineBreaks;
}

function textCounter(field, counter_field, maxlimit) {
	var lineBreaks = countLineBreaks(field);
	var adjust = isInternetExplorer ? 1 : 0;
	if (field.value.length - lineBreaks * adjust > maxlimit){
		field.value = field.value.substring(0, maxlimit + lineBreaks * adjust);
		field.focus();
	} else {
		counter_field.value = maxlimit - field.value.length + lineBreaks * adjust;
	}
}

function backToSection(backToURL, forceBackToURL){
	if(forceBackToURL == null) forceBackToURL = false;
	if(history.length > 1 && !forceBackToURL) history.back(); else window.location.href = backToURL;
}

function hideStatus() {
	window.defaultStatus='';
	window.status='';
	return true;
}

function searchReset() {
	tot = document.search_form.elements.length;
	for(i=0;i<tot;i++) {
		if (document.search_form.elements[i].type == 'text') {
			document.search_form.elements[i].value = "";
		} else if (document.search_form.elements[i].type == 'checkbox' || document.search_form.elements[i].type == 'radio') {
			document.search_form.elements[i].checked = false;
		} else if (document.search_form.elements[i].type == 'select-one') {
			document.search_form.elements[i].selectedIndex = 0;
		}
	}
	if ((document.search_form.country_id) || (document.search_form.state_id) || (document.search_form.region_id) || (document.search_form.city_id) || (document.search_form.area_id)) {
		searchLocationReset();
	}
}

function easyFriendlyUrl(name2friendlyurl, target, validchars, separator) {
	var str = "";
	var i;
	var exp_reg = new RegExp("[" + validchars + separator + "]");
	var exp_reg_space = new RegExp("[ ]");
	name2friendlyurl.toString();
	for (i=0 ; i<name2friendlyurl.length; i++) {
		if (exp_reg.test(name2friendlyurl.charAt(i))) {
			str = str+name2friendlyurl.charAt(i);
		} else {
			if (exp_reg_space.test(name2friendlyurl.charAt(i))) {
				if (str.charAt(str.length-1) != separator) {
					str = str + separator;
				}
			}
		}
	}
	if (str.charAt(str.length-1) == separator) str = str.substr(0, str.length-1);
	document.getElementById(target).value = str.toLowerCase();
}

function searchLocationReset() {
	if (document.search_form.country_id) {
		if (document.search_form.state_id) {
			while (document.search_form.state_id.options.length>1) {
				deleteIndex=document.search_form.state_id.options.length-1;
				document.search_form.state_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.region_id) {
			while (document.search_form.region_id.options.length>1) {
				deleteIndex=document.search_form.region_id.options.length-1;
				document.search_form.region_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.state_id) {
		if (document.search_form.region_id) {
			while (document.search_form.region_id.options.length>1) {
				deleteIndex=document.search_form.region_id.options.length-1;
				document.search_form.region_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.region_id) {
		if (document.search_form.city_id) {
			while (document.search_form.city_id.options.length>1) {
				deleteIndex=document.search_form.city_id.options.length-1;
				document.search_form.city_id.options[deleteIndex]=null;
			}
		}
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	} else if (document.search_form.city_id) {
		if (document.search_form.area_id) {
			while (document.search_form.area_id.options.length>1) {
				deleteIndex=document.search_form.area_id.options.length-1;
				document.search_form.area_id.options[deleteIndex]=null;
			}
		}
	}
}

function $(id) {
	return document.getElementById(id);
}

function showText(text) {
	return unescape(text);
}

onload=function(){
	if (document.getElementsByClassName == undefined) {
		document.getElementsByClassName = function(className)
		{
			var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
			var allElements = document.getElementsByTagName("*");
			var results = [];

			var element;
			for (var i = 0; (element = allElements[i]) != null; i++) {
				var elementClass = element.className;
				if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
					results.push(element);
			}

			return results;
		}
	}
	/* Remove eDirectory activation warnings for now
	var warnings = document.getElementsByClassName('warning');
	for (i in warnings) {
		if (warnings[i].innerHTML == "eDirectory activation required!<br><strong>PLEASE CONTACT THE SITE MANAGER</strong>") {
			warnings[i].parentNode.removeChild(warnings[i]);
		}
	}*/
	
	if (document.banner && document.banner.type) {
        if (document.banner.type.value) {
            changeBannerLevel(document.banner.type.value);
        } else if (document.banner.type[0]) {
            changeBannerLevel(document.banner.type[0].value);
        }
	}
    
    if (document.getElementById('country_id') && !document.location_operation) {
        document.getElementById('country_id').onchange();
    }
}

function focusInput(element, defaultValue) {
	if (element.value == "Password" && defaultValue == "Password") {
		element.setAttribute('type', 'password');
	}
    if (element.value == defaultValue) {
        element.value = "";
    }
}

var newLeft;
var theElement;
var timer = null;

var voucherWidth = 244;

function nextVoucher(elementID, max) {
    if (timer == null) {
	    theElement = document.getElementById(elementID);
	    var oldLeft = theElement.style.left;
	    oldLeft = oldLeft ? parseInt(oldLeft) : 0;
	    
	    max *= voucherWidth * -1;
	    max += (voucherWidth * 2);
	    
	    if (oldLeft == max) {
	    	timer = setInterval('resetVoucher()', 3);
	    	return;
	    }
	    
	    newLeft = oldLeft - voucherWidth;
    
    	timer = setInterval('moveVoucher()', 3);
    }
}

function moveVoucher() {
    var thisLeft = theElement.style.left;
    thisLeft = thisLeft ? parseInt(thisLeft) : 0;
    
    if (thisLeft > newLeft) {
        var thisNewLeft = thisLeft - 4;
        theElement.style.left = thisNewLeft + "px";
    } else {
        clearInterval(timer);
        timer = null;
    }
    
}

function resetVoucher() {
    var thisLeft = theElement.style.left;
    thisLeft = thisLeft ? parseInt(thisLeft) : 0;
    
    if (thisLeft < 0) {
        var thisNewLeft = thisLeft + 18;
        theElement.style.left = thisNewLeft + "px";
    } else {
        theElement.style.left = 0 + "px";
        clearInterval(timer);
        timer = null;
    }
}

function addCycle(source, destination, cycle, unit, increment) {
	var parts = source.value.split('/');
	
	var day = parts[0] - 0;
	var month = parts[1] - 1;
	var year = parts[2] - 0;
	
	var theDate = new Date(year, month, day);
	
	if (!increment) cycle *= -1;
	
	if (unit == 'week') {
		cycle *= 7;
		theDate.setDate(theDate.getDate() + cycle);
	} else if (unit == 'month') {
		theDate.setMonth(theDate.getMonth() + cycle);
		if (theDate.getDate() < day) {
			theDate.setDate(1);
			theDate.setDate(theDate.getDate() - 1);
		}
	} else if (unit == 'year') {
		theDate.setFullYear(year + cycle);
	}
	
	var newDay = theDate.getDate();
	if (newDay < 10) newDay = "0" + newDay;
	var newMonth = theDate.getMonth() + 1;
	if (newMonth < 10) newMonth = "0" + newMonth;
	var newYear = theDate.getFullYear();
	
	var newValue = newDay + '/' + newMonth + '/' + newYear;
	destination.value = newValue;
}

var loginTimer1 = null;
var loginTimer2 = null;

function overLoginBox(x) {
    if (x == 1) {
        clearInterval(loginTimer1);
        loginTimer1 = null;
        loginTimer1 = setInterval('expandLoginBox(' + x + ')', 3);
    }
    if (x == 2) {
        clearInterval(loginTimer2);
        loginTimer2 = null;
        loginTimer2 = setInterval('expandLoginBox(' + x + ')', 3);
    }
	
	document.getElementById('login-contract' + x).style.display = 'inline';
	document.getElementById('login-expand' + x).style.display = 'none';
}

function expandLoginBox(x) {
	var theElement = document.getElementById('login-box' + x);
	
    var thisHeight = theElement.style.height;
    thisHeight = thisHeight ? parseInt(thisHeight) : 0;

    if (thisHeight < 130) {
        var thisNewHeight = thisHeight + 2;
        theElement.style.height = thisNewHeight + "px";
    } else {
        theElement.style.height = "130px";
        if (x == 1) {
            clearInterval(loginTimer1);
            loginTimer1 = null;
        }
        if (x == 2) {
            clearInterval(loginTimer2);
            loginTimer2 = null;
        }
    }
}

function outLoginBox(x) {
    if (x == 1) {
        clearInterval(loginTimer1);
        loginTimer1 = null;

        loginTimer1 = setInterval('contractLoginBox(' + x + ')', 3);
    }
    if (x == 2) {
        clearInterval(loginTimer2);
        loginTimer2 = null;

        loginTimer2 = setInterval('contractLoginBox(' + x + ')', 3);
    }
	
	document.getElementById('login-contract' + x).style.display = 'none';
	document.getElementById('login-expand' + x).style.display = 'inline';
}

function contractLoginBox(x) {
	var theElement = document.getElementById('login-box' + x);
	
    var thisHeight = theElement.style.height;
    thisHeight = thisHeight ? parseInt(thisHeight) : 0;

    if (thisHeight > 20) {
        var thisNewHeight = thisHeight - 2;
        theElement.style.height = thisNewHeight + "px";
    } else {
        theElement.style.height = "20px";
        if (x == 1) {
            clearInterval(loginTimer1);
            loginTimer1 = null;
        }
        if (x == 2) {
            clearInterval(loginTimer2);
            loginTimer2 = null;
        }
    }
}
