// *********************
//  UTILITY FUNCTIONS
// *********************


function debug(str, level){
	var level = level || 1
	var dontdebug		= document.dontdebug || false;

	if (this.dontdebug == true || (location.host.indexOf('-vd') < 0 && location.host.indexOf('-vs') < 0 ))
		return true;	

	candebug		= false;
	consoledebug	= false;
	warndebug		= false;
	forceOutput		= document.forceOutput || false;
	
	if ($('#debug'))
		this.candebug	= true;	
	
	if (window.console)	{
		if (window.console.log){
			this.consoledebug	= true;
			this.candebug		= true;	
		}
		
		if (window.console.warn){
			this.warndebug	= true;
		}
	}
	
	if (level == 1 && consoledebug) {		
		console.log(str);
	}
	if (level == 2 && warndebug) {		
		console.warn(str);
	}
	return true;
};

function hideProc(){ 
	topWin.$('TACoverlay').hide();
	topWin.$('Processing').hide();
}

function showDimensions() {
	var selected = document.formEmbedOptions.size.selectedIndex;
	if (document.formEmbedOptions.size.options[selected].value.indexOf("custom") != -1) {
		$("widthHeight").style.display = "block";
	} else {
		$("widthHeight").style.display = "none";
		if (document.formEmbedOptions.size.options[selected].value.indexOf("300") != -1) {
			document.formEmbedOptions.customWidth.value = 300;
			document.formEmbedOptions.customHeight.value = 400;
		} else {
			document.formEmbedOptions.customWidth.value = 200;
			document.formEmbedOptions.customHeight.value = 267;
		}
	}
}

function debugNoFB(str){
	var dbem = $('debugDiv');
	if (!dbem){	
		dbem	= document.createElement('DIV');
        dbem.style.display = "block";    
        dbem.id = "debugDiv";    
        dbem.style.zIndex = "2147483647";
        dbem.style.position = "absolute";
        dbem.style.background = "#FFFFFF";
        dbem.style.color = "#000000";
        dbem.style.width = "100%";
        dbem.style.left = "0";
        dbem.style.overflow = "auto";
        dbem.style.bottom = "0";
        dbem.style.height = "200px";
        document.body.appendChild(dbem);
	}	
	
	dbem.innerHTML	+= str + '<br>';
}

function grabQueryValue(qName){
	try{
		var q = location.search;
		
		if (q.indexOf(qName + '=') == -1)
			return false;
	
		q = q.substring(q.indexOf(qName) + (qName.length + 1));
	
		q = q.split('&')[0];
	} catch (e){alert(e);}

	return q;
}


function spaceTrim(text) {

	while (text.charAt(0) == " ") {
		text = text.substring(1);
	}

	while (text.charAt(text.length-1) == " ") {
		text = text.substring(0,text.length-1);
	}

	return text;
}

/** Depecarte this later.  **/
function truncate(text,chars) {
	if (text.length <= chars) {
		return text;
	} else {
		var newText = text.substring(0,chars);
		var breakSpot = /[ \.\?\!\;\,\:]/;
		// make sure there is at least one breakspot, if not return as is plus elipses
		if (newText.match(breakSpot) == null) {
			document.write(newText += "&#0133;");
		}
		// find the first break character from the end
		while (newText.charAt(newText.length-1).match(breakSpot) == null) {
			newText = newText.substring(0,newText.length-1);
		}
		// find the first real character from the new end
		while (newText.charAt(newText.length-1).match(breakSpot) != null) {
			newText = newText.substring(0,newText.length-1);
		}
		return newText += "&#0133;";
	}
}


function validateEmail(email)
{
	var addressPattern = /^(.+)@(.+)\.(.+)$/;

	if (addressPattern.test(email))
		return true;
	else
		return false;
}

// ***********************
//  MISC FUNCTIONS
// ***********************

function VHSS_DoFSCommand() {}


