$.ajaxSetup({cache: false});

// AJAX load content
function loadContent1(id) {
	$('#subCat2').html("").addClass("subCat3On").load(id,'',function(){ $(this).removeClass("subCat3On")});
}

$(function() {
	$(".bg0").fadeTo('slow', 0.5);
	$('.TTip1').tipsy({fade: true, gravity: 's'});
	$('.TTip3').tipsy({fade: true, gravity: 'w'});
});

function ShowHideTip(obj, SH, ofst){
	if (ofst == null) ofst = 25;
	if (SH == "S"){
		$(obj).attr('ttl',$(obj).attr('alt'));
		$(obj).tipsy({trigger: 'focus', fade: true, html: true, gravity: 'w', title: 'ttl', offset: ofst});
		$(obj).tipsy("show");
	} else {
		try {
			$(obj).attr('ttl', '');
			$(obj).tipsy("hide");
		} catch(err){}
	}
}

function ajaxOperation(vars,op){
	$.ajax({
		url : "cp_ajax_operations.php?OP="+op,
		type: "POST",
		dataType: "json",
		async: false,
		data: (vars),
		success:function(result){
			switch (op){
				case 1:
				case 2:
					if(result.rslt == "success"){
						$('#CPCatDiv').prev().remove();
						if ($(".CatItem").length > 0){
							$('#CPCatDiv').insertAfter($(".CatItem:first"));
						} else {
							$('#CPCatDiv').remove();
							$('#CatHead').after("<div style='line-height:20px; padding:0 0 0 5px;'>There is no unapproved records found.</div>");
						}
					} else {
						alert("Unexpected error occured while approving category.");
					}
				break
			}
		}
	});
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
	}
	
//product_page.php

	function CmntValidate(){
		if (document.FCmnt.CName.value==""){
			alert("Please enter your name.")
			document.FCmnt.CName.focus()
			return false
		}
		if (document.FCmnt.CEmail.value != '' && echeck(document.FCmnt.CEmail.value)==false){
			document.FCmnt.CEmail.focus()
			return false
		}
		if (document.FCmnt.COpinion.value==""){
			alert("Please write your comment regarding <?php echo($PTitle); ?>.")
			document.FCmnt.COpinion.focus()
			return false
		}
		if (document.FCmnt.captcha.value==""){
			alert("Please enter image verification to prove that your are not a robot.")
			document.FCmnt.captcha.focus()
			return false
		}
		Load(1); 
	}
	
	function FAQValidate(){
		if (document.FFAQ.QSubmit.alt == "1")
		{
			if (document.FFAQ.QName.value==""){
				alert("Please enter your name.")
				document.FFAQ.QName.focus()
				return false
			}
			if (document.FFAQ.QEmail.value != '' && echeck(document.FFAQ.QEmail.value)==false){
				document.FFAQ.QEmail.focus()
				return false
			}
			if (document.FFAQ.FQ.value==""){
				alert("Please write the question that you want to ask.")
				document.FFAQ.FQ.focus()
				return false
			}
			if (document.FFAQ.Qcaptcha.value==""){
				alert("Please enter image verification to prove that your are not a robot.")
				document.FFAQ.Qcaptcha.focus()
				return false
			}
		}
		else
		{
			if (document.FFAQ.AName.value==""){
				alert("Please enter your name.")
				document.FFAQ.AName.focus()
				return false
			}
			if (document.FFAQ.AEmail.value != '' && echeck(document.FFAQ.AEmail.value)==false){
				document.FFAQ.AEmail.focus()
				return false
			}
			if (document.FFAQ.FA.value==""){
				alert("Please write the question that you want to ask.")
				document.FFAQ.FA.focus()
				return false
			}
			if (document.FFAQ.Acaptcha.value==""){
				alert("Please enter image verification to prove that your are not a robot.")
				document.FFAQ.Acaptcha.focus()
				return false
			}
		}
		Load(1); 
	}
	
	function AddAnswer(QID)
	{
		var AA = document.getElementsByTagName('td');
		for (var I = 0; I < AA.length; I++)
		{
			if (AA[I].id.substring(0,6) ==  "AddAns")
			{
				AA[I].innerHTML="<a class='FAQA1' href='javascript: AddAnswer("+AA[I].id.replace("AddAns","")+");'>Add your answer &gt;&gt;</a>";
			}
		}
		document.getElementById('AddAns'+QID).innerHTML = "<table width='100%' bgcolor='#006000' cellpadding='5' cellspacing='1' border='0'><tr><td class='FAQA2' colspan='2'>Submit your answer</td></tr><tr><td class='FAQBG1' width='220'><strong>Your name:</strong></td><td class='FAQBG2'><input type='text' name='AName' id='AName' maxlength='50' style='width:300px;' />(required)</td></tr><tr><td class='FAQBG1'><strong>Your Email:</strong></td><td class='FAQBG2'><input type='text' name='AEmail' id='AEmail' style='width:300px;' /> (optional)</td></tr><tr><td class='FAQBG1' valign='top'><strong>Your answer:</strong><br />(required)</td><td class='FAQBG2'><textarea class='textbox' name='FA' id='FA' rows='6' style='width:450px;'></textarea><br /><br /></td></tr><tr><td class='FAQBG1' colspan='2'><strong>Image Verfication:</strong></td><tr><td class='FAQBG2' colspan='2'><div style='height:50px; padding:5px 0 0 0;'><div style='float:left; width:180px;'><img id='Acaptchaimg' src='"+ document.getElementById('Qcaptchaimg').src +"'></div><div style='float:left; font-size: 10px;'>Write the code you see on the left image: <input id='Acaptcha' name='Acaptcha' type='text' size='10' /><br /><br />Cannot read the image? <a href='javascript: FAQCaptcha();'>Click Here</a> to generate a new one.</div><div style='float:right; margin:0 0 0 10px;'> <input type='submit' name='ASubmit' id='ASubmit' value=' Submit ' style='height:45px; width:130px; font-weight:bold;' onclick=\"document.getElementById('QSubmit').alt='0'; document.getElementById('QID').value='"+ QID +"'; \" /></div></div></td></tr></table>";
		window.location='#AddAns'+QID;
	}
	
	function FAQCaptcha()
	{
		var lnk = './scripts/secureimage/securimage_show.php?sid=' + Math.random();
		document.getElementById('Qcaptchaimg').src = lnk;
		document.getElementById('Acaptchaimg').src = lnk;
	}
	
	function SelectTab(Tab)
	{
		$(".item7_3").removeClass("item7_3").addClass("item7_4");
		$(Tab).removeClass("item7_4").addClass("item7_3");
	}
	
	function vtomorrow(obj,pid){
		$("#TufoxyBtnTxt").html("Processing...");
		$(obj).fadeTo('slow', 0.5, function() {
			$.ajax({
				url : "http://www.tufoxy.com/plugins/vtomorrow.php?f=2&pid="+pid,
				type: "GET",
				dataType: "jsonp",
				success:function(result){
					if(result.rslt == "success"){
						$(obj).attr("title","Show current results of tomorrow's product on Tufoxy.");
						$("#TufoxyBtnTxt").html("Total votes for this product: <strong>" + result.VtNo + "</strong>");
						$(obj).fadeTo('slow', 1);
					} else {
					   window.location = "http://www.tufoxy.com/results_tomorrow.php";
					}
				}
			});
		});
	}
	
//Function to convert hex format to a rgb color
var hexDigits = new Array
        ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"); 

function rgb2hex(rgb) {
 if (rgb.indexOf("#") != -1) return rgb;
 rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
}

function hex(x) {
  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
 }

