function us_addListener(element, type, expression, bubbling) {
	bubbling = bubbling || false;
	if(window.addEventListener) { // Standard
		element.addEventListener(type, expression, bubbling);
		return true;
	} else if(window.attachEvent) { // IE
		element.attachEvent('on' + type, expression);
	return true;
	} else return false;
}
function us_getPosition(inItem) {
	var l_result = {};
	var tmp_elem = inItem;
	
	    var offsetTop  = 0;
	    var offsetLeft = 0;
	    while (tmp_elem) {
	        offsetTop   = offsetTop + tmp_elem.offsetTop;
	        offsetLeft 	= offsetLeft + tmp_elem.offsetLeft;
			tmp_elem    = tmp_elem.offsetParent;
	    }

	    try {
	    l_result['L'] = offsetLeft;
	    l_result['T'] = offsetTop;
	    l_result['W'] = parseInt(inItem.offsetWidth);
	    l_result['H'] = parseInt(inItem.offsetHeight);
	    } catch(e) {}
	    return l_result;
}
function chr(inOrd) {
	return String.fromCharCode(inOrd)
}
function doPost(inURL,inArr,inBack) {
	if(window.XMLHttpRequest){
	var http = new XMLHttpRequest( );
	} else if (window.ActiveXObject){
	var http=new ActiveXObject("Msxml2.XMLHTTP");
	if (! http){
	var http=new ActiveXObject("Microsoft.XMLHTTP");
	}
	}	
	var url = inURL;
	var params = '';
	for (var x in inArr) {
		params +=x+'='+encodeURIComponent(inArr[x])+'&';
	} 
	params = params.replace(/%20/g, "+")
	http.open("POST", url, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.onreadystatechange = function() {
		if(http.readyState == 4 && http.status == 200) {
			inBack(http.responseText);
		}
	}
	http.send(params);
}
// --- DROP DOWN -------
var ddown = {
	need_initilize : true,
	initilizeDrowDowns : function () {
		var mass = document.getElementsByTagName('SELECT');
			ddown.a_downs = mass;
		for (var x = 0; x < mass.length; x++) {
			var parent_id = mass[x].getAttribute('parent_id');
			if (parent_id) {
				var tmp = parent_id.split(',');
				for (var y = 0; y < tmp.length; y++) {
					var el = document.getElementById(tmp[y]);
					if (!el) {
						alert('Incorrect parent ID:'+tmp[y]);
						clearInterval(ddown.iniInterval);
					}
					if (el.getAttribute('ddAssigned') != 'Y') {
						el.setAttribute('ddAssigned','Y');
						us_addListener(el,'change',new Function ("ddown.fillChilds(document.getElementById('"+el.id+"'))"))
					}					
				}
			}
		}
	},
	
	fillDdown : function (inStr) {
		
		var inArr = inStr.split(chr(4));

		for (var z = 0; z < inArr.length; z++) {
			var arr = inArr[z].split(chr(1));
			var el = document.getElementById(arr[0]);
			if (inStr.indexOf(chr(1)) < 0) alert('Cannot define Ajax Response Data : ' + inStr);
			if (!arr[1]) arr[1] = '';
			if (ddown.onFill) ddown.onFill(el);
			var tmp = arr[2].split(chr(3));
			for (var x = 0; x < tmp.length; x++) {
				var pel = document.getElementById(tmp[x].split(chr(2))[0]);
				if (pel && pel.value != tmp[x].split(chr(2))[1]) return;
			}
			ddown.clearDdown(el);
			var arr = arr[1].split(chr(3));
			var iniVal = '';
			if (arr.length > 0) {
				for (var x = 0; x < arr.length; x++) {
					var tmp = arr[x].split(chr(2));
					if (!tmp[1]) continue;
					if (x == 0) iniVal = tmp[0];
					el.options.length++;
				    el.options[el.options.length - 1].value = tmp[0];
					el.options[el.options.length - 1].text  = tmp[1];	
				}
			}
		}
	},
	
	fillChilds : function (inItem,noPrime) { 
		if (!noPrime) {
			ddown.p_items = '';
			ddown.p_used  = {};
			ddown.p_arr   = {};
		}
		if (ddown.onChange) ddown.onChange(inItem)
		for (var x = 0; x < ddown.a_downs.length; x++) {
			
			if (String(','+ddown.a_downs[x].getAttribute('parent_id')+',').indexOf(','+inItem.id+',') >= 0) {
				ddown.clearDdown(ddown.a_downs[x]);
				ddown.fillChilds(ddown.a_downs[x],true);
							
				// -- CALL AJAX
					var can_post = true;
					var arr = {'ID':ddown.a_downs[x].id,'REF_ID':inItem.value,'PARENT_ID':ddown.a_downs[x].getAttribute('parent_id')}
					var tmp = ddown.a_downs[x].getAttribute('parent_id').split(',');
					for (var y = 0; y < tmp.length; y++) {
						var el = document.getElementById(tmp[y]);
						if (ddown.a_downs[x].getAttribute('dd_req') != 'N') {
							if (!el) can_post = false;
							if (el && el.value == '') can_post = false;
						}
						ddown.p_arr[el.id] = el.value;
					}
					if (can_post) {
						if (!ddown.p_used[ddown.a_downs[x].id]) {
							ddown.p_used[ddown.a_downs[x].id] = 1;
							ddown.p_items += ddown.a_downs[x].id+chr(1)+ddown.a_downs[x].getAttribute('parent_id')+chr(2);
						}
						
						ddown.a_downs[x].options[0].text = 'Loading';
						clearInterval(ddown.a_downs[x].getAttribute('dd_INTERVAL'))
						ddown.a_downs[x].setAttribute('dd_INTERVAL',setInterval('ddown.doDots("'+ddown.a_downs[x].id+'")',300))
					}
			}				
		}
		
		if (!noPrime) {
			ddown.p_arr['P_ITEMS'] = ddown.p_items;
			doPost ('ajax/drop_down_data.php',ddown.p_arr,ddown.fillDdown)
		}
		
	},
	
	doDots : function (inID) {
		var el = document.getElementById(inID);
		if (el.options[0].text.indexOf('Loading') != 0) return;
		if (el.options[0].text.indexOf('...') < 0) {
			el.options[0].text += '.';
		} else {
			el.options[0].text = 'Loading';
		}
	},
	
	clearDdown : function (inItem) {
		if (ddown.onClear) ddown.onClear(inItem)
		while (inItem.options.length!=0) {
		if(inItem.options && inItem.options.remove)
			inItem.options.remove(0);
		else if(inItem.remove)
			inItem.remove(0);
		else inItem.options[0]=null;
		}
		// -- add default option --
		inItem.options.length++;
	    inItem.options[0].value='';
		inItem.options[0].text='-- select --';		
	},
	stopInitilize : function () {
		ddown.initilizeDrowDowns();
		clearInterval(ddown.iniInterval);
	}
}
ddown.iniInterval = setInterval('ddown.initilizeDrowDowns()', 100);
us_addListener(window, 'load', ddown.stopInitilize)

var div = {
	showHide : function (inID,inOP) {
		if (!div.intID) div.intID = 0;
		var el = document.getElementById(inID);
		if (!Number(parseInt(el.style.height))) el.style.height = parseInt(el.offsetHeight) + 'px';
		el.style.display  = 'block';
		el.style.overflow = 'hidden';		
		
		clearInterval(div.intID);
		var op = el.getAttribute('div_op');
		
		if (op == 'OPEN' || parseInt(el.style.height) > 2) {
			op = 'CLOSE'
		} else {
			op = 'OPEN'
		} 
		
		if (inOP) op = inOP;
		el.setAttribute('div_op',op)
		if (!div.arr) {
			div.arr = {};
		}
		div.arr[inID] = 1;
		div.intID = setInterval ('div._processAllItems()',20);
		
		return op;
	},
	
	_pricessItem : function(inID) {
		var el = document.getElementById(inID);
		var op = el.getAttribute('div_op');
		var mx = el.getAttribute('div_max');
		var mn = el.getAttribute('div_min');
		if (isIE) {
			if (!Number(mn)) mn = 1;
		} else {
			if (!Number(mn)) mn = 0;
		}
		
		if (op == 'CLOSE') {
			el.style.height = Math.max(parseInt(el.style.height) - 80,mn) + 'px';
		} else {
			el.style.height = Math.min(parseInt(el.style.height) + 80,mx) + 'px';
		}
		
		if (parseInt(el.style.height) == mn || parseInt(el.style.height) == mx ) {
			if (el.getAttribute('div_hide') == 'Y') { 
				el.style.left = -10000;
			}
			return 0;
		} else {
			return 1;
		}
	},
	
	_processAllItems : function () {
		for (var x in div.arr) {
			if (div.arr[x] == 1) {
				div.arr[x] = div._pricessItem(x)				
			}
		}
		var need_kill = true;
		for (var x in div.arr) {
			if (div.arr[x] == 1) {
				need_kill = false;
				break;
			}
		}
		if (need_kill) clearInterval (div.intID)
	}
}

// ---- CUSTOM FUNCTIONS -------
var ad_lang = {
	addNewLang : function (inItem) {
		var master = document.getElementById('dd_adv_sep_lang');
		if (!ad_lang.id) ad_lang.id = 0;
		ad_lang.id++;
		if (arguments.length == 0) {
			var html   = document.getElementById('lang_td').innerHTML;
				html = html.replace(/dd_adv_sep_lang/g,'dd_adv_sep_lang#'+ad_lang.id);
				
			var tbl    = document.getElementById('lang_tbl');
			var row    = tbl.insertRow(tbl.rows.length - 1);
				row.id = 'ad_row#'+ad_lang.id;
			var cell_1 = row.insertCell(0); 
			var cell_2 = row.insertCell(1); 
			
			cell_2.innerHTML = html;
			cell_2.className = document.getElementById('lang_td').className;
		} 		
	
		// ---- CHANGE DD
		var a_vals = {};
			a_vals[master.value] = 1;

		for (var x = 1; x <= ad_lang.id; x++) {
			var el = document.getElementById('dd_adv_sep_lang#' + x)
			if (el) a_vals[el.value] = el.id;
		}
	
		var el = document.getElementById('adv_sep_lang');
			el.value = '';
		for (var x in a_vals) {
			if (!Number(x)) continue;
			el.value += x + ',';
		}
	}
}

var isIE = window.navigator.userAgent.indexOf("MSIE")>-1; 
var block = {
	blockWin : function (show){
		block._show = show;
		if (show && block.onResize) {
			block.onResize(show)
		}
		if(block._blDiv==null){
			block._blDiv=document.createElement('DIV');
			with(block._blDiv.style){
				display='none'; 
				position='absolute'; 
				height=0; width=0;	
				zIndex=10000; 
				if(isIE){
					backgroundColor = '#000';
					filter="progid:DXImageTransform.Microsoft.Alpha(Opacity=40, Style=0)";
				}
				else 
					backgroundColor = '#000';
					opacity = 0.4;
			}
			block._blDiv.id = '__block_div'
			document.body.appendChild(block._blDiv);
		}
		if(show){
			var tmp = block.docSize();
			
			block._blDiv.style.left    = 0;
			block._blDiv.style.top     = 0;
			block._blDiv.style.width   = '100%';
			block._blDiv.style.height  = tmp[1] + 'px';
			block._blDiv.style.display = 'block';
		} else {
			block._blDiv.style.display = 'none';			
		}

		return block.getCenter();
	},	
	
	docSize : function (){
		return [
		document.body.scrollWidth > document.body.offsetWidth ? 
			document.body.scrollWidth : document.body.offsetWidth,
		document.body.scrollHeight > document.body.offsetHeight ? 
			document.body.scrollHeight : document.body.offsetHeight
		];
	},
	getClientSize : function (){
		if(document.compatMode=='CSS1Compat')
			return [document.documentElement.clientWidth, document.documentElement.clientHeight];
		else
			return [document.body.clientWidth, document.body.clientHeight];
	},
	getDocumentScroll : function(){
		return [
		self.pageXOffset || (document.documentElement && document.documentElement.scrollLeft) 
			|| (document.body && document.body.scrollLeft),
		self.pageYOffset || (document.documentElement && document.documentElement.scrollTop) 
			|| (document.body && document.body.scrollTop)
		];
	},
	getCenter : function (){
		var sizes = block.getClientSize();
		var scrl  = block.getDocumentScroll();
		return [parseInt(sizes[0]/2)+scrl[0], parseInt(sizes[1]/2)+scrl[1]];
	},
	resizeDelay : function () {
		if (block._show) setTimeout('block.blockWin(true)',100);
	}
	
}
if (!isIE) {
	us_addListener (window, 'resize', block.resizeDelay)
} else {
	window.onresize = block.resizeDelay
}

var imr = {
	getImagePath : function () {
		return imr._imgPath;
	},
	
	getImageSize : function () {
		return imr.W + ':' + imr.H;
	},
	
	showCutTool : function (inPath, inW, inH) {
		imr.W = inW;
		imr.H = inH;

		imr._imgPath = inPath;
		imr._isShown = true;
		
		imr.cut_div = document.getElementById('cut_div');
		imr.makeDivCenter();
		setTimeout('block.blockWin(true)',20);
		try {
			if (thisMovie('cut_flash') && thisMovie('cut_flash').doImageLoad) thisMovie('cut_flash').doImageLoad();		
		} catch(e) {
			
		}
	},
	
	hideCutTool : function () {
		imr._isShown = false;
		document.getElementById('cut_div').style.left = '-10000px';

		block.blockWin(false);
	},
	
	doRemove : function () {
		$("#progress1").text("File was successfully removed.");
		imr.hideCutTool();
	},
	
	doAfterRemove : function () {
		
	},
	
	doImageRemove : function (inPath,inItem) {
		if (!confirm('Do you really want to remove this image?')) return;
		var mass = document.getElementsByTagName('IMG');
		for (var x = 0; x < mass.length; x++) {
			if (String(mass[x].src).indexOf(inPath) >= 0) {
				mass[x].parentNode.removeChild(mass[x]) ;
			}
		}
		inItem.parentNode.removeChild(inItem) ;
		var cnt = Number($("#img_count").text()) - 1;
		if (!cnt) cnt = 0;
		$("#img_count").html(String(cnt));
		var res = {'OP':'REMOVE','URL':inPath};
		doPost ('ajax/adv_sep_img_resize.php',res,imr.doAfterRemove)
	},
	
	doAfterCut : function (inStr) { 
		if (inStr.indexOf('ERROR') < 0) {
			var id = inStr.split('_sep_')[1];
			document.getElementById(id).src = inStr + '?id=' + Math.round(10000 * Math.random());
		} else {
			alert('Error during file saving. Image was not saved.');
		}
		imr.hideCutTool();
	},
	
	makeDivCenter : function(pos) {
		if (arguments.length == 0) pos = block.getCenter()
		if (imr._isShown) {
			imr.cut_div.style.left = (pos[0] - 250) + 'px';
			imr.cut_div.style.top  = (pos[1] - 250) + 'px';
		}
	},
	
	doOnResize : function (show) {
		if (!show) return;
		imr.makeDivCenter();
	}
}

block.onResize = imr.doOnResize;

function _flash (version, filename, name, width, height, wmode, flashvars,bgcolor) {
   if (!bgcolor) bgcolor = '#C2D6E1'
  
   var menu    = '';
   document.write("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="+version+"' width='"+width+"' height='"+height+"' id='"+name+"' align='middle'>");
   document.write("<param name='FlashVars' value='"+flashvars+"' />");
   document.write("<param name='allowScriptAccess' value='always' />");
   document.write("<param name='wmode' value='"+wmode+"' />");
   document.write("<param name='bgcolor' value='"+bgcolor+"' />");
   document.write("<param name='salign' value='lt' />");
   document.write("<param name='movie' value='"+filename+"' /><param name='quality' value='high' /><embed src='"+filename+"' wmode='"+wmode+"' quality='high' FLashVars='"+flashvars+"' salign='lt' width='"+width+"' height='"+height+"' bgcolor='"+bgcolor+"' name='"+name+"' align='middle' allowScriptAccess='always' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />");
   document.write("</object>");
} 

function thisMovie(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
      return window[movieName]
    } else {
      return document[movieName]
    }
}

