﻿function Rotator(divId,slotId){
	this.banners = new Array();
	this.currentImage = 0;
	this.divName = divId;
	this.divId = $(divId);
	this.slotId = slotId;
	
	this.start = function(){
		this.changeImage();
		setInterval(this.divName+'.changeImage()',3000);
	}
		
	this.add = function(banner){
		this.banners.push(banner);
	}
	
	this.changeImage = function(){
		if(this.banners.length !== 0){
			var banner = this.banners[this.currentImage];
			if(banner !== ""){
				this.divId.style.backgroundImage="url('"+banner[0]+"')";
				if(banner[1] !== ""){
					$('url'+slotId).href=banner[1];
				}else{	
					//
				}
			}
			if(this.currentImage === (this.banners.length-1)){
				this.currentImage = 0;
			}else{
				this.currentImage++;
			}
		}			
	}		
}

function merchantRotator(){
	this.merchants = new Array();
	this.currentImage = 0;
	this.prevImage = 0;
	
	this.start = function(){
		this.changeImage();
		setInterval('merchantRotator.changeImage()',3000);
	}
		
	this.add = function(merchant){
		this.merchants.push(merchant);
	}
	this.changeImage = function(){
		if(this.merchants.length !== 0){
			var merchant = this.merchants[this.currentImage];
			var prevMerchant = this.merchants[this.prevImage];
			var liId = prevMerchant[3];
			$(liId).style.backgroundColor= "#FFFFFF";
			
			var id = merchant[0];
			var url = merchant[1];
			
			var desc = merchant[2];
			desc = desc.replace('\n','');
			var liId = merchant[3];
			
			
			
			$(liId).style.backgroundColor= "#E8D8B9";
			$('merchantText').innerHTML = desc;
			$('merchantUrl').href= url;
			$('merchantImage').src= "../images/upload/merchants/"+id+".jpg";
				
			if(this.currentImage === (this.merchants.length-1)){
				this.prevImage = (this.merchants.length-1);
				this.currentImage = 0;
			}else{
				this.prevImage = this.currentImage;
				this.currentImage++;
			}	
		}		
	}		
}


