function setPage(page, menuName)
{
    $(menuName).page.value=page;
    return true;
}

function $(id) {
   return document.getElementById(id)
}

function setAction(action, menuName)
{
    $(menuName).action.value=action;
    return true;
}

function submitForm(forma)
{
    $(forma).submit();
}
function isMail(x){
    return x.search(/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,4}$/ig) != -1;
}

function CheckSS(idEl, formaId)
{
	var field = $(idEl);
	var formF= $(formaId);
	if(!isMail(field.value)){
		alert("Please enter a valid email address.");
		field.focus();
		field.select();
		return false;
	}
	formF.submit();
}

function perDeleteM(forma, action)
{
    if(confirm("Are sure?")){
        setAction(action, forma);
        submitForm(forma);
    }else
        return false;
}

function setField(fieldName, val)
{
    var field=$(fieldName);
    field.value=val;

    return true;
}

function confAction(msg, action, forma)
{
    if(confirm(msg)){
        setAction(action, forma);
        submitForm(forma);
    }else
        return false;
}

function replaceWindow(page, action)
{
	window.document.location=""+page+"/"+action+"";
}

function doLoad(divId, url) {
    var req = new Subsys_JsHttpRequest_Js();
    req.onreadystatechange = function() {
    if (req.readyState == 4) {
        if(req.responseText=='') document.getElementById(divId).innerHTML = '';
        else{ 
            document.getElementById(divId).innerHTML = req.responseText;
        }
    }
    }
    req.caching = false;
    req.open('POST', url, true);
    req.send({d: 1});
}

function settingsTinyMCE(){
tinyMCE.init({
	mode : "textareas",
	theme : "advanced",
	plugins : "style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,flash,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable",
	theme_advanced_buttons1_add_before : "save,newdocument,separator",
	theme_advanced_buttons1_add : "fontselect,fontsizeselect",
	theme_advanced_buttons2_add : "separator,insertdate,inserttime,preview,separator,forecolor,backcolor",
	theme_advanced_buttons2_add_before: "cut,copy,paste,pastetext,pasteword,separator,search,replace,separator",
	theme_advanced_buttons3_add_before : "tablecontrols,separator",
	theme_advanced_buttons3_add : "emotions,iespell,flash,advhr,separator,print,separator,ltr,rtl,separator,fullscreen",
	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_path_location : "bottom",
	content_css : "example_full.css",
    plugin_insertdate_dateFormat : "%m/%d/%Y",
    plugin_insertdate_timeFormat : "%H:%M:%S",
	extended_valid_elements : "hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]",
	external_link_list_url : "example_link_list.js",
	external_image_list_url : "example_image_list.js",
	flash_external_list_url : "example_flash_list.js",
	file_browser_callback : "fileBrowserCallBack",
	theme_advanced_resize_horizontal : false,
	theme_advanced_resizing : true
});
}

function fileBrowserCallBack(field_name, url, type, win) {
	// This is where you insert your custom filebrowser logic
	//alert("Example of filebrowser callback: field_name: " + field_name + ", url: " + url + ", type: " + type);

	// Insert new URL, this would normaly be done in a popup
	win.document.forms[0].elements[field_name].value = "images/upload/";
}

function hideUpload() {
	var delField= $('delete_image');
	var uplField= $('userfile');
	var uplDivField= $('upload_div');
	if(delField.checked==true){
		uplDivField.style.display='none';
		uplField.value='';
	}else{
		uplDivField.style.display='block';
	}
}

function replBlog()
{
	window.document.location="http://telegraphlive.com/blog/";
}

function replaceMainForm(page)
{
    window.document.location=page+"/show_form";
}
function showDiv(divId)
{
    var div=$('div_'+divId);
    var td=$('td_'+divId);

    div.style.display="";
    td.className='over';
    td.style.cursor="pointer";
}

function hideDiv(divId, cl)
{
    var div=$('div_'+divId);
    var td=$('td_'+divId);

    div.style.display="none";
    td.className=cl;
}

function doSearch()
{
	var search_v=$('search');
	window.document.location="global_search/show_form&search="+search_v.value+"";
}
    

function replEvent(page, month)
{
    window.location=page+"/show_form&month="+month+"";
}

