var invbD = 'invisibleWorker';
var imgFolder = '/templates/t2437/images/';


function handleDefaultField(fld,defVal) {
    if (getFieldValue(fld)==defVal) {
        changeFieldValue(fld,'');
        changeClass(fld,'active');
    }   else {
        if (!getFieldValue(fld)) {
            changeFieldValue(fld,defVal);
            changeClass(fld,'inactive');
        }
    }
}

function handleDefaultPassField(fld,defVal) {
	if (getFieldValue(fld)==defVal) {
		document.getElementById(fld).type='password';
		changeFieldValue(fld,'');
		changeClass(fld,'active');
	} 
}


function in_array(needle, haystack, argStrict) {
	var key = '',
	strict = !! argStrict;
	if (strict) {
		for (key in haystack) {
			if (haystack[key] === needle) {
				return true;
			}
		}
	} else {
		for (key in haystack) {
			if (haystack[key] == needle) {
				return true;
			}
		}
	}
	return false;
}

function whatkeypressed(e) {
	var charCode;
	if(e && e.which) {
		e = e;
		charCode = e.which;
	} else {
		e = event;
		charCode = e.keyCode;
	}
	return charCode;
}


function validateField(val,min,max,valCap,vldOkFld) {
	var valLength = val.length;
	if (valLength < min || min > max) {
		showBlockByID(valCap);
		changeFieldValue(vldOkFld,0);
	} else {
		hideByID(valCap);
		changeFieldValue(vldOkFld,1);	
	}
}

function validateEmail(str,req,valCap,vldOkFld) {
	if (req==1 || str) {
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(str)) {
			hideByID(valCap);
			return true;
		} else {
			showBlockByID(valCap);
			return false;
		}
	} else {
		hideByID(valCap);
		return true;	
	}
}

function createSelection(start, end, field) {

    if ( field.createTextRange ) {
        /* 
        IE calculates the end of selection range based 
        from the starting point.
        Other browsers will calculate end of selection from
        the beginning of given text node.
        */
        var newend = end - start;
        var selRange = field.createTextRange();
        selRange.collapse(true);
        selRange.moveStart("character", start);
        selRange.moveEnd("character", newend);
        selRange.select();
    }  else if( field.setSelectionRange ){
        field.setSelectionRange(start, end);
    } 
    field.focus();
}


function phoneCheck(val,field,valCap0,valCap1,valCap2,valCap3,evt,vldOkFld) {
	/*
	// valCap0  checks that  the last character is a number
	// valCap1  checks that the first number is 0
	// valCap2  checks that  the phone prefix is legal
	// valCap3  checks that  there is any value in the phone
	*/
	var capToShow;
	hideByID(valCap0);
	hideByID(valCap1);
	hideByID(valCap2);
	hideByID(valCap3);
	if (val) {
		curIn = val[val.length-1];
		if (!IsNumeric(curIn)) {
			start = val.length-1;
			end = val.length;
			var newVal = val.substring(0, val.length-1);
			changeFieldValue(field,newVal);
			capToShow = valCap0;
		} else {
			hideByID(valCap0);
			if (val.length==1) {
				if (val > 0) {
					capToShow = valCap1;
					var newVal = val.substring(0, val.length-1);
					changeFieldValue(field,newVal);
				} 
			} else {
				keycode = whatkeypressed(evt);
				if (val.length==2 || val.length==3) {
					var prefixArray = new Array('02','03','04','05','050','052','054','057','072','073','074','076','077','078','08','09');
					if(in_array(val, prefixArray, '')) {
						if ((val!='05' && keycode!=8) || !keycode) {
							changeFieldValue(field,val+'-');
						}
					} else {
						capToShow = valCap2;
						start = val.length-1;
						end = val.length;
						createSelection(start, end, document.getElementById(field));
					}
				} 
			}
		}
	} else {
		capToShow = valCap3;
	}
	
	if (capToShow) {
		showBlockByID(capToShow);	
		changeFieldValue(vldOkFld,0);
	} else {
		if (val.length==11 || val.length==10) {
			changeFieldValue(vldOkFld,1);
		} else {
			changeFieldValue(vldOkFld,0);	
		}
	}
}