function showHideSearchbox(){
	if (!isIE) {
		document.getElementById('sb-container').setAttribute('div_max', 330)
	} else {
		document.getElementById('sb-container').setAttribute('div_max', 340)
	}
	
	var op = div.showHide('sb-container');
	if (op == 'OPEN') {
		document.getElementById('showhide').className='hide';
	} else {
		document.getElementById('showhide').className='';
	}
}
function _() {
	
}

function doAfterProfileSave(inStr) {
	var json = eval("(" + inStr + ")");
	if (json.url) {
		document.location = json.url
	}
	if (json.msg) {
		alert(json.msg)
	}
}

// -- SAVE SEP INFO ---
function doSepSave(inFep, inFree) {
	if (arguments.length == 1) inFep  = true;
	if (arguments.length == 2) inFree = true;
	
	var arr  = {};
	var mass = document.getElementsByTagName('SELECT');
	for (var x in mass) {
		if (mass[x].id == '') {
			mass[x].id = mass[x].name;
		}
	}
	
	document.getElementById('adv_sep_name').value = initCap(document.getElementById('adv_sep_name').value);
	
	// -- CHECK GIRL NAME
	var nm = document.getElementById('adv_sep_name').value;
	if (nm.replace(/ /g,'') == '') {
		alert('You did not enter Girl Name. Please enter Girl Name and try again.');
		return;
	}
	
	var a_tmp = {};
		a_tmp['adv_sep_height']      = 'Height';
		a_tmp['adv_sep_age_num']     = 'Age';
		a_tmp['adv_sep_build']       = 'Body Type';
		a_tmp['adv_sep_origin']      = 'Origin';
		a_tmp['adv_sep_stats']       = 'Bust Size';
		a_tmp['adv_sep_orientation'] = 'Orientation';
		a_tmp['adv_sep_hair']        = 'Hair Color';
		a_tmp['adv_sep_eye']         = 'Eyes Color';
		a_tmp['adv_sep_country']     = 'Country';
		a_tmp['adv_sep_city']        = 'City';
		a_tmp['adv_sep_region']      = 'Area';
	
	for (var x in a_tmp) {
		try {
		if (String(document.getElementById(x).value).replace(/ /g,'') == '') {
			alert("You did not select " + a_tmp[x] + '. Please select ' + a_tmp[x] + ' and try again.');
			return;
		}
		} catch(e) {alert(x)}
	}		
	
	arr['adv_sep_name'] = nm;
	var txt = document.getElementById('adv_sep_about').value;
	if (txt.length < 350 || txt.length > 500) {
		alert('You should enter 350...500 characters into Intro Text field. Please do it and try again.');
		return;	
	}
	
	arr['adv_sep_about'] = txt;
	
	
	arr['adv_sep_region'] = document.getElementById('adv_sep_region').value;
	arr['adv_sep_lang']   = document.getElementById('adv_sep_lang').value;
	arr['adv_sep_price']  = Number(document.getElementById('adv_sep_price').value);
	
	if (!arr['adv_sep_price']) {
		alert('You did not enter value into Price From field. Please fill it and try again.');
		return;
	}
	
	for (var x = 1; x < 10; x++) {
		var el = document.getElementById(x + '.jpg');
		if (!el) break;
		if (String(el.src).indexOf('null.gif') > 0) {
			alert("Please make sure all required photos are uploaded!");
			return;
		}
	}
	
	for (var x = 0; x < mass.length; x++) {
		if (mass[x].name == '') continue;
		arr[mass[x].name] = mass[x].value;
	}
	if (inFree) {
		doPost("srv/ajax/adv_free_form_post.php",arr,doAfterProfileSave)
		return;
	}
	if (!inFep) {
		doPost("srv/ajax/adv_sep_form_post.php",arr,doAfterProfileSave)
	} else {
		doPost("srv/ajax/adv_fep_form_post.php",arr,doAfterProfileSave)
	}
}

