var MSG_MAX_LENGTH			= '%1 may only be a maximum of %2 characters long.';
var MSG_MIN_LENGTH			= '%1 must be a minimum of %2 characters long.';
var MSG_REQ_FIELD           = '%1 is a required field.';
var MSG_INVALID_EMAIL       = 'Invalid email address: %1';
var MSG_REQUIRED_SELECT		= 'Please select a value for %1.';
var MSG_ALPHA_NUMERIC		= '%1 may only contain alphanumeric characters.';
var MSG_NUMERIC             = '%1 may only contain numeric characters!';
var MSG_TWO_FIELDS			= '%1 and %2 must be the same.';
var MSG_NOT_TWO_FIELDS      = '%1 and %2 may not have the same value.';


function validateMaxLength(field, name, maxLength) {
	var value = field.value;
	var originalVal = value;	//store a copy with the \n's in it
	var newVal = "";	//new value with any extra characters removed from it so as not to go over maxLength
	var character = null;
	value = value.replace(/\n/g,'**'); // bug #4830 when the javascript validates it sees \n's and java validates it sees \r\n's so a string may pass javascript validation but fail java validation, solution validate on a copy of the string with all \n's replaced with 2 characters to simulate the java length

	if (value.length > maxLength)
	{
		//loop through the string getting one character at a time.
		//If we encounter a \n we have to count it as 2 characters due to bug #4830
		for(var i=0, count=1; count<=maxLength; i++, count++){
				character = originalVal.charAt(i);

				//if this is a new line char make sure we have 2 spaces available in the new string
				if(character == "\n" && count<=maxLength-1){
					newVal = newVal.concat(character);
					count++;
				}else{
					newVal = newVal.concat(character);
				}
		}

		var msg = MSG_MAX_LENGTH.replace('%1', name);
		msg = msg.replace('%2', maxLength);
		alert(msg);
		try{
			//substitute in the shortened string into the field.
			field.value = newVal;
			field.focus();
		}catch(e){}
		return false;
	}
	return true;
}
function validateMinLength(field, name, minLength) {
	if (field.value.length < minLength) {
		var msg = MSG_MIN_LENGTH.replace('%1', name);
		msg = msg.replace('%2', minLength);
		alert(msg);
		try{field.focus();}catch(e){}
		return false;
	}
	else {
		return true;
	}
}
function nonEmptyDependency(field1, field1Name, field2, field2Name, message) {
	if(!isEmpty(field1) && isEmpty(field2)){
		alert(message);
		return false;
	}else{
		return true;
	}
}
function validateRequiredField(field, name, dv) {
	try
	{
		field.value = field.value.trim();
		dv = dv.trim();
	}
	catch(e) {}

	if (field.value.length == 0 || field.value == dv)
	{
		alert(MSG_REQ_FIELD.replace('%1', name));
		try{field.focus();}catch(e){}
		return false;
	}
	return true;
}
function validateEmailField(emailField, name) {
	if (isEmpty(emailField)) return true;
	if (!checkEmail(emailField.value)) {
		alert(MSG_INVALID_EMAIL.replace('%1', emailField.value));
		try{emailField.focus();}catch(e){}
		return false;
	}
	return true;
}
function validateRequiredCheckbox(field, name, msg) {
	if (!isCheckBoxChecked(field)) {
		alert(msg.replace('%1', name));
		try{field.focus();}catch(e){}
		return false;
	}
	return true;
}
function validateRequiredSelect(field, name, defaultValue,selectallValue) {
	if (field.value == null || field.value == '' || field.value == defaultValue || field.value == selectallValue) {
		alert(MSG_REQUIRED_SELECT.replace('%1', name));
		try{field.focus();}catch(e){}
		return false;
	}
	else {
		return true;
	}
}
function validateTwoFields(field,name,field2,name2) {
	if (field.value != field2.value){
		var msg = MSG_TWO_FIELDS.replace('%1', name);
		msg = msg.replace('%2', name2);
		alert(msg);
		try{field.focus();}catch(e){}
		return false;
	}
	return true;
}
function validateNotTwoFields(field,name,field2,name2) {
	if (field.value == field2.value){
		var msg = MSG_NOT_TWO_FIELDS.replace('%1', name);
		msg = msg.replace('%2', name2);
		alert(msg);
		try{field.focus();}catch(e){}
		return false;
	}
	return true;
}
function validateAlphaNumeric(field, name) {
	var mask = /^[_0-9a-zA-Z-\.]*[_0-9a-zA-Z-\.]$/
	if (!mask.test(field.value)) {
		alert(MSG_ALPHA_NUMERIC.replace('%1', name));
		try{field.focus();}catch(e){}
		return false;
	}
	return true;
}
function validateAlphaNumeric_search(field, name) {
	var mask = /^[_0-9a-zA-Z-\.\s]*[_0-9a-zA-Z-\.\s]$/
	if (!mask.test(field.value)) {
		alert(MSG_ALPHA_NUMERIC.replace('%1', name));
		try{field.focus();}catch(e){}
		return false;
	}
	return true;
}
function validateNumeric(field, name) {
	var val = trim(field.value);
	field.value = val;
	var mask = /^-?[0-9]*(\.)?[0-9]*$/
	if (!mask.test(val)) {
		alert(MSG_NUMERIC.replace('%1', name));
		try{field.focus();}catch(e){}
		return false;
	}
	return true;
}



