var directURL = 'http://ti.site/';
var directURL = 'http://www.tracyeinny.com.sg/';

/*
*	QUOTE COMMENTS REPLY (SIMILAR TO LIVE JOURNAL)
*/
function quoteThis(commentid)
{
	document.getElementById('newcomment').style.display = 'none';
	document.getElementById('quotecomment').style.display = 'block';	
	var temp = document.getElementById(commentid).innerHTML;
	var cid = replacestring(commentid, 'quote', '');
	document.getElementById('quotedcomments').innerHTML = temp;
	document.getElementById('qcommentid').value = cid;	
}

function quoteThisFromCollection(commentid, productid)
{
	document.getElementById('newcomment').style.display = 'none';
	document.getElementById('quotecomment').style.display = 'block';
	document.getElementById('quotecommentcollection').style.display = 'none';	
	var temp = document.getElementById(commentid).innerHTML;
	var cid = replacestring(commentid, 'quote', '');
	document.getElementById('quotedcomments').innerHTML = temp;
	document.getElementById('qcommentid').value = cid;
	document.getElementById('qproductid').value = productid;
}

function quoteThisFromCollectionComments(commentid, collectionid)
{
	document.getElementById('newcomment').style.display = 'none';
	document.getElementById('quotecommentcollection').style.display = 'block';	
	document.getElementById('quotecomment').style.display = 'none';	
	var temp = document.getElementById(commentid).innerHTML;	
	var cid = replacestring(commentid, 'quote', '');
	document.getElementById('quotedcommentscollection').innerHTML = temp;
	document.getElementById('qcommentidcollection').value = cid;
	document.getElementById('qcollectionidcollection').value = collectionid;
}

function showForm()
{
	if (document.getElementById('newcomment').style.display == 'block')
	{
		document.getElementById('newcomment').style.display = 'none';
	} else {
		document.getElementById('newcomment').style.display = 'block';	
	}
}

function submitSignup()
{
	var emailadd = document.getElementById('signupname');
	var divid = document.getElementById('response');

	emailaddress = emailadd.value;	
		
	if (emailaddress != "" && (emailadd.value.indexOf("@") != -1 || emailadd.value.indexOf(".") != -1))
	{				
		var div = 'newslettersignup';
		params = 'email=' + emailaddress;

		divid.innerHTML = 'Adding you in...';

		actionURL = directURL + 'autoinsert.php';
		// do the ajax thing
		new Ajax.Updater(div, actionURL, {
			asynchronous:true,
			method:'get',
			parameters: params,
			onSuccess: function(t) {				
				if(t.responseText != '') {
					divid.innerHTML = t.responseText;
					emailadd.value = '';
				}
				else divid.innerHTML = 'Failed to process your request at this moment.';
			}
		})

	} else {
		alert("Please insert a valid email to secure your very own Tracyeinny Newsletters.");
	}
}

/*
*	DISPLAY COMMENTS
*/
function showComment(productid)
{
	ajax('comments', 'ajaxcomments', productid, 'Retrieving comments ... please wait')
	Effect.Appear('comments', {duration: 2});
}

/*
*	FUNCTION TO REPLACE STRING
*/
function replacestring(inputString, badString, goodString, caseSensitive) {
	fixedReplace = "";
	UI = inputString;
	UB = badString;
	if ((caseSensitive != 1) && (caseSensitive != true)) {
		UI = inputString.toUpperCase();
		UB = badString.toUpperCase();
	}
	badEnd = -1;
	badLoc = UI.indexOf(UB);
	if (badLoc != -1) {
		for (x=1; (badLoc != -1); x++) {
			fixedReplace = fixedReplace +
			inputString.substring((badEnd +
			1), badLoc) + goodString
			badEnd = badLoc + UB.length - 1;
			badLoc = UI.indexOf(UB, (badLoc + 1)); }
			fixedReplace = fixedReplace +
			inputString.substring((badEnd + 1),
			inputString.length); }
			else { fixedReplace = inputString;    }
			return fixedReplace;
};

