﻿function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function data_Hover(target,action){ //v3.0
	 target.className='data_'+action;
}

function pageWidth() {return  window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?  document.documentElement.clientWidth : document.body != null? document.body.clientWidth : null;} 

var oldClassName;

function PopUp(object, message)
{
    obj = findObj(object);
    if(obj)
    {
        if(obj.checked)
        {
            if(confirm(message))
            return true;
            else
            return false;
         }
         else
         {
         return true;
         }
    }
    else
    {
    return true;
    
    }

}

function GetConfirm(message)
{
    if(confirm(message))
        return true;
    else
        return false;
}

function changeView(object)
{
obj = findObj(object);
 if (obj)
  {
  	oldClassName=(obj.className!='hide')?obj.className:oldClassName;
  	
	if(obj.style.display=='none')
		{
		obj.style.display='';
		//obj.style.visi='';
		//alert(obj.offsetLeft+ ' '+ obj.clientWidth);
		if((obj.offsetLeft + obj.clientWidth) > (document.body.clientWidth + document.body.scrollLeft))
			 {
				 obj.style.right="10px";
			 }		
		}
	else{
		obj.style.display='none';
	}  
  }
  return true;
}
var _yCoordinate = 0;
var _xCoordinate = 0;
function ShowHideComment(smallComment, bigComment)
{
    // changeView(smallComment);
    obj = findObj(smallComment);
    changeView(bigComment);
    var elementLoc = Sys.UI.DomElement.getLocation(obj);
     if(_xCoordinate == 0)
        _xCoordinate = elementLoc.x;
        
    if(_yCoordinate == 0)
        _yCoordinate = elementLoc.y - 20;
        
    bigComment = findObj(bigComment);                
    bigComment.style.posLeft = _xCoordinate ;
    bigComment.style.posTop = _yCoordinate ;                     
    
    bigComment.style.left = elementLoc.x + 'px' ;
    bigComment.style.top= _yCoordinate + 'px'; 
}

function hideThis(hide)
{
obj = findObj(hide);
 if (obj)
  {
	obj.style.display='none';	
  }
}
function showThis(show)
{
obj = findObj(show);
 if (obj)
  {
	obj.style.display='block';	
  }
}

function showFilter(object)
{
obj = findObj(object);
 if (obj)
  {
	obj.style.display='';
	
	if(timeOnDict[object] != null)
	{
		clearTimeout(timeOnDict[object]);
	}
	//clearTimeout(timeOn);	
  }
}

function showFilterOut(object) {
	if(timeOnDict[object] != null)
	{
	    timeOnDict[object] = null;
	}
	timeOnDict[object] = setTimeout("hideThis('"+object+"')", 500);
	
	//timeOn = setTimeout("hideThis('"+object+"')", 500);
}

function showFilterOver(object) {
	
	if(timeOnDict[object] != null)
	{
		clearTimeout(timeOnDict[object]);
		timeOnDict[object] = null;
	}
	//clearTimeout(timeOn);
	//timeOn=null;
}
var timeOn = null;

var timeOnDict = new Array(); 
var array = new Array();
var selectedMenu;
var currentSelectedSubMenu;
var currentSubMenu;


function menuMouseOut(object)
{
    if(object != selectedMenu)
    {
        object.className = 'menuContainerItem';
    }

}

function changeMenu(object)
{
    if(object == selectedMenu)
    {
        if(object.title != 'currentMenu')
            object.className = 'menuContainerItem';
        
        selectedMenu = null;
        return;
    }
    
    selectedMenu = object;
    
     var divs = object.parentNode.getElementsByTagName('div');

    // loop through all input elements in form

    for(var i = 0; i < divs.length; i++)

    {
        if(divs.item(i).title != 'currentMenu')
        { 
            divs.item(i).className = 'menuContainerItem';
        }
     }
    if(object.title != 'currentMenu')
    object.className = 'menuContainerHover';
}

