$(document).ready(function(){
	$('div.png').ifixpng();
	$("#menu a").mouseover(function(){
		var img = $(this).children("img");
		img.attr("src", img.attr("src").replace(/inactive/, "active"));
		return false;
	});
	$("#menu a").not(".active").mouseout(function(){
		var img = $(this).children("img");
		img.attr("src", img.attr("src").replace(/active/, "inactive"));
		return false;
	});
});
function getSearchResult(){
	var data = "";
	$(":input").each(function(){
		data += $(this).attr("name")+"="+$(this).val()+"&";
	});
//	data += "url="+self.location.href;
	$.ajax({
		type:"GET",
		url:"ajax/action/recipesearch.php",
		data:data,
		success:function(xml){
			if(xml!=0){
				self.location.href=xml;	
				//alert(xml);
			}	
		}
	});	
}
function resetform(formId){
	$("#"+formId+" :input:not(#formURL):not(hidden)").each(function(){
		$(this).val('');
	});
	$("div.selectbox").html('');
	$("ul.selectbox li").removeClass("selected");
	$("#"+formId+" div.left").html('');
}
function submitform(formId){
	$("#"+formId+" #status").css("font-size", "11px").css("color", "green").html('<img src="images/loading.gif" />&nbsp;Please wait...');
	if(validate(formId)){
		var data = "";
		$("#"+formId+" :input").each(function(){
			if($(this).attr("type")=="checkbox" || $(this).attr("type")=="radio"){
				if($(this).attr("checked")){
					data += $(this).attr("name")+"="+$(this).val()+"&";
				}
			}else{
				data += $(this).attr("name")+"="+$(this).val()+"&";
			}
		});
		//console.log(data);
		//return;
		$.ajax({
			type:"POST",
			url:"ajax/action/checkcaptcha.php",
			data:data,
			success:function(xml){
				if(xml==0){
					$("#"+formId+" #captcha").parent().children("div.left").html('<img src="images/pass.gif" />');
					$.ajax({
						type:"POST",
						url:$("#"+formId+" #formURL").val(),
						data:data,
						dataType:'xml',
						success:function(xml){
							if($(xml).find('validate').text() == "SUCCESS"){
								$("#"+formId+">div").css('width', '100%').css("color",'#809754').css("margin-top", '30px').css("font-size", '14px').html(($(xml).find('msg').text()+'<p>&nbsp;</p><div style="text-align:center;margin-top:30px;"><img src="images/logo.gif" width="135" height="167" /></div>'));
								setTimeout("tb_remove()", 4000);
							}else{
								$("#"+formId+" #status").css("color", "red").html($(xml).find('msg').text());
							}
							/*if(result==0){
								if(formId=="productfeedbackform"){
									$("#"+formId+">div").html('<p style="color:#809754;margin-top:30px;font-size:14px;">Thank you very much for your feedback. All information will be kept confidential and be used to further develop redisland products.</p>');
								}else{
									$("#"+formId+">div").html('<p style="color:#809754;margin-top:30px;font-size:12px;">Your request has been completed successfully. Thanks for submitting the form.</p>');
									setTimeout("tb_remove()", 2000);
								}
							}else{
							$("#"+formId+" #status").css("color", "red").html("We are unable to process your request right now. Please try later.");
							}*/
						}
					});
				}else{
					$("#"+formId+" #captcha").parent().children("div.left").html('<img src="images/error.gif" />');					
					$("#"+formId+" #status").html('&nbsp;');
				}
			}
		});
	}else{
		$("#"+formId+" #status").html('&nbsp;');
	}
}
function validate(formId){
	var re = true;
	$("#"+formId+" :input[type!=radio]:not(#captcha)").each(function(){
		if($(this).parent().children("label").children("span.mustfill").html() != null){
			if($(this).val()=="" || $(this).val()==null){
				$(this).parent().children("div.left").html('<img src="images/error.gif" />');
				re = false;
			}else if($(this).hasClass('email') && !isValidEmail($(this).val())){
				$(this).parent().children("div.left").html('<img src="images/error.gif" />');
				re = false;
			}else if($(this).hasClass('file')){
				if(self.frames['uploadframe'].document.getElementById('result').innerHTML != "0"){
					$(this).parent().children("div.left").html('<img src="images/error.gif" />');
					$(this).parent().parent().next().children('div').children('span').css("color", "red");
					re = false;
				}else{
					$(this).parent().children("div.left").html('<img src="images/pass.gif" />');
				}
			}else{
				$(this).parent().children("div.left").html('<img src="images/pass.gif" />');
			}
		}
	});
	if($("#"+formId+" :radio").size()>0){//let's see if there is radio around
		var pass = new Array();
		var fail = new Array();
		$("#"+formId+" :radio").each(function(){
			if($(this).attr("checked")){
				pass.push($(this).attr("name"));
			}else{
				fail.push($(this).attr("name"));
			}
		});
		array_unique(fail);
		for(var i=0; i < fail.length; i++){
			var name = fail[i];
			if(!in_array(name, pass)){
				$(":radio[name="+name+"]:first").parent().children("div.left").html('<img src="images/error.gif" />');
				re = false;
			}else{
				$(":radio[name="+name+"]:first").parent().children("div.left").html('<img src="images/pass.gif" />');
			}
		}
	}
	return re;
}
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
function NewWindow(mypage, myname, w, h, scroll, resizable, status) 
{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable='+resizable+',status='+status+'';
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
//print this page
function printPage()
{
//	var txtQueryString = window.location.search.substring(1);
	var url = "printpage.php?";
	var txtQueryString = window.location.search.substring(1);
	var txtPrint = (txtQueryString=="")?'?print=1':'&print=1';
	NewWindow(url + txtQueryString +txtPrint, 'PrintPage', 680, 500, 1, 1, 0);
}
function attachFile(ele){
	for(var i=0;i<3;i++){
		ele.next().remove();
	}
	ele.after($("#upload").html());
}
function fillFields(ele){
	for(var i=0;i<3;i++){
		ele.next().remove();
	}
	ele.after($("#field").html());
}
function uploadFile(){
	var file = $('#filefield').val();
	var ext = file.substr(file.lastIndexOf("."), file.length);
	ext = ext.toLowerCase();
	if(ext == '.pdf' || ext == '.doc'){
		$("#file").submit();
	}else{
		alert("Only .pdf or .doc files accepted");
		$("#filefield").val("");
	}
}
function in_array(needle, haystack, strict) {
    // Checks if a value exists in an array
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_in_array/
    // +       version: 801.3120
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true

    var found = false, key, strict = !!strict;

    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }

    return found;
}
function array_unique( array ) {
    // Removes duplicate values from an array
    // 
    // +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_array_unique/
    // +       version: 801.1011
    // +   original by: Carlos R. L. Rodrigues
    // *     example 1: array_unique(['Kevin','Kevin','van','Zonneveld']);
    // *     returns 1: true

    var p, i, j;
    for(i = array.length; i;){
        for(p = --i; p > 0;){
            if(array[i] === array[--p]){
                for(j = p; --p && array[i] === array[p];);
                i -= array.splice(p + 1, j - p).length;
            }
        }
    }

    return true;
}