function isEmpty(field) {
	if (field.disabled){return true;}

	if (field.type=='checkbox'||(field[0]&&field[0].type == 'checkbox')) {
		return !isCheckBoxChecked(field);
	}
	if (field.type=='radio'||(field[0]&&field[0].type == 'radio')) {
		return !isCheckBoxChecked(field);
	}
	//Try trim - will fail for input type="file".
	try
	{
		field.value = field.value.trim();
	}
	catch(e) {}
	if (field.value.length == 0) {
		return true;
	}
}
function isCheckBoxChecked(field) {
	if (field[0]) {
		for (i = 0;i<field.length;i++) {
			theField = field[i];
			if (theField.checked) {
				return true;
			}
		}
		return false;
	} else {
		if (!field.checked) {
			return false;
		}
	}
	return true;
}
function setFocus(form,field) {
	if (form != '') {
		try	{document.forms[form][field].focus();} catch(e) {}
	}
	else {
		try	{document.forms[0][field].focus();} catch(e) {}
	}
}
function giveFocus(frm, elm) {
  eval("document."+frm+"."+elm+".focus()");
}
function winpop(loc,w,h,scroll) {
	var name = loc.replace(/\W/g, "");
	window.open(loc,name,'width='+w+', height='+h+', location=no, directories=no, menubar=no, scrollbars='+scroll+', resizable=no, status=no, toolbar=no');
}

