var agt=navigator.userAgent.toLowerCase();
var appVer = navigator.appVersion.toLowerCase();
var is_minor = parseFloat(appVer);
var is_major = parseInt(is_minor);
var iePos = appVer.indexOf('msie');
if (iePos !=-1) {
is_minor = parseFloat(appVer.substring(iePos+5,appVer.indexOf(';',iePos)))
is_major = parseInt(is_minor);
}
var is_getElementById = (document.getElementById) ? "true" : "false";
var is_getElementsByTagName = (document.getElementsByTagName) ? "true" : "false";
var is_documentElement = (document.documentElement) ? "true" : "false";
var is_ie = ((iePos!=-1));
var is_ie6 = (is_ie && is_major == 6);



var isIE=is_ie;

//change innerHTML of element
function changeInnerHTML(el, text)
{
	document.getElementById(el).innerHTML=text;
}
//change innerHTML of object
function changeObjInnerHTML(obj, text)
{
	obj.innerHTML=text;
}
//show element by id - change actual style.display property to opposed value.
function showHideElement(id){

	if(!(getStyle(document.getElementById(id),"display")=='none')){
		document.getElementById(id).style.display='none';
		
	}
	else{
		document.getElementById(id).style.display='inline';
	}
}
//show object - change actual style.display property to opposed value.
function showObj(obj){
	if(!(getStyle(obj,"display")=='none')){
		obj.style.display='none';
	}
	else{
		obj.style.display='inline';
	}
}
//show object as display:block - change actual style.display property to opposed value.
function showBlockObj(obj){
	if(!(getStyle(obj,"display")=='none')){
		obj.style.display='none';
	}
	else{
		obj.style.display='block';
	}
}
//show element by id  as display:block - change actual style.display property to opposed value.
function showBlockElement(id){
	if(!(getStyle(document.getElementById(id),"display")=='none')){
		document.getElementById(id).style.display='none';
	}
	else{
		document.getElementById(id).style.display='block';
	}
}

//returns the rendered style of an element (as opposed to specified style)
function getStyle(oElm, strCssRule){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
//		alert('get style= '+strValue + "    style= "+oElm.style.display)
	}
	else if(oElm.currentStyle){
		strCssRule = strCssRule.replace(/-(w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
//		alert('get style= '+strValue + "    style= "+oElm.style.display)
	}
	return strValue;
}
//add function to window.onload
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}

//add function loadRightMenuFlash (for example) to window.onload
/*addLoadEvent(loadRightMenuFlash);*/

//returns an array of elements that match full or partial (indexOf) classname
function ElementsByClass(rootElement,ElementClass,ElementType,partialMatch){        
	ElementType=ElementType?ElementType:'*';
	var elementsArray=new Array();			
	var theElements=rootElement.getElementsByTagName(ElementType);                  

	for (var x=0;x<theElements.length;x++){
		if(partialMatch){
		   if (theElements[x].className.indexOf(ElementClass)>-1){ 
				elementsArray[elementsArray.length]=theElements[x];
			}
		}else{
			if (theElements[x].className==ElementClass){ 
			  elementsArray[elementsArray.length]=theElements[x];
			}
		}
	}                          

	return elementsArray;
} 

function hideSelectBox(){
    //hide selects to fix annoying select bug in ie
	if(isIE){
		var allSelects=document.getElementsByTagName("select");
		for(var x=0;x<allSelects.length;x++){		
			//if not in a modal box
			if(allSelects[x].className.indexOf('modal')==-1){
			    allSelects[x].style.visibility="hidden";	
			    }
		}
	}
}

function showSelectBox(){
    //show selects to fix annoying select bug in ie
	if(isIE){	
		var allSelects=document.getElementsByTagName("select");

		for(var x=0;x<allSelects.length;x++){
			allSelects[x].style.visibility="visible";	
		}
	}
}

//change element class to new one
function changeClass2El(elId,newClass){
	var el = document.getElementById(elId);
	el.className=newClass;
}
//change object class to new one
function changeClass2Obj(obj,newClass){
	obj.className=newClass;
}

//change object class on mouseOver / mouseOut event. Classes names should end with _mouseOver or _mouseOut
function mouseOverToggle(obj,eventType){

	var isPNG=false;

	if (obj.className.indexOf("pngFix")!=-1 && is_ie6){
			obj.style.backgroundImage="";		
			obj.style.filter="";
			obj.className=obj.className.replace(/pngFix/g,'')
			isPNG=true;
	}
	if (eventType.type=="mouseover"){		
		obj.className=obj.className.replace(/mouseOut/g,'mouseOver')
	}
	if (eventType.type=="mouseout"){
		obj.className=obj.className.replace(/mouseOver/g,'mouseOut')
	}		
	
	if (isPNG){
		obj.className=obj.className+" pngFix";	
	}
}