function repl2Lst(page, date)
{
    //var type_ref=$('type_id');
    
    window.location=page+"/event_list&date="+date+"";
}

function repl2LstByName(page)
{
    var name_ref=$('search2');
    
    if(name_ref.value==''){
        alert('You need fill field for search');
        return false;
    }
    window.location=page+"/do_search&name="+name_ref.value+"";
}

function SetSortOrder(event, divId){
    var sort_order_popup = null;
    sort_order_popup = new PopupLayer(divId, "Event");
    var div = document.getElementById(divId);
    var dx=-100;
    var dy=10;
    var width=250;
    var height=230;
    var scroll=document.documentElement.scrollTop;
    var x=event.clientX-width-dx;
    var y=event.clientY+scroll-height-dy;
    divId.innerHTML=x +" y " + y;
    if (div) {
        div.style.display = "block";
        sort_order_popup.moveTo(x , y);
        sort_order_popup.resizeTo(width, height);    
        sort_order_popup.show();
    }
}

function SetSortOrder2(divId, smesh){
    var sort_order_popup = null;
    sort_order_popup = new PopupLayer(divId, "Event");
    var div = document.getElementById(divId);
    if (div) {
        div.style.display = "block";
        sort_order_popup.moveTo(400+10*smesh, 50+5*smesh);
        sort_order_popup.resizeTo(500, 350);    
        sort_order_popup.show();
    }
}

function closePopup(divId)
{
    var div = document.getElementById(divId);
    if (div) {
        div.style.display = "none";
    }
}
function replWin(page, action)
{
	var b_n=$('business_name');

	window.location=page+"/"+action+"&business_name="+b_n.value+"";
}
function showLiDiv(id)
{
	var fId= $(id);
	if(fId.style.display=='block')
		fId.style.display='none';
	else
		fId.style.display='block';
}
function checkUserField()
{
	var fname_v=$('fname');
	var lname_v=$('lname');
	var passwd_v=$('passwd');
	var email_v=$('email');
	var auto_pwd_v=$('auto_pwd');
	
	if(fname_v.value==""){
		alert('Please fill field First Name.');
		fname_v.focus();
		return false;
	}
	if(lname_v.value==""){
		alert('Please fill field Last Name.');
		lname_v.focus();
		return false;
	}
	if(passwd_v.value=="" && auto_pwd_v.checked==false){
		alert('Please fill field Password.');
		passwd_v.focus();
		return false;
	}
	if(email_v.value==""){
		alert('Please fill field Email.');
		email_v.focus();
		return false;
	}
	if(!isMail(email_v)){
		alert('Your email is incorrect.');
		email_v.focus();
		return false;
	}
}
function showPwd()
{
	var check = $('auto_pwd');
	var div = $('pwd_div');
	
	
	if(check.checked==true){
		div.style.display="none";
	}else{
		div.style.display="block";
	}
}
function perDelete()
{
	var id_f=$('id').value;
	if(confirm("Are you sure?")){
		window.document.location="/event_month/do_del&id="+id_f+"";
	}
}
function prepareDelItem(form, action)
{
	if(confirm("Are you sure?")){
		$('action').value= action;
		form.submit();
	}
}

function replGlobSearch()
{
	if( !validate.isEmpty($('search_value').value) ){
		$('search_merch1').submit();
	}
}
function repl(page, id)
{
	window.document.location=page+"/do_search&sub_action=special_lst&shop_id="+id+"";
}
function prepareSendEmail(forma)
{
	if(confirm("Are you sure wish to send email with the password to user?")){
		forma.page.value='user_editor';
		forma.action.value='send_password';
		forma.submit();
	}
}

//-- Merchant User --

function loadSub(id, idImg, idDiv)
{
	var img=$(idImg);
	var div=$(idDiv);
	if(div.style.display=='none'){
		on_button_click(id);
		div.innerHTML='downloading...'
		switchDiv(idImg, idDiv);
	}else{
		switchDiv(idImg, idDiv);
	}
}

function switchDiv(idImg, idDiv)
{
	var img=$(idImg);
	var div=$(idDiv);
	if(div.style.display=='none'){
		div.style.display='block';
		img.src='images/folder.png';
	}else{
		div.style.display='none';
		img.src='images/folder_closed.png';
	}
}

