

//open image modal dialog
function openBusinessPartnerImageModalDialog(fileid, businesspartnerid)
{
    //open modal dialog
    openModalDialog();
    
    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxlargeimage.aspx?fileid=" + fileid + "&businesspartnerid=" + businesspartnerid, "modalWindow", "");
}

//open content image modal dialog
function openContentImageModalDialog(fileid, docid) 
{
    //open modal dialog
    openModalDialog();

    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxlargeimage.aspx?fileid=" + fileid + "&docid=" + docid, "modalWindow", "");
}

//open jobad image modal dialog
function openJobAdImageModalDialog(fileid, jobadid) 
{
    //open modal dialog
    openModalDialog();

    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxlargeimage.aspx?fileid=" + fileid + "&jobadid=" + jobadid, "modalWindow", "");
}

//open jobseekercv activate popup
function openActivateJobSeekerCV(jobseekercvid) {
    //open modal dialog
    openModalDialog();

    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxjobseekercvactivate.aspx?jobseekercvid=" + jobseekercvid, "modalWindow", "");
}

//open jobad sendtofriend modal dialog
function openJobAdSendToFriendModalDialog(jobadid, senderemail, sendername, recipientemail, message, validate) 
{
    //open modal dialog
    if (validate == 2) 
    {
        advancedHeight = 460;
        advancedWidth = 611;
        openModalDialog();
        validate = 0;
    }

    //submit form
    if (validate == 1) 
    {
        //set input values
        senderemail = document.getElementById("senderemail").value;
        sendername = escape(document.getElementById("sendername").value);
        recipientemail = document.getElementById("recipientemail").value;
        message = escape(document.getElementById("message").value.substring(0, 1100));
    }

    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxjobadsendtofriend.aspx?jobadid=" + jobadid + "&senderemail=" + senderemail + "&sendername=" + sendername + "&recipientemail=" + recipientemail + "&validate=" + validate + "&message=" + message, "modalWindow", "");
}

//open jobad contact modal dialog
var advancedHeight = 0;
var advancedWidth = 0;
function openJobAdContactModalDialog(jobadid, name, address, phone, email, contactmessage, validate) 
{
    //open modal dialog
    if (validate == 2) 
    {
        advancedHeight = 750;
        advancedWidth = 750;
        openModalDialog();
        validate = 0;
    }

    //submit form
    if (validate == 1) 
    {
        //set input values
        name = document.getElementById("name").value;
        address = escape(document.getElementById("address").value);
        phone = document.getElementById("phone").value;
        email = document.getElementById("email").value;
        contactmessage = escape(document.getElementById("contactmessage").value.substring(0, 1100));
    }
    var contactbyemail="";
    if(document.getElementById("contactbyemail"))
        contactbyemail = document.getElementById("contactbyemail").value;

    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxjobadcontact.aspx?jobadid=" + jobadid + "&name=" + escape(name) + "&address=" + address + "&phone=" + phone + "&email=" + email + "&validate=" + validate + "&contactmessage=" + contactmessage + "&contactbyemail=" + contactbyemail, "modalWindow", "");
}

//open sendtofriend modal dialog
function openSendToFriendModalDialog(senderemail, sendername, recipientemail, message, validate) {
    //open modal dialog
    if (validate == 2) 
    {
        openModalDialog();
        validate = 0;
    }

    //submit form
    if (validate == 1) 
    {
        //set input values
        senderemail = document.getElementById("senderemail").value;
        sendername = escape(document.getElementById("sendername").value);
        recipientemail = document.getElementById("recipientemail").value;
        message = escape(document.getElementById("message").value.substring(0, 1100));
    }

    //set content by ajax request
    sendAjaxRequest(rooturl + "ajaxsendtofriend.aspx?senderemail=" + senderemail + "&sendername=" + sendername + "&recipientemail=" + recipientemail + "&validate=" + validate + "&message=" + message, "modalWindow", "");
}

//open login modal dialog
function openLoginModalDialog(validate) {
    //open modal dialog
    openModalDialog();
    
    var email = '';
    var password = '';
    var autologin = '';
    
    //submit
    if (validate == 3 || validate == 4) {
        email = $('loginmail').value;
        password = $('loginpassword').value;
        autologin = $('loginautologin').checked;
        
        //set values
        $('email').value = email;
        $('password').value = password;
        $('autologin').checked = autologin;
    } 

    //send ajax to validate login - if login validated, then the handler submits form
    sendAjaxRequest(rooturl + "ajaxlogin.aspx?email=" + email + "&password=" + password + "&autologin=" + autologin + "&validate=" + validate, "", "handleResponseLogin");
}

//show login popup or submit
function handleResponseLogin(responseText) {
    //check for returned parameters
    if (responseText == '') {    
        //submit form
        document.getElementById('loginform').submit();
    }
    else {
        //reload popup window
        $('modalWindow').innerHTML = responseText;
    }
}


