

var agent = navigator.userAgent.toLowerCase();
var ns = ((agent.indexOf('mozilla')!=-1) && ((agent.indexOf('spoofer')==-1) && (agent.indexOf('compatible') == -1)));
var ie = (agent.indexOf("msie") != -1);
var pc = (agent.indexOf("win") != -1);


var day_names_en = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
var month_names_en = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var month_size = new Array(31,28,31,30,31,30,31,31,30,31,30,31);



function confirm_click(l, q){ // link object, question message
	var is_confirmed = confirm(q);
	if(is_confirmed){ l.href += '&is_js_confirmed=1';}
	return is_confirmed;
}



function sleep(sec){
	sleep_time_start = new Date();
	sec = sec ? sec : 1;
	if(sec > 5){ alert("We don't recommend sleeping for more of 5 sec!"); return false;}
	while(sec){
		for(var iii = 0; iii < 640; iii++){
			aiii = 2.34;
			biii = a * a;
		}
		sleep_time_stop = new Date();
		if(sleep_time_stop - sleep_time_start > sec * 1000){ sec = 0; break;}
	}
}



// set_cookie("name_of_cookie", "value_of_cookie", 30, "/", ".domain.com")
function set_cookie(name,value,expires,path,domain,secure){
	if(expires && typeof(expires) == "number"){
		// expires in days;
		var expdate = new Date ();
		expires = expdate.setTime(expdate.getTime() + (24 * 60 * 60 * expires));
	}
	document.cookie = name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toUTCString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}

function get_cookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var offset = dc.indexOf("; " + prefix);
	if(offset == -1){
		offset = dc.indexOf(prefix);
		if(offset != 0) return '';
	}else offset += 2;
	var end = dc.indexOf(";", offset);
	if(end == -1) end = dc.length;
	return unescape(dc.substring(offset + prefix.length, end));
}


function raw_ip(ip){
	var ip_ = ip.split(".");
	return (ip_[0] << 24) | (ip_[1] << 16) | (ip_[2] << 8) | ip_[3];
}

function parse_query(str){
	str = str ? str : location.search;
	var query = str.charAt(0) == '?' ? str.substring(1) : str;
	var args = new Object();
	if(query){
		var fields = query.split('&');
		for(var f = 0; f < fields.length; f++){
			var field = fields[f].split('=');
			args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
		}
	}
	return args;
}



function get_debug_info(){
	var html = "";
	//scroll(10000,100000);
	html += "Desktop available: " + screen.availWidth + "x" + screen.availHeight + "@" + screen.colorDepth + "<br>\n";
	html += "Desktop total(with task bar): " + screen.width + "x" + screen.height + "<br>\n";

	//html += "Window inner: " + window.innerWidth + "x" + window.innerHeight + "<br>\n";
	//html += "Window outer: " + window.outerWidth + "x" + window.outerHeight + "<br>\n";
	//html += "Current page position: " + window.pageXOffset + "x" + window.pageYOffset + "<br>\n";
	//html += "Document total: " + (window.innerWidth + window.pageXOffset) + "x" + (window.innerHeight + window.pageYOffset) + "<br>\n";


	tb_w=screen.width - screen.availWidth;
	tb_h=screen.height - screen.availHeight;
	if(tb_w > 0){ html += "Taskbar is vertical. Size: " + tb_w + "x" + screen.availHeight + "<br>\n";}
	if(tb_h > 0){ html += "Taskbar is horizontal. Size: " + screen.availWidth + "x" + tb_h + "<br>\n";}

	//scroll(0,0);


	//html += "<a href=\"#\" onClick=\"self.moveTo(0,0);self.resizeTo(640,480);self.moveTo((screen.availWidth-640)/2,(screen.availHeight-480)/2);self.focus();return false;\">640x480</a>&nbsp;\n";
	html += "<a href=\"#\" onClick=\"self.moveTo(0,0);self.resizeTo(800,600);self.moveTo((screen.availWidth-800)/2,(screen.availHeight-600)/2);self.focus();return false;\">800x600</a>&nbsp;\n";
	html += "<a href=\"#\" onClick=\"self.moveTo(0,0);self.resizeTo(1024,768);self.moveTo((screen.availWidth-1024)/2,(screen.availHeight-768)/2);self.focus();return false;\">1024x768</a>&nbsp;\n";
	html += "<a href=\"#\" onClick=\"self.moveTo(0,0);self.resizeTo(1152,864);self.moveTo((screen.availWidth-1152)/2,(screen.availHeight-864)/2);self.focus();return false;\">1152x864</a>&nbsp;\n";
	html += "<a href=\"#\" onClick=\"self.moveTo(-1,-1);self.resizeTo(screen.availWidth+4,screen.availHeight+4);self.focus();return false;\">Max</a><br>\n";

	html += "<a href=\"#\" >" + document.referrer + "</a><br>\n";
	html += "" + document.cookie + "<br>\n";

	return html;
}


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

