var submitted = false;

function de(id) {
	return document.getElementById(id);
}

function AddResume()
{
	if ( submitted == false ) {
		document.getElementById('join_form').submit();
		submitted = true; 
	}
}

function removeMyResume()
{
	document.getElementById('remove_resume_reason_form').submit();
}

function checkResumeForm()
{
	var valid = true;
	
	if(!document.getElementById(resume_field).value) {
		document.getElementById('ResumeFile_error').innerHTML = resume_error;
		valid = false;
	}
	if(!document.getElementById(agree_terms_field).checked) {
		document.getElementById('AgreeTerms_error').innerHTML = agree_terms_error;
		valid = false;
	}
	
	if(valid) {
		AddResume();
	}
}

function process_field_focus(input_field, field_name) {
	if(document.getElementById(field_name+'_hint')) {
		document.getElementById(field_name+'_hint').style.display='';
	}
	if(document.getElementById(field_name+'_error')) {
		document.getElementById(field_name+'_error').innerHTML='';
	}
	if(input_field) {
		if(input_field.className == 'form-input-text-error') {
			input_field.className = 'form-input-text';
		}
		if(input_field.style.backgroundColor && input_field.style.backgroundColor != '#ffffff') {
			input_field.style.backgroundColor = '#ffffff';
		}
	}
	return true;
}

function process_field_blur(input_field, field_name) {
	if(document.getElementById(field_name+'_hint')) {
		document.getElementById(field_name+'_hint').style.display='none';
	}
}

function isset(variable)
{
	if(variable==null) return false;
	return (typeof(variable)=='undefined')?false:true;
}