// cut object value to max length
function MaxLength(obj, length) {
    if (obj.value.length > length) {
        obj.value = obj.value.substr(0, length);
    }
}

var lastID =''
function showHideSearchItem(elID){
	var titleObj=document.getElementById(elID +'_title');
	var detObj=document.getElementById(elID+'_det');
	
	if(titleObj.className=="title_on"){
		titleObj.className="title_off";
		$("#"+elID+'_det').hide('blind');
		lastID = '';		
	} else {
		titleObj.className="title_on";
		$("#"+elID+'_det').show('blind');
		if(lastID) {
			document.getElementById(lastID +'_title').className="title_off"
			$("#"+lastID+'_det').hide('blind');
		}
		lastID = elID;
	}
}
function showHideAllSearchItems(obj, cont_id){	
	var searchItemsArray=new Array();			
	var searchTitlesArray=new Array();
	
	searchItemsArray =ElementsByClass(document.getElementById(cont_id),'det','div',false);                  
	searchTitlesArray =ElementsByClass(document.getElementById(cont_id),'title','div',true); 
	if(obj.className=='title_off'){
		for (var x=0;x<searchItemsArray.length;x++){
			searchItemsArray[x].style.display='inline';
		}
		for (var x=0;x<searchTitlesArray.length;x++){
			searchTitlesArray[x].className='title_on';
		}		
		obj.innerHTML='סגור כל הקטגוריות';
	}
	else{
		for (var x=0;x<searchItemsArray.length;x++){
			searchItemsArray[x].style.display='none';
		}
		for (var x=0;x<searchTitlesArray.length;x++){
			searchTitlesArray[x].className='title_off';
		}	
		obj.innerHTML='פתח כל הקטגוריות';
	}
	
}

function showOnOffObj(obj){
	var isPNG=false;

	if (obj.className.indexOf("pngFix")!=-1 && is_ie6){
			obj.style.backgroundImage="";		
			obj.style.filter="";
			obj.className=obj.className.replace(/pngFix/g,'')
			isPNG=true;
	}
	if(obj.className.indexOf('off')<0)
		obj.className=obj.className.replace(/_on/g,'_off');
	else
		obj.className=obj.className.replace(/_off/g,'_on');
	if (isPNG){
		obj.className=obj.className+" pngFix";	
	}			
}		

var selectedCategories = {};
var usedParents = {};
function genCategoriesList(obj,id,parent_id){
	
	if (!usedParents[parent_id]) { usedParents[parent_id] = 0;}
	
	showOnOffObj(obj);
	if (!selectedCategories[id]){
		document.getElementById("label_"+id).style.fontWeight="bold";
		selectedCategories[id] = true;
		 usedParents[parent_id]++;
	} else {
		document.getElementById("label_"+id).style.fontWeight="";
		delete selectedCategories[id];
		usedParents[parent_id]--;
	}
	
	if (usedParents[parent_id] == 0) {
		document.getElementById("item_"+parent_id+"_title").style.fontWeight="";
	} else {
		document.getElementById("item_"+parent_id+"_title").style.fontWeight="bold";
	}
	
	var vals = '';
	$.each(selectedCategories, function(value) {
      vals += value + ";";
    });
	
	document.getElementById('categories').value = vals;
}

function policy_agree(){
	//alert('hi');
	//obj = document.GetElementById('checkbox_policy');
	//showOnOffObj(obj);
	//if ($("#policy_agree").val() != "1"){ $("#policy_agree").val("1"); } else { $("#policy_agree").val("0"); }
}