function changeSubMenu(object)
{
 //defaultSub = findObj('subDefault');
obj = findObj(object);

//Searching current

var divs = obj.parentNode.getElementsByTagName('div');

// loop through all input elements in form

for(var i = 0; i < divs.length; i++)

{
    if(divs.item(i).title == 'currentSubMenu')
    { 
        currentSubMenu = divs.item(i);
    }

    if(divs.item(i).style == '' || divs.item(i).style.display == '')
    {
        currentSelectedSubMenu =  divs.item(i);
    }
    
    
    
 } 

 if (obj)
  {
  	if(obj.style.display=='none')
		{
		if(currentSelectedSubMenu != null)
		    {currentSelectedSubMenu.style.display='none';}
		    	
		obj.style.display='';
		//defaultSub.style.display='none';	
		currentSelectedSubMenu = obj;
		}
	else{
	  
	  
	  if(obj != currentSelectedSubMenu)
	  {
	    obj.style.display='none'; 
	  }
	  else
	  {
	  obj.style.display='none'; 
	  currentSubMenu.style.display='';
	    
	  }
		
		//defaultSub.style.display='';
	}  
  }
}

function changeView2(hide)
{
obj = findObj(hide);
 if (obj)
  {
  	if(obj.className=='hide')
		{
		obj.className='show';	
		}
	else{
		obj.className='hide';
	}
  
  }
}


function showTimeOut(object, milliseconds) {
	timeOn = setTimeout("hideThis('"+object+"')", milliseconds);
}

/**
*
*  Crossbrowser Drag Handler
*  http://www.webtoolkit.info/
*
**/

var DragHandler = {


	// private property.
	_oElem : null,


	// public method. Attach drag handler to an element.
	attach : function(oElem) {
		oElem.onmousedown = DragHandler._dragBegin;

		// callbacks
		oElem.dragBegin = new Function();
		oElem.drag = new Function();
		oElem.dragEnd = new Function();

		return oElem;
	},


	// private method. Begin drag process.
	_dragBegin : function(e) {
		var oElem = DragHandler._oElem = this;
		if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
		if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		e = e ? e : window.event;
		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;

		oElem.dragBegin(oElem, x, y);

		document.onmousemove = DragHandler._drag;
		document.onmouseup = DragHandler._dragEnd;
		return false;
	},


	// private method. Drag (move) element.
	_drag : function(e) {
		var oElem = DragHandler._oElem;

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		e = e ? e : window.event;
		oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
		oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';

		oElem.mouseX = e.clientX;
		oElem.mouseY = e.clientY;

		oElem.drag(oElem, x, y);

		return false;
	},


	// private method. Stop drag process.
	_dragEnd : function() {
		var oElem = DragHandler._oElem;

		var x = parseInt(oElem.style.left);
		var y = parseInt(oElem.style.top);

		oElem.dragEnd(oElem, x, y);

		document.onmousemove = null;
		document.onmouseup = null;
		DragHandler._oElem = null;
	}

}

function WorkingNow(checkBox, tr, validatorClientId)
{
    var tr = findObj(tr);
    var input;
    if(checkBox.checked == true)
    {
       input = tr.getElementsByTagName("INPUT");
       input[0].value = '';
       tr.style.display = 'none';
       
       var myVal = document.getElementById(validatorClientId);
       ValidatorEnable(myVal, false); 
       
       //input[0].disabled = 'disabled';
    }
    
    if(checkBox.checked == false)
    {
       input = tr.getElementsByTagName("INPUT");
       input[0].value = '';
       tr.style.display = 'block';
       
         var myVal = document.getElementById(validatorClientId);
       ValidatorEnable(myVal, true);
       
    }
    
}

function onKeyPressValidate (e, control, buttonToFocusOnAndClick)
{
 	var KeyID = (window.event) ? event.keyCode : e.which;
    var ctrl = control;
    
    if(KeyID == 13)
    {
       var focuspocus = document.getElementById(buttonToFocusOnAndClick);
       focuspocus.focus();
       // control.form[(getIndex(control)+1) % control.form.length].focus();
       control.onchange(e);
       return;
    }
    
    //control.onchange(e);
}

 function getIndex(input)
  {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)
    index = i;
    else i++;
    
    return index;
  }