//open modal dialog
function openModalDialog() {
    //display div modal dialogs
	$('modalWindow').style.display = $('modalBackground').style.display = 'block';

	//special < IE7 -only processing for windowed elements, like select	
	if (window.XMLHttpRequest == null)
	{
	    if ($('hideType') != null) 
	    {
		    var type = $('hideType').value;

		    if (type == 'iframe')
			    $('modalIframe').style.display = 'block';
		    if (type == 'replace')
			    ReplaceSelectsWithSpans();
		}
	}

	//call once to center everything
	OnWindowResize();
	
	//hook events
	if (window.attachEvent)
		window.attachEvent('onresize', OnWindowResize);
	else if (window.addEventListener)
		window.addEventListener('resize', OnWindowResize, false);
	else
		window.onresize = OnWindowResize;
	
	// we won't bother with using javascript in CSS to take care keeping the window centered
	if (document.all)
		document.documentElement.onscroll = OnWindowResize;
}

//event: on window resize
function OnWindowResize() {
	//we only need to move the dialog based on scroll position if
	//we're using a browser that doesn't support position: fixed, like < IE 7
	var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
	var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
	var div = $('modalWindow');

	//set current position
	if (advancedHeight == 0 && advancedWidth == 0) 
	{
	    div.style.left = Math.max((left + (GetWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
	    div.style.top = Math.max((top + (GetWindowHeight() - div.offsetHeight) / 2), 0) + 'px';
	}
	else 
	{
	    if (GetWindowHeight() < advancedHeight) 
	    {
	        div.style.overflowY = "scroll";
	        div.style.width = advancedWidth + 20 + "px";
	        div.style.height = GetWindowHeight() + "px";
	    }
	    else 
	    {
	        div.style.overflowY = "hidden";
	        div.style.width = advancedWidth + "px";
	        div.style.height = advancedHeight + "px";
	    }
	    div.style.left = Math.max((left + (GetWindowWidth() - advancedWidth) / 2), 0) + 'px';
	    div.style.top = Math.max((top + (GetWindowHeight() - advancedHeight) / 2), 0) + 'px';
	}
}

//event: on window close
function OnModalWindowClose()
{
	$('modalWindow').style.display = $('modalBackground').style.display = 'none';
	
	// special IE-only processing for windowed elements, like select	
	if (document.all)
	{
	    if ($('hideType') != null) 
	    {
		    var type = $('hideType').value;
		    alert("OK");
		    if (type == 'iframe')
			    $('modalIframe').style.display = 'none';
    		
		    if (type == 'replace')
			    RemoveSelectSpans();
		}
	}
	
	if (window.detachEvent)
		window.detachEvent('onresize', OnWindowResize);
	else if (window.removeEventListener)
		window.removeEventListener('resize', OnWindowResize, false);
	else
		window.onresize = null;
	
}

/* These functions deal with IE's retardedness in not allowing divs to 
 * cover select elements by replacing the select elements with spans */

//IE: remove select spans
function RemoveSelectSpans()
{
	var selects = document.getElementsByTagName('select');
	
	for (var i = 0; i < selects.length; i++)
	{
		var select = selects[i];
		
		if (select.clientWidth == 0 || select.clientHeight == 0 || 
			select.nextSibling == null || select.nextSibling.className != 'selectReplacement')
		{
			continue;
		}
			
		select.parentNode.removeChild(select.nextSibling);
		select.style.display = select.cachedDisplay;
	}
}

//IE: replace selects with spans
function ReplaceSelectsWithSpans()
{
	var selects = document.getElementsByTagName('select');
	
	for (var i = 0; i < selects.length; i++)
	{
		var select = selects[i];
		
		if (select.clientWidth == 0 || select.clientHeight == 0 || 
			select.nextSibling == null || select.nextSibling.className == 'selectReplacement')
		{
			continue;
		}
			
		var span = document.createElement('span');
		
		// this would be "- 3", but for that appears to shift the block that contains the span 
		//   one pixel down; instead we tolerate the span being 1px shorter than the select
		span.style.height = (select.clientHeight - 4) + 'px';
		span.style.width = (select.clientWidth - 6) + 'px';
		span.style.display = 'inline-block';
		span.style.border = '1px solid rgb(200, 210, 230)';
		span.style.padding = '1px 0 0 4px';
		span.style.fontFamily = 'Arial';
		span.style.fontSize = 'smaller';
		span.style.position = 'relative';
		span.style.top = '1px';
		span.className = 'selectReplacement';
		
		span.innerHTML = select.options[select.selectedIndex].innerHTML + 
			'<img src="custom_drop.gif" alt="drop down" style="position: absolute; right: 1px; top: 1px;" />';
		
		select.cachedDisplay = select.style.display;
		select.style.display = 'none';
		select.parentNode.insertBefore(span, select.nextSibling);
	}
}

/* utiltiy functions */

//tools: get window width
function GetWindowWidth()
{
    //calculate width
	var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;
	
	//return
	return width;
}

//tools: get window height
function GetWindowHeight()
{
    //calculate height
    var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
  		0;
  		
  	//return
  	return height;
}

//tools: get element by id
function $(id)
{
    //return
	return document.getElementById(id);
}
