﻿
function toggleDiv(id) {
	if (document.getElementById(id).style.display == "none") {
		document.getElementById(id).style.display = "block";
	}
	else {
		document.getElementById(id).style.display = "none";
	}
}

function WriteEmailAddress(name, domain, topLevelDomain, css) {
	document.write('<a');
	if (css.length > 0) {
		document.write(' class=\"' + css + '\"');
	}
	document.write(' href=\"mailto:' + name + '@' + domain + '.' + topLevelDomain + '\">');
	document.write(name + '@' + domain + '.' + topLevelDomain + '</a>;');
}

function WriteFlashObject(flashURL, width, height, uniqueId) {
	var theUniqueid = '';
	if (uniqueId != null) {
		if (uniqueId.length > 0) {
			theUniqueid = ' id=\"' + uniqueId + '\"';
		}
	}
	if (navigator.userAgent.indexOf("MSIE") == -1) {
		document.write('<embed src="' + flashURL + '"' + theUniqueid + ' quality="high" wmode="transparent" pluginspage="<http://www.macromedia.com/go/getflashplayer>http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"></embed>');
	} else if (navigator.userAgent.indexOf("MSIE") != -1) {
		document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="<http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0>http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0"  width="' + width + '" height="' + height + '"><param name="movie" value="' + flashURL + '" /><param name="wmode" value="transparent" /><param name="quality" value="high" /></object>');
	}
}
function CustomValidatorIsNumberAndNotEmpty(args) {
	var s = args.Value;
	s = Trim(s);
	if (/^\d+$/.test(s)) {
		if (s = '') {
	
			args.IsValid = false;
		}
	}
	else {
		args.IsValid = false;
	}
	
}
function IsEmpty(s) {
	s = Trim(s)
	return ((s == null) || (s.length == 0))
}

function LTrim(str) {
	// Trims all spaces to the left of a specific string
	var whitespace = new String(" \t\n\r "); // last space character is not a space, but alt+0160, another invisible char. Added by Imar Spaanjaars at 07-09-2000
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		// We have a string with leading blank(s)...
		var j = 0, i = s.length;
		// Iterate from the far left of string until we
		// don't have any more whitespace...
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		// Get the substring from the first non-whitespace
		// character to the end of the string...
		s = s.substring(j, i);
	}
	return s;
}

function RTrim(str) {
	// Trims all spaces to the right of a specific string
	// We don't want to trip JUST spaces, but also tabs,
	// line feeds, etc.  Add anything else you want to
	// "trim" here in whitespace
	var whitespace = new String(" \t\n\r "); // last space character is not a space, but alt+0160, another invisible char. Added by Imar Spaanjaars at 07-09-2000
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length - 1)) != -1) {
		// We have a string with trailing blank(s)...
		var i = s.length - 1;       // Get length of string
		// Iterate from the far right of string until we
		// don't have any more whitespace...
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		// Get the substring from the front of the string to
		// where the last non-whitespace character is...
		s = s.substring(0, i + 1);
	}
	return s;
}

function Trim(str) {
	// Trims all spaces to the left and right of a specific string by calling RTim and LTrim
	return RTrim(LTrim(str));
}

function IsDate(year, month, day) {
	// Checks if the three date elements together form a valid date
	if (IsEmpty(year) || isNaN(year)) return false;
	if (IsEmpty(month) || isNaN(month)) return false;
	if (IsEmpty(day) || isNaN(day)) return false;
	var longMonth;
	var isFebruary;
	longMonth = false;
	isFebruary = false;
	if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
		longMonth = true;
	}
	if (month == 2) {
		isFebruary = true;
	}
	if (longMonth && day > 31) return false;
	if (!longMonth && day > 30) return false;
	if (isFebruary && day > DaysInFebruary(year)) return false;
	return true;
}

function DaysInFebruary(year) {
	// Internal function. Returns 29 when a leap year, else 28
	// 29 days in febr, when it's a leap year. Not with a century, unless dev. by 400
	return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}

function setSummaryError(object, val) {
	//	var ckeditorinstance = object.ckeditorGet();
	//	ckeditorinstance.updateElement();
	//	debugger;
}

function openPreview(url) {
	window.open(url, 'preview');
}

function numberTest() {
	return (event.keyCode >= 48 && event.keyCode <= 57);
}

function gotoUrl(url) {
	document.location.href = url;
}

function verifyExtension(sender, e) {
	var isValid = false;
	if (endsWith(e.Value.toString().toLowerCase(), '.pdf')) {
		isValid = true;
	}
	e.IsValid = isValid;
}

function endsWith(source, pattern) {
	var d = source.length - pattern.length;
	return d >= 0 && source.lastIndexOf(pattern) === d;
};

function verifyExtensionImages(sender, e) {
	var isValid = false;
	var extensions = ['.jpg', '.jpeg', '.gif', '.png'];
	for (var i in extensions) {
		if (endsWith(e.Value.toString().toLowerCase(), extensions[i])) {
			isValid = true;
		}
	}
	e.IsValid = isValid;
}

function checkMaxLength(sender, args, length, objectIdToValidate) {
	if (args.Value.length <= length) {
		args.IsValid = true;
	}
	else {
		args.IsValid = false;
	}	
}