function getById(tag) {
  if (document.getElementById) //  Netscape, Mozilla, etc.
  {
	return document.getElementById(tag);
  }
  else if (document.all)      //  IE, Konqueror, etc.
  {
	return document.all[tag];
  }
}
function switchdiv(div1_id, div2_id, form) {
	if (document.getElementById)	{
		if(!document.getElementById(div1_id)) return ;
		if(!(document.getElementById(div1_id).style)) return ;
		if(!(document.getElementById(div1_id).style.display)) return ;

		var state1 = document.getElementById(div1_id).style.display;
		if(state1=="none") {
				document.getElementById(div1_id).style.display="block";
				document.getElementById(div2_id).style.display="none";
				if (form != null)
				{
				   form[div1_id].value='true';
				   form[div2_id].value='false';
				 }
		 }
		if(state1=="block") {
				document.getElementById(div2_id).style.display="block";
				document.getElementById(div1_id).style.display="none";
				if (form != null)
				{
				   form[div1_id].value='false';
				   form[div2_id].value='true';
				 }
		 }
	}
	else if (document.all)	{
		if(!document.all[div1_id]) return ;
		if(!(document.all[div1_id].style)) return ;
		if(!(document.all[div1_id].style.display)) return ;

		var state1 = document.all[div1_id].style.display;
		if(state1=="none") {
				document.all[div1_id].style.display = "block";
				document.all[div2_id].style.display = "none";
				if (form != null)
				{
				   form[div1_id].value='true';
				   form[div2_id].value='false';
				 }
		}
		if(state1=="block") {
				document.getElementById(div1_id).style.display="none";
				document.getElementById(div2_id).style.display="block";
				if (form != null)
				{
				   form[div1_id].value='false';
				   form[div2_id].value='true';
				 }
		 }
	}
}
function getRefToDiv(divID) {
	if( document.layers ) { //Netscape layers
		return document.layers[divID]; }
	if( document.getElementById ) { //DOM; IE5, NS6, Mozilla, Opera
		return document.getElementById(divID); }
	if( document.all ) { //Proprietary DOM; IE4
		return document.all[divID]; }
	if( document[divID] ) { //Netscape alternative
		return document[divID]; }
	return false;
}
function characterCounter(fieldName, maxLength, elementName) {
	var field = getById(fieldName);
	var value = field.value.replace(/\n/g,'**'); // bug #4830 when the javascript validates it sees \n's and java validates it sees \r\n's so a string may pass javascript validation but fail java validation, solution validate on a copy of the string with all \n's replaced with 2 characters to simulate the java length
	getById(elementName).innerHTML = value.length;
}
function trim(str) {
	str = new String(str);
	return str.replace(/^\s+/,'').replace(/\s+$/,'');
}
function submitForm(form, action) {
	form.action = action;
	form.submit();
}
var gOnload = new Array();
function addOnload(f) {

	if (window.onload)
	{
		if (window.onload != runOnload)
		{
			gOnload[0] = window.onload;
			window.onload = runOnload;
		}
		gOnload[gOnload.length] = f;
	}
	else
		window.onload = f;
}
function runOnload() {
	for (var i=0;i<gOnload.length;i++)
		gOnload[i]();
}
function checkEmail(emailStr) {
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]!%";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
		if (user.charCodeAt(i)>127) {
			return false;
		}
	}
	for (i=0; i<domain.length; i++) {
		if (domain.charCodeAt(i)>127) {
			return false;
		}
	}
	if (user.match(userPat)==null) {
		return false;
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				return false;
			}
		}
		return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
		if (domArr[i].search(atomPat)==-1) {
			return false;
		}
	}
	if (domArr[len-1].length < 2) {
		return false;
	}
	if (len<2) {
		return false;
	}
	/*mask=/^(root|abuse|webmaster|help|postmaster|sales|resumes|contact|advertising|spam|spamtrap|nospam|noc|admin|support|daemon|listserve|listserver|autoreply)@/i;
	if (mask.test(emailStr.toLowerCase())) {
		return false;
	}*/

	return true;
}
function updateDay(change,formName,yearName,monthName,dayName)
{
	var form = document.forms[formName];
	var yearSelect = form[yearName];
	var monthSelect = form[monthName];
	var daySelect = form[dayName];
	var year = yearSelect[yearSelect.selectedIndex].value;
	var month = monthSelect[monthSelect.selectedIndex].value;
	var day = daySelect[daySelect.selectedIndex].value;

	if (change == 'month' || (change == 'year' && month == 2))
	{
		var i = 31;
		var flag = true;
		while(flag)
		{
			var date = new Date(year,month-1,i);
			if (date.getMonth() == month - 1)
			{
				flag = false;
			}
			else
			{
				i = i - 1;
			}
		}

		daySelect.length = 0;
		daySelect.length = i;
		var j = 0;
		while(j < i)
		{
			daySelect[j] = new Option(j+1,j+1);
			j = j + 1;
		}
		if (day <= i)
		{
			daySelect.selectedIndex = day - 1;
		}
		else
		{
			daySelect.selectedIndex = daySelect.length - 1;
		}
	}
}
function checkedCount(field) {
	var	checked	= 0;

	if (field != null) {
		if (field.length == null)
		{
			if (field.checked == true)
			{
				checked++;
			}
		}
		else
		{
			for	(var i = 0 ; i < field.length	; i++)
			{
				if (field[i].checked == true)
				{
					checked++;
				}
			}
		}
	}

	return checked;
}
function isChecked(field) {
	if (checkedCount(field) == 0)
	{
		return false;
	}
	return true;
}
function isOneChecked(field) {
	if (checkedCount(field) == 1)
	{
		return true;
	}
	return false;
}



// FLASH LINKS

var cn1 = "a1";
var cn2 = "a2";
var cycle = 0;

var cnf1 = "flash_border_1";
var cnf2 = "flash_border_2";

var if_next_1 = "mod-home-blog-next";
var if_next_2 = "mod-home-blog-next-2";
var if_prev_1 = "mod-home-blog-prev";
var if_prev_2 = "mod-home-blog-prev-2";

