function trim(str) {
    return str.replace(/^\s+|\s+$/g, '');
}
function overLayTitleText(id, defaultClass, noDataClass){
	if(document.getElementById(id)){
		var El = document.getElementById(id);
	}else{
		return false;
	}
	
	if(trim(El.value) == ""){
		El.value = El.getAttribute('title');
		El.className = noDataClass;
	}
	
	function handleFocus(){
		var El = (window.event)?window.event.srcElement:this;
		if(trim(El.value) == El.getAttribute('title')){
			El.value = "";
			El.className = defaultClass;
		}
	}
	
	function handleBlur(){
		var El = (window.event)?window.event.srcElement:this;
		if(trim(El.value) == ""){
			El.value = El.getAttribute('title');
			El.className = noDataClass;
		}
	}
	
	
	if(El.addEventListener){
		El.addEventListener("focus", handleFocus, false);
		El.addEventListener("blur", handleBlur, false);
	}
	if(El.attachEvent){
		El.attachEvent("onfocus", handleFocus);
		El.attachEvent("onblur", handleBlur);
	}
}

function submitContactus(){
	
	var validated = true;
	//Hide all error messages here
	$("#usernameError").hide();
	$("#emailError").hide();
	$("#bookNameError").hide();

	if($.trim($("#username").attr("value")).length == 0 || $("#username").attr("value") == $("#username").attr("title")){
		validated = false;
		//Unhide appr. error
		$("#usernameError").show();
	}

	var email_pattern = /^[^@]+@[^@]+\.(([a-zA-Z]{2}\.[a-zA-Z]{2})|([a-zA-Z]{3})|([a-zA-Z]{4}))$/i;
	var email = $("#email").attr("value");
	if($.trim(email).length == 0 || email == $("#email").attr("title") || email.match(email_pattern) == null){
		validated = false;
		//Unhide appr. error
		$("#emailError").show();
	}
	if($.trim($("#phone").attr("value")).length == 0 || $("#phone").attr("value") == $("#phone").attr("title")){
		validated = false;
		//Unhide appr. error
		$("#phoneError").show();
	}
	if($.trim($("#bookName").attr("value")).length == 0 || $("#bookName").attr("value") == $("#bookName").attr("title")){
		validated = false;
		//Unhide appr. error
		$("#bookNameError").show();
	}
	if($.trim($("#desc").attr("value")).length == 0 || $("#desc").attr("value") == $("#desc").attr("title")){
		validated = false;
		//Unhide appr. error
		$("#descError").show();
	}

	if(validated == false){
		return false;
	}

	//alert($("#contactForm").serialize());
	$.ajax({
		type: "POST",
		async: true,
		url: 'contactus.php',
		data: $("#contactForm").serialize(),
		success: function(data){
			//if(data == 'Success'){
				//show thank you message
				$("#contactUs").hide();
				$("#inputsDiv").hide();
				$("#thnxMsg").fadeIn("slow");
			//}
		}
	});

	//Show progress bar
	return false;
}

function submitRegistration(){
	
	var validated = true;
	//Hide all error messages here
	$("#unameError").hide();
	$("#remailError").hide();
	$("#pwdError").hide();
	$("#cpwdError").hide();

	if($.trim($("#uname").attr("value")).length == 0 || $("#uname").attr("value") == $("#uname").attr("title")){
		validated = false;
		//Unhide appr. error
		$("#unameError").show();
	}

	var email_pattern = /^[^@]+@[^@]+\.(([a-zA-Z]{2}\.[a-zA-Z]{2})|([a-zA-Z]{3})|([a-zA-Z]{4}))$/i;
	var email = $("#remail").attr("value");
	if($.trim(email).length == 0 || email == $("#email").attr("title") || email.match(email_pattern) == null){
		validated = false;
		//Unhide appr. error
		$("#remailError").show();
	}
	if($.trim($("#pwd").attr("value")).length == 0 || $("#pwd").attr("value") == $("#pwd").attr("title")){
		validated = false;
		//Unhide appr. error
		$("#pwdError").show();
	}
	if($.trim($("#cpwd").attr("value")) != $.trim($("#pwd").attr("value"))){
		validated = false;
		//Unhide appr. error
		$("#cpwdError").show();
	}

	if(validated == false){
		return false;
	}

	return true;
	
	//alert($("#contactForm").serialize());
	/*$.ajax({
		type: "POST",
		async: true,
		url: 'register.php',
		data: $("#regForm").serialize(),
		success: function(data){
			alert(data);
			//if(data == 'Success'){
				//show thank you message
				$("#contactUs").hide();
				$("#inputsDiv").hide();
				$("#thnxMsg").fadeIn("slow");
			//}
		}
	});*/

}