animatedcollapse.ontoggle=function($, divobj, state){ 
	//fires each time a DIV is expanded/contracted
	//$: Access to jQuery
	//divobj: DOM reference to DIV being expanded/ collapsed. Use "divobj.id" to get its ID
	//state: "block" or "none", depending on state
}
animatedcollapse.init()

$(function() {
	$(".newsticker-jcarousellite").jCarouselLite({
		vertical: true,
		hoverPause:true,
		visible: 3,
		auto:500,
		speed:1000
	});
});

function openChatWindow(link,id,block) {
	var x = window.open('/?section=157'+link,'chatWindow','status=1,toolbars=1,resizeable=1,width=650,height=680,scrollbars=1');	
	x.focus();
}
var persistclose=0;var startX = 30;var startY = 5;var verticalpos="fromtop";
function iecompattest(){return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body}
function get_cookie(Name) {var search = Name + "=";var returnvalue = "";if(document.cookie.length > 0) {offset = document.cookie.indexOf(search);if(offset != -1) {offset += search.length;end = document.cookie.indexOf(";", offset);if(end == -1) end = document.cookie.length;returnvalue=unescape(document.cookie.substring(offset, end))}}return returnvalue;}
 
function chatRequests(sectionID, templateID){reloadPage('/chatRequests.php?sectionID='+sectionID+'&templateID='+templateID,'','topbar');getChatRequests(sectionID, templateID);}
function getChatRequests(sectionID, templateID){setTimeout(function(){chatRequests(sectionID, templateID)},25000);}
function closebar(){if(persistclose)document.cookie="remainclosed=1";if(document.getElementById("topbar")) document.getElementById("topbar").style.visibility="hidden";}
function denyChat(denyChatLink, element){reloadPage(denyChatLink, '', element);}
function staticbar(){
	if (document.getElementById("topbar")) {
	barheight=document.getElementById("topbar").offsetHeight
	var ns = (navigator.appName.indexOf("Netscape") != -1) || window.opera;
	var d = document;
	function ml(id){
		var el=d.getElementById(id);
		if (!persistclose || persistclose && get_cookie("remainclosed")=="")
		el.style.visibility="visible"
		if(d.layers)el.style=el;
		el.sP=function(x,y){this.style.left=x+"px";this.style.top=y+"px";};
		el.x = startX;
		if (verticalpos=="fromtop")
		el.y = startY;
		else{
		el.y = ns ? pageYOffset + innerHeight : iecompattest().scrollTop + iecompattest().clientHeight;
		el.y -= startY;
		}
		return el;
	}
	window.stayTopLeft=function(){
		if (verticalpos=="fromtop"){
		var pY = ns ? pageYOffset : iecompattest().scrollTop;
		ftlObj.y += (pY + startY - ftlObj.y)/8;
		}
		else{
		var pY = ns ? pageYOffset + innerHeight - barheight: iecompattest().scrollTop + iecompattest().clientHeight - barheight;
		ftlObj.y += (pY - startY - ftlObj.y)/8;
		}
		ftlObj.sP(ftlObj.x, ftlObj.y);
		setTimeout("stayTopLeft()", 10);
	}
	ftlObj = ml("topbar");
	stayTopLeft();
	}
}
if(window.addEventListener)window.addEventListener("load", staticbar, false);else if(window.attachEvent)window.attachEvent("onload", staticbar);else if(document.getElementById);window.onload=staticbar;


/* @@@@@@@@@@@@@@@@@@@@@@ */



function handleDefaultField(fld,defVal) {
    if (getFieldValue(fld)==defVal) {
        changeFieldValue(fld,'');
        changeClass(fld,'active');
    }   else {
        if (!getFieldValue(fld)) {
            changeFieldValue(fld,defVal);
            changeClass(fld,'inactive');
        }
    }
}

function toTop(id){ 
	document.getElementById(id).scrollTop=0 
} 

defaultStep=1 
step=defaultStep 
function scrollDivDown(id){ 
	document.getElementById(id).scrollTop+=step 
	timerDown=setTimeout("scrollDivDown('"+id+"')",10) 
} 

