function checkAllCheckbox(self){
	var check = true;
	//alert(self.attr('id'));
	if(/email/i.test(self.attr('id')) || /im/i.test(self.attr('id')) || /skill/i.test(self.attr('id')) || /certification/i.test(self.attr('id')))
	{
		if(/_all_/i.test(self.parent().parent().parent().parent().prev().find("input:checkbox").attr("id"))){
			self.parent().parent().parent().parent().find("input:checkbox").each(function(){
				if(check == false) {
					return;
				}
				if($(this).attr("checked") === false) {
					check = false;
				}
			});
			if(check === true) {
				self.parent().parent().parent().parent().prev().find("input:checkbox").attr("checked","true");
			}
			else {
				self.parent().parent().parent().parent().prev().find("input:checkbox").attr("checked","");
			}
		}
	}else{
		if(/_all_/i.test(self.parent().parent().parent().prev().find("input:checkbox").attr("id"))){
			self.parent().parent().parent().find("input:checkbox").each(function(){
				if(check == false) {
					return;
				}
				if($(this).attr("checked") === false) {
					check = false;
				}
			});
			if(check === true) {
				self.parent().parent().parent().prev().find("input:checkbox").attr("checked","true");
			}
			else {
				self.parent().parent().parent().prev().find("input:checkbox").attr("checked","");
			}
		}
	}
}

//Modified this function,add a new parameter "privacy", if privacy=1 then display the pop-up message.
function editLoadRepSheet(repsheetId, key, map, privacy) {

	// Get attribute map
	$.postJSON("/repsheet/attribute.php", { cmd: "load", id: repsheetId, key: key }, 
		function(json) {
			if (json && json.error){
				$(".repsheet_mainbox:first").find(".error").each(function() {
					$(this).remove();
				});
				$(".repsheet_mainbox:first").prepend('<span class="error">' + json.error + '</span>');
			}
			
			// Click event
			$(":checkbox")
				.each(function() {
					if(/_all_/i.test($this.attr("id")) && $.browser.msie) 
					{
						$(this).unbind();
					}
				})
				.click(function() {
					$this = $(this);
					if(/_all_/i.test($this.attr("id"))) 
					{	
						if($.browser.msie){
							if($(this).attr("checked")){
								$(this).parent().parent().parent().next().find("input:checkbox").each(function(){
									if(!$(this).attr("checked")) 
									{
										$(this).attr("checked","true");
										if($(this).attr("key") == '1'){
											reset_checks_with_key_value($(this).attr("id"));
										}
									}
								});
							}else{
								$(this).parent().parent().parent().next().find("input:checkbox").each(function(){
									if($(this).attr("checked"))
									{
										$(this).attr("checked","");
										if($(this).attr("key") == '1'){
											reset_checks_with_key_value($(this).attr("id"));
										}
									}
								});
							}
							window.saveKeyGmap($(this).attr("id"));
						}
						return true;
					}
					var cat = $this.attr("cat");
					if (!cat) {
						throw("ASSERT: category undefined for element '" + $this.attr("id") + "'");
					}
					keyid = $this.attr("id").split("-");
					if (!map[cat]) {
						map[cat] = {};
					}
					if (!map[cat][keyid[1]]) {
						map[cat][keyid[1]] = {};
					}
					delete map[cat][keyid[1]][$this.attr("id")];
					if ($this.attr("checked")) {
						map[cat][keyid[1]][$this.attr("id")] = { orderby: $this.attr("sort") };
					}
					if($(this).attr("key") == '1'){
							reset_checks_with_key_value($(this).attr("id"));
					}
					checkAllCheckbox($this);
				});				
				bindXfn();
				for (cat in json) {
					if(cat) {
						map[cat] = {};
						for (i in json[cat]) {
							if(i) {
								map[cat][i] = {};
								ids = json[cat][i].split(";");
								for (id in ids) {
									if(id) {
										if (!ids[id].length) {
											continue;
										}
										var c = $("#" + ids[id]);
										c.attr("checked", true);
										map[cat][i][c.attr("id")] = { orderby: c.attr("sort") };
										if(/birthday/i.test(c.attr("id"))){ 
											re_set_showYearOnly();
										}
										if(c.attr("key") == "1"){
											reset_checks_with_key_value(c.attr("id"));
										}
										checkAllCheckbox(c);
									}
								}
							}
						}
					}
					if(cat == 'mi90' || cat == 'mi180' || cat == 'mi360'){
						for(i in json[cat] ){
						jsonlist = $.toJSON(json[cat][i]);
						showlist = jsonlist.split("\"");
						showArray = showlist[1].split(";");
						for(i=0;i<showArray.length;i++){
							showArray[i] = getAreaForKey(showArray[i]);
						}
						
						$(".content_fullwidth").find('.repsheet_left_boxholder').each(function() {
							if(jQuery.inArray($(this).attr('id'),showArray) != -1) {
								$(this).find('.repsheet_left_header_holder').children("img").attr("src", "/image/id_repsheets/icons/collapse.png");
								$(this).find('.repsheet_left_body_head').show('slow');
								$(this).find('.repsheet_left_body_body').show('slow');
								$(this).find('.repsheet_left_body_footer').show('slow');							
							}else{			
								$(this).find('.repsheet_left_header_holder').children("img").attr("src", "/image/id_repsheets/icons/expand.png");
								$(this).find('.repsheet_left_body_head').hide('slow');
								$(this).find('.repsheet_left_body_body').hide('slow');
								$(this).find('.repsheet_left_body_footer').hide('slow');
							}
						});
						$(".content_fullwidth").find('.repsheet_right_boxholder').each(function() {
							if(jQuery.inArray($(this).attr('id'),showArray) != -1) {
								$(this).find('.repsheet_right_header_holder').children("img").attr("src", "/image/id_repsheets/icons/collapse.png");
								$(this).find('.repsheet_right_body_head').show('slow');
								$(this).find('.repsheet_right_body_body').show('slow');
								$(this).find('.repsheet_right_body_footer').show('slow');							
							}else{	
								$(this).find('.repsheet_right_header_holder').children("img").attr("src", "/image/id_repsheets/icons/expand.png");
								$(this).find('.repsheet_right_body_head').hide('slow');
								$(this).find('.repsheet_right_body_body').hide('slow');
								$(this).find('.repsheet_right_body_footer').hide('slow');
							}
						});
					}
				}
			}	
		}
	);
	return false;
}