// write in id
//function w(id, m){ document.getElementById(id).innerHTML += m;}

function w(id, m){
	if(document.getElementById(id)){
		if(m){ document.getElementById(id).innerHTML += m;}
		else{ document.getElementById(id).innerHTML = "";}
	}
}

function wd(id, m){
	if(document.getElementById(id)){
		if(m){
			document.getElementById(id).innerHTML = m;
			document.getElementById(id).style.display = 'block';
		}else{
			document.getElementById(id).innerHTML = "";
			document.getElementById(id).style.display = 'none';
		}
	}
}


function weff(cn, m){
	if(document.getElementById(cn)){ var cno = document.getElementById(cn);}
	else{ alert("Can not find form field " + cn); return false;}

	//if(m && !cno.is_error){
	if(m){
		//cno.is_error = true;
		cno.className = "input_field_error";
		cno.title = m;

		if(document.getElementById(cn + "_label")){
			document.getElementById(cn + "_label").className = "label_error";
			document.getElementById(cn + "_label").title = m;
		}

		if(document.getElementById(cn + "_error_text")){
			document.getElementById(cn + "_error_text").innerHTML = m;
			document.getElementById(cn + "_error_text").style.display = 'block';
		}
	}else{// if(!m && cno.is_error){
		//cno.is_error = false;
		cno.className = "input_field";
		cno.title = "";

		if(document.getElementById(cn + "_label")){
			document.getElementById(cn + "_label").className = "";
			document.getElementById(cn + "_label").title = "";
		}

		if(document.getElementById(cn + "_error_text")){
			document.getElementById(cn + "_error_text").innerHTML = "";
			document.getElementById(cn + "_error_text").style.display = 'none';
		}
	}
}



function weff1(cn, m){
	//if(document.getElementById(cn)){ var cno = document.getElementById(cn);}
	//else{ alert("Can not find form field " + cn); return false;}

	//if(m && !cno.is_error){
	if(m){
		//cno.is_error = true;
		//cno.className = "input_field_error";
		//cno.title = m;

		if(document.getElementById(cn + "_label")){
			document.getElementById(cn + "_label").className = "label_error";
			document.getElementById(cn + "_label").title = m;
		}

		if(document.getElementById(cn + "_error_text")){
			document.getElementById(cn + "_error_text").innerHTML = m;
			document.getElementById(cn + "_error_text").style.display = 'block';
		}
	}else{// if(!m && cno.is_error){
		//cno.is_error = false;
		//cno.className = "input_field";
		//cno.title = "";

		if(document.getElementById(cn + "_label")){
			document.getElementById(cn + "_label").className = "";
			document.getElementById(cn + "_label").title = "";
		}

		if(document.getElementById(cn + "_error_text")){
			document.getElementById(cn + "_error_text").innerHTML = "";
			document.getElementById(cn + "_error_text").style.display = 'none';
		}
	}
}