function on_button_click(id) {
    var parameters = {
        type_id: id
    };
    
    https.get_object({url: "index.php?page=merchant_user&action=get_merchant", parameters: parameters, callback: on_button_click_handle});
}
function on_button_click_handle(result) {
    if (result === null) {  //  error
    } else {
        var ul = document.createElement("ul");
        ul.setAttribute('class', 'menumerchantsubul');
        var div=$(result.type_id+'_div');
        for (var i = 0; i < result.lis.length; i++) {
        	var li = document.createElement("li");
        	li.setAttribute('class', 'menumerchantsubli');
        	var a = document.createElement("a");
        	a.setAttribute('href', '#');
        	a.setAttribute('onclick', "on_link_click('"+result.lis[i].id+"'); return false;");
        	var ele_text = document.createTextNode(result.lis[i].business_name);
    		a.appendChild(ele_text);
    		li.appendChild(a);
    		ul.appendChild(li);
        }
        div.innerHTML='';
        div.appendChild(ul);
    }
}
function on_link_click(id)
{
	var parameters = {
        id: id
    };
    
    https.get_object({url: "index.php?page=merchant_user&action=get_merchant_user", parameters: parameters, callback: on_link_click_handle});
}
function on_link_click_handle(result)
{
	if (result === null) {  //  error
    } else {
    	var ele=$('shop_id');
    	ele.value=result.shop_id;
    	
    	var ele=$('fname');
    	ele.innerHTML=result.fname;
    	
    	var ele=$('lname');
    	ele.innerHTML=result.lname;
    	
    	var ele=$('email');
    	ele.innerHTML=result.email;
    	
    	var ele=$('group');
    	ele.innerHTML=result.group;
    	
    	var ele=$('business_name');
    	ele.innerHTML="<h3><b>"+result.business_name+"</b></h3>";
    }
}
function on_change()
{
	var userId=$('selectuser').value;
	var shopId=$('shop_id').value;
	var parameters = {
        shop_id: shopId,
        user_id: userId
    };
    
    https.get_object({url: "index.php?page=merchant_user&action=change_user", parameters: parameters, callback: on_change_handle});
}
function on_change_handle(result)
{
	if (result === null) {  //  error
    } else {
    	var shopId=$('shop_id').value;
    	on_link_click(shopId);
    }
}
    
function isSubmitEnter(e) {
    if (typeof e!='object') return false;
    var whichKey, targ;

    if (e.target) {targ = e.target;}
    else if (e.srcElement) {targ = e.srcElement;}

    if(document.all) {whichKey = window.event.keyCode;}
    else if(document.layers) {whichKey = e.which;}
    else {whichKey = e.keyCode;}

    if(whichKey == 13) {
        switch(targ.tagName.toUpperCase()) {
            case "TEXTAREA":
            case "SELECT":
                return false;
                break;
            case "INPUT":
                targ.blur();
                targ.focus();
                return true;
                break;
            default:
                return true;
        }
    }else
        return false;
}
var validate= {

	    isMail: function (x){
	        return x.search(/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,}$/ig) != -1;
	    },
	    
	    isEmpty: function(x){
	        if(!this.isDefined(x))
	            return false;
	        if(this.isNumeric(x))
	            return false;
	        if(x=="")
	            return true;
	        return false;
	    },
	    
	    isDefined: function(x){
	        return typeof(x)!="undefined";
	    },

	    isNumeric: function(x){
	        return this.isFloat(x);
	    },
	    
	    isFloat: function(x){
	        if(!this.isDefined(x))
	            return false;
	        return x!=""&&!isNaN(x);
	    },
	    
	    isBlank: function(val){
	        if(val==null){
	            return true;
	        }
	        for(var i=0;i<val.length;i++){
	            if((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r")){
	                return false;
	            }
	        }
	        return true;
	    },
	    
	    isDigit: function(num){
	        if(num.length>1){
	            return false;
	        }
	        var string="1234567890";
	        if(string.indexOf(num)!=-1){
	            return true;
	        }
	        return false;
	    },
	    
	    isInteger: function(val,message){
	        if(validate.isBlank(val)){
	            return false;
	        }
	        for(var i=0;i<val.length;i++){
	            if(!validate.isDigit(val.charAt(i)))
	                return false;
	        }
	        
	        return true;
	    }
	}