function TriggerValidator (checkBox, reqValClientId, rangeValClientId, compValClientId, textBox, checkBox2)
{
    var txtBox = findObj(textBox);
    var chkBox2 = findObj(checkBox2);
    
    if(checkBox.checked == true)
    {
       txtBox.value = '';
       txtBox.parentNode.style.display = 'none';
       
       var myVal = document.getElementById(reqValClientId);
       ValidatorEnable(myVal, false);
       var myVal2 = document.getElementById(rangeValClientId);
       ValidatorEnable(myVal2, false);
       var myVal3 = document.getElementById(compValClientId);
       ValidatorEnable(myVal3, false);
    }
    
    if(checkBox.checked == false && chkBox2.checked == false)
    {
       txtBox.value = '';
       txtBox.parentNode.style.display = 'block';
       
       var myVal = document.getElementById(reqValClientId);
       ValidatorEnable(myVal, true);
       myVal = document.getElementById(rangeValClientId);
       ValidatorEnable(myVal, true);
       myVal = document.getElementById(compValClientId);
       ValidatorEnable(myVal, true);
    }

}

/*
function ParsePersonalCodeToCalendar (textBox, calendar,validationClientId)
{
    var personalCode = textBox.value;
    var dateOfBirth ;
    
    var val = document.getElementById(validationClientId);
    ValidatorValidate(val);
    var calendarTextBox = document.getElementById(calendar);
    
    if(val.isvalid == true)
    {
      // dateOfBirth = personalCode.substring(1,2);
     if(personalCode.length == 11)
     {
        dateOfBirth = personalCode.substring(5,7)+"." + personalCode.substring(3,5)+"." + "19" + personalCode.substring(1,3);
        var myJSDate = new Date(personalCode[1] + personalCode[2],personalCode[3] + personalCode[4],personalCode[5] + personalCode[6]);
        calendarTextBox.value = dateOfBirth;
        fireEvent(calendarTextBox, 'change');
     }  
    }
}*/

function ParsePersonalCodeToCalendar (textBox, calendar)
{
    var personalCode = textBox.value;
    var dateOfBirth ;
    
    //var val = document.getElementById(validationClientId);
   // ValidatorValidate(val);
    var calendarTextBox = document.getElementById(calendar);
    var re = /^\d{11}$/g;
    
    if(re.test(personalCode) == true)
    {
      // dateOfBirth = personalCode.substring(1,2);
     if(personalCode.length == 11)
     {
        dateOfBirth = personalCode.substring(5,7)+"." + personalCode.substring(3,5)+"." + "19" + personalCode.substring(1,3);
        var myJSDate = new Date(personalCode[1] + personalCode[2],personalCode[3] + personalCode[4],personalCode[5] + personalCode[6]);
        calendarTextBox.value = dateOfBirth;
        fireEvent(calendarTextBox, 'change');
     }  
    }
}

function fireEvent(element, event){
    if (document.createEventObject){
        // dispatch for IE
        var evt = document.createEventObject();
        return element.fireEvent('on'+event,evt)
    }
    else{
        // dispatch for firefox + others
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(event, true, true ); // event type,bubbling,cancelable
        return !element.dispatchEvent(evt);
    }
}


//Checkboxide grupeering<
var portfolioInfo = 1;
var clientInfo = 1;
var experience = 1;
var experienceName = '';
var knowledge = 1;
var educationName = '';
var skills = 1;
var requirements = 1;
var requirementsName = '';
var additionalInfo = 1;
function CheckOrUncheckBoxes(groupType)
{
    var div = findObj(groupType);
    var tempResult;
    
    if(groupType == 'portfolioInfo')
      tempResult = portfolioInfo = portfolioInfo * (-1);
    
    if(groupType == 'clientInfo')
      tempResult = clientInfo = clientInfo * (-1);
    
    if(groupType == experienceName)
      tempResult = experience = experience * (-1);
    
    if(groupType == educationName)
      tempResult = knowledge = knowledge * (-1);
    
    if(groupType == requirementsName)
      tempResult = requirements = requirements * (-1);
    
    if(groupType == 'skills')
      tempResult = skills = skills * (-1);
    
    if(groupType == 'additionalInfo')
      tempResult = additionalInfo = additionalInfo * (-1);
    
    var checkboxes, i;
    checkboxes = div.getElementsByTagName("INPUT");
    for(i in checkboxes)
    {
       if(tempResult<0)
       {
        checkboxes[i].checked = true;
       }
       else
       {
       checkboxes[i].checked = false;
        }
    }
    //requirements are split into 2 div-s, here we check the second one
    if(groupType == requirementsName)
    {
        div = findObj('requirements');
        checkboxes = div.getElementsByTagName("INPUT");
        for(i in checkboxes)
        {
           if(tempResult<0)
           {
            checkboxes[i].checked = true;
           }
           else
           {
           checkboxes[i].checked = false;
            }
        }
    }

}