var wizard_errors;
$(document).ready(function(){
	
	/*Wizard form*/
	if (wizard_errors){
		if (wizard_errors.ids){
			$.each(wizard_errors.ids, function() {
				$("#wizard_" + this).addClass('inpError').removeClass('inpText');
    		});
		}
    	$("#bubble_title").html(wizard_errors.title);
    	$("#bubble_text").html(wizard_errors.msg);
    	$("#bubble_teaser").html(wizard_errors.teaser);
	}
	
	/*step 2 wizard*/
	$("#checkbox_policy").click(function(){
		obj = document.getElementById('checkbox_policy');
		showOnOffObj(obj);
		if ($("#policy_agree").val() != "1"){ $("#policy_agree").val("1"); } else { $("#policy_agree").val("0"); }
	});
	
	
	/*Member signup Page*/
	$("#signup_submit").click(function(){
		$.post("/member/ajax_signup", {
			name : $("#wizard_name").val(),
			email : $("#wizard_email").val(),
			phone : $("#wizard_phone").val(),
			password : $("#wizard_password").val()
		}, function(json){
			notify(json.type,json.msg,json.ms);
			if (json.type == "success") {
				signup_clean_inputs();
				setTimeout('window.location.replace("http://www.digger.co.il/member")',6000)
			} else {
				signup_clean_errors();
				$.each(json.ids, function() {
					$("#wizard_"+this).addClass('inpError').removeClass('inpText');	
				});
			}
		},"json");		
	});
	
	/*Contact Page*/
	$("#contact_submit").click(function(){
		$.post("/contact/ajax_contact", {
			name : $("#contact_name").val(),
			email : $("#contact_email").val(),
			subject : $("#contact_subject").val(),
			message : $("#contact_message").val()
		}, function(json){
			notify(json.type,json.msg,json.ms);
			if (json.type == "success") {
				$("#contact_clear").trigger("click");
			} else {
				contact_clean_errors();
				$.each(json.ids, function() {
					$("#contact_"+this).addClass('inpError').removeClass('inpText');	
				});
			}
		},"json");		
	});
	
	/*Recommend Page*/
	$("#recommend_submit").click(function(){
		$.post("/recommend/ajax_recommend", {
			name : $("#recommend_name").val(),
			email : $("#recommend_email").val(),
			name1 : $("#recommend_name1").val(),
			email1 : $("#recommend_email1").val()
		}, function(json){
				notify(json.type,json.msg,json.ms);
			if (json.type == "success") {
				recommend_clean_inputs();
			} else {
				recommend_clean_errors();
				$.each(json.ids, function() {
					$("#recommend_"+this).addClass('inpError').removeClass('inpText');	
				});
			}
		},"json");		
	});
	
	/*forgot password Page*/
	$("#forgot_submit").click(function(){
		$.post("/member/ajax_forgot", {
			email : $("#forgot_email").val()
		}, function(json){
				notify(json.type,json.msg,json.ms);
			if (json.type == "success") {
				setTimeout('window.location.replace("http://www.digger.co.il/member")',6000)
			}
		},"json");		
	});
	
	function signup_clean_errors() {
		$("#wizard_name").addClass('inpText').removeClass('inpError');
		$("#wizard_email").addClass('inpText').removeClass('inpError');
		$("#wizard_phone").addClass('inpText').removeClass('inpError');
		$("#wizard_password").addClass('inpText').removeClass('inpError');
	};
	
	function signup_clean_inputs(){
		signup_clean_errors();
		$("#wizard_name").val('');
		$("#wizard_email").val('');
		$("#wizard_phone").val('');
		$("#wizard_password").val('');
	};
	
	function contact_clean_errors() {
		$("#contact_name").addClass('inpText').removeClass('inpError');
		$("#contact_email").addClass('inpText').removeClass('inpError');
		$("#contact_subject").addClass('inpText').removeClass('inpError');
		$("#contact_message").removeClass('inpError');
	};
	
	$("#contact_clear").click(function(){
		contact_clean_errors();
		$("#contact_name").val('');
		$("#contact_email").val('');
		$("#contact_subject").val('');
		$("#contact_message").val('');
	});
	
	function recommend_clean_errors() {
		$("#recommend_name").addClass('inpText').removeClass('inpError');
		$("#recommend_email").addClass('inpText').removeClass('inpError');
		$("#recommend_name1").addClass('inpText').removeClass('inpError');
		$("#recommend_email1").addClass('inpText').removeClass('inpError');
	};
	
	function recommend_clean_inputs(){
		recommend_clean_errors();
		$("#recommend_name1").val('');
		$("#recommend_email1").val('');
	};
	
	/*Panel*/
	/*Profile page*/
	$("#profile_submit").click(function(){
		$.post("/panel/ajax_profile", {
			name : $("#name").val(),
			old_pass : $("#old_pass").val(),
			new_pass1: $("#new_pass1").val(),
			new_pass2: $("#new_pass2").val(),
			country: $("#country").val(),
			city: $("#city").val(),
			address: $("#address").val(),
			zipcode: $("#zipcode").val(),
			phone: $("#phone").val(),
			fax: $("#fax").val()
		}, function(json){
				notify(json.type,json.msg,json.ms);
		},"json");		
	});
	
	/*Packages page*/
	$("#order1").click(function(){
		$("#paypal_item_name").val("Digger.co.il - Basic Package (1 Year)");
		$("#paypal_item_number").val("2");
		$("#paypal_amount").val("1920");
		$("#paypal_form").submit();
	});
	
	$("#order2").click(function(){
		$("#paypal_item_name").val("Digger.co.il - Gold Package (1 Year)");
		$("#paypal_item_number").val("3");
		$("#paypal_amount").val("3360");
		$("#paypal_form").submit();
	});
	
	$("#order3").click(function(){
		$("#paypal_item_name").val("Digger.co.il - Premium Package (1 Year)");
		$("#paypal_item_number").val("4");
		$("#paypal_amount").val("7680");
		$("#paypal_form").submit();
	});
});
