var scripttag = document.getElementsByTagName('script')[0];
wwwbase = scripttag.src.match(/(.*)\/js.*/)[1];

function login( what ){
	var url = wwwbase+'/ajax/login';
	$('hash').value = hex_md5(hex_md5($('password').value) + $('challenge').value);
	$('password').value = '';
	var pars = Form.serialize(what);
	var myAjax = new Ajax.Request( url, { method: 'post', parameters: pars, onComplete: onsendData } );
}

function onsendData (originalRequest) {
	eval("var result = "+originalRequest.responseText+";");

	if(!result.error )
		window.location = wwwbase;
	else {
		$('help').className = 'warning';
		$('help').innerHTML = '<span>' + result.error + '</span>';
	}
}

function mark_stars(elm) {
	var elms = document.getElementById('rating_list').childNodes;
	for(var i=0; i<elms.length; i++) {
		if(elm.__Identifier >= elms[i].__Identifier)
			elms[i].className = 'selected';
		else
			elms[i].className = '';
	}
}

function clear_stars(elm) {
	for(var i=0; i<elm.childNodes.length; i++)
		elm.childNodes[i].className = '';
}

window.onload = function() {
	if(document.getElementById('rating_list') != null && typeof document.getElementById('rating_list') != "undefined" ) {
		var childs = document.getElementById('rating_list').childNodes;
		for(var i=0; i<childs.length; i++) {
			childs[i].__Identifier = i;
			childs[i].onmouseover = function() { mark_stars(this); };
		}
		document.getElementById('rating_list').onmouseout = function() { clear_stars(this); }
	}
}

function getUpdate(typ,pr1,pr2,pid) {
	if(pr2 == 0 && pr1 > 0)
		trailer_done();
}

function testCC () {
	myCardNo = document.getElementById('card_number').value;
	myCardType = document.getElementById('card_type').value;
	if (checkCreditCard (myCardNo,myCardType)) {
		document.getElementById('ccForm').submit();
	} 
	else {
		document.getElementById('help').className = 'warning';
		document.getElementById('help').innerHTML = ccErrors[ccErrorNo];
	}
}

function change_tab(id) {
	if(id == "five_wishes") {
		document.getElementById('five_wishes').style.display = 'block';
		document.getElementById('editors_pick').style.display = 'none';
	}
	else if (id == "editors_pick") {
		document.getElementById('five_wishes').style.display = 'none';
		document.getElementById('editors_pick').style.display = 'block';
	}
}

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}



function showLeftSymbols(area,maxSize,divID){
	left=maxSize;
	text = area.value;
	if (text.length>0){
		if (maxSize-text.length>0){
	      left = maxSize-text.length;
		}else{
		  left=0;
		  area.value =text.substr(0,maxSize);	
		}	
	}
	document.getElementById(divID).innerHTML = left;
}


function showDiv(state,divID){
 if (state.checked){
	document.getElementById(divID).style.display = 'inline';
 }else{
	document.getElementById(divID).style.display = 'none';
 }
}