function checkKeyAndCat(key,cat){
	if(cat=='user' && key=='about-basic') { return true;}
	else if(cat=='email' && key=='about-contact') { return true;}
	else if(cat=='phone' && key=='about-contact') { return true;}
	else if(cat=='address' && key=='about-contact') { return true;}
	else if(cat=='im' && key=='about-contact') { return true;}
	else if(cat=='description' && key=='about-Personal_Summary') { return true;}
	else if(cat=='school' && key=='about-schools') { return true;}
	else if(cat=='work' && key=='about-work') { return true;}
	else if(cat=='site' && key=='about-site') { return true;}
	else if(cat=='photo' && key=='about-photo') { return true;}   
	else if((cat=='mi90'||cat=='mi180'||cat=='mi360') && key=='about-show_table') { return true;}
	else if(cat=='patents' && key=='about-patents') { return true;}
	else if(cat=='skill' && key=='about-skills') { return true;}
	else if(cat=='certification' && key=='about-certifications') { return true;}
	else if(cat=='xfns' && key=='about-xfns') { return true;}
	return false;
}

function editSaveRepSheet(repsheetId, key, map) {
	// Sort the map
	var key_click = key;
	var keyMap = {};
	for (cat in map) {
		if(cat) {
			if(!checkKeyAndCat(key_click,cat)) {
				continue;
			}
			keyMap[cat] = {};
			for (i in map[cat]) {
				if(i) {
					keyMap[cat][i] = [];
					for (id in map[cat][i]) {
						if(id) {
							/**
							 * used to order the field
							 * by Jeffery
							 */
							var orderby = map[cat][i][id].orderby;
							keyMap[cat][i][orderby] = id;
						}
					}
				}
			}
		}
	}
	
	// Save
	var json = $.toJSON(keyMap);
	$.postJSON("/repsheet/attribute.php", { cmd: "save", id: repsheetId, key: key, map: json },
		function(json) {
			if (json && json.error){
				$(".repsheet_mainbox:first").find(".error").each(function() {
					$(this).remove();
				});
				$(".repsheet_mainbox:first").prepend('<span class="error">' + json.error + '</span>');
			}
		}
	);
	return false;
}

