﻿// JScript File
// Fuwalah Scripts

var DivUploadCVClientID = "ctl00_ContentPlaceHolderFuwalah_DivUploadCV";
var HFDivUploadCVClientID = "ctl00_ContentPlaceHolderFuwalah_HFDivUploadCV";
var BtnSignInClientID = "ctl00_ContentPlaceHolderFuwalah_BtnSignIn";
var HFFileUploadSuccesfulClientID = "ctl00_ContentPlaceHolderFuwalah_HFFileUploadSuccesful";
var LblMessageClientID = "ctl00_ContentPlaceHolderFuwalah_LblMessage";
var LblSuccessClientID = "ctl00_ContentPlaceHolderFuwalah_LblSuccess";
var HFSendInvitesSuccessfulClientID = "ctl00_ContentPlaceHolderFuwalah_HFSendInvitesSuccessful";
var HFDivInvitationClientID = "ctl00_ContentPlaceHolderFuwalah_HFDivInvitation";
var LblErrorInvitationClientID = "ctl00_ContentPlaceHolderFuwalah_LblErrorInvitation";
var HFDivSendMessageClientID = "ctl00_ContentPlaceHolderFuwalah_HFDivSendMessage";
var LblErrorSendMessageClientID = "ctl00_ContentPlaceHolderFuwalah_LblErrorSendMessage";
var CBoxSendAllClientID = "ctl00_ContentPlaceHolderFuwalah_CBoxSendAll";
var TxtToClientID = "ctl00_ContentPlaceHolderFuwalah_TxtTo";
var TxtToTextClientID = "ctl00_ContentPlaceHolderFuwalah_TxtTo_Text";
var TxtOptionalMessageClientID = "ctl00_ContentPlaceHolderFuwalah_TxtOptionalMessage";
var TxtOptionalMessageTextClientID = "ctl00_ContentPlaceHolderFuwalah_TxtOptionalMessage_Text";
var HFMembersEmailsClientID = "ctl00_ContentPlaceHolderFuwalah_HFMembersEmails";
var HFSelectedImagesClientID = "ctl00_ContentPlaceHolderFuwalah_HFSelectedImages";
var HFSelectedImagesFileNamesClientID = "ctl00_ContentPlaceHolderFuwalah_HFSelectedImagesFileNames";
var HFExtractedYouTubeVideoIDClientID = "ctl00_ContentPlaceHolderFuwalah_HFExtractedYouTubeVideoID";
var HFSelectedIDsClientID = "ctl00_ContentPlaceHolderFuwalah_HFSelectedIDs";

function TextBoxChanged()
{                
    radalert("We do our best to keep Fuwalah safe, but we cannot guarantee it. We need your help in order to do that, which includes the following commitments:\ni. You will not send or otherwise post unauthorized commercial communications to users (such as spam). ");
}

function SetPopupHFValues()
{
	var HFDivUploadCV = document.getElementById(HFDivUploadCVClientID);
	
	if(HFDivUploadCV != null)
		HFDivUploadCV.value = "1";
}

//Trigger sign in btn onkeypress of passwd textbox
function TriggerSignIn(e)
{
	//the purpose of this function is to allow the enter key to 
	//point to the correct button to click.
	var key;

	 if(window.event)
		  key = window.event.keyCode;     //IE
	 else
		  key = e.which;     //firefox

	if (key == 13)
	{
		//Get the button the user wants to have clicked
		var btnSignIn = document.getElementById(BtnSignInClientID);
		if(btnSignIn != null)
		{
			btnSignIn.click();
		}
	}
}

//Trigger sign in btn onkeypress of passwd textbox
function TriggerBtnClick(e, btnClientID)
{
	//the purpose of this function is to allow the enter key to 
	//point to the correct button to click.
	var key;

	 if(window.event)
		  key = window.event.keyCode;     //IE
	 else
		  key = e.which;     //firefox

	if (key == 13)
	{
		//Get the button the user wants to have clicked
		var btn = document.getElementById(btnClientID);
		if(btn != null)
		{
			btn.click();
		}
	}
}

