function getPhoto(prdID, photoID, isFeed) {
	ajaxresp = new Ajax.Request(root + 'public_panel/ajax/photo_gal.php?prdid='+prdID+'&photoid='+photoID+'&isfeed='+isFeed, {
		method: 'get',
		onSuccess: function(resp) {
			$('main_photo_holder').innerHTML = resp.responseText;
		}
	});
}

function nextPhoto() {
    prdid = document.photo_gal.prdid.value;
	cImage = document.photo_gal.cImage.value;
	vImages = document.photo_gal.upImages.value.split(',');



	if (cImage >= vImages.length - 1) {
		cImage = -1;
	}

	getPhoto(prdid, vImages[++cImage]);
	document.photo_gal.cImage.value = cImage;
}

function prevPhoto() {
    prdid = document.photo_gal.prdid.value;
	cImage = document.photo_gal.cImage.value;
    vImages = document.photo_gal.upImages.value.split(',');

	getPhoto(prdid, vImages[--cImage]);
	if (cImage <= 0) {
		cImage = vImages.length;
	}
	document.photo_gal.cImage.value = cImage;
}

function fbs_click() {
 u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;
}

function msp_click() {
u=location.href;t=document.title;window.open('http://www.myspace.com/Modules/PostTo/Pages/?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;
}

function showMessageTab() {
    HideMsgTabs();

    $('send_msg_tab_link').addClassName('active');
    $('send_msg_tab').show();
}

function showWatchlistMsgTab(id) {
    HideMsgTabs();

     addToWatchlist(id);

     $('watchlist_msg_tab_link').addClassName('active');

    $('watchlist_msg_tab').show();

    showWatchlistTab();
}

function showTellFriendTab() {
    HideMsgTabs();
    $('tell_friend_tab_link').addClassName('active');
    $('tell_friend_tab').show();
}

function HideMsgTabs() {
	$('send_msg_tab_link').removeClassName('active');
	$('watchlist_msg_tab_link').removeClassName('active');
	 $('tell_friend_tab_link').removeClassName('active');

    $('send_msg_tab').hide();
    $('watchlist_msg_tab').hide();
    $('tell_friend_tab').hide();
}

function sendMessage(root) {
    f = document.send_msg;

   var postBody = 'name='+f.name.value;
    postBody += '&email='+f.email.value;
    postBody += '&validate='+f.validate.value;
    postBody += '&prdid='+f.prdid.value;
    postBody += '&msg='+f.msg.value;
    postBody += '&offer='+f.offer.value;



    ajaxresp = new Ajax.Request(root + 'public_panel/ajax/product/send_msg.php', {
		method: 'post',
		postBody: postBody,
		onSuccess: function(resp) {
			respXML = resp.responseXML.documentElement;
			var status = respXML.getElementsByTagName("status")[0].childNodes[0].nodeValue;

			var rand_no = Math.floor(11*Math.random());

			if (status == "SENT") {
				$('send_msg_tab').innerHTML = '<div class="success_msg" style="height: 90px; padding-top: 68px; text-align: center;"><span>Message Sent</span></div>';
				$('send_msg_tab').className += ' message-box_large_msg';
			}
			else if (status == "WRONG_VAL") {
				$('form_error_msg').innerHTML = '<span class="error_msg">Wrong validation code</span>';
				$('form_error_msg').style.height = "14px";
				$('msg_captcha1').src =  root+'/public_panel/includes/captcha.php?id=1&a='+rand_no;
			}
			else if (status == "FORM_INVALID") {
				$('form_error_msg').innerHTML = '<span class="error_msg">Form incorrectly filled</span>';
				$('form_error_msg').style.height = "14px";
			}
			else {
				$('form_error_msg').innerHTML = '<span class="error_msg">Your message could not be sent</span>';
				$('form_error_msg').style.height = "14px";
			}

		}
	});
}
function checkWatchlisted(prdid) {
	postBody = 'action=isWatchlisted';
	postBody += '&prdid='+prdid;


    ajaxresp = new Ajax.Request(root + 'public_panel/ajax/product/watchlist.php', {
		method: 'post',
		postBody: postBody,
		onSuccess: function(resp) {

			respXML = resp.responseXML.documentElement;
			var status = respXML.getElementsByTagName("status")[0].childNodes[0].nodeValue;

			if (status == "TRUE") {
				$('watchlist_msg_tab').innerHTML = '<div class="success_msg" style="height: 90px; padding-top: 62px; text-align: center;"><span>Item already watched</span></div>';
				$('watchlist_msg_tab').className += ' message-box_large_msg';
			}
		}
	});
}

function addToWatchlist(prdid) {
    postBody = 'action=addToWatchlist';
    postBody += '&prdid='+prdid;

    ajaxresp = new Ajax.Request(root + 'public_panel/ajax/product/watchlist.php', {
		method: 'post',
		postBody: postBody,
		onSuccess: function(resp) {


			respXML = resp.responseXML.documentElement;
			var status = respXML.getElementsByTagName("status")[0].childNodes[0].nodeValue;

			var status = respXML.getElementsByTagName("status")[0].childNodes[0].nodeValue;

			if (status == "SAVED") {


				$('watchlist_msg_tab').innerHTML = '<div class="success_msg" style="height: 90px; padding-top: 62px; text-align: center;"><span>Added to Watchlist</span></div>';

				$('watchlist_msg_tab').className += ' message-box_large_msg';



				setTimeout("getWatchlistData()",1000); // refresh the mini db
				setTimeout("getWatchlistData()",5000); // refresh the mini db
			}
		}
	});
}

function tellAFriend(prdid, root) {

    f = document.tell_friend_msg;
	postBody = 'action=tellFriend';
	postBody += '&prdid='+prdid;
    postBody += '&name='+f.name.value;
	postBody += '&email='+f.email.value;
	postBody += '&friends_name='+f.friends_name.value;
	postBody += '&friends_email='+f.friends_email.value;
	postBody += '&msg='+f.msg.value;
	postBody += '&validate='+f.validate.value;


    ajaxresp = new Ajax.Request(root + 'public_panel/ajax/product/tell_friend.php', {
		method: 'post',
		postBody: postBody,
		onSuccess: function(resp) {

			respXML = resp.responseXML.documentElement;
			var status = respXML.getElementsByTagName("status")[0].childNodes[0].nodeValue;



			var rand_no = Math.floor(11 * (Math.random() + 5));

			if (status == "SENT") {
				$('tell_friend_tab').innerHTML = '<div class="success_msg" style="height: 90px; padding-top: 65px; text-align: center;"><span>Message Sent to Friend</span></div>';
				$('tell_friend_tab').className += ' message-box_large_msg';
			}
			else if (status == "WRONG_VAL") {
				$('friend_error_msg').innerHTML = '<span class="error_msg">Wrong Validation</span>';
				$('friend_error_msg').style.height = "14px";


				var url = root + 'public_panel/includes/captcha.php?id=2&a=' + rand_no.toString();

				$('msg_captcha2').src = url;
			}
			else {
				$('friend_error_msg').innerHTML = '<span class="error_msg">Form Invalid</span>';
				$('friend_error_msg').style.height = "14px";
			}
		}
	});
}

