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 _stopEvent(ev) {
	if ( is.ie ) {
		ev.cancelBubble = true;
		ev.returnValue = false;
	} 
	else {
		ev.preventDefault();
		ev.stopPropagation();
	}
}

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

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);
	}*/
	
	Dialog(href, null, this, name ,"toolbar=no,menubar=no,personalbar=no,width="+width+",height="+height+"," +
		  "scrollbars=yes,resizable=yes,status=no,modal=yes,dependable=yes,top=" + (window.screen.height-height)/2 + ", left=" + (window.screen.width-width)/2 + ""); 

	
	//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);
}


function initCalendar(field){
	    Calendar.setup({
        inputField     :    field,
        ifFormat       :    "%d.%m.%Y",
        button         :    field + "_button",
        align          :    "BR",
        singleClick    :    true,
		showsTime      :    false,
        timeFormat     :    "24"
    });
}

/* agency functions */
function agencyNewMessage(step) {
	var foundError = false;
	
	if ( step == 1 ) {
		
		if ( $('id_agency_user').selectedIndex == 0 ) {
			$('errorPleaseChooseWoman').style.display = '';
			foundError = true;
		}
		
		if ( $('typeOfChoosen1').checked == false && $('typeOfChoosen2').checked == false ) {
			$('errorPleaseChooseTypeOfSeacrh').style.display = '';
			foundError = true;
		}
		
		if ( !foundError ) {
			$('errorPleaseChooseWoman').style.display 		 = 'none';
			$('errorPleaseChooseTypeOfSeacrh').style.display = 'none';
			
			var choosen;
			if (  $('typeOfChoosen1').checked ) {
				choosen = 1;
			}
			else if ( $('typeOfChoosen2').checked ) {
				choosen = 2;
			}
			
			window.location = AGENCY_MESSAGE_NEXT_LOCATION + '&womanId=' + $('id_agency_user').value + '&choosen=' + choosen;
		}
	}
}

function checkIfHasContactMan(obj) {
	if ( obj.getAttribute("isCanUse") ) {
		alert(MESSAGE_YOU_HAVE_NOT_CONTACT_MAN);
		obj.checked = false;
	}
}

function SelectPerson(id, man) {
	var obj = typeof man != "undefined" ? $('id_agency_man') : $('id_agency_user');
	for ( var i=0; i<obj.options.length; i++ ) {
		if ( obj.options[i].value == id ) {
			obj.options[i].selected = true;
			break;
		}
	}
}

function CheckAllCheckBox(obj, gridId) {
	var grid 	= $(gridId);
	var checked = obj.checked;
	
	var elements = grid.getElementsByTagName("input");
	var l 		 = elements.length;
	
	for ( var i=0; i<l; i++ ) {
		if ( elements[i].type == "checkbox" ) {
			elements[i].checked = checked;
		}
	}
}

function checkSelectedMessage(gridId) {
	var grid 	 = $(gridId);
	var elements = grid.getElementsByTagName("input");
	var l 		 = elements.length;
	
	for ( var i=0; i<l; i++ ) {
		if ( elements[i].type == "checkbox" && elements[i].checked && !elements[i].getAttribute("mainCheckbox") ) {
			return true;
		}
	}
	
	alert(TITLE_NO_MESSAGE_SELECTED);
	return false;
}

function MessageManageFormSubmit(action, message) {
	if ( !checkSelectedMessage('messageGrid') )
		return false;

	if ( action == 'delete' && !confirm(message) ) { 
		return false;
	}
	
	
	$('messageForm').action = window.location + '&action=' + action;
	$('messageForm').submit();
}

function MansCatalogWriteMessage(obj) {
	if ( $('id_agency_user').selectedIndex == 0 ) {
		$('errorPleaseChooseWoman').style.display = '';
	}
	else {
		$('errorPleaseChooseWoman').style.display = 'none';
		window.location = obj.getAttribute("url") + "&womanId=" + $('id_agency_user').value;
	}
	
}
/* end agency functions */