var overchar_count = 0;

function char_check( field, field_left, max_char){
	var left = max_char - field.value.length;
	if(left != field_left.value){
		if(left < 0){
			if(field_left.value >= 0){
				field_left.style.color = "#CC0000";
				overchar_count ++;
				if(overchar_count == 1){
					alert("You are over limit of characters.\nCharacters over limit is an negative red integer.");
				}
			}
		}else{
			if(field_left.value < 0){field_left.style.color = "#336699";}
		}
		field_left.value = left;
	}
}


// is checked radio button ( field_name.radio_button)
function is_cr(r){
	var cc = -1;
	for(var i = 0; i< r.length; i++) if(r[i].checked) cc = i;
	if(cc == -1)return false;
	else return true;
}


// check value( form_field, what)
function cv(ff, what_type){ //number
	switch(what_type){ // what
		case "d": //digit
			re = /[^\d]/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			break;
		case "n": //number
			re = /[^\d\-\.]/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			re = /^-0+/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "-");}
			break;
		case "u": //integer unsigned
			re = /[^\d]/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			re = /^0+/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			break;
		case "s": //integer signed
			re = /[^\d\-]/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			re = /^-0+/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "-");}
			break;
		case "b": // blank; spaces
			re = / +/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, " ");}
			re = /^ /g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			re = / $/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			break;
		case "l": // letters (no spaces)
			re = / +/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			break;
		case "e": //email
			if(ff.value){
				if(/^.{1,64}\@[\w\d\.\-]{1,62}\.\w{2,4}$/.test(ff.value)){return true;}
				else{
					//alert('Please enter valid email.');
					//ff.focus();
					return false;
				}
			}
			break;
		case "r": //resource (url)
			if(ff.value){
				if(/^(http:\/\/)?([\w\d\.\-]{2,61}\.\w{2,4}(\/.{1,200})?)$/.test(ff.value)){return true;}
				else{
					//alert('Please enter valid email.');
					//ff.focus();
					return false;
				}
			}
			break;
		case "t": //text
			re = /\"/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "'");}
			re = /</g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "&lt;");}
			break;
		case "h": //html
			re = /\s+/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, " ");}
			re = /^ /g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			re = / $/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			re = /\"/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "'");}
			break;
		case "c": //credit card
			re = /[^\d]/g;
			if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
			return validate_cc(ff.value);
			break;
	}
	return true;
}



function gv(n){
	var v = "";
	if(typeof n == "string"){
		if(document.getElementById(n)){
			ff = document.getElementById(n);
		//}else if(document.getElementByName(n)){
		//	ff = document.getElementByName(n);
		}else{
			alert("can not find form field " + n);
			return "";
		}
	}else if(typeof n == "object"){
		ff = n;
	}else{
		alert("Please specify parameter (name, id or object of form field).\nCurrent parameter is: " + n);
		return "";
	}

	if(typeof ff.type == "string"){
		if(ff.type == "text" || ff.type == "password" || ff.type == "hidden" || ff.type == "textarea" || ff.type == "submit" || ff.type == "reset" || ff.type == "button"){
			v = ff.value;
		}else if(ff.type == "select-one"){
			if(ff.selectedIndex > -1){
				v = ff.options[ff.selectedIndex].value;
			}
		}else if(ff.type == "select-muiltiple"){
			/*
			var selVals = new Array( );
			for(var i = 0; i < ff.length; i++){
			    if(ff.options[i].selected){
			        selVals[selVals.length] = ff.options[i].value;
			    }
			}
			*/

			for(var i = 0; i < ff.length; i++){
			    if(ff.options[i].selected){ v += (v?", ":"") + ff.options[i].value;}
			}
		}else if(ff.type == "checkbox"){
			if(ff.checked){
				v = ff.value;
			}
		}else if(ff.type == "radio"){
			for(var i = 0; i < ff.length; i++){
				if(ff[i].checked){ v = ff[i].value; break;}
			}
		}else if(ff.type == "file" || ff.type == "image"){
			v = ff.value;
		}else{
			//
			alert("Unsupported form field type " + ff.type);
		}
	}

	return v;
}