//AJAX Resize Control
 var resizeFired = false;
    function OnClientResize() {
        resizeFired = true;
    }

function ResizeTextBox(sender, eventArgs)

            {
                var element = sender.get_element();
            //var element = sender.control.element;
           
          var textarea, i;
          textarea = element.getElementsByTagName("TEXTAREA");
          for(i in textarea)
          {
                 if(textarea[i].style)
                  {
                    //textarea[i].style.height = element.style.height;
                    textarea[i].style.height  = (element.style.height.substring(0,element.style.height.length-2) - 25)+ 'px';
                    textarea[i].style.width  = (element.style.width.substring(0,element.style.width.length-2) - 6)+ 'px';
                  }
            }
               // element.childNodes[i].style.height = element.style.height; //index 2 for IE
        
          return false;

            }

function bringFocusToElement(elem)
{
    obj = findObj(elem);
    if(obj != null )
        obj.focus();

}


var lastScrollLeft;
function MoveEditComments(comment, mainDiv)
{  
   
   obj = findObj(mainDiv);
   
   comment = findObj(comment);
   if(comment.style.display != 'none')
   {
   if(lastScrollLeft)
   {
   var delta = obj.scrollLeft - lastScrollLeft;
   comment.style.left = comment.offsetLeft - delta + 'px';
   }
   lastScrollLeft = obj.scrollLeft;
   }
   
   // $find('MyRCEBID').get_element().style.left = obj.class.clientLeft;
   
}

//function ShowDiv(e,element)
//   {
//    var div = findObj(element);
//    if(div.className=='hide' || div.style.display == 'none')
//     {
//     // Get the click location
//     var height = e.clientY + parseInt('5') + 'px';
//     // Get the height inside the clicked image
////     var offsetHeight = parseInt(e.offsetY);
////     height = height - offsetHeight;
//   
//     // Get the click location(width)
//     var width = e.clientX
//     // Get the width inside the clicked image
////     var offsetWidth = parseInt(e.offsetX);
////     width = width - offsetWidth;
//     
//     div.style.position = 'absolute';
//     div.className='showGeneral';
//     
//     // Set the div top to the height
//     div.style.top = parseInt(height) + parseInt('20');

//     // Set the div Left to the height
//     div.style.left = parseInt(width) + parseInt('20');
//     
//     // Show the div layer
//     div.style.display = 'block';
//    }
//    else
//    {
//        div.className='hide';
//    }
//   }


function changeViewGeneral(hide)
{
obj = findObj(hide);
 if (obj)
  {
  	if(obj.className=='hide' || obj.style.display == 'none')
		{
		obj.className='showGeneral';
		obj.style.display = '';
//		obj.style.top = e.clientY + parseInt('5') + 'px';
//		obj.style.left = e.clientX + parseInt('5') + 'px';		
//			if((obj.offsetLeft + obj.clientWidth) > (document.body.clientWidth + document.body.scrollLeft))
//            {
//               obj.style.right="10px";
//            }
//            else
//            {
//                obj.style.right="";
//                if((obj.offsetLeft + obj.clientWidth) > (document.body.clientWidth + document.body.scrollLeft))
//                    obj.style.right="10px";
//            } 		
		}
	else{
		obj.className='hide';
	}
  
  }
}

/*---   SearchFilter filledfields  ---*/

function checkForValue(source, clientside_arguments)
{  
  if(clientside_arguments.Value != '' && clientside_arguments.Value != '0')
     {
        clientside_arguments.IsValid = false;
     }
  else
    {
        clientside_arguments.IsValid = true;
    }
}