function DoPageLoad()
{
	//show upload cv popup
	var HFDivUploadCV = document.getElementById(HFDivUploadCVClientID);
	if(HFDivUploadCV != null)
	{
		if(HFDivUploadCV.value == "1")
		{
			var ModalBehaviour_MPE1 = $find("ModalBehaviour_MPE1"); 
			if(ModalBehaviour_MPE1 != null) 
				ModalBehaviour_MPE1.show()
		}
	}
	
	//show send invites popup
	var HFDivInvitation = document.getElementById(HFDivInvitationClientID);
	if(HFDivInvitation != null)
	{
		if(HFDivInvitation.value == "1")
		{
			var ModalBehaviour_MPE2 = $find("ModalBehaviour_MPE2"); 
			if(ModalBehaviour_MPE2 != null)
				ModalBehaviour_MPE2.show();
		}	
	}
	var HFSendInvitesSuccessful = document.getElementById(HFSendInvitesSuccessfulClientID);
	if(HFSendInvitesSuccessful != null && HFSendInvitesSuccessful.value == "1")
	{ 
		HFSendInvitesSuccessful.value = "0";		
		radalert("Your invitation has been sent. Thank you.");
	}
}

function DoLoadCompanyProfilePage()
{
	//show send message div
	var HFDivSendMessage = document.getElementById(HFDivSendMessageClientID);
	if(HFDivSendMessage != null)
	{
		var ModalBehaviour_MPE1 = $find("ModalBehaviour_MPE1");
		
		if(ModalBehaviour_MPE1 != null)
		{
			if(HFDivSendMessage.value == "1")
			{
				ModalBehaviour_MPE1.show()
			}
			else
			{
				ModalBehaviour_MPE1.hide(); 
			}
		}
	}
}

function ClearSendMessagePopupMessages()
{
	var LblErrorSendMessage = document.getElementById(LblErrorSendMessageClientID);
	if(LblErrorSendMessage != null)
	{
		LblErrorSendMessage.innerText = "";
	}
	
	//var CBoxSendAll = document.getElementById(CBoxSendAllClientID);
	//if(CBoxSendAll != null)
	//{
	//	CBoxSendAll.checked = false;
	//}
}

function ClearDivAllMembersHF()
{
	var HFDivSendMessage = document.getElementById(HFDivSendMessageClientID);
	if(HFDivSendMessage != null)
	{
		HFDivSendMessage.value = '';
	}
}

//Show upload CV div
function ShowDivUploadCV(isPageLoad)
{
	var DivUploadCV = document.getElementById(DivUploadCVClientID);
	var HFDivUploadCV = document.getElementById(HFDivUploadCVClientID);
	
	if(DivUploadCV != null & HFDivUploadCV != null)
	{
		HFDivUploadCV.value = "1";
		CenterAndShowObj(DivUploadCV);
	}
	return false;
}

//Hide upload CV div		
function HideDivUploadCV()
{
	var DivUploadCV = document.getElementById(DivUploadCVClientID);
	var HFDivUploadCV = document.getElementById(HFDivUploadCVClientID);
	
	if(DivUploadCV != null && HFDivUploadCV != null)
	{
		var ModalBehaviour_MPE1 = $find("ModalBehaviour_MPE1");
		if(ModalBehaviour_MPE1 != null)
			ModalBehaviour_MPE1.hide();
			
		HFDivUploadCV.value = "0";		
	}
}

//Hide send invites div		
function HideDivInvitation()
{
	var HFDivInvitation = document.getElementById(HFDivInvitationClientID);
	
	if(HFDivInvitation != null)
	{
		HFDivInvitation.value = "0";		
	}
}

function ClearPopupMessages()
{
	//messages for upload CV
	var LblMessage = document.getElementById(LblMessageClientID);
	
	if(LblMessage != null)
		LblMessage.innerText = "";
		
	var LblSuccess = document.getElementById(LblSuccessClientID);
	
	if(LblSuccess != null)
		LblSuccess.innerText = "";
		
	//messages for send invitation popup
	var LblErrorInvitation = document.getElementById(LblErrorInvitationClientID);
	
	if(LblErrorInvitation != null)
		LblErrorInvitation.innerText = "";
}

function CenterAndShowObj(obj)
{
	CenterObj(obj);
	obj.style.display = "block";		
}

//Center upload CV div		
function CenterObj(obj)
{
	if(obj != null)
	{
		var width = obj.clientWidth;
		var height = obj.clientHeight;
		//alert(height + ","+ width);
		var myWidth = 0, myHeight = 0;
		  if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		  }
		  
		var left = (myWidth - width)/2;
		var top = (myHeight - height)/2;
		
		window.status = top + "," + left;		
		
		obj.style.top = top + "px";
		obj.style.left = left + "px";
	}
}
		