function f_get_next(ff){
	var current_input = -1;
	for(var i = 0; i < ff.form.length; i++){
		//alert(i+" "+ff.form[i].name+" "+ff.form[i].type+" "+ff.form[i].value+" "+ff.form[i].disabled+" "+ff.form[i].readonly);
		if(current_input == -1){
			if(ff.form[i] == ff){ current_input = i;}
		}else{
			if(ff.form[i].type == "text" || ff.form[i].type == "textarea" || ff.form[i].type == "select-one"){ // && !ff.form[i].disabled && !ff.form[i].readonly){
				return i;
			}
		}
	}
	return -1;
}

function next_field(ff,len){
	if(!len || ff.value.length >= len){
		next_input = f_get_next(ff);
		if(next_input >= 0){
			ff.form[next_input % ff.form.length].focus();
			if(ff.form[next_input % ff.form.length].type == "text" ||
			   ff.form[next_input % ff.form.length].type == "textarea"){
				ff.form[next_input % ff.form.length].select();
			}
		}
	}
	return true;
}

function make_dolars(ff){
	re = /[^\d]/g;
	if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
	re = /^0+/g;
	if(re.test(ff.value)){ff.value = ff.value.replace( re, "");}
	if(ff.value > 0){
		if(ff.value.length > 6){ ff.value = '$' + ff.value.substr(0, ff.value.length - 6) + ',' + ff.value.substr(ff.value.length - 6, 3) + ',' + ff.value.substr(ff.value.length - 3);}
		else if(ff.value.length > 3){ ff.value = '$' + ff.value.substr(0, ff.value.length - 3) + ',' + ff.value.substr(ff.value.length - 3);}
		else{ ff.value = '$' + ff.value;}
	}

}


// show who invalid characters has used
function Validate_String(string, return_invalid_chars){
	valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	invalid_chars = '';

	if(string == null || string == '') return(true);

	//For every character on the string.
	for(index = 0; index < string.length; index++){
		char_ = string.substr(index, 1);

		//Is it a valid character?
		if(valid_chars.indexOf(char_) == -1){
		//If not, is it already on the list of invalid characters?
			if(invalid_chars.indexOf(char_) == -1){
				//If it's not, add it.
				if(invalid_chars == '') invalid_chars += char_;
				else invalid_chars += ', ' + char_;
			}
		}
	}

	//If the string does not contain invalid characters, the function will return true.
	//If it does, it will either return false or a list of the invalid characters used
	//in the string, depending on the value of the second parameter.
	if(return_invalid_chars == true && invalid_chars != ''){
		last_comma = invalid_chars.lastIndexOf(',');
		if(last_comma != -1){
			invalid_chars = invalid_chars.substr(0, $last_comma) + ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
		}
		return(invalid_chars);
	}else return(invalid_chars == '');
}


function validate_cc(s){
	var v = "0123456789";
	var w = "";
	for(var i=0; i < s.length; i++){
		x = s.charAt(i);
		if(v.indexOf(x,0) != -1){ w += x;}
	}

	var j = w.length / 2;
	if(j < 6.5 || j > 8 || j == 7){ return false;}
	var k = Math.floor(j);
	var m = Math.ceil(j) - k;
	var c = 0;

	for(var i=0; i<k; i++){
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}

	for(var i=0; i<k+m; i++){ c += w.charAt(i*2+1-m) * 1;}

	return (c%10 == 0);
}

function testCookies(){
	if(document.cookie.indexOf('PHPSESS') < 0) window.location.replace("nocookie_no_session.html");
}