function scrollDivUp(id){
	document.getElementById(id).scrollTop-=step 
	timerUp=setTimeout("scrollDivUp('"+id+"')",10) 
} 

function toBottom(id){ 
	document.getElementById(id).scrollTop=document.getElementById(id).scrollHeight 
} 

function toPoint(id){ 
	document.getElementById(id).scrollTop=100 
} 

var brandArray = new Array();
var curAlbum = 'defaultAlbum';
function showBrand(id) {
	changePic('brand'+id,brandArray[id][1]);
	changePic('brandLogo',brandArray[id][0]);
	hideByID(curAlbum);
	showBlockByID('album'+brandArray[id][2]);
	curAlbum = 'album'+brandArray[id][2];
	changePic('bigPic',getFieldValue('bigP_'+brandArray[id][2]+'_1'));
}

function checkHomeForm() {
	var fail=0;
	if (!getFieldValue('comments')) {
		fail=1;
		changeBackroundColor('comments','red');	
	} else {
		changeBackroundColor('comments','white');	
	}
	var phone = getFieldValue('phone');
	if (!phone || phone.length<7 ) {
		fail=1;
		changeBackroundColor('phone','red');	
	} else {
		changeBackroundColor('phone','white');	
	}
	
	if (checkEmail1('email',1)==false) {
		fail=1;
		changeBackroundColor('email','red');
	} else {
		changeBackroundColor('email','white');	
	}
	
	if (fail!=1) {
		hideByID('contactForm');
		showBlockByID('thanks');	
	} else {
		return false;	
	}
}

function checkEmail1(emailField,req) {
	if (req==1 || getFieldValue(emailField)) {
		var str = document.getElementById(emailField).value;
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if (filter.test(getFieldValue(emailField))) {
			return true;
		} else {
			return false;
		}
	} else {
		return true;	
	}
}

function openBranch(id) {
	if (document.getElementById('brnch'+id).style.display=='block') {
		hideByID('brnch'+id);
		changePic('brnchBtn'+id,'/templates/t2295/images/infoOpen.png');
	} else {
		showBlockByID('brnch'+id);	
		changePic('brnchBtn'+id,'/templates/t2295/images/infoClose.png');
		
	}
}

function starSlider(){
	if(document.getElementById("itemsHolder_1")){
		document.getElementById("itemsHolder_1").startMe = autoscrollMenuObj
		document.getElementById("itemsHolder_1").startMe()
	}	
} 


var curPic = 1;
var picsList = new Array();
function showGal(actn,pic) {
	var totalPics = getFieldValue('numPics');
	changeClass('linkPic'+curPic,'picNumLink');
	if (actn=='n' || actn=='p') {
		if (actn=='n') {
			next = curPic+1;
			if (next > totalPics) next=1;
			changePic('bigPic',picsList[next]);
			changeClass('linkPic'+next,'picNumLinkSel');
			curPic = next;
		} else {
			prev = curPic-1;
			if (prev==0) prev=totalPics;
			changePic('bigPic',picsList[prev]);
			changeClass('linkPic'+prev,'picNumLinkSel');
			curPic = prev;
		}
	} else {
		changePic('bigPic',picsList[actn]);
		changeClass('linkPic'+actn,'picNumLinkSel');
	}
}

var xTo = new Array();
function fadeIn(id,x) {
	value = 1;
	document.getElementById(id).style.opacity='0.1';
	document.getElementById(id).style.filter  = 'alpha(opacity=' + value*10 + ')';
	xTo[x] = setInterval(function(){fadeInDo(id,x)},40);
}

function fadeInDo(id,x) {
	elem = document.getElementById(id);
	opacityAsDecimal = (elem.style.opacity*1)+(0.1*1);
	opacityAsInt = opacityAsDecimal*100;
	if (opacityAsDecimal<=1) {
		elem.style.opacity = opacityAsDecimal;	
		elem.style.filter  = "alpha(opacity=" + opacityAsInt + ")";
	} else {
		clearTimeout(xTo[x]); 
	}
}