/***********************************************
* Cool DHTML tooltip script II- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var offsetfromcursorX=40 //Customize x offset of tooltip
var offsetfromcursorY=10 //Customize y offset of tooltip
var offsetdivfrompointerX=10 //Customize x offset of tooltip DIV relative to pointer image
var offsetdivfrompointerY=14 //Customize y offset of tooltip DIV relative to pointer image. Tip: Set it to (height_of_pointer_image-1).

document.write('<div id="dhtmltooltip"></div>'); //write out tooltip DIV
document.write('<img id="dhtmlpointer" src="/images/merchantpage/arrow2.gif">'); //write out pointer image

var ie=document.all;
var ns6=document.getElementById && !document.all;
var enabletip=false;
if (ie||ns6)
var tipobj=document.all? document.all["dhtmltooltip"] : document.getElementById? document.getElementById("dhtmltooltip") : "";

var pointerobj=document.all? document.all["dhtmlpointer"] : document.getElementById? document.getElementById("dhtmlpointer") : "";

function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function ddrivetip(thetext, thewidth, thecolor){
	if (ns6||ie){
		if (typeof thewidth!="undefined"){ 
		 tipobj.style.width=thewidth+"px";
	 	}
	 	
		if (typeof thecolor!="undefined" && thecolor!=""){
			tipobj.style.backgroundColor=thecolor;
		} 
	tipobj.innerHTML=thetext;
	enabletip=true;
	return false;
	}
}

function positiontip(e){
	if (enabletip){
		var nondefaultpos=false
		var curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
		var curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;
		
		//Find out how close the mouse is to the corner of the window
		var winwidth=ie&&!window.opera? ietruebody().clientWidth : window.innerWidth-150;
		var winheight=ie&&!window.opera? ietruebody().clientHeight : window.innerHeight-150;
		
		var rightedge=ie&&!window.opera? winwidth-event.clientX-offsetfromcursorX : winwidth-e.clientX-offsetfromcursorX;
		var bottomedge=ie&&!window.opera? winheight-event.clientY-offsetfromcursorY : winheight-e.clientY-offsetfromcursorY;
		
		var leftedge=(offsetfromcursorX<0)? offsetfromcursorX*(-1) : -1000;

		//if the horizontal distance isn't enough to accomodate the width of the context menu
		if (rightedge<tipobj.offsetWidth){
		//move the horizontal position of the menu to the left by it's width
			tipobj.style.left=curX-tipobj.offsetWidth+"px";
			nondefaultpos=true
		}else if (curX<leftedge){
			tipobj.style.left="5px";
		}else{
			//position the horizontal position of the menu where the mouse is positioned
			tipobj.style.left=curX+offsetfromcursorX-offsetdivfrompointerX+"px";
			pointerobj.style.left=curX+offsetfromcursorX+"px";
		}
		//same concept with the vertical position
		if (bottomedge<tipobj.offsetHeight){
			tipobj.style.top=curY-tipobj.offsetHeight-offsetfromcursorY+"px";
			nondefaultpos=true
		}else{
			tipobj.style.top=curY+offsetfromcursorY+offsetdivfrompointerY+"px";
			pointerobj.style.top=curY+offsetfromcursorY+"px";
		}
		
		tipobj.style.visibility="visible";
		if (!nondefaultpos){
			pointerobj.style.visibility="hidden";
		}else{
			//increase left margin when div is at the right of the screen
			if (tipobj.style.left){
				var modleft = tipobj.style.left.replace('px','');
				tipobj.style.left = (modleft - 30) + 'px';
			}
		}
		pointerobj.style.visibility="hidden";
	}
}

function hideddrivetip(){
	if (ns6||ie){
		enabletip=false;
		tipobj.style.visibility="hidden";
		pointerobj.style.visibility="hidden";
		tipobj.style.left="-1000px";
		tipobj.style.backgroundColor='';
		tipobj.style.width='';
	}
}
	document.onmousemove=positiontip;




//Onmouseover function
function fnOver(title, text, id){
	
	var image = "<img src='images/upload/merchants/" + id + ".jpg' width='200' height='150' alt='' />";
	
	var strhtml = "<table class='merchantdisplay' border='0' cellspacing='4' cellpadding='4'>";
	strhtml += "<tr><td align='center'><b>" + title + "</b></td></tr>";
	strhtml += "<tr><td align='center' height='160'>" + image + "</td></tr>";
	strhtml += "<tr><td height='20' class='normaltext' align='center' valign='top' style='padding-left:6px;'>" + text + "&nbsp;</td></tr>";
	strhtml += "<tr><td height='20' >&nbsp;</td></tr>";
	strhtml += "</table>";
	ddrivetip(strhtml, 340);
}

//onmouseout function
function fnOut(objTd)
{
	hideddrivetip();
}


function showTopMerchant(id,url,desc,liId){
	$(liId).style.backgroundColor= "#E8D8B9";
	$('merchantText').innerHTML = desc;
	$('merchantUrl').href= url;
	$('merchantImage').src= "images/upload/merchants/"+id+".jpg";
}
function unShowTopMerchant(liId){
	$(liId).style.backgroundColor= "";	
}



function trim(string)
{
	if (string != '')
	{
		while(string.substr(0,1)==" ")
		{
			string = string.substring(1,string.length);
		}

		while(string.substr(string.length-1,1)==" ")
		{
			string = string.substring(0,string.length-2);
		}
		
		return string;
	}
	else
	{
		return '';
	}
}

// Safely escape quotes in javascript
function safeJs(string)
{
	if (string != '')
	{
		while(string.substr(0,1)=="'")
		{
			string = string.substring(1,string.length);
		}

		while(string.substr(string.length-1,1)=="'")
		{
			string = string.substring(0,string.length-2);
		}
		
		return string;
	}
	else
	{
		return '';
	}
}

//Check the syntax of an emailaddress
function checkEmail(emailAddress)
{
	emailaddress = trim(emailAddress);
	//alert(emailAddress)
	if (emailaddress != '')
	{
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(emailAddress))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		return false;
	}
}

function trim(value) {
	value = value.replace(/^\s+/,'');
	value = value.replace(/\s+$/,'');
  return value;
}

function showHideElement(element){
	if($(element).style.display == 'none'){
		$(element).style.display = "block";
	}else{
		$(element).style.display = "none";
	}	
}
function checkContestForm(emailError, nameError, answerError, formError){
	var name = $('nameField').value;
	var email = $('emailField').value;
	var answers = document.getElementsByClassName('answer');
	var valid = true;
	var msg = "";
	
	if(name === ""){
		valid = false;
		msg += "- "+nameError+"\n";
		
	}
	if(!checkEmail(email)){
		valid = false;
		msg += "- "+emailError+"\n";
	}

	for(var i=0;i<answers.length;i++){
		var value = answers[i].value;
		value = trim(value);
		if(value === ""){
			answers = false;
		}
	}	
	
	if(!answers){
		msg += "- "+answerError+"\n";
		valid = false;
	}
	
	if(!valid){
		alert(formError+"\n"+msg);
		return false;	
	}else{
		return true;
	}	
}

function submitenter(myfield,e)
{
	
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
	   {
	   alert(myfield.form.name);
	   myfield.form.submit();
	   
	   return false;
	   }
	else
	   return true;
}

/* check balance */
function keyup(obj) {
	if (obj.name == 'sc5' && obj.value.length > 2) {
		$('btn_checkCode').focus();
	}else{
		// go to next field
		if (obj.value.length > 3) {
			switch (obj.name) {
				case 'sc1':
					document.checkform.sc2.focus();
					break;
				case 'sc2':
					document.checkform.sc3.focus();
					break;
				case 'sc3':
					document.checkform.sc4.focus();
					break;
				case 'sc4':
					document.checkform.sc5.focus();
					break;
			}
		}
		// backspace to previous field
		if(obj.value.length < 1){
			if(obj.keyCode = "8"){
				var focusedElement;
				switch (obj.name) {
					
					case 'sc2':
						document.checkform.sc1.focus();
						focusedElement = 'sc1';
						break;
					case 'sc3':
						document.checkform.sc2.focus();
						focusedElement = 'sc3';
						break;
					case 'sc4':
						document.checkform.sc3.focus();
						focusedElement = 'sc3';
						break;
					case 'sc5':
						document.checkform.sc4.focus();
						focusedElement = 'sc4';
						break;
				}
				
				//fix for IE, move cursor to last character in field
				if	(typeof document.selection != 'undefined') {
					var oSel = document.selection.createRange ();
					// Move selection start and end to 0 position
			       	oSel.moveStart ('character', -$(focusedElement).value.length);
			  
			       	// Move selection start and end to desired position
			       	oSel.moveStart('character',4);
			       	oSel.moveEnd('character', 0);
			      	oSel.select();
		      	}	
			}		
		}
	}
}

//check saldo
function checkSaldo(language, langid)
{
	sc1 = $('sc1').value;
	sc2 = $('sc2').value;
	sc3 = $('sc3').value;
	sc4 = $('sc4').value;
	sc5	= $('sc5').value;
	var imgUrl = "/scripts/testShowSaldo.php?lang="+language+"&code="+sc1+sc2+sc3+sc4+sc5+'&langid='+langid;
    $('img_saldoChecker').src = imgUrl; 

	$('div_saldoChecker').style.display = "block";
	$('btn_clearCode').style.display = "block";
}
// clear the saldochecker fields
function clearCode()
{
	 $('sc1').value = '';
	 $('sc2').value = '';
	 $('sc3').value = '';
	 $('sc4').value = '';
	 $('sc5').value = '';
	// document.checkform.submit();
	$('div_saldoChecker').style.display = "none";
	$('btn_clearCode').style.display = "none";
}

function checkContest(){
	var fields = $('questionField').value;
	
	alert(fields);
}