function addLoadEvent(func, wnd) {
	if (!wnd) wnd = window
  var oldonload = wnd.onload;
  if (typeof wnd.onload != 'function') {
    wnd.onload = func;
  } else {
    wnd.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function GetWindowHeight() {
	var currWinHeight;
		
//	if (document.body.clientHeight) {
//		currWinHeight = document.body.clientHeight;	

	if (window.innerHeight) {//FireFox with correction for status bar at bottom of window
		currWinHeight = window.innerHeight;
	} else if (document.documentElement.clientHeight) {//IE 7 with correction for address bar
		currWinHeight = document.documentElement.clientHeight;
	} else if (document.body.offsetHeight) {//IE 4+
		currWinHeight = document.body.offsetHeight + 10;
	}
	return currWinHeight - 10; // 10 - horizontal scrollbar height
}

function GetPageHeight()
{
	var iebody = (document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
	var pageTop = document.all? iebody.scrollTop : pageYOffset
	return pageTop + GetWindowHeight();
}

function update_checkbox(cb, cb_hidden)
{
	cb_hidden.value = cb.checked ? 1 : 0;
}

						
function showBlock(block_id){
if (document.getElementById(block_id).style.display == 'none') {
	document.getElementById(block_id).style.display = '';
} else {document.getElementById(block_id).style.display = 'none';}}



function is_msie()
{
	var browser = "";
	browser = navigator.appName;
	if (browser == 'Microsoft Internet Explorer')
		return 1;
	else
		return 0;
}


function SetClipboard(copyText)
{
	if (is_msie())
	{
		if(window.clipboardData)
		{
			// IE send-to-clipboard method.
			window.clipboardData.setData('Text', copyText);
		}
		else if (window.netscape)
		{
			// You have to sign the code to enable this or allow the action in about:config by changing user_pref("signed.applets.codebase_principal_support", true);
			
			try
	  		{
				netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
			} catch(err)
			{
				return SetClipboard2(copyText);
	  		}
	
			// Store support string in an object.
			var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
			if (!str) return false;
			
			str.data=copyText;
	
			// Make transferable.
			var trans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
			if (!trans) return false;
	
			// Specify what datatypes we want to obtain, which is text in this case.
			trans.addDataFlavor("text/unicode");
			trans.setTransferData("text/unicode",str,copyText.length*2);
	
			var clipid=Components.interfaces.nsIClipboard;
			var clip = Components.classes["@mozilla.org/widget/clipboard;1"].getService(clipid);
			if (!clip) return false;
	
			clip.setData(trans,null,clipid.kGlobalClipboard);
		}
	} else {
		SetClipboard3(copyText);
	}
	close_PopUp();
}

function SetClipboard3(text) {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
}

// for FF 1.5 only
function SetClipboard2(text){
	var url = [
		'data:text/html;charset=utf-8;base64,PGJvZHk+PC9ib2',
		'R5PjxzY3JpcHQgdHlwZT0idGV4dC9qYXZhc2NyaXB0Ij4KKGZ1',
		'bmN0aW9uKGVuY29kZWQpe3ZhciBzd2ZfZGF0YSA9IFsKICdkYX',
		'RhOmFwcGxpY2F0aW9uL3gtc2hvY2t3YXZlLWZsYXNoO2Jhc2U2',
		'NCxRMWRUQjJ3JywKICdBQUFCNG5EUGdZbGpBd01qSTRNejAlMk',
		'YlMkY5JTJGZTJaZkJnYUdhV3dNRE1uNUthJywKICdrTU10TjRH',
		'ZGdaZ1NJTXdaWEZKYW01UUFFJTJCQm9iaTFCTG5uTXlDcFB6RW',
		'9oU0dJJywKICdQRnAlMkZBeHNEREJRa3BGWkRGUUZGQ2d1eVM4',
		'QXlqSTRBRVVCaXkwVndBJTNEJTNEJwpdLmpvaW4oIiIpOwpkb2',
		'N1bWVudC5ib2R5LmlubmVySFRNTCA9IFsKICc8ZW1iZWQgc3Jj',
		'PSInLHN3Zl9kYXRhLCciICcsCiAnRmxhc2hWYXJzPSJjb2RlPS',
		'csZW5jb2RlZCwnIj4nLAogJzwvZW1iZWQ+JwpdLmpvaW4oIiIp',
		'Owp9KSgi',
		base64encode( encodeURIComponent(text) + '")</'+'script>')
	].join("");
	var tmp = document.createElement("div");
	tmp.innerHTML = [
		 '<iframe src="',url,'"'
		,' width="0" height="0">'
		,'</iframe>'
	].join("");
	with(tmp.style){
		position ="absolute";
		left = "-10px";
		top  = "-10px";
		visibility = "hidden";
	};
	document.body.appendChild(tmp);
	setTimeout(function(){document.body.removeChild(tmp)},1000);
	function base64encode(str){
		var Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
		var c1, c2, c3;
		var buf = [];
		var len = str.length;
		var i = 0;
		while(i < len){
			c1 = str.charCodeAt(i) & 0xff;
			c2 = str.charCodeAt(i+1);
			c3 = str.charCodeAt(i+2);
			buf.push(Chars[(c1 >> 2)]);
			if(i+1 == len){
				buf.push(Chars[(c1 & 0x3) << 4],"==");
				break;
			}
			buf.push(Chars[((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)]);
			if(i+2 == len){
				buf.push(Chars[(c2 & 0xF) << 2],"=");
				break;
			}
			buf.push(
				Chars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)],
				Chars[(c3 & 0x3F)]
			);
			i+=3;
		}
		return buf.join("")
	}
}

function position_Top() {return typeof window.pageYOffset != 'undefined' ? window.pageYOffset:document.documentElement && document.documentElement.scrollTop? document.documentElement.scrollTop: document.body.scrollTop?document.body.scrollTop:0;}

function positionPopOver(width, height, target_id, source_id, dont_process_div1) {
	var div_id = 'form_div';
	if (source_id) div_id = source_id;
	
	if (!dont_process_div1) {
		document.getElementById(div_id).style.left = 0;
		document.getElementById(div_id).style.top = 0;
		document.getElementById(div_id).style.height = (GetPageHeight() + 20) + 'px';
	}
	
	if (!document.all) {
		var $winW = window.innerWidth;
		var $winH = window.innerHeight;
	}
	else {
		var $winW = window.document.body.offsetWidth;
		var $winH = window.document.body.offsetHeight;
	}
	
	var left = Math.round(($winW - width)/2)+'px';
	var top = Math.round(position_Top() + ($winH / 2) - height) + 'px';
	
	document.getElementById(target_id).style.top = top;
	document.getElementById(target_id).style.left = left;
	document.getElementById(target_id).style.display = 'block';
}

var $form_name = 'kernel_form';

// if the field does not exist - creates it
function set_hidden_field($field_id, $value)
{
	var $kf = document.getElementById($form_name);
	var $field = $kf.elements[$field_id];
	if ($field) {
		if ($value === null) {
			$kf.removeChild($field);
		}
		else {
			$field.value = $value;
		}
		return true;
	}

	$field = document.createElement('INPUT');
	$field.type = 'hidden';
	$field.name = $field_id;
	$field.id = $field_id;
	$field.value = $value;

	$kf.appendChild($field);
	return false;
}

function submit_event(prefix_special, event, t, form_action)
{
	if (event) {
		set_hidden_field('events[' + prefix_special + ']', event);
	}

	if (form_action) {
		var old_env = '';
		if (!form_action.match(/\?/)) {
			document.getElementById($form_name).action.match(/.*(\?.*)/);
			old_env = RegExp.$1;
		}
		document.getElementById($form_name).action = form_action + old_env;
	}
	submit_kernel_form();
}
	
function submit_kernel_form()
{
	if (submitted) return;

	submitted = true;

	var $form = document.getElementById($form_name);
	if (typeof $form.onsubmit == "function") {
		$form.onsubmit();
	}

	$form.submit();
	$form.target = '';
	window.setTimeout(function() {submitted = false}, 500);
}

function update_checkbox_options($cb_mask, $hidden_id)
{
	var $kf = document.getElementById($form_name);
	var $tmp = '';
	for (var i = 0; i < $kf.elements.length; i++)
	{
		if ( $kf.elements[i].id.match($cb_mask) )
		{
			if ($kf.elements[i].checked) $tmp += '|'+$kf.elements[i].value;
		}
	}
	if($tmp.length > 0) $tmp += '|';
	document.getElementById($hidden_id).value = $tmp.replace(/,$/, '');
}

function imposeMaxLength(Object, Event, MaxLen)
{
	var k = Event.keyCode;
	if (k==8 || k==37 || k==38 || k==39 || k==40 || k==46) return true;
	return (Object.value.length <= MaxLen);
}

function success_populate_states(request)
{
	var states = request.responseText;
	var state_field = 'searchparams_State';
	document.getElementById(state_field).options.length = 0;
	states = states.split('|');

	var option = new Option('', '');
	try {
		document.getElementById(state_field).add(option, null);
	}
	catch(e) {
		document.getElementById(state_field).add(option, -1);
	}
	for (i=0;i<states.length;i++) {

		var selected_item = false;
		if (states[i].substring(0,3) == selected_state) {
			var selected_item = true;
		}
		var option = new Option(states[i].substring(4), states[i].substring(0,3));
		option.selected = selected_item;

		try {
			document.getElementById(state_field).add(option, null);
		}
		catch(e) {
			document.getElementById(state_field).add(option, -1);
		}
	}
}

function search_populate_states($url) {
//	search_populate_states('<inp2:m_Link job_event="OnSelectCountry" pass="m,job" no_amp="1"/>');
	var country = document.getElementById('searchparams_SearchCountry');
	var	c_index = country.options[country.selectedIndex].value;

	if (c_index == 225 || c_index == 38 || c_index == 224 || c_index == 104 || c_index == 100 || c_index == 13) {
		document.getElementById('searchparams_State').style.display = 'inline';
	document.getElementById('searchparams_CustomState').style.display = 'none';
	document.getElementById('searchparams_CustomState').value = '';
//			document.getElementById('state_req').style.display = 'block';

	$url += '&c_index=' + c_index;
	Request.makeRequest($url, $busyRequest, '', success_populate_states, error, '', this);
	}
else {
	document.getElementById('searchparams_State').style.display = 'none';
	document.getElementById('searchparams_State').value = '';
	document.getElementById('searchparams_CustomState').style.display = 'inline';
//			document.getElementById('state_req').style.display = 'none';
}

// if not USA, then disable zip and proximity fields
	if (c_index == 225) {
	document.getElementById('searchparams_Distance').disabled = 0;
	} else {
	document.getElementById('searchparams_Distance').disabled = 1;
	}		
}

function dosearch()
{
	if (
		(document.search_form.search_keyword.value == '')
		&& (document.search_form.search_type.value == 0)
	)	
	{
		return;
	}
	$form_name = 'search_form';
	submit_event('custom-sections', 'OnKeywordSearch');
}

function addEvent(el, evname, func, traditional) {
	if (traditional) {
		eval('el.on'+evname+'='+func);
		return;
	}

	if (is.ie) {
		el.attachEvent("on" + evname, func);
	} else {
		el.addEventListener(evname, func, true);
	}
};

function addEvent(el, evname, func) {
	if (document.all) {
		el.attachEvent("on" + evname, func);
	} else {
		el.addEventListener(evname, func, true);
	}
}

function findPos(obj, with_scroll) {
	if (!with_scroll) var with_scroll = false;
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft - (with_scroll ? obj.scrollLeft : 0)
		curtop = obj.offsetTop - (with_scroll ? obj.scrollTop : 0)
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft - (with_scroll ? obj.scrollLeft : 0)
			curtop += obj.offsetTop - (with_scroll ? obj.scrollTop : 0)
		}
	}
	return [curleft,curtop];
}