function validateSignin(){
	var frm = document.getElementById('loginForm');
	if(($.trim(frm.uname.value) == '' || frm.uname.value == frm.uname.title) || ($.trim(frm.pwd.value) == '' || frm.pwd.value == frm.pwd.title)){
		//remove alert and put clean validation
		alert("Username and password are required");
		return false;
	}
	return true;
}

//syntax isValidFileType(html_element, extensions(separated by '|'), bool)
//example isValidFileType(frm.myImage, "jpg|gif|png|jpeg", true);
function isValidFileType(Element, fileExt, required){ 

	//set default required to true;
	if(required == false) required = false;
	else required = true;

	var file = trim(Element.value);
	//file = file.toLowerCase();
	if(file.length == 0){
		if(required) return false;
		else return true; //not required, i.e, field can be empty 
	}
	var pat = RegExp(".*\.("+fileExt+")$", "i");

	if(file.match(pat) == null) return false;
	else return true;
}

function checkFiletype(eleId, formId, ext){
	var bool = isValidFileType(document.getElementById(eleId), ext, true);
	if(bool){
		startUpload(formId);
	}else{
		alert("Invalid extension");
		document.getElementById(eleId).value="";
		return false;
	}
}

function startUpload(formId){
	var frm = document.getElementById(formId);
	if(frm.myfile.value.length > 0){
		$("#"+formId+"Progress").show();
		frm.myfile.style.display = 'none';
		frm.myParam.value = formId;
	}
	frm.submit();
}

function stopUpload(msg, url, formId, filepath){
	$("#"+formId+"Progress").hide(); 
	frm = document.getElementById(formId);
	frm.myfile.style.display = '';
	frm.imgName.value=url;
	if (msg == 1) {
		var frm = document.getElementById(formId);
		frm.imgShw.style.display="";
		frm.imgShw.src = filepath;
	}
}

/*function startUpload(formId){
	var frm = document.getElementById(formId);
alert(formId);
	
	if(!document.getElementById(formId+'frame')){
		var x = document.createElement('iframe');
		x.setAttribute('id', formId+'frame');
		document.body.appendChild(x);
	}
	//document.getElementById(formId+'frame').style.display= 'none';
	frm.target = formId+'frame';
	
	
	
	if(frm.myfile.value.length > 0){
		$("#"+formId+"Progress").show();
		//$("#"+elId).hide();
		frm.myfile.style.display = 'none';
		frm.myParam.value = formId;
	}
	frm.submit();
	//alert(formId);
}*/

function addFrmValid() {
	document.getElementById('bkfile').value = document.uploadform.imgName.value;
	document.getElementById('icnfile').value = document.iconForm.imgName.value;
	document.getElementById('cvrfile').value = document.coverForm.imgName.value;
	var title = document.getElementById('title');
	var author = document.getElementById('author');
	var edition = document.getElementById('edition');
	var cright = document.getElementById('cright');
	var stext = document.getElementById('stext');
	var ltext = document.getElementById('ltext');

	if (trim(title.value) == '') {
		alert("Title should not be empty");
		title.focus();
		return false;		
	}
	if (trim(author.value) == '') {
		alert("Author should not be empty");
		author.focus();
		return false;		
	}
	if (trim(edition.value) == '') {
		alert("Edition should not be empty");
		edition.focus();
		return false;		
	}
	if (trim(cright.value) == '') {
		alert("Copy rights field should not be empty");
		cright.focus();
		return false;		
	}
	if (trim(stext.value) == '') {
		alert("Short text should not be empty");
		stext.focus();
		return false;		
	}
	if (trim(ltext.value) == '') {
		alert("Long text should not be empty");
		ltext.focus();
		return false;		
	}
	
	if (document.getElementById('bkfile').value == '') {
		alert("Please Upload Book File");
		return false;		
	}
	if (document.getElementById('icnfile').value == '') {
		alert("Please Upload App Icon");
		return false;		
	}
	if (document.getElementById('cvrfile').value == '') {
		alert("Please Upload Book Cover");
		return false;		
	}	
}