/*	
*	COMMENT SUBMISSIONS
*/
function addNewComment() {

	var tempname = document.getElementById('tempname').value;
	var tempemail = document.getElementById('tempemail').value;
	var productid = document.getElementById('productid').value;	
	var comment = document.getElementById('comment').value;
	comment = replacestring(comment, '&', '%26');
	comment = replacestring(comment, '+', '%2B');

	params = 'action=ajaxaddcomment&var1=' + tempname + '&var2=' + comment + '&var3=' + productid + '&var4=' + tempemail;	

	actionURL = directURL + 'index.php';

	// do the ajax thing
	new Ajax.Updater('', actionURL, {
		asynchronous:true,
		method:'post',
		parameters: params,
		onSuccess: function(t) {
			if(t.responseText != '') {
				if(t.responseText == '50000') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">Empty comments are not allowed. Please try again.</div>';					
				}
				else if(t.responseText == '50001') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">Sorry, you can\'t log consecutive comments within such a short time. Please take a deep breath and try again in a minute.</div>';					
				}
				else if(t.responseText == '50002') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">An error occured while trying to add your comment. Please try again.</div>';					
				}
				else {
					//showComment(productid);
					document.getElementById('tempname').value = '';
					document.getElementById('tempemail').value = '';					
					document.getElementById('comment').value = '';
					document.getElementById('indicator').innerHTML = 'Your comments has been submitted successfully and is currently awaiting approval. Upon approval you may see it here.';
				}
			}
			else {				
				document.getElementById('indicator').innerHTML = 'Failed to process your request.';
			}
		}
	})
}

/*	
*	COMMENT SUBMISSIONS
*/
function addNewCommentCollection() {

	var tempname = document.getElementById('tempname').value;
	var tempemail = document.getElementById('tempemail').value;
	var collectionid = document.getElementById('collectionid').value;	
	var comment = document.getElementById('comment').value;
	comment = replacestring(comment, '&', '%26');
	comment = replacestring(comment, '+', '%2B');

	params = 'action=ajaxaddcommentcollection&var1=' + tempname + '&var2=' + comment + '&var3=' + collectionid + '&var4=' + tempemail;	

	actionURL = directURL + 'index.php';

	// do the ajax thing
	new Ajax.Updater('', actionURL, {
		asynchronous:true,
		method:'post',
		parameters: params,
		onSuccess: function(t) {
			if(t.responseText != '') {
				if(t.responseText == '50000') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">Empty comments are not allowed. Please try again.</div>';					
				}
				else if(t.responseText == '50001') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">Sorry, you can\'t log consecutive comments within such a short time. Please take a deep breath and try again in a minute.</div>';					
				}
				else if(t.responseText == '50002') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">An error occured while trying to add your comment. Please try again.</div>';					
				}
				else {
					//showComment(productid);
					document.getElementById('tempname').value = '';
					document.getElementById('tempemail').value = '';					
					document.getElementById('comment').value = '';
					document.getElementById('indicator').innerHTML = 'Your comments has been submitted successfully and is currently awaiting approval. Upon approval you may see it here.';
				}
			}
			else {				
				document.getElementById('indicator').innerHTML = 'Failed to process your request.';
			}
		}
	})
}