$(document).ready(function() {	
	$(".qrcode_thumb")
		.css("cursor", "pointer")
		.click(function(){
			var qrcode_thumb_element = $(this);
			var get_qrcode_div = true;
			$(".qrcode_thumb_hover").each(function(){
				$(this).slideUp(1);
			});
			$(this).siblings(".qrcode_thumb_hover").each(function(){
				if($(this).is("div")) {
					get_qrcode_div = false;
					$(this).show();
				}
			});
			if(get_qrcode_div) {
				$.get(gTLD + "/repsheet/qrcode.php",
					{url:$(this).children("img").attr("src")},
					function(data) {
						if(qrcode_thumb_element.parent().is('div')){
							qrcode_thumb_element.parent().append(data);
						}else{
							qrcode_thumb_element.parent().parent().append(data);
						}
						$(".qrcode_thumb_hover")
							.each(function(){
								$(this).bind("click", function(){
									if($(this).is(':visible')) {
										$(this).slideUp(1);
									}
								});
							});
					});
			}
		});
	$(".qrcode_thumb_hover")
		.each(function(){
			$(this).bind("click", function(){
					if($(this).is(':visible')) {
						$(this).slideUp(1);
					}
				});
		});

});


function countryLoadRepSheet(parent_id, key, id , ifclick) {	

	// Get attribute map
	$.postJSON("/repsheet/country.php", { cmd: "load", parent_id: parent_id, key: key }, 
		function(json) {
			// Initialize map
			$(".edit-select").each(function(){
				if(/address_country/i.test($(this).attr("id")) && parent_id == 1){
					$(this).attr("data",$.toJSON(json));
					country_types = $.toJSON(json);
					readySelects();
				}
				else if($(this).attr("id")== id && parent_id > 1){
					//alert($.toJSON(json));
					$(this).attr("data",$.toJSON(json));
					readySelects();
					if(ifclick == 1){
						$(this).click();
					}
				}
			});
		}
	);
	return false;
}


function languageLoadRepSheet() {	

	// Get attribute map
	$.postJSON("/repsheet/language.php", { cmd: "load" }, 
		function(json) {
			// Initialize map
			$(".edit-select").each(function(){
				if(/language/i.test($(this).attr("id"))){
					$(this).attr("data",$.toJSON(json));
					language_types = $.toJSON(json);
					readySelects();
				}
			});
		}
	);
	return false;
}


//load much date,find the key	
function getKeyforcat(ob){	
	var thekey;
	if(ob.attr("cat")=='user') { thekey = keyList[0];}
	else if(ob.attr("cat")=='email') { thekey = keyList[1];}
	else if(ob.attr("cat")=='phone') { thekey = keyList[1];}
	else if(ob.attr("cat")=='address') { thekey = keyList[1];}
	else if(ob.attr("cat")=='im') { thekey = keyList[1];}
	else if(ob.attr("cat")=='description') { thekey = keyList[2];}
	else if(ob.attr("cat")=='school') { thekey = keyList[3];}
	else if(ob.attr("cat")=='work') { thekey = keyList[4];}
	else if(ob.attr("cat")=='site') { thekey = keyList[5];}
	else if(ob.attr("cat")=='photo') { thekey = keyList[6];}
	else if(ob.attr("cat")=='patents') { thekey = keyList[8];}
	else if(ob.attr("cat")=='skill') { thekey = keyList[9];}
	else if(ob.attr("cat")=='certification') { thekey = keyList[10];}
	else if(ob.attr("cat")=='xfn') { thekey = keyList[11];}
	return thekey;
}


function getAreaForKey(thekey){
		var areas = '';
		switch (thekey) {
		    case 'Basic':
		        areas = 'basic_info';
		        break;
		    case 'Schools':
		        areas = 'school_info';
		        break;
		    case 'Work_History':
		        areas = 'work_info';
		        break;
		    case 'Site':
		        areas = 'site_info';
		        break;
		    case 'Contacts':
		        areas = 'contact_info';
		        break;
		    case 'Photos':
		        areas = 'photo_info';
		        break;
		    case 'Electronic_Contacts':
		        areas = 'electronic_contact_info';
		        break;
		    case 'Patents':
		        areas = 'patents_info';
		        break;
		    case 'Skills_Certifications':
		        areas = 'skills_certifications_info';
		        break;
		    case 'XFN_Title':
		        areas = 'xfn_info';
		        break;
	}
	return areas;
}