function editFrmValid() {
	//document.getElementById('bkfile').value = document.uploadform.imgName.value;
	document.getElementById('icnfile').value = document.iconForm.imgName.value;
	document.getElementById('cvrfile').value = document.coverForm.imgName.value;

	var title = document.getElementById('title');
	var author = document.getElementById('author');
	var edition = document.getElementById('edition');
	var cright = document.getElementById('cright');
	var stext = document.getElementById('stext');
	var ltext = document.getElementById('ltext');

	if (trim(title.value) == '') {
		alert("Title should not be empty");
		title.focus();
		return false;		
	}
	if (trim(author.value) == '') {
		alert("Author should not be empty");
		author.focus();
		return false;		
	}
	if (trim(edition.value) == '') {
		alert("Edition should not be empty");
		edition.focus();
		return false;		
	}
	if (trim(cright.value) == '') {
		alert("Copy rights field should not be empty");
		cright.focus();
		return false;		
	}
	if (trim(stext.value) == '') {
		alert("Short text should not be empty");
		stext.focus();
		return false;		
	}
	if (trim(ltext.value) == '') {
		alert("Long text should not be empty");
		ltext.focus();
		return false;		
	}

}

function loadPage(obj){
	var frm = document.getElementById('postForm');
	frm.method = 'POST';

	if(obj.action != undefined){
		frm.action = obj.action;
	}

	if(obj.id){
		frm.selId.value = obj.id;
	}
	if(obj.page){
		frm.page.value = obj.page;
	}
	if(obj.type){
		frm.type.value = obj.type;
	}
	if(obj.show){
		frm.show.value = obj.show;
	}
	if(obj.searchString){
		frm.searchString.value = obj.searchString;
	}
	frm.submit();
}

//forgot password
function fpwdValid() { 
	var mailExp= /^[\w\-\.\+]+\@[0-9a-zA-Z\.\-]+\.[0-9A-Za-z]{2,4}$/;
	var fmail = document.getElementById('femail');
	var fmailVal = trim(fmail.value);
	if(fmailVal == '')
	{
		document.getElementById('validMsg').innerHTML = "Please Enter E-mail";
		return false;
	}
	if(!fmailVal.match(mailExp))
	{
		document.getElementById('validMsg').innerHTML = "Please enter valid E-mail";
		return false;
	}
	return true;
}

//new password
function npwdValidation() { 
	var npwd = document.getElementById('npwd');
	var vpwd = document.getElementById('vpwd');
	if(trim(npwd.value) == '')
	{
		document.getElementById('validateMsg').innerHTML = "Please Enter New Password";
		return false;
	}
	if(trim(npwd.value) != trim(vpwd.value))
	{
		document.getElementById('validateMsg').innerHTML = "New password and verify password doesn't match";
		return false;
	}
	return true;
}


function showReview(eve, ele, obj){
	
	$('#buttonsBox').show();
	$('#commentBox').hide();
	$('#postReview').attr("value", "");

	//Set review box position
	$("#reviewBox").css("top", eve.pageY - 15).css("left", eve.pageX + 20).show();

	//Attache events
	$('#approveBtn').unbind('click');	//Removes previous click event.
	$("#approveBtn").click(function(){
		$.post(obj.url, {bid: obj.id, mode:'approveBook'}, function(data){ 
			if(data == 'Success'){
				$('#commentBox').hide();
				window.location.reload();
			}else{
				//alert(data);
			}
		});
	});
	$('#postReviewLnk').unbind('click');
	$("#postReviewLnk").click(function(){
		if($.trim($('#postReview').attr("value")).length == 0){
			alert("Please enter comment");
			return false;
		}
		$.post(obj.url, {bid: obj.id, desc:$('#postReview').attr("value"), mode:'rejectBook'}, function(data){ 
			if(data == 'Success'){
				$('#commentBox').hide();
				window.location.reload();
			}else{
				alert(data);
			}
		});
	});
}