function getDimensions(obj) {
	var style
	if (obj.currentStyle) {
		style = obj.currentStyle;
	}
	else {
		style = getComputedStyle(obj,'');
	}
	padding = [parseInt(style.paddingTop), parseInt(style.paddingRight), parseInt(style.paddingBottom), parseInt(style.paddingLeft)]
	border = [parseInt(style.borderTopWidth), parseInt(style.borderRightWidth), parseInt(style.borderBottomWidth), parseInt(style.borderLeftWidth)]
	for (var i in padding)  if ( isNaN( padding[i] ) ) padding[i] = 0
	for (var i in border)  if ( isNaN( border[i] ) ) border[i] = 0

	var result = new Object();
	result.innerHeight = obj.clientHeight - padding[0] - padding[2];
	result.innerWidth = obj.clientWidth - padding[1] - padding[3];
	result.padding = padding;
	result.borders = border;

	result.outerHeight = obj.clientHeight + border[0] + border[2];
	result.outerWidth = obj.clientHeight + border[1] + border[3];

	return result;
}

function addElement($dst_element, $tag_name) {
	var $new_element = document.createElement($tag_name.toUpperCase());
	$dst_element.appendChild($new_element);
	return $new_element;
}


function popup(url,name,width,height) {
	var scr_w = screen.width;
	var scr_h = screen.height;
	var left = ( scr_w - width ) / 2;
	var top =( scr_h - height ) / 2;
	var win = window.open(url,name,'height='+height+',width='+width+',top='+top+',left='+left+',scrollbars=yes');
	win.focus();
}
