var is 			  = new Object();
var DISPLAY 	  = 'display';
var NOT_DISPLAY   = 'notDisplay';
var selectControlsonload = new Array();
var funcLoad 	= new Array();
var blinkCount  = 0;

is.ie    = /MSIE/.test(navigator.userAgent); 
//alert(navigator.userAgent);
is.ie8   = /MSIE 8.0/.test(navigator.userAgent); 
//alert(is.ie8);
is.opera = /Opera/.test(navigator.userAgent);

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

function $V(id) {
	return document.getElementById(id).value;
}

function checkAdminSaveForm() {
	var length 			  = __fieldForCheck.length;
	var i				  = 0;
	verify.setIsCanSubmitForm(true);
	
	for ( i; i<length; i++ ) {
		verify.checkControl(__fieldForCheck[i].typeCheck, __fieldForCheck[i].idCheckControl, __fieldForCheck[i].idErrorDiv);
	}
	
	return verify.getIsCanSubmitForm();
}

function __submitManageForm(action) {
	$("id_op").value = action;
	if ( checkAdminSaveForm() ) {
		$("id_manageForm").submit();
	}
}

/* Quick Search form */
function __checkQuickSearchForm(WRONG_AGE,WRONG_SEX,WRONG_COUNTRY) {
	var from 	= parseInt($V('id_age_from'));
	var to 		= parseInt($V('id_age_to'));
	var sex 	= $V('id_sex');
	var country = $('id_country').selectedIndex;
	
	/* Check Age */
	if ( to < from ) {
		alert(WRONG_AGE);
		return false;
	} // check sex
	else if ( sex == "" ) {
		alert(WRONG_SEX);
		return false;
	}
	else if ( country == 0 ) {
		alert(WRONG_COUNTRY);
		return false;
	}
	
	return true;
}

function __checkInformLadyForm(WRONG_EMAIL) {
	if ( !verify.checkEmail($V('id_il_email')) ) {
		alert(WRONG_EMAIL);
		return false;
	}
	return true;
}

function __checkInformManForm(WRONG_EMAIL) {
	if ( !verify.checkEmail($V('id_im_email')) ) {
		alert(WRONG_EMAIL);
		return false;
	}
	return true;
}


function __checkForgotPasswordForm(WRONG_EMAIL) {
	if ( !verify.checkEmail($V('id_fq_email')) ) {
		alert(WRONG_EMAIL);
		return false;
	}
	return true;
}


function __aOnclick(url) {
	var r = new RegExp(/(&scroll=\d*)?/ig);
	url = url.replace(r,"");
	window.location.href = url + '&scroll=' + document.body.scrollTop;
	return false;
}

/* resize window */
function __onresizeWindow() {
	position.__calculatePosition();
	__changeSelectPosition();
}

function __changeSelectPosition() {
	var l = selectControls.length;
	var i;
	for( i=0; i<l; i++ ) {
		selectControls[i].calculatePosition();
	}
}

/* faq */
function showFaq(id) {
	var contener = $(id);
	if ( contener.className == 'faq notDisplay' ) {
		contener.className = 'faq display';
	}
	else {
		contener.className = 'faq notDisplay';
	}
}

/* Pagination */
function showPagination(idShow, idHidden) {
	$("pagination_"+idShow).style.display = "block";
	$("pagination_"+idHidden).style.display = "none";
}

/* Add selectElemet for load*/
function addSE(id, zIndex) {
	selectControlsonload[id] = zIndex;
}

function initSelectElements() {
	/*for ( var id in selectControlsonload ) {
		new elSelect(id, selectControlsonload[id]);
	}*/
}

function __addToLoad(func) {
	funcLoad.push(func);
}

function __loadFunc() {
	for( var i=0; i<funcLoad.length; i++ ) {
		eval(funcLoad[i]);
	}
}

function openPopup(href, name, width, height) {
	
	if ( is.ie ) {
		var wnd = window.open(href, name,'left=' + (screen.width - width)/2 + ',top=' + (screen.height - height)/2 + ',location=0,scrollbars=1,resizable=1,center=yes,width='+width+',height='+height);
	}
	else {
		var wnd = window.open(href, name,'screenX=' + (screen.width - width)/2 + ',screenY=' + (screen.height - height)/2 + ',location=0,scrollbars=1,resizable=1,center=yes,width='+width+',height='+height);
	}
	
	wnd.focus();
}

/* PayPal */
function SendPayPalForm(id) {
	$('paypal_form_id_'+id).submit();
}

//Translate text using google api	

function translate(sourceLang, resultLang) {
	   var source = $('translation').value;
	   var len = source.length;
	
	   // Google Language API accepts 500 characters per request 
	   var words = 500;
	
	   $('translation').value = '';
	   for(i = 0; i <= (len/words); i++) {
	     google.language.translate(source.substr(i*words, words), sourceLang, resultLang, function (result) {
	    	 if (!result.error) {
		    	 $('translation').value = $('translation').value + result.translation;
		    	 //$('source').style.display = 'none';
		    	 //$('translation').style.display = 'block';
	    	 } }); }  
	
	   // Hide the text written in the original language
	   //$('source').style.display = 'none';
	   return false;
	 }
	
	 
//Switch to the original language	

function original() {
	$('translation').style.display = 'none';
	$('source').style.display = 'block';
	return false;
}

var isHide = 1;
function blinkTitle() {
	var value = isHide == 1 ? 'hidden' : 'visible';
	isHide = !isHide;
	
	for ( var i=1; i<=blinkCount; i++ ) {
		if ( $('blinkDiv'+i) ) {
			$('blinkDiv'+i).style.visibility = value;
		}
	}
	
	setTimeout('blinkTitle()', 800);
}

function AnswerForMessage(id, idButton, answerText, hideText) {
	if ( $(id).style.display == 'block' ) {
		$(id).style.display = 'none';
		$(idButton).value = answerText;
	}
	else {
		$(id).style.display = 'block';
		$(idButton).value = hideText;
	}
}

//
//	email decryption
//

function documentClick(event) {
	event = event || window.event;
	
	var srcElement = event.target || event.srcElement;
	var string, code, res = '';
	
	while (srcElement && srcElement.tagName != 'A') {
		srcElement = srcElement.parentNode;
	}
	if (!srcElement) {
		return true;
	}
	
	var match = srcElement.href.match(/^mailto:(.+)/);
	
	if (!match) {
		return true;
	}
	string = unescape(match[1]);
	
	for (var i = 0; i < string.length; i++) {
		code = string.charCodeAt(i);
		if (code >= 8364) {
			code = 128;
		}
		res += String.fromCharCode(code - (3));
	}
	window.location = "mailto:" + res;
	
	srcElement.returnValue = true;
	
	if (event.cancelable) {
		event.preventDefault();
	}
	
	return false;	
}

if (is.ie) {
	document.attachEvent("onclick", documentClick);
} else {
	document.addEventListener('click', documentClick, false);
}