function FileStateChanged(uploader, file, httpStatus, isLast)
{
    Flajaxian.fileStateChanged(uploader, file, httpStatus, isLast);
    if(file.state > Flajaxian.File_Uploading)
    {
        //document.getElementById('MyDiv').innerHTML += "bytes:"+file.bytes+" name:"+file.name+" state:"+file.state+" httpStatus:"+httpStatus+" isLast:"+isLast+"</br>";
        if(isLast)
			document.getElementById('MyDiv').innerHTML = "Successfully uploaded";
    }
}

function SendAll(cBox)
{
	var txtBox1 = document.getElementById(TxtToTextClientID);
	var txtBox2 = document.getElementById(TxtToClientID);
	
	var HFMembersEmails = document.getElementById(HFMembersEmailsClientID);                         

	if(txtBox1 != null && txtBox2 != null && HFMembersEmails != null)
	{
		if(cBox.checked)
		{
			txtBox1.value = HFMembersEmails.value;
			txtBox2.value = HFMembersEmails.value;
		}
		else
		{
			txtBox1.value = '';
			txtBox2.value = '';
		}
	}
}

function SelectImg(checkBox, selectedImgID, selectedImgFileName)
{
	var HFSelectedImages = document.getElementById(HFSelectedImagesClientID);
	var HFSelectedImagesFileNames = document.getElementById(HFSelectedImagesFileNamesClientID);
	
	if(HFSelectedImages != null && HFSelectedImagesFileNames != null)
	{
		if(checkBox.checked)
		{
			HFSelectedImages.value += "," + selectedImgID;
			HFSelectedImagesFileNames.value += "," + selectedImgFileName;
		}
		else
		{
			var selectedImagesIdsArray = HFSelectedImages.value.split(',');
			selectedImagesIdsArray = removeItem(selectedImagesIdsArray, selectedImgID);
			HFSelectedImages.value = "";
			
			for(var i = 0; i < selectedImagesIdsArray.length; i++)
			{
				HFSelectedImages.value += "," + selectedImagesIdsArray[i];
			}
			
			var selectedImagesFileNamesArray = HFSelectedImagesFileNames.value.split(',');
			selectedImagesFileNamesArray = removeItem(selectedImagesFileNamesArray, selectedImgFileName);
			HFSelectedImagesFileNames.value = "";
			
			for(var i = 0; i < selectedImagesFileNamesArray.length; i++)
			{
				HFSelectedImagesFileNames.value += "," + selectedImagesFileNamesArray[i];
			}
		}
	}
}

//remove item (string or number) from an array
function removeItem(originalArray, itemToRemove) 
{	
	var j = 0;	
	while (j < originalArray.length) 
	{	
		if (originalArray[j] == itemToRemove) 
		{			
			originalArray.splice(j, 1);	
		} 
		else 
		{ 
			j++; 
		}
	}	
	
	return originalArray;
}

function ConfirmDeleteAll()
{
	return confirm("Do you really want to delete all the images from this gallery?"); 
}

function ConfirmDelete()
{
	if(ItemsSelected())
		return confirm("Do you really want to delete the select images from this gallery?"); 
	else
	{
		radalert("No image has been selected!");
		return false;
	}
}

function ItemsSelected()
{
	var HFSelectedImages = document.getElementById(HFSelectedImagesClientID);
	if(HFSelectedImages != null && HFSelectedImages.value.length > 0)
		return true;
	else
		return false;
}

var cBoxSelectPrefix = "ctl00_ContentPlaceHolderFuwalah_cBoxSelect_";
function LoadChecBoxValues()
{
	var HFSelectedImages = document.getElementById(HFSelectedImagesClientID);
	if(HFSelectedImages != null)
	{
		var selectedImagesIdsArray = HFSelectedImages.value.split(',');
		
		for(var i = 0; i < selectedImagesIdsArray.length; i++)
		{
			var cBoxSelect = document.getElementById(cBoxSelectPrefix + selectedImagesIdsArray[i]);
			if(cBoxSelect != null)
			{
				cBoxSelect.checked = true;
			}
		}
	}
}

function youtubeIDextract(urlwa)
{
  var youtube_id;
  url = document.getElementById(urlwa).value;
  youtube_id = url.replace(/^[^v]+v.(.{11}).*/,"$1");
  
  var HFExtractedYouTubeVideoID = document.getElementById(HFExtractedYouTubeVideoIDClientID);
  if(HFExtractedYouTubeVideoID != null)
  {
	HFExtractedYouTubeVideoID.value = youtube_id;
  }
  return true;
}