function doAfterReloadAntiSpam(inStr) {
	var json = eval("(" + inStr + ")");
	
	if (json.OK) {
		alert('Your message was succefully saved. Thank you.');
		document.location = String(document.location).replace('contact.htm','')
		return;
	}
	
	if (json.MAIL) {
		alert('You entered incorrect Email. Please enter correct Email and try again.');
		return;
	}	

	document.getElementById('anti_spam').src = json.img;
	if (json.ERROR) {
		alert('You entered incorrect Anti-Spam code. Please retype it and try again.');
		return;
	}
}

function doReloadAntiSpamImage() {
	var src = String(document.getElementById('anti_spam').src).split('/');
		src = src[src.length - 1].split('?')[0];
	
	var arr = {'SRC' : src, 'OP' : 'RELOAD_IMAGE'}
	doPost("srv/ajax/contact.php",arr,doAfterReloadAntiSpam)
}

function initCap(str) {
	var str = str.substring(0,1).toUpperCase() + str.substring(1,str.length);	
	return str;
} 

function doContactPost() {
	var your_name = document.getElementById('your_name').value;
	if (your_name.replace(/ /g,'') == '') {
		alert('You did not enter your name. Please enter your name and try again.');
		return;
	}
	
	var email = document.getElementById('email').value;
	if (email.replace(/ /g,'') == '') {
		alert('You did not enter E-mail Address. Please enter E-mail Address and try again.');
		return;
	}

	var website = document.getElementById('website').value;
	if (website.replace(/ /g,'') == '') {
		alert('You did not enter Website Address. Please enter Website Address and try again.');
		return;
	}
	
	var message = document.getElementById('message').value;
	if (message.replace(/ /g,'') == '') {
		alert('You did not enter Message. Please enter Message and try again.');
		return;
	}	
	
	var spam = document.getElementById('spam').value;
	if (spam.replace(/ /g,'') == '') {
		alert('You did not enter Anti-Spam Word. Please enter Anti-Spam Word and try again.');
		return;
	}	
	
	var arr = {'OP' : 'SAVE', 'your_name' : your_name, 'email' : email, 'website' : website, 'message' : message, 'spam' : spam}
	
	doPost("srv/ajax/contact.php",arr,doAfterReloadAntiSpam)
}