function showModalPopupViaClient(divId) {
            var modalPopupBehavior = $find('programmaticModalPopupBehavior');
            ShowHideElement(divId);
            modalPopupBehavior.show();
        }

function showRemindersPopUp()
{
    var modalPopupBehavior = $find('reminderPopupBehavior');
    modalPopupBehavior.show();
}
/*
function TrimString (source, clientside_arguments)
{
    var string = clientside_arguments.Value;
    string = string.replace(/^\s+|\s+$/g, '') ;
    
    var textBox = findObj(source.controltovalidate);
    textBox.value = string;
    
}*/

function TrimString (obj)
{
    var string = obj.value;
    //var string = clientside_arguments.Value;
    string = string.replace(/^\s+|\s+$/g, '') ;
    obj.value = string;
    
    //var textBox = findObj(source.controltovalidate);
    //textBox.value = string;
    
}

/*---- Arvutioskused ----*/
function selectProgram(fromId, hiddenFieldId, programTypeId)
{
    var from = findObj(fromId);
    var hiddenField = findObj(hiddenFieldId);
    
    if(from.options.selectedIndex == -1)
    {
       hiddenField.value = '';
        return;    
        }
    
    var id = from.options[from.options.selectedIndex].value;
    hiddenField.value = id;
    
    var programType = findObj(programTypeId);
    if(programType.options.selectedIndex == -1)
        return;
        
    var typeId = programType.options[programType.options.selectedIndex].value;
    lisa_programmid(from, typeId);
}

function set_progliik_prog(from, toId, text)
{
	var id = from.options[from.options.selectedIndex].value;
	var option;
	
	var to = findObj(toId);
	to.options.length = 0;

	tmp = 0;
	if (text)
	{
		option = new Option("", 0, 0, 0);
		to.options[0] = option;
		tmp = 1;
	}

	update_prog(to, id, tmp);
}

function lisa_programmid(from, id)
{       
	for (i = 0; i < from.length; i++)
	{
		if (from.options[i].selected && from.options[i].value > 0)
		{

				for (t = 0; t < Prog.length; t++)
					if (Prog[t][0] == from.options[i].value)
						Prog[t][3] = 0;
		}

		from.options[i].selected = 0;
	}

	update_prog(from, id, 0);
}

function eemalda_programm(selectedId, programId, programTypeId)
{
    
    for (j = 0; j < Prog.length; j++)
    {
		if (Prog[j][0] == selectedId)
			Prog[j][3] = 1;
	}
	
	var program = findObj(programId);
	var programType = findObj(programTypeId);
	var typeId = programType.options[programType.options.selectedIndex].value;
	
	update_prog(program, typeId, 0);
	return true;
}

function update_prog(to, id, start)
{
	to.options.length = 0;
	i = 0;
	j = 0;
	while(Prog[i])
	{
		if (Prog[i][3] == 1 && Prog[i][1] == id)
		{
			option = new Option(Prog[i][2], Prog[i][0], 0, 0);
			to.options[j+start] = option;
			j++;
		}
		i++;
	}
}


//===========================================================================
// Provides a Dictionary object for client-side java scripts
//===========================================================================

function Lookup(key) 
{
  return(this[key]);
}


function Delete() 
{
  for (c=0; c < Delete.arguments.length; c++) 
  {
    this[Delete.arguments[c]] = null;
  }
  // Adjust the keys (not terribly efficient)
  var keys = new Array()
  for (var i=0; i<this.Keys.length; i++)
  {
    if(this[this.Keys[i]] != null)
      keys[keys.length] = this.Keys[i];
  }
  this.Keys = keys;
}

function Add() 
{
  for (c=0; c < Add.arguments.length; c+=2) 
  {
    // Add the property
    this[Add.arguments[c]] = Add.arguments[c+1];
    // And add it to the keys array
    this.Keys[this.Keys.length] = Add.arguments[c];
  }
}

function Dictionary() 
{
  this.Add = Add;
  this.Lookup = Lookup;
  this.Delete = Delete;
  this.Keys = new Array();
}

