// JavaScript Document
function getElementPosition(elemID) 
{
    var offsetTrail = document.getElementById(elemID);
    var offsetLeft = 0;
    var offsetTop = 0;
	var offsetBottom=0;
    while (offsetTrail) 
	{
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
		offsetBottom +=offsetTrail.offsetBottom;
        offsetTrail = offsetTrail.offsetParent;
    }
    
	if(navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined") 
	{
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
		offsetBottom += document.body.bottomMargin;
    }
    
	alert(offsetLeft+", "+ offsetTop+", "+ offsetBottom);
}

function Trim(strValue)
{	
	return LTrim(RTrim(strValue));
}

function LTrim(strValue)
{
	var LTRIMrgExp = /^\s */;
	return strValue.replace(LTRIMrgExp, '');
}

function RTrim(strValue)
{
	var RTRIMrgExp = /\s *$/;
	return strValue.replace(RTRIMrgExp, '');
}

function StringLength(ctrName,strMinLength,strMaxLength,fieldLabel,required)
{		
	if(Trim(ctrName.value)!='')
	{
		var str=ctrName.value;
		if(str.length < strMinLength || str.length > strMaxLength)
		{
			alert(fieldLabel);
			ctrName.focus();
			return false;
		}
		return true;
	}
	else if(required==true && Trim(ctrName.value)=='')
	{
			alert(fieldLabel);
			ctrName.focus();
			return false;
	}
	else
	{
		return true;
	}
	
}

function isEmailAddr(email,required,label)
{
	var reEmail = /^.+\@.+\..+$/;
	if(Trim(email.value)=='' && required==true)
	{
		alert(label);
		email.focus();
		return false;
	}
    else if(Trim(email.value)!='')
	{
	   	if(reEmail.test(email.value)==false)
		{
			alert(label);
			email.focus();
			return false;
		}
		else
		{
			return true;	
		}
	}    
}

function validInt(formField,fieldLabel,required)
{	
	if (isNaN(formField.value))
	{
		alert('Please enter valid 10 digit code for the "' + fieldLabel +'" field.');
		formField.focus();
		return false;
	}
	else
	{
		return true;
	}
}

function AllowAlphaNumeric(formField,fieldLabel) 
{
	var myregExp = /^[a-zA-Z0-9]+$/;
	if (!myregExp.test(formField.value)) 
	{
		alert(fieldLabel);
		formField.focus();
		return false;
	} 
	else 
	{
		return true;
    	}
}

function isNumeric(formField,fieldLabel) 
{
	var myregExp = /^[.0-9]+$/;
	if (!myregExp.test(formField.value)) 
	{
		alert(fieldLabel);
		formField.focus();
		return false;
	} 
	else 
	{
		return true;
    }
}

function isPhone(formField,required,fieldLabel) 
{
	if(Trim(formField.value) != '')
	{
		var myregExp = /^[0-9]+$/;
		if (!myregExp.test(formField.value) || formField.value.length < 10) 
		{
			alert(fieldLabel);
			formField.focus();
			return false;
		} 
		else 
		{
			return true;
		}
	}
	else
	{
		if(required==false)
		{
			return true;
		}
		else
		{
			alert(fieldLabel);
			formField.focus();
			return false;
		}
	}
}

function limitText(limitField, limitCount, limitNum)
{
	if(limitField.value.length>=limitNum)
	{
		limitField.value = limitField.value.substring(0,limitNum);
	}
	else
	{
		limitCount.value = limitNum-limitField.value.length;
	}
}

var wlimit;
var cnt;	
function limitWord(limitField, limitCount, limitNum)
{	
	var words = limitField.value.split(/\s/);
	cnt = words.length;
	
	if(cnt > limitNum)
	{
		limitField.value = limitField.value.substring(0,wlimit);
		limitCount.style.fontWeight='bold';
		limitCount.style.color='#CC0000';
		alert("You have reached the max. allowed words limit!");
	}
	else
	{
		limitCount.style.fontWeight='normal';
		limitCount.style.color='#000000';
		
		if(cnt == limitNum)
			wlimit = limitField.value.length;
		limitCount.value = limitNum-cnt;
	}
	if(Trim(limitField.value)=='')
	{
			if(limitCount.value==(limitNum-1))
				limitCount.value=limitNum;
	}
}

function LimitChars(limitField, limitCount, limitNum) 
{
	var words = limitField.value;
	cnt = words.length;
	
	if(cnt > limitNum)
	{
		limitField.value = limitField.value.substring(0,limitNum);
	}
	else
	{
		if(cnt == limitNum)
			wlimit = limitField.value.length;
		limitCount.value = limitNum-cnt;
	}
	if(Trim(limitField.value)=='')
	{
			if(limitCount.value==(limitNum-1))
				limitCount.value=limitNum;
	}
}

function CheckNum(evt)
{
	evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
	//charCode=parseInt(charCode);

	if ((charCode >= 48 && charCode <=57) || (charCode >= 37 && charCode <=40) || (charCode==8 || charCode==9))	
		return true;
	else
		return false;
}


function ShowHideDiv(div)
{
	if(div.style.display=='none')
		div.style.display='block';
	else
		div.style.display='none';
}

function OnlyNum(evt)
{	
	evt = (evt) ? evt : window.event;
    var charCode = (evt.which) ? evt.which : evt.keyCode;
	//charCode=parseInt(charCode);

	if ((charCode >= 48 && charCode <=57) || (charCode >= 37 && charCode <=38) || (charCode==8 || charCode==9 || charCode==46))
		return true;
	else
		return false;	
}

var curr_lb_div; 
var is_modal = false; 
	 
function ShowLightBox(lb_div) 
{ 
	document.getElementById(lb_div).style.display='block'; 
	$().ready(function() {
		var $scrollingDiv = $("#"+lb_div);
 		
			$scrollingDiv
				.stop()
				.animate({"marginTop": ($(window).scrollTop() + 100) + "px"}, "slow" );	
			
			$(window).scroll(function(){		
			$scrollingDiv
				.stop()
				.animate({"marginTop": ($(window).scrollTop() + 100) + "px"}, "slow" );			
		});
	});
		
	//document.getElementById('fade').style.display='block';
	curr_lb_div = lb_div; 
	//if (isModal) 
		is_modal = true;
	//else is_modal = false; 
} 
	 
function HideLightBox() 
{ 
	if(document.getElementById(curr_lb_div)) 
	{ 
		document.getElementById(curr_lb_div).style.display='none'; 
		document.getElementById('fade').style.display='none'; 
		curr_lb_div = '';
	} 
}

function HideParentFrameLightBox() 
{ 
	var pdoc=window.parent.document;
	pdoc.getElementById('div_lb_pop').style.display='none';		
	pdoc.getElementById('fade').style.display='none'; 
	curr_lb_div = '';
}

var manage_page='';
var view_page='';

function ViewList(list_page, div)
{	
	view_page=list_page;
	ajax_do(list_page, div);	
}

function ShowAddForm(add_page_name)
{
	document.getElementById('div_save_msg').style.display='none';
	document.getElementById('div_del_msg').style.display='none';
	manage_page=add_page_name;
	div=document.getElementById('div_lb_pop');	
	div.innerHTML='<img src="images/timer.gif" alt="">';				
	ajax_do(add_page_name, div);
	ShowLightBox(div.id);
}

function ShowResErrors(msg)
{
	var pdoc=window.parent.document;
	div=pdoc.getElementById('div_lb_pop');	
	div.innerHTML=msg;
	div.style.display='block';
	pdoc.getElementById('fade').style.display='block'; 
	
	curr_lb_div = div.id; 
	is_modal = true;			
}

function ShowEditForm(add_page_name, edit_id)
{
	document.getElementById('div_save_msg').style.display='none';
	document.getElementById('div_del_msg').style.display='none';
	manage_page=add_page_name;
	div=document.getElementById('div_lb_pop');	
	div.innerHTML='<img src="images/timer.gif" alt="">';					
	ajax_do(add_page_name+"?edit_id="+edit_id, div);
	ShowLightBox(div.id);	
}

function HideAddForm() 
{	
	var pdoc=window.parent.document;	
	pdoc.getElementById('div_lb_pop').style.display='none'; 
	pdoc.getElementById('fade').style.display='none'; 
	curr_lb_div = '';	
	javascript:ViewList(view_page, pdoc.getElementById('div_list'));
}