function updateViewOption(newTarget)
{
	if (document.getElementById('divMyVokis_viewoptions'))
	{
		var optionLinks = document.getElementsByTagName('a');

		for (a=0; a < optionLinks.length; a++)
		{
			optionLinks[a].className = '';
		}
	}

	newTarget.className = 'viewOptions_selected';
	newTarget.blur();
}


function populateSelect(source,sTarget)
{
	//if (!document.getElementById(source) || !document.getElementById(sTarget)) return;
	var t = document.getElementById(sTarget);
	for(j=0; j < document.setAccountInfo.elements.length; j++) {
		var elem = document.setAccountInfo.elements[j];
		if (elem.type == "hidden" && elem.name == source) {
			var theValue = elem.value;
			break;
		}
	}
	for (p=0; p < t.options.length; p++) {
		if (t.options[p].value == theValue) {
			t.selectedIndex = p;
			return;
		}
	}
}


function howLongAgo(dateStr, level){
	if (!level)
		level	= 2;
		
	var dateArray 	= dateStr.split(' ');
	var dayArr 		= dateArray[0].split('-');
	var year 		= parseInt(dayArr[0]);
	var month 		= parseInt(dayArr[1].replace('0','')) - 1;
	var day 		= parseInt(dayArr[2]);
	var hourArr 	= dateArray[1].split(':');
	var hour 		= parseInt(hourArr[0]);
	var minute 		= parseInt(hourArr[1]);
	var sec 		= parseInt(hourArr[2]);
	var created 	= new Date(year,month,day,hour,minute,sec);
	var rightNow 	= new Date();
	var dateDiff 	= new Date(parseInt(rightNow.getTime()) - parseInt(created.getTime()));
	var years		= dateDiff.getYear();
	var months 		= dateDiff.getMonth();
	var days 		= dateDiff.getDate();
	var hours 		= dateDiff.getHours();
	var mins 		= dateDiff.getMinutes();
	var ret			= '';
	var curLvl		= 0;
	var years		= 0;
	//years
	if (years = 1){
		ret		= years + ' Year ';
		curLvl++;
	}
	
	if (years > 1){
		ret		= years + ' Year(s) ';
		curLvl++;
	}
	
	if (curLvl	== level){
		return ret;
	}
	//months
	if (months == 1){
		ret		+=  months + ' Month ';
		curLvl++;
	}
	
	if (months > 1){
		ret		+=  months + ' Months ';
		curLvl++;
	}
	
	if (curLvl	== level){
		return ret;
	}
	//days
	if (days == 1){
		ret		+=  days + ' Day ';
		curLvl++;
	}
	
	if (days > 1){
		ret		+=  days + ' Days ';
		curLvl++;
	}
	if (curLvl	== level){
		return ret;
	}
	
	//hours
	if (hours == 1){
		ret		+=  hours + ' Hour ';
		curLvl++;
	}
	
	if (hours > 1){
		ret		+=  hours + ' Hours ';
		curLvl++;
	}
	if (curLvl	== level){
		return ret;
	}
	
	//mins
	if (mins == 1){
		ret		+=  mins + ' Min ';
	}
	
	if (mins > 1){
		ret		+=  mins + ' Mins ';
	}
	
	return ret;
}

function getPageSize(parent){
	parent = parent || document.body;              
	var windowWidth, windowHeight;
	var pageHeight, pageWidth;
	
	if (parent != document.body) {
		windowWidth = parent.getWidth();
		windowHeight = parent.getHeight();                                
		pageWidth = parent.scrollWidth;
		pageHeight = parent.scrollHeight;                                
	} else {
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {  
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
	
		if (self.innerHeight) {  // all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}  
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
	// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){  
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	}  
	           
	return {pageWidth: pageWidth ,pageHeight: pageHeight , windowWidth: windowWidth, windowHeight: windowHeight};
};


function hideProcessLogin(){
	//very triky thing should be remebered
	top.frames['frmLogin'].document.getElementById('Processing').style.display = 'none';
	parent.closeModal();	
};