//Bind XFN Creator js function
function bindXfn(){
	$(".xfn_creator").find("input, select").each(function (){
		var xfnid = $(this).attr('id').split('-').pop();
		var fieldid = 'xfn_relation-'+xfnid;
		var meid = '#me-'+xfnid;
		
		$("#xfn-"+xfnid).unbind();
		$(this).css('display','');
		$("#xfn-"+xfnid).click(function() {
			vdata = {};
			vdata["cmd"] = "save";
			vdata["id"] = page_repsheet_id;
			vdata['key'] = 'about-xfns';
			if($(this).attr('checked') == true){
				vdata['map'] = '{\"xfn\":{\"'+xfnid+'\":[\"xfn-'+xfnid+'\"]}}';
			}else{
				vdata['map'] = '{\"xfn\":{\"'+xfnid+'\":[\"\"]}}';
			}

			$.postJSON("/repsheet/attribute.php", vdata,
				function(json) {
					if (json && json.error){
						$(".repsheet_mainbox:first").find(".error").each(function() {
							$(this).remove();
						});
						$(".repsheet_mainbox:first").prepend('<span class="error">' + json.error + '</span>');
					}	
				}
			);	
		});
		
		$(this).unbind();
		$(this).css('display','');
		//input
		if($(this).attr('type')=='checkbox'){
			$(this).click(function() {
				var relstr = '';
				$("#xfn_creator-"+xfnid).find("select, input").each(function (){
					if(($(this).attr('type') == 'checkbox') && ($(this).attr('checked') == true)) {
						relstr = relstr + ' ' + $(this).val();
					}else if(($(this).attr('type') == 'select-one') && ($(this).val() != '')){
						relstr = relstr + ' ' + $(this).val();
					}
					
					if($(meid).attr('checked') == true && $(this).val()!='me'){
						if($(meid).attr('checked') == true){
							$(this).attr("disabled","disabled");
						}
						relstr = 'me';
					}else{
							$(this).removeAttr("disabled");
					}
				});

			qdata = {};
			qdata["cmd"] = "save";
			qdata["label"] = fieldid;
			qdata[fieldid] = relstr;
			$.postJSON("/repsheet/profile_attribute.php", qdata,
			function(json) {
				if (json && json.error){
					$(".repsheet_mainbox:first").find(".error").each(function() {
						$(this).remove();
					});
					$(".repsheet_mainbox:first").prepend('<span class="error">' + json.error + '</span>');
				}
				$('#xfn_relation-'+xfnid).text(json.value);
				
			}
		);	
			} );
		}else{
			$(this).change(function() {
				var relstr = '';
				$("#xfn_creator-"+xfnid).find("input, select").each(function (){
					if(($(this).attr('type') == 'checkbox') && ($(this).attr('checked') == true)) {
						relstr = relstr + ' ' + $(this).val();
					}else if((($(this).attr('type') == 'select-one') || ($(this).attr('type') == 'select-multiple') ) && ($(this).val() != '')){
						relstr = relstr + ' ' + $(this).val();
					}
					
					if($(meid).attr('checked') == true && $(this).val()!='me'){
						if($(meid).attr('checked') == true){
							$(this).attr("disabled","disabled");
						}
						relstr = 'me';
					}else{
							$(this).removeAttr("disabled");
					}
				});

			qdata = {};
			qdata["cmd"] = "save";
			qdata["label"] = fieldid;
			qdata[fieldid] = relstr;
			$.postJSON("/repsheet/profile_attribute.php", qdata,
			function(json) {
				if (json && json.error){
					$(".repsheet_mainbox:first").find(".error").each(function() {
						$(this).remove();
					});
					$(".repsheet_mainbox:first").prepend('<span class="error">' + json.error + '</span>');
				}
				$('#xfn_relation-'+xfnid).text(json.value);
				
			}
		);	
			} );
		}
		
		
	});
}

function loadVerifyRepSheet(page_user_id){
		$.postJSON("/repsheet/attribute.php", { cmd: "verify", id: page_user_id, key: 'verify' }, 
		function(json) {
			if (json && json.error){
				$(".repsheet_mainbox:first").find(".error").each(function() {
					$(this).remove();
				});
				$(".repsheet_mainbox:first").prepend('<span class="error">' + json.error + '</span>');
			}			
			for (cat in json) {
				$(cat).attr("style","");
			}
		});
		/*
		$.postJSON("/repsheet/attribute.php", { cmd: "verify", id: page_user_id, key: 'verify' }, 
		function(json) {
			if (json && json.error){
				$(".repsheet_mainbox:first").find(".error").each(function() {
					$(this).remove();
				});
				$(".repsheet_mainbox:first").prepend('<span class="error">' + json.error + '</span>');
			}
			for (cat in json) {
				$("#social_verify_"+cat).attr("style","display:none;");
				$("#verify_"+cat).attr("style","");
			}
		});*/
}