function onchange_category(ff){
	var old_r = 0;

	if(ff.selectedIndex > -1){
		v = ff.options[ff.selectedIndex].value;
	}

	var rff = document.df_form.regionid;

	if(rff.selectedIndex > -1){
		old_r = rff.options[rff.selectedIndex].value;
	}

	rff.length = 0;

	if(regions_publisheds[v] !== undefined){
		j = 0;

		//rff.length = j+1;
		//rff.options[j].value = "0";
		//rff.options[j].text = " - ";
		//j++;

		for(var i = 0; i < regions_publisheds[v].length; i++){
			//if(regions_publisheds[v][i] == "1"){//alert("zhc"+regions_names[v][i]);
				rff.length = j+1;
				rff.options[j].value = regions_ids[v][i];
				rff.options[j].text = regions_names[v][i];

				if(old_r == regions_ids[v][i]){
					rff.options[j].selected = true;
				}

				j++;
			//}
		}
	}else{
		j = 0;
		rff.length = j+1;
		rff.options[j].value = "0";
		rff.options[j].text = " - ";
	}
}

function onchange_category_all(ff){
	var old_r = 0;

	if(ff.selectedIndex > -1){
		v = ff.options[ff.selectedIndex].value;
	}

	var rff = document.df_form.regionid;

	if(rff.selectedIndex > -1){
		old_r = rff.options[rff.selectedIndex].value;
	}

	rff.length = 0;

	if(regions_ids[v] !== undefined){
		j = 0;

		rff.length = j+1;
		rff.options[j].value = "0";
		rff.options[j].text = " - ";
		j++;

		for(var i = 0; i < regions_ids[v].length; i++){
			//if(regions_ids[v][i] == "1"){//alert("zhc"+regions_names[v][i]);
				rff.length = j+1;
				rff.options[j].value = regions_ids[v][i];
				rff.options[j].text = regions_names[v][i];

				if(old_r == regions_ids[v][i]){
					rff.options[j].selected = true;
				}

				j++;
			//}
		}
	}else{
		j = 0;
		rff.length = j+1;
		rff.options[j].value = "0";
		rff.options[j].text = " - ";
	}
}

function make_preview(){
	var h = "";
	var f = document.df_form;
	var w = window.open('','preview_window','width=800, height=500, resizable=yes, scrollbars=yes');
	w.document.open();
	odescription = FCKeditorAPI.GetInstance('description');
	oqualifications = FCKeditorAPI.GetInstance('qualifications');


	h += '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">'
+'<html>'
+'<head>'
+''
+'<title>Oil and Gas Jobs Africa Jobs Contracts</title>'
+'<link href="styles.css" rel="stylesheet" type="text/css">'
+''
+'<LINK REL="SHORTCUT ICON" HREF="http://www.energyplacement.com/favicon.ico">'
+'</head>'
+'<body>'
+'<input type="button" name="asdfasdfg" value="Close" style="float:right;" onClick="self.close();"><br><br>'
+'<table border="0" cellpadding="4" cellspacing="2" width="760" align="center">'
+'<tr><td>'
+'<font color="black" face="Verdana" size="2">'
+'<b> Job Title: <font color="#990000">' + f.title.value + '</font></b><br>'
+'<b> Location: <font color="#990000">' + f.location.value + '</font></b><br>'
+''
+'<b> Discipline: <font color="#990000">' + f.designation.value + '</font></b><br>'
+'<b> Reference: </b><font color="#990000">' + f.reference.value + '</font><br>'
+'</font>'
+'</td>'
+'<td align="right" valign="top">'
+''
+''
+'</td></tr>'
+'<tr><td colspan="2">'
+'<font color="black" face="Verdana" size="2">'
+'<b> Description: </b> <font color="black">' + odescription.GetXHTML() + '</font><br><br>'
+''
+'<b> Qualifications: </b> <font color="black">' + oqualifications.GetXHTML() + '</font><br><br>'
+''
+'</td></tr>'
+'</table>'
+'</body>'
+'</html>';

	w.document.write(h);
	w.document.close();
	w.focus();
	return true;
}