/*	
*	QUOTED COMMENT SUBMISSIONS
*/
function addQuotedComment() {

	var commentid = document.getElementById('qcommentid').value;
	var tempname = document.getElementById('qtempname').value;
	var tempemail = document.getElementById('qtempemail').value;
	var productid = document.getElementById('qproductid').value;	
	var comment = document.getElementById('qcomment').value;
	comment = replacestring(comment, '&', '%26');
	comment = replacestring(comment, '+', '%2B');

	params = 'action=ajaxaddcomment&var1=' + tempname + '&var2=' + comment + '&var3=' + productid + '&var4=' + tempemail + '&var5=' + commentid;

	actionURL = directURL + 'index.php';

	// do the ajax thing
	new Ajax.Updater('', actionURL, {
		asynchronous:true,
		method:'post',
		parameters: params,
		onSuccess: function(t) {
			if(t.responseText != '') {
				if(t.responseText == '50000') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">Empty comments are not allowed. Please try again.</div>';					
				}
				else if(t.responseText == '50001') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">Sorry, you can\'t log consecutive comments within such a short time. Please take a deep breath and try again in a minute.</div>';					
				}
				else if(t.responseText == '50002') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">An error occured while trying to add your comment. Please try again.</div>';					
				}
				else {
					//showComment(productid);
					document.getElementById('qtempname').value = '';
					document.getElementById('qtempemail').value = '';					
					document.getElementById('qcomment').value = '';
					document.getElementById('indicator').innerHTML = 'Your comment has been received. Once it has been approved by our moderators it will be displayed here.';
				}
			}
			else {				
				document.getElementById('indicator').innerHTML = 'Failed to process your request.';
			}
		}
	})
}

/*	
*	QUOTED COMMENT SUBMISSIONS FOR COLLECTION
*/
function addQuotedCommentForCollection() {

	var commentid = document.getElementById('qcommentidcollection').value;
	var tempname = document.getElementById('qtempnamecollection').value;
	var tempemail = document.getElementById('qtempemailcollection').value;
	var collectionid = document.getElementById('qcollectionidcollection').value;	
	var comment = document.getElementById('qcommentcollection').value;
	comment = replacestring(comment, '&', '%26');
	comment = replacestring(comment, '+', '%2B');

	params = 'action=ajaxaddcommentcollection&var1=' + tempname + '&var2=' + comment + '&var3=' + collectionid + '&var4=' + tempemail + '&var5=' + commentid;

	actionURL = directURL + 'index.php';

	// do the ajax thing
	new Ajax.Updater('', actionURL, {
		asynchronous:true,
		method:'post',
		parameters: params,
		onSuccess: function(t) {
			if(t.responseText != '') {
				if(t.responseText == '50000') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">Empty comments are not allowed. Please try again.</div>';					
				}
				else if(t.responseText == '50001') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">Sorry, you can\'t log consecutive comments within such a short time. Please take a deep breath and try again in a minute.</div>';					
				}
				else if(t.responseText == '50002') {
					document.getElementById('indicator').innerHTML = '<div class="commentserror">An error occured while trying to add your comment. Please try again.</div>';					
				}
				else {
					//showComment(productid);
					document.getElementById('qtempnamecollection').value = '';
					document.getElementById('qtempemailcollection').value = '';					
					document.getElementById('qcommentcollection').value = '';
					document.getElementById('indicator').innerHTML = 'Your comment has been received. Once it has been approved by our moderators it will be displayed here.';
				}
			}
			else {				
				document.getElementById('indicator').innerHTML = 'Failed to process your request.';
			}
		}
	})
}

/*
*	SHOW/HIDE LOADING MSG
*/
function toggleMsg(block, msg) {
	// the needed vars
	if(document.getElementById(block))
	var block = document.getElementById(block);

	// show loading sign
	if(msg) block.innerHTML = msg;
	// unset the loading msg
	else block.innerHTML = '';
}

/*
*	AJAX LOADER (loads all ajax functions)
*/
function ajax(div, func, cond, toggle) {
	// check if any empty parameter
	if(div != '' && func != '') {
		
		params = 'action=' + func + '&id=' + cond;

		actionURL = directURL + 'index.php';		

		toggleMsg(div, toggle);

		// do the ajax thing
		new Ajax.Updater(div, actionURL, {
			asynchronous:true,
			method:'post',
			parameters: params,
			onSuccess: function(t) {
				/* if there's response, insert into display block */
				if(t.responseText != '') {
				if(div) div.innerHTML = t.responseText;
				}
				else div.innerHTML = 'Failed to process your request at this moment.';
			}
		})
	}
	return false;
}
