
//Create Parent Categories
function SetSearchEnginePCategoriesBox()
{
	var divSearchEngineHitechCategoriesConteiner;
	var checkbox;
	var Table;
	var TBody;
	var tr;
	var td;
	var span;

	divSearchEngineHitechCategoriesConteiner = document.createElement('div'); 
	divSearchEngineHitechCategoriesConteiner.setAttribute('id','divSearchEngineHitechCategoriesConteiner');
	Table = document.createElement('table');
	Table.setAttribute('id','tblSearchEngineHitechCategories');
	Table.cellPadding = "0px";
	Table.cellSpacing = "0px";
	Table.setAttribute('width','100%');
	Table.setAttribute('border','0');
	TBody = document.createElement('tbody');
	document.getElementById('divSearchEnginePCategoriesBox').innerHTML = '';

	//Create all parent categories
	for(i=0;i<arrPOnly.length;i++)
	{
		
		tr = document.createElement('tr'); 
		tr.setAttribute("id","trPCategory_" + arrPOnly[i]);
			
		//Set checkbox
		td = document.createElement('td'); 
		td.setAttribute("id", "tdPCategoryChk_" + arrPOnly[i]);
		checkbox = document.createElement('input'); 
		checkbox.type = "checkbox";
		checkbox.setAttribute('id','chkPCategory_' + arrPOnly[i]);
		checkbox.onclick = function(){SetPCaterogyMarkedRowBox(this.id);};
		checkbox.setAttribute('value',arrPOnly[i]);
		td.setAttribute('width','10px');
		td.appendChild(checkbox);
		tr.appendChild(td);
		
		//Set CategoryName
		td = document.createElement('td'); 
		td.onclick = function(){SetPCaterogyMarkedRowBox(this.id);};
		td.setAttribute("id", "tdPCategoryName_" + arrPOnly[i]);
		$(td).css('cursor','see below');
		td.appendChild(document.createTextNode(arrPNameOnly[i]));
		tr.appendChild(td);
		
		//Set unvisible param
		td = document.createElement('td'); 
		span = document.createElement('span'); 
		span.setAttribute("id", "spnShowChildrenCategory_" + arrPOnly[i]);
		span.appendChild(document.createTextNode('open'));
		$(span).attr('class','DisplayN');
		td.appendChild(span);
		tr.appendChild(td);
		TBody.appendChild(tr);
		
		//Row conteiner for Children of this category
		tr = document.createElement('tr'); 
		tr.setAttribute("id","trChildrenCategories_" + arrPOnly[i]);
		td = document.createElement('td'); 
		$(td).attr("class","tdChildrenCategoriesName");
		$(td).attr('colSpan','3');
		td.setAttribute("id", "tdChildrenCategories_" + arrPOnly[i]);
		tr.appendChild(td);
		TBody.appendChild(tr);
		
	}
	
	Table.appendChild(TBody);
	divSearchEngineHitechCategoriesConteiner.appendChild(Table);
	document.getElementById('divSearchEnginePCategoriesBox').appendChild(divSearchEngineHitechCategoriesConteiner);
	document.getElementById('divSearchEnginePCategoriesBox').className = 'ComboViewHitechAgent';
}

//Marked or Unmarked Parent category Row
function SetPCaterogyMarkedRowBox(PCategoryID)
{
	PCategoryID = PCategoryID.split('_')[1];

	//Check if checkbox checked by unvisible param
	if($('#spnShowChildrenCategory_'+PCategoryID).html() == 'open' )
	{
		$('#chkPCategory_'+PCategoryID).attr('checked',true);
		GetHitechChildrenBox(PCategoryID);
	}
	else
	{
		$('#chkPCategory_'+PCategoryID).attr('checked',false);
		GetHitechChildrenBox(PCategoryID);	
	}
	
	//Unmarked all rows	
	$('#tblSearchEngineHitechCategories tr input[@type="checkbox"]').parent().parent().attr('class', 'RowPCategoryUnMarked');
	
	//Marked rows where checkbox is checked
	$('#tblSearchEngineHitechCategories tr input[@type="checkbox"][@checked]').parent().parent().attr('class', 'RowPCategoryMarked');
	
}