function SelectItem(checkBox, selectedID, selectedIdsHFId)
{
	//var HFSelectedIDs = document.getElementById(HFSelectedIDsClientID);
	var HFSelectedIDs = document.getElementById(selectedIdsHFId);
	if(HFSelectedIDs != null)
	{
		if(checkBox.checked)
		{
			HFSelectedIDs.value += "," + selectedID;
		}
		else
		{
			var selectedIDsArray = HFSelectedIDs.value.split(',');
			selectedIDsArray = removeItem(selectedIDsArray, selectedID);
			HFSelectedIDs.value = "";
			
			for(var i = 0; i < selectedIDsArray.length; i++)
			{
				HFSelectedIDs.value += "," + selectedIDsArray[i];
			}
		}
	}
}

function LoadSelectedCheckBoxValues(clientIdPrefix)
{
	var HFSelectedIDs = document.getElementById(HFSelectedIDsClientID);
	if(HFSelectedIDs != null)
	{
		var controls = document.getElementsByTagName("input");
		
		for(var i = 0; i < controls.length; i++)
		{
			if(controls[i].type == "checkbox")
			{
				var str = "cBoxSelect_";
				var id = controls[i].id.substring(controls[i].id.indexOf(str)+str.length);
				var selectedIDsArray = HFSelectedIDs.value.split(',');
				for(var j = 0; j < selectedIDsArray.length; j++)
				{
					if(id == selectedIDsArray[j])
					{
						controls[i].checked = true;
					}
				}
			}
		}
	}
}

function ConfirmDeleteLogo(msg)
{
	return confirm(msg); 
}

function ConfirmDeleteGeneric(msg)
{
	return confirm(msg); 
}

function HideControls(obj, divId, btnId)
{
	var div = document.getElementById(divId);
	var btn = document.getElementById(btnId);
	
	if(div != null)
	{
		var controls = div.getElementsByTagName("div");
			
		for(var i = 0; i < controls.length; i++)
		{
			if(controls[i].id == "divCbox")
				controls[i].style.display = "none";
		}
	}
	
	if(btn != null)
	{	
		btn.style.display = "none";
	}
} 

function ShowControls(obj, divId, btnId)
{
	var div = document.getElementById(divId);
	var btn = document.getElementById(btnId);
	
	if(div != null)
	{
		var controls = div.getElementsByTagName("div");
			
		for(var i = 0; i < controls.length; i++)
		{
			if(controls[i].id == "divCbox")
				controls[i].style.display = "block";
		}
	}
	
	if(btn != null)
	{	
		btn.style.display = "block";
	}
}

function CheckMaxCharacters(textbox,prefixLength,message1,message2,cboxId)
{
	//calculate the prefix length
	var max = 160 - prefixLength;
	var message = message1;
	
	var cbox = document.getElementById(cboxId);	
	if(cbox != null && cbox.checked)
	{
		max = 70 - prefixLength;
		message = message2;
	}
	
	if(textbox.value.length > max)
	{	
		alert(message);
		textbox.value = textbox.value.substring(0,max);
	}
}

function ShowTruncateWarning(cbox,txtboxId,prefixLength,message)
{
	if(cbox.checked) //arabic selected
	{
		var txtbox = document.getElementById(txtboxId);
		var max = 70 - prefixLength;
		
		if(txtbox != null)
		{
			if(txtbox.value.length > max)
			{
				txtbox.value = txtbox.value.substring(0,max);
				(document.getElementById(txtbox.id + '_text')).value = txtbox.value;
				alert(message);
			}	
		}
	}
} 

function ToggleSelectAll(cboxObj, containerObjId, hfSelected, hfGroups)
{
	var containerObj = document.getElementById(containerObjId);

	//Check/Uncheck all checkboxes
	if(containerObj != null)
	{
		var checkboxes = containerObj.getElementsByTagName("input");
	
		if(checkboxes != null)
		{
			for(var i = 0; i < checkboxes.length; i++)
			{
				if(checkboxes[i].type == "checkbox")
				{
					checkboxes[i].checked = cboxObj.checked;
				}
			} 
		}
	}
	
	//Set/Clear HF
	var hfSelectedObj = document.getElementById(hfSelected);
	var hfGroupsObj = document.getElementById(hfGroups);

	if(hfSelectedObj != null && hfGroupsObj != null)
	{
		if(cboxObj.checked)
		{
			hfSelectedObj.value = hfGroupsObj.value;
		}
		else
		{
			hfSelectedObj.value = "";
		}
	}
}