function change_class(){
cycle++;
if(cycle==1) {
cn1="a1";
cn2="a2";

cnf1 = "flash_border_1";
cnf2 = "flash_border_2";

if_next_1 = "mod-home-blog-next";
if_next_2 = "mod-home-blog-next-2";
if_prev_1 = "mod-home-blog-prev";
if_prev_2 = "mod-home-blog-prev-2";

}

if(cycle==2) {
cn1="a2";
cn2="a3";

cnf1 = "flash_border_2";
cnf2 = "flash_border_3";

if_next_1 = "mod-home-blog-next-2";
if_next_2 = "mod-home-blog-next-3";
if_prev_1 = "mod-home-blog-prev-2";
if_prev_2 = "mod-home-blog-prev-3";

}

if(cycle>2) {
cycle=0;
cn1="a3";
cn2="a1";

cnf1 = "flash_border_3";
cnf2 = "flash_border_1";

if_next_1 = "mod-home-blog-next-3";
if_next_2 = "mod-home-blog-next";
if_prev_1 = "mod-home-blog-prev-3";
if_prev_2 = "mod-home-blog-prev";

}

var divs=document.getElementsByTagName('*');

for( var i=0; i<divs.length; i++) {
		if(divs[i].className==cn1) {
			divs[i].className=cn2;
			}
		if(divs[i].className==cnf1) {
			divs[i].className=cnf2;
			}	
			
			
		if(divs[i].className==if_next_1) {
			divs[i].className=if_next_2;
			}
		if(divs[i].className==if_prev_1) {
			divs[i].className=if_prev_2;
			}			
			
		/*	
		if(divs[i].className=="mod-home-blog-next-img") {
			divs[i].src = '_themes/main/new_age_mt/' + if_next_2;
			}	
		if(divs[i].className=="mod-home-blog-prev-img") {
			divs[i].src = '_themes/main/new_age_mt/' + if_prev_2;
			}	
		
		
		if(divs[i].className=="mod-home-blog-next") {
			divs[i].innerHTML="";
			img_next = document.createElement('img');
			img_next.src = '_themes/main/new_age_mt/images/' + if_next_2;
			divs[i].appendChild(img_next);
			}	
		if(divs[i].className=="mod-home-blog-prev") {
			divs[i].innerHTML="";
			img_prev = document.createElement('img');
			img_prev.src = '_themes/main/new_age_mt/images/' + if_prev_2;
			divs[i].appendChild(img_prev);
			//alert(1);
			}				
			*/
	}

setTimeout("change_class()",200);
}

change_class();

function translatorGeneral(ids, dest)
{
	$(document).ready(function(){
		//this will work,
		//returns a jQuery object and translates the text when the Language API is loaded
		len = ids.length;
		for(i=0; i < len; i++) {
			$('#'+ids[i]).translate(dest);
		}	
	});
}


// IM functions 
function popupform(myform, windowname)
{
	if (! window.focus)return true;
	window.open('', windowname, 'width=367,height=600,scrollbars=no,resizable=yes');
	myform.target=windowname;
	return true;
}

function openWindowIM(mid)
{
	cur_time = new Date().getTime();
	openwindow = window.open ("v3messengerpro/popup.php?mid="+mid+"&rand="+cur_time+"_"+randomNumber(100,10000),"owindow","scrollbar=0,statusbar=0, menubar=0,resizable=1,width=297,height=250"); 
	openwindow.moveTo(100,100);
}

// IM open link
function openWindowImStart(gUserId, uid, newC)
{
	// chat was started after accept
	if(newC) {
		newC = '&new_c=1';
	} else {
		newC = '';
	}
	open('v3messengerpro/index.php?id=' + uid + '&m=' + gUserId + newC + '&rand=' + gUserId + '_' + uid +randomNumber(100,10000), uid,'toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, width=580, height=554, left=280, top=120');

	if(newC == '') {
		xajax_im_invite(uid);
	}
	
}

function newwin(myform, windowname, size)
{
	if (! window.focus)return true;
	window.open('', windowname, 'width=367,height=600,scrollbars=no,resizable=yes');
	myform.target=windowname;
	return true;
}

function randomNumber (m,n)
{
	m = parseInt(m);
	n = parseInt(n);
	return Math.floor( Math.random() * (n - m + 1) ) + m;
}


// WIDGETS

function widget_show(wid,status){
	widget = document.getElementById('widget_content_'+wid);
	widget_inner = document.getElementById('widget_inner_'+wid);
	if(widget) {
		status2 = 0;
		
		if(status==0) {
		status2 = 1;
			widget_inner.style.display = "none";
			widget.className = "bl_widget_shadow_"+wid;
		} else {
			if(wid==5 && widgets_calendar_long == true) widget.className = "bl_widget_shadow_calendar";
			else widget.className = "bl_widget_shadow";
			widget_inner.style.display = "block";
		}
		
		document.getElementById('widget_show_'+wid).onclick = function(){
			widget_show(wid,status2); return false;
		}

	
		
		// save status to database
		xajax_widget_show(wid,status);
	}
}

function widget_start(wid)
{
	xajax_widget_site(wid);

	r = document.getElementById('widget_link_show');
	if(r) {
		r.style.display = 'none';
	}
	r2 = document.getElementById('widget_link_hide');
	if(r2) {
		r2.style.display = '';
	}	
}

function widget_close(wid){
	z = 51;
	if(document.getElementById('widget_'+wid)) {
		z = document.getElementById('widget_'+wid).style.zIndex;
		document.getElementById('widget_'+wid).parentNode.removeChild(document.getElementById('widget_'+wid));
		// radio checked
		r = document.getElementById('widget_link_show');
		if(r) {
			r.style.display = '';
		}
		r2 = document.getElementById('widget_link_hide');
		if(r2) {
			r2.style.display = 'none';
		}		
		
		
		// change Z of others
		for(i=1;i<6;i++) {
		widget = document.getElementById('widget_'+i);	
		if(widget) {
			if(widget.style.zIndex>z) widget.style.zIndex = widget.style.zIndex-1;
			}
		}	
	}
	widgets_count--;
	xajax_widget_close(wid,z);
}

function widget_home(wid){
	if(document.getElementById('widget_'+wid)) {
	document.getElementById('widget_'+wid).parentNode.removeChild(document.getElementById('widget_'+wid));
	}
	widgets_count++;
	xajax_widget_home(wid);
}

function widget_up(wid){
	// z of clicked widget
	z_old = document.getElementById('widget_'+wid).style.zIndex;

	if(z_old<51) z_old = 51;


	for(i=1;i<6;i++) {
		widget = document.getElementById('widget_'+i);	
		if(widget) {
			if(i!=wid) {
				if(widget.style.zIndex>z_old) widget.style.zIndex = widget.style.zIndex-1;
			}
				else {
					widget.style.zIndex = widgets_count + 50;
				}
			}
		}

	if(z_old<51) z_old = 51;

	// IMs
	for(n in opens) {
		if (document.getElementById('xajax_im_open_' + opens[n])) {
			document.getElementById('xajax_im_open_' + opens[n]).style.zIndex = 50;
		}
	}	
	
	xajax_widget_up(wid,widgets_count + 50,z_old);
}

function widget_down(wid,z){

	for(i=1;i<6;i++) {
		widget = document.getElementById('widget_'+i);	
		if(widget) {
			if(i!=wid) {
				if(widget.style.zIndex>z) widget.style.zIndex = widget.style.zIndex-1;
			}
			
			}
		}
	//alert(wid+"::"+z);
	
}	

function getAbsolutePosition ( elem ) {
	r = {x:0,y:0};
	elem = document.getElementById(elem);

	while (elem) {
		r.x += (elem.offsetLeft + elem.clientLeft);
		r.y += (elem.offsetTop + elem.clientTop);
		elem = elem.offsetParent;
	}

	// correct position of widget
	r.x -= 10;
	r.y += 9;

	return r;
}

function getWHSizes() { 
	var w = document.documentElement; 
	var d = document.body;
	h = Math.max( w.scrollHeight, d.scrollHeight, w.clientHeight); 
	wd = Math.max( w.scrollWidth, d.scrollWidth, w.clientWidth);

	return { 
		ww:w.clientWidth, //window width
		wh:w.clientHeight, //window height
		wsl:w.scrollLeft, //window scroll left
		wst:w.scrollTop, //window scroll top
		dw:wd, //document width
		dh:h //document height
	}
}