//Get children for this parent
function GetHitechChildrenBox(PCategoryID)
{
	//Hide children
	if($('#spnShowChildrenCategory_'+PCategoryID).html() == 'close')
	{
		$('#spnShowChildrenCategory_'+PCategoryID).html('open');
		$('#trPCategory_'+PCategoryID).attr('class', 'RowPCategoryUnMarked');
		UncheckedChildrenByPCategoryIDBox(PCategoryID);
	}
	//Show children
	else
	{
		$('#spnShowChildrenCategory_'+PCategoryID).html('close')
		$('#trPCategory_'+PCategoryID).attr('class', 'RowPCategoryMarked');
		CreateChildrenPCategoryTableBox(PCategoryID);
	}	
}



//Unchecked children by PCategoryID
function UncheckedChildrenByPCategoryIDBox(PCategoryID)
{
	//Conteiner row in Category Parent table
	var ChildrenID = "tdChildrenCategories_" + PCategoryID;
	document.getElementById(ChildrenID).innerHTML = '';
	
}

//Create children for this PCategoryID
function CreateChildrenPCategoryTableBox(PCategoryID)
{
	//Conteiner row in ategory Parent table
	var ChildrenID = "tdChildrenCategories_" + PCategoryID;

	//Conteiner for Children
	var divSearchEngineChildrenCategoryConteiner;
	var checkbox;
	var Table;
	var TBody;
	var tr;
	var td;
	
	//DivID = PCategoryID
	divSearchEngineChildrenCategoryConteiner = document.createElement('div'); 
	divSearchEngineChildrenCategoryConteiner.setAttribute('id','divSearchEngineChildrenCategoryConteiner_' + PCategoryID);
	Table = document.createElement('table');
	//TableID = PCategoryID
	Table.setAttribute('id','tblSearchEngineChildrenCategoryParent_' + PCategoryID);
	Table.cellPadding = "0px";
	Table.cellSpacing = "0px";
	Table.setAttribute('width','100%');
	Table.setAttribute('border','0');
	TBody = document.createElement('tbody');
	document.getElementById(ChildrenID).innerHTML = '';
	
	//Checkbox select all children
	tr = document.createElement('tr');
	td = document.createElement('td');
	checkbox = document.createElement('input'); 
	checkbox.type = "checkbox";
	checkbox.setAttribute('id','chkChildrenCategory_' + PCategoryID + '_CategoryID_-1_PRealCatID_-1'); 
	checkbox.onclick = function(){SetChildrenCategoryMarkedRow(this.id);};
	checkbox.setAttribute('value','-1');
	td.appendChild(checkbox);
	$(checkbox).attr('checked',true);
	tr.setAttribute('id','trChildrenCategory_' + PCategoryID + '_CategoryID_-1_PRealCatID_-1');
	tr.onclick = function(){SetChildrenCategoryMarkedRow(this.id);};
	tr.appendChild(td);
	td = document.createElement('td');
	$(td).css('cursor','see below'); 
	td.appendChild(document.createTextNode('הכל'));
	tr.appendChild(td);
	TBody.appendChild(tr);
			
	for(i=0;i<CatIDs.length;i++)
	{
		if(CatPIDs[i] == PCategoryID && CatPIDs[i] != CatIDs[i])
		{
			tr = document.createElement('tr'); 
			tr.setAttribute("id","trChildrenCategory_" + PCategoryID + "_CategoryID_" + CatIDs[i] + "_PRealCatID_" + arrRealPCatID[i]);
			tr.onclick = function(){SetChildrenCategoryMarkedRow(this.id);};
			
			//Set checkbox
			td = document.createElement('td'); 
			td.setAttribute("id", "tdChildrenCategoryChk_" + PCategoryID + "_CategoryID_" + CatIDs[i] + "_PRealCatID_" + arrRealPCatID[i]);
			checkbox = document.createElement('input'); 
			checkbox.type = "checkbox";
			checkbox.setAttribute('id','chkChildrenCategory_' + PCategoryID + "_CategoryID_" + CatIDs[i] + "_PRealCatID_" + arrRealPCatID[i]);
			checkbox.onclick = function(){SetChildrenCategoryMarkedRow(this.id);};
			checkbox.setAttribute('value',CatIDs[i]);
			td.setAttribute('width','10px');
			td.appendChild(checkbox);
			tr.appendChild(td);
			
			//Set ChildrenCategoryName
			td = document.createElement('td'); 
			td.setAttribute("id", "tdChildrenCategoryName_" + PCategoryID + "_CategoryID_" + CatIDs[i] + "_PRealCatID_" + arrRealPCatID[i]);
			td.appendChild(document.createTextNode(CatNames[i]));
			$(td).css('cursor','see below');
			tr.appendChild(td);
			TBody.appendChild(tr);
		}
		
	}
	
	Table.appendChild(TBody);
	divSearchEngineChildrenCategoryConteiner.appendChild(Table);
	document.getElementById(ChildrenID).appendChild(divSearchEngineChildrenCategoryConteiner);
}

//Marked or Unmarked Row
function SetChildrenCategoryMarkedRow(ChildObj)
{
	var PCategoryID = ChildObj.split('_')[1];
	var ChildID = ChildObj.split('_')[3];
	var RealChildPCatID = ChildObj.split('_')[5];
	//Check if checkbox checked
	if($('#chkChildrenCategory_'+PCategoryID+'_CategoryID_'+ChildID+'_PRealCatID_'+RealChildPCatID).attr('checked'))
		$('#chkChildrenCategory_'+PCategoryID+'_CategoryID_'+ChildID+'_PRealCatID_'+RealChildPCatID).attr('checked',false);
	else
		$('#chkChildrenCategory_'+PCategoryID+'_CategoryID_'+ChildID+'_PRealCatID_'+RealChildPCatID).attr('checked',true);

	
	//Unchecked others checkboxs exept ChildID == '-1'
	if(ChildID == '-1' && $('#chkChildrenCategory_'+PCategoryID+'_CategoryID_-1_PRealCatID_-1').attr('checked'))
	{
		$('#tblSearchEngineChildrenCategoryParent_'+ PCategoryID + ' tr input[@type="checkbox"][@checked]').each(
			function()
			{
				if(this.value != '-1')
					$(this).attr('checked',false);
			}
		);
	}
	//Uncheked checkbox with ChildID == '-1'
	if(ChildID != '-1' && $('#chkChildrenCategory_'+PCategoryID+'_CategoryID_'+ChildID+'_PRealCatID_'+RealChildPCatID).attr('checked'))
	{
		$('#chkChildrenCategory_'+PCategoryID+'_CategoryID_-1_PRealCatID_-1').attr('checked',false);
	}	
	//Unmarked all rows	
	$('#tblSearchEngineChildrenCategoryParent_'+ PCategoryID + ' tr input[@type="checkbox"]').parent().parent().attr('class', 'RowChCategoryUnMarked');
	
	//Marked rows where checkbox is cheked
	$('#tblSearchEngineChildrenCategoryParent_'+ PCategoryID + ' tr input[@type="checkbox"][@checked]').parent().parent().attr('class', 'RowChCategoryMarked');
}
function CreateAgent()
{
	$('#divLoadAjaxAgent').show();
	$('#SaveBtn').hide();
	
	//Check user status
	var IsCheckSignIn = $('input:hidden[@id$="hdnExistsUserState"]').val();
	var isOK = true;

	//If user not populated check if user exist or create new user
	if(IsCheckSignIn != '2')
		isOK = CheckRegisterForm('MailCheckAjaxCall');
		
	if(!isOK)
	{
		if($('input:hidden[@id$="hdnExistsUserState"]').val() == '1')
			$('div[@id$="ConfirmMailList_divChk"]').hide();
		
		$('#divLoadAjaxAgent').hide();
		$('#SaveBtn').show();
		return false;
	}	
	
	var IsCheckboxChecked = false;
	var arrVirtPCatIDs = new Array();
	var arrChildCatIDs = new Array();
	var arrRealPCategoryIDs = new Array();
	var arrRelocationCatIDs = new Array();
	var ChildrenIDsAndParentIDs = '';
	var i = 0;
	var k = 0;
	$('#divSearchEnginePCategoriesBox input[@type="checkbox"][@checked]').each(
		function()
		{
			//Run on all children checkboxes
			if(this.id.indexOf('chkChildrenCategory_') != -1)
			{	IsCheckboxChecked = true;
				//Get categories without relacation categories
				if(this.id.split('_')[1] != '15')
				{
					arrVirtPCatIDs[i] = this.id.split('_')[1];
					arrChildCatIDs[i] = this.id.split('_')[3];
					arrRealPCategoryIDs[i] = this.id.split('_')[5];
					i++;
				}
				else
				{
					arrRelocationCatIDs[k] = this.id.split('_')[3];
					k++;
				}	
			}
		}
	);
	
	//Check if checkboxs checked
	if(!IsCheckboxChecked)
	{	
		$('#spnRegisterMsg').html("<img src='/images/Icons/Error.gif'> חובה לבחור תחום מהרשימה");
		$('#divLoadAjaxAgent').hide();
		$('#SaveBtn').show();
		return false;
	}	
	
	//Check if relocation checkbox was checked CatPVirtIDs = 15
	if(arrRelocationCatIDs.length > 0)
	{
		var strRelocationCatIDs = '';
		if(arrRelocationCatIDs[0] == '-1')
		{
			//strRelocationCatIDs = GetChildrenByPCategoryID('15');
			//Get all children of this CategoryID
			for(i=0;i<CatIDs.length;i++)
			{
				if(CatPIDs[i] == '15' && CatPIDs[i] != CatIDs[i])
					strRelocationCatIDs += CatIDs[i] + ',';
			}
			//Erase last seperator
			strRelocationCatIDs = strRelocationCatIDs.substring(0, strRelocationCatIDs.length - 1);
			$('input[@type=hidden][@id*="hdnRelocationAgent"]').val(strRelocationCatIDs)
		}
		else
			$('input[@type=hidden][@id*="hdnRelocationAgent"]').val(arrRelocationCatIDs)
	}
		
	//Get all children where was choosen ('הכל'=>all) checkbox of parent
	for(i=0;i<arrChildCatIDs.length;i++)
	{
		//Get children by  VirtPCatID
		if(arrChildCatIDs[i] == '-1')
			ChildrenIDsAndParentIDs += GetChildrenByPCategoryID(arrVirtPCatIDs[i]);
		else
			ChildrenIDsAndParentIDs += arrChildCatIDs[i] + ',' + arrRealPCategoryIDs[i] + ',';
	}
	//Erase last seperator
	ChildrenIDsAndParentIDs =   ChildrenIDsAndParentIDs.substring(0, ChildrenIDsAndParentIDs.length - 1);
	//Sort children by real Parents
	SortChildrenByRealParents(ChildrenIDsAndParentIDs);
	
	//Get params for ajax
	var hdnIsNewUser = $('[@id$="hdnIsNewUser"]').val()
	var chkLessonsEmail = '';
	var chkAdvertiseEmail = '';
	var txtNewMail = $('[@id$="txtNewMail"]').val()
	var txtMail = $('[@id$="txtMail"]').val()
	var hdnSupportType = $('[@id$="hdnSupportType"]').val()
	var hdnAgents = $('[@id$="hdnAgents"]').val()
	var hdnRelocationAgent = $('[@id$="hdnRelocationAgent"]').val()
	var Email ='';
	
	//If Not New User
	if(hdnIsNewUser == "0")
		Email = $('[@id$="txtMail"]').val();
	else
	{	//New User
		Email = $('[@id$="txtNewMail"]').val();
		chkLessonsEmail = $('input:checkbox[@id$="chkLessonsEmail"]').get(0).checked;
		chkAdvertiseEmail = $('input:checkbox[@id$="chkAdvertiseEmail"]').get(0).checked;
	}
	
	//Create agents for user and if this new user also create him and do populate
	var Params = "Action=SaveUserAgent&IsNewUser="+hdnIsNewUser+"&IsLessonsEmail="+chkLessonsEmail+"&IsAdvertiseEmail="+chkAdvertiseEmail+"&Email="+Email+"&SupportType="+hdnSupportType+"&Agents="+hdnAgents+"&RelocationAgent="+hdnRelocationAgent;
	$.ajax({
		type: "POST",
		url: "/Partners/Hitech/HitechAction.aspx",
		data:Params,
		async: false,
		success: function(Res){
				if(Res == "1")
				{
					if(IsAuth == '0')
						//Redirect to Hitech/Default.aspx after user create agent
						document.location.href = "/Partners/10001/%D7%93%D7%A8%D7%95%D7%A9%D7%99%D7%9D_%D7%91%D7%94%D7%99%D7%99%D7%98%D7%A7.html?IsAgent=1"

					if (hdnIsNewUser == "1")
					    ReportGoogleGoal("/User/Register/RegisterGoogleGoal.aspx");

					$('[@id$="divRegister"]').hide()
					$('[@id$="trConfirmMailList"]').hide()
					//User IsAuthenticated value for UserSignIn.js -> CheckRegisterForm()(<- don't call this func from AgentHitech.js -> CreateAgent() )
					$('[@id$="hdnExistsUserState"]').val('2')
					SetSearchEnginePCategoriesBox();
					$('[@id$="spnAgentMsg"]').show();
					setTimeout(function(){$('[@id$="spnAgentMsg"]').hide();},5000);
			
				}
				else
				{
					$('#spnRegisterMsg').html("<img src='/images/Icons/Error.gif'>ארעה תקלה בשמירת הנתונים. נסה שוב");
				}
				
			}
		});
	$('[@id$="divLoadAjaxAgent"]').hide();
	$('#SaveBtn').show();
}	

//Get Children By Parent CategoryID
function GetChildrenByPCategoryID(CategoryID)
{	
	var positionParent = CategoryID;
	var categoriesIDs = '';

	//Get all children of this CategoryID
	for(i=0;i<CatIDs.length;i++)
	{
		if(CatPIDs[i] == positionParent && CatPIDs[i] != CatIDs[i])
			categoriesIDs += CatIDs[i] + ',' + arrRealPCatID[i] + ',';
	}
	return categoriesIDs;
}
//Sort Children By Real Parents
function SortChildrenByRealParents(ChildrenIDsAndParentIDs)
{
	var arrChildrenIDsAndParentIDs = new Array();
	var SplitChildrenIDsAndParentIDs = ChildrenIDsAndParentIDs.split(",");
	var strAgents = '';
	//Split string to array
	for(i = 0; i < SplitChildrenIDsAndParentIDs.length; i++)
		arrChildrenIDsAndParentIDs[i] = SplitChildrenIDsAndParentIDs[i];
	
	//Find for each Child his Parent and create string of differents agents	
	for(i = 0; i < arrRealPCatIDsOnly.length;i++)
	{
		for(k = 1; k < arrChildrenIDsAndParentIDs.length;k+=2)
		{
			if(arrChildrenIDsAndParentIDs[k] == arrRealPCatIDsOnly[i])
			{
				strAgents += arrChildrenIDsAndParentIDs[k-1]+ ',' ;
			}
		}
		
		//Erase last seperator
		strAgents =  strAgents.substring(0, strAgents.length - 1);
		//Add seperatoe between Agents
		strAgents += '|';
	}
	//Erase last seperator
	strAgents =  strAgents.substring(0, strAgents.length - 1);
	//Erase first '|' if exist
	if(strAgents.charAt(0) == '|')
		strAgents =  strAgents.substring(1,strAgents.length);
	
	$('input[@type=hidden][@id*="hdnAgents"]').val(strAgents)
}

