/*********************************
	Misc functions
*********************************/
function isValidEmail(Email) {
	var str=Email;
	var emailresults;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	
	if (filter.test(str))
		emailresults=true;
	else
		emailresults=false;
		
	return (emailresults);
}

function trim(strText) { 
    
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;

} 

function doPopUp(url, winName, winW, winH, winFeatures){
	var xPos = (screen.width - winW) / 2;
	var yPos = (screen.height - winH) / 2;
	//scrollbars=no,resizable=yes
	if (winFeatures!='')
		winFeatures  = winFeatures + ','
	
	winFeatures = winFeatures + 'height=' + winH + ',width=' + winW + ',top=' + yPos + ',left=' + xPos;
	theWin = window.open(url, winName, winFeatures); 
	theWin.focus();
	return theWin;
}


function checkMaxChar(objTxt,	maxChar){
	if (objTxt.value.length >= maxChar){
		if (isie) 
			event.returnValue=false;
		else 
			objTxt.value = objTxt.value.substring(0, maxChar-1);
	}
}

function isAlphaNumeric(str){
	for(var i=0;i<str.length;i++){
		if (!((str.charCodeAt(i)>=48 && str.charCodeAt(i)<=57) || (str.charCodeAt(i)>=65 && str.charCodeAt(i)<=90) || (str.charCodeAt(i)>=97 && str.charCodeAt(i)<=122))){
			return false
		}
	}
	
	return true;
}


function isEmptyChecked(obj){
	for (var i=0; i<obj.length; i++) {
		if (obj[i].checked)
			return false;
	}

	return true;
}


function getRadioIndex(obj) {
	for (var i=0; i<obj.length; i++) {
		if (obj[i].checked)
			return i;
	}

	return -1;
}


/*********************************
	Cookie functions
*********************************/
function getCookie(NameOfCookie){
	if (document.cookie.length > 0){
		begin = document.cookie.indexOf(NameOfCookie+"="); 
	
		if (begin != -1){
			begin += NameOfCookie.length+1;
			end = document.cookie.indexOf(";", begin);

			if (end == -1) 
				end = document.cookie.length;
			
			return unescape(document.cookie.substring(begin, end));
		} 
	}
	return ''; 
}



function setCookie(NameOfCookie, value, expiredays){
	var ExpireDate = new Date ();
	ExpireDate.setTime(ExpireDate.getTime() + (expiredays * 24 * 3600 * 1000));
	document.cookie = NameOfCookie + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + ExpireDate.toGMTString());
}



function delCookie (NameOfCookie){
	var cookieValue=getCookie(NameOfCookie)
	if (cookieValue.length>0) {
		document.cookie = NameOfCookie + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}


function setSelectObject(objSelect, v){
	for (var i=0; i<objSelect.length; i++){
		if (objSelect.options[i].value==v){
			objSelect.selectedIndex=i;
			break;
		}
	}
}




/*********************************
	Star Rating functions
*********************************/
function setImgRate(objId, imgId) {
	if (document.getElementById(objId).src!=document.getElementById(imgId).src) {
		document.getElementById(objId).src=document.getElementById(imgId).src;
	}
}

function rateIt(objId, score) {
	var post_data = {
               score : score,
               listEbookId : objId
           };
		$.post("/starrate/dbrate.php", post_data, 
		function(data){
			setVote(data, objId);
	}, 'json');
}


function setVote(returnData, objId) {
//alert(returnData.voteRecorded);
	var divId = "totVote" + objId.toString();
	var newImgScore = "star" + returnData.curScore;
	var scoreId = "score" + objId;
	/*var curTotVotes = document.getElementById(divId).innerHTML;
	if (returnData.voteRecorded == 'Y') {
		document.getElementById(divId).innerHTML = ++curTotVotes;
		//document.getElementById(scoreId).value = newImgScore;
	}*/
	setImgRate(objId, document.getElementById(scoreId).value)
	document.getElementById(objId).useMap = '';
	document.getElementById(objId).removeAttribute("style");
	document.getElementById(objId).title = 'Your vote has been recorded.';
	document.getElementById(objId).alt = 'Your vote has been recorded.';
}
