scrigo.js.php 28.9 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9
include '../../lib/config.inc.php';
header('Content-Type: text/javascript; charset=utf-8'); // the config file sets the content type header so we have to override it here!
require_once '../../lib/app.inc.php';
$lang = (isset($_SESSION['s']['language']) && $_SESSION['s']['language'] != '')?$_SESSION['s']['language']:'en';
include_once ISPC_ROOT_PATH.'/web/strengthmeter/lib/lang/'.$lang.'_strengthmeter.lng';

$app->uses('ini_parser,getconf');
$server_config_array = $app->getconf->get_global_config();
10
?>
mcramer's avatar
mcramer committed
11 12 13 14 15
var pageFormChanged = false;
var tabChangeWarningTxt = '';
var tabChangeDiscardTxt = '';
var tabChangeWarning = false;
var tabChangeDiscard = false;
16 17 18 19
var requestsRunning = 0;
var indicatorPaddingH = -1;
var indicatorPaddingW = -1;
var indicatorCompleted = false;
Marius Cramer's avatar
Marius Cramer committed
20
var registeredHooks = new Array();
21 22
redirect = '';

23
function reportError(request) {
24 25
	/* Error reporting is disabled by default as some browsers like safari
	   sometimes throw errors when a ajax request is delayed even if the
26
	   ajax request worked. */
27

28 29 30
	/*alert(request);*/
}

Marius Cramer's avatar
Marius Cramer committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44
function registerHook(name, callback) {
    if(!registeredHooks[name]) registeredHooks[name] = new Array();
    var newindex = registeredHooks[name].length;
    registeredHooks[name][newindex] = callback;
}

function callHook(name, params) {
    if(!registeredHooks[name]) return;
    for(var i = 0; i < registeredHooks[name].length; i++) {
        var callback = registeredHooks[name][i];
        callback(name, params);
    }
}

45 46 47 48
function resetFormChanged() {
    pageFormChanged = false;
}

49
function showLoadIndicator() {
50 51
    document.body.style.cursor = 'wait';

52 53 54
<?php
if($server_config_array['misc']['use_loadindicator'] == 'y'){
?>
55
    requestsRunning += 1;
56

57 58 59 60 61 62 63 64 65
    if(requestsRunning < 2) {
        var indicator = jQuery('#ajaxloader');
        if(indicator.length < 1) {
            indicator = jQuery('<div id="ajaxloader" style="display: none;"></div>');
            indicator.appendTo('body');
        }
        var parent = jQuery('#content');
        if(parent.length < 1) return;
        indicatorCompleted = false;
66

67 68 69 70 71 72 73 74
        var atx = parent.offset().left + 150; //((parent.outerWidth(true) - indicator.outerWidth(true)) / 2);
        var aty = parent.offset().top + 150;
        indicator.css( {'left': atx, 'top': aty } ).fadeIn('fast', function() {
            // check if loader should be hidden immediately
            indicatorCompleted = true;
            if(requestsRunning < 1) $(this).fadeOut('fast', function() { $(this).hide();});
        });
    }
75 76 77
<?php
}
?>
78 79 80
}

function hideLoadIndicator() {
81 82
    document.body.style.cursor = '';

83 84 85 86 87 88 89
    requestsRunning -= 1;
    if(requestsRunning < 1) {
        requestsRunning = 0; // just for the case...
        if(indicatorCompleted == true) jQuery('#ajaxloader').fadeOut('fast', function() { jQuery('#ajaxloader').hide(); } );
    }
}

90 91 92
function onAfterContentLoad(url, data) {
    if(!data) data = '';
    else data = '&' + data;
93 94 95
<?php
if($server_config_array['misc']['use_combobox'] == 'y'){
?>
96
    $('#pageContent').find("select").combobox();
97 98 99
<?php
}
?>
100
    callHook('onAfterContentLoad', {'url': url, 'data': data });
101 102
}

103
function loadContentRefresh(pagename) {
104

105
  if(document.getElementById('refreshinterval').value > 0) {
106
	var pageContentObject2 = jQuery.ajax({	type: "GET",
107
											url: pagename,
108
											data: "refresh="+document.getElementById('refreshinterval').value,
109
											dataType: "html",
110 111 112
											beforeSend: function() {
												showLoadIndicator();
											},
113
											success: function(data, textStatus, jqXHR) {
114
                                                hideLoadIndicator();
115
												jQuery('#pageContent').html(jqXHR.responseText);
116
                                                onAfterContentLoad(pagename, "refresh="+document.getElementById('refreshinterval').value);
mcramer's avatar
mcramer committed
117
                                                pageFormChanged = false;
118 119
											},
											error: function() {
120
                                                hideLoadIndicator();
121
												reportError('Ajax Request was not successful.'+pagename);
cfoe's avatar
cfoe committed
122
											}
123
										});
124
  	setTimeout( "loadContentRefresh('"+pagename+"&refresh="+document.getElementById('refreshinterval').value+"')", document.getElementById('refreshinterval').value*1000*60 );
125 126 127
  }
}

128
function capp(module, redirect) {
129 130
	var submitFormObj = jQuery.ajax({		type: "GET",
											url: "capp.php",
131
											data: "mod="+module+((redirect != undefined) ? '&redirect='+redirect : ''),
132
											dataType: "html",
133 134 135
											beforeSend: function() {
												showLoadIndicator();
											},
136 137 138 139 140
											success: function(data, textStatus, jqXHR) {
												if(jqXHR.responseText != '') {
													if(jqXHR.responseText.indexOf('HEADER_REDIRECT:') > -1) {
														var parts = jqXHR.responseText.split(':');
														loadContent(parts[1]);
141 142 143
													} else if (jqXHR.responseText.indexOf('URL_REDIRECT:') > -1) {
														var newUrl= jqXHR.responseText.substr(jqXHR.responseText.indexOf('URL_REDIRECT:') + "URL_REDIRECT:".length);
														document.location.href = newUrl;
144
													} else {
145
														//alert(jqXHR.responseText);
146 147 148
													}
												}
												loadMenus();
149
                                                hideLoadIndicator();
150 151
											},
											error: function() {
152
                                                hideLoadIndicator();
153
												reportError('Ajax Request was not successful.'+module);
cfoe's avatar
cfoe committed
154
											}
155
									});
156 157 158 159 160 161 162 163 164 165 166 167 168 169
}

function submitLoginForm(formname) {
    //* Validate form. TODO: username and password with strip();
    var frm = document.getElementById(formname);
    var userNameObj = frm.username;
    if(userNameObj.value == ''){
        userNameObj.focus();
        return;
    }
    var passwordObj = frm.passwort;
    if(passwordObj.value == ''){
        passwordObj.focus();
        return;
170
    }
171

172 173 174 175
	$('#dummy_username').val(userNameObj.value);
	$('#dummy_passwort').val(passwordObj.value);
	$('#dummy_login_form').submit();

176
	var submitFormObj = jQuery.ajax({		type: "POST",
177 178
											url: "content.php",
											data: jQuery('#'+formname).serialize(),
179
											dataType: "html",
180 181 182
											beforeSend: function() {
												showLoadIndicator();
											},
183 184 185 186 187 188 189 190 191 192 193
											success: function(data, textStatus, jqXHR) {
												if(jqXHR.responseText.indexOf('HEADER_REDIRECT:') > -1) {
													var parts = jqXHR.responseText.split(':');
													//alert(parts[1]);
													loadContent(parts[1]);
													//redirect = parts[1];
													//window.setTimeout('loadContent(redirect)', 1000);
												} else if (jqXHR.responseText.indexOf('LOGIN_REDIRECT:') > -1) {
													// Go to the login page
													document.location.href = 'index.php';
												} else {
194
													jQuery('#pageContent').html(jqXHR.responseText);
195
                                                    onAfterContentLoad('content.php', jQuery('#'+formname).serialize());
mcramer's avatar
mcramer committed
196
                                                    pageFormChanged = false;
197 198
												}
												loadMenus();
199
                                                hideLoadIndicator();
200 201
											},
											error: function() {
202
                                                hideLoadIndicator();
203
												reportError('Ajax Request was not successful.110');
cfoe's avatar
cfoe committed
204
											}
205
									});
206 207 208 209 210
	/*
	if(redirect != '') {
		loadContent(redirect);
		redirect = '';
	}
211
	document.getElementById('footer').innerHTML = 'Powered by <a href="http://www.ispconfig.org" target="_blank">ISPConfig</a>';
212
	*/
213

214 215
}

216
function submitForm(formname,target) {
217
	var submitFormObj = jQuery.ajax({		type: "POST",
218 219
											url: target,
											data: jQuery('#'+formname).serialize(),
220
											dataType: "html",
221 222 223
											beforeSend: function() {
												showLoadIndicator();
											},
224 225 226 227 228 229 230 231
											success: function(data, textStatus, jqXHR) {
												if(jqXHR.responseText.indexOf('HEADER_REDIRECT:') > -1) {
													var parts = jqXHR.responseText.split(':');
													//alert(parts[1]);
													loadContent(parts[1]);
													//redirect = parts[1];
													//window.setTimeout('loadContent(redirect)', 1000);
												} else {
232
													jQuery('#pageContent').html(jqXHR.responseText);
233
                                                    onAfterContentLoad(target, jQuery('#'+formname).serialize());
mcramer's avatar
mcramer committed
234
                                                    pageFormChanged = false;
235
												}
236
                                                hideLoadIndicator();
237 238
											},
											error: function(jqXHR, textStatus, errorThrown) {
239
                                                hideLoadIndicator();
240 241
												var parts = jqXHR.responseText.split(':');
												reportError('Ajax Request was not successful. 111');
cfoe's avatar
cfoe committed
242
											}
243
									});
244 245 246 247 248 249 250 251
	/*
	if(redirect != '') {
		loadContent(redirect);
		redirect = '';
	}
	*/
}

252
function submitFormConfirm(formname,target,confirmation) {
253
	var successMessage = arguments[3];
254
	if(window.confirm(confirmation)) {
255
		var submitFormObj = jQuery.ajax({	type: "POST",
256 257 258
											url: target,
											data: jQuery('#'+formname).serialize(),
											dataType: "html",
259 260 261
											beforeSend: function() {
												showLoadIndicator();
											},
262
											success: function(data, textStatus, jqXHR) {
263
												if(successMessage) alert(successMessage);
264 265 266 267 268 269 270 271
												if(jqXHR.responseText.indexOf('HEADER_REDIRECT:') > -1) {
													var parts = jqXHR.responseText.split(':');
													//alert(parts[1]);
													loadContent(parts[1]);
													//redirect = parts[1];
													//window.setTimeout('loadContent(redirect)', 1000);
												} else {
													jQuery('#pageContent').html(jqXHR.responseText);
272
                                                    onAfterContentLoad(target, jQuery('#'+formname).serialize());
mcramer's avatar
mcramer committed
273
                                                    pageFormChanged = false;
274
												}
275
                                                hideLoadIndicator();
276 277
											},
											error: function(jqXHR, textStatus, errorThrown) {
278
                                                hideLoadIndicator();
279 280
												var parts = jqXHR.responseText.split(':');
												reportError('Ajax Request was not successful. 111');
cfoe's avatar
cfoe committed
281
											}
282 283 284 285
									});
	}
}

286
function submitUploadForm(formname,target) {
287 288
	var handleResponse = function(loadedFrame) {
		var response, responseStr = loadedFrame.contentWindow.document.body.innerHTML;
289

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
		try {
			response = JSON.parse(responseStr);
		} catch(e) {
			response = responseStr;
		}
		var msg = '';
		var okmsg = jQuery('#OKMsg',response).html();
		if(okmsg){
			msg = '<div id="OKMsg">'+okmsg+'</div>';
		}
		var errormsg = jQuery('#errorMsg',response).html();
		if(errormsg){
			msg = msg+'<div id="errorMsg">'+errormsg+'</div>';
		}
		return msg;
305

306
    };
307

308 309
	var frame_id = 'ajaxUploader-iframe-' + Math.round(new Date().getTime() / 1000);
	jQuery('body').after('<iframe width="0" height="0" style="display:none;" name="'+frame_id+'" id="'+frame_id+'"/>');
310
	jQuery('input[type="file"]').closest("form").attr({target: frame_id, action: target}).submit();
311 312 313 314 315
	jQuery('#'+frame_id).load(function() {
        var msg = handleResponse(this);
		jQuery('#errorMsg').remove();
		jQuery('#OKMsg').remove();
		jQuery('input[name="id"]').before(msg);
316
		jQuery(this).remove();
317 318
      });

319 320 321 322 323 324 325 326 327
	/*
	if(redirect != '') {
		loadContent(redirect);
		redirect = '';
	}
	*/
}

function loadContent(pagename) {
mcramer's avatar
mcramer committed
328
  var params = arguments[1];
329
  var pageContentObject2 = jQuery.ajax({	type: "GET",
330
											url: pagename,
mcramer's avatar
mcramer committed
331
                                            data: (params ? params : null),
332
											dataType: "html",
333
											beforeSend: function() {
334
												showLoadIndicator();
335
											},
336 337 338 339 340 341 342 343
											success: function(data, textStatus, jqXHR) {
												if(jqXHR.responseText.indexOf('HEADER_REDIRECT:') > -1) {
													var parts = jqXHR.responseText.split(':');
													loadContent(parts[1]);
												} else if (jqXHR.responseText.indexOf('URL_REDIRECT:') > -1) {
													var newUrl= jqXHR.responseText.substr(jqXHR.responseText.indexOf('URL_REDIRECT:') + "URL_REDIRECT:".length);
													document.location.href = newUrl;
												} else {
344 345 346 347
													//document.getElementById('pageContent').innerHTML = jqXHR.responseText;
													//var reponse = jQuery(jqXHR.responseText);
													//var reponseScript = reponse.filter("script");
													//jQuery.each(reponseScript, function(idx, val) { eval(val.text); } );
348

349
													jQuery('#pageContent').html(jqXHR.responseText);
350
                                                    onAfterContentLoad(pagename, (params ? params : null));
mcramer's avatar
mcramer committed
351
                                                    pageFormChanged = false;
352
												}
353
                                                hideLoadIndicator();
354 355
											},
											error: function() {
356
                                                hideLoadIndicator();
357
												reportError('Ajax Request was not successful. 113');
cfoe's avatar
cfoe committed
358
											}
359
									});
360 361 362 363
}


function loadInitContent() {
364
	var pageContentObject = jQuery.ajax({	type: "GET",
365 366
											url: "content.php",
											data: "s_mod=login&s_pg=index",
367
											dataType: "html",
368 369 370
											beforeSend: function() {
												showLoadIndicator();
											},
371 372 373 374 375
											success: function(data, textStatus, jqXHR) {
												if(jqXHR.responseText.indexOf('HEADER_REDIRECT:') > -1) {
													var parts = jqXHR.responseText.split(":");
													loadContent(parts[1]);
												} else {
376
													jQuery('#pageContent').html(jqXHR.responseText);
377
                                                    onAfterContentLoad('content.php', "s_mod=login&s_pg=index");
mcramer's avatar
mcramer committed
378
                                                    pageFormChanged = false;
379
												}
380
                                                hideLoadIndicator();
381 382
											},
											error: function() {
383
                                                hideLoadIndicator();
384
												reportError('Ajax Request was not successful. 114');
cfoe's avatar
cfoe committed
385
											}
386
										});
387

388 389 390 391 392 393 394
  loadMenus();
  keepalive();
  setTimeout("setFocus()",1000);

}

function setFocus() {
395
	try {
396
		jQuery('form#pageForm').find('input[name="username"]').focus();
397 398
	} catch (e) {
	}
399 400 401 402
}


function loadMenus() {
403
  var sideNavObject = jQuery.ajax({			type: "GET",
404 405
											url: "nav.php",
											data: "nav=side",
406
											dataType: "html",
407 408 409
											beforeSend: function() {
												showLoadIndicator();
											},
410
											success: function(data, textStatus, jqXHR) {
411
                                                hideLoadIndicator();
412
												jQuery('#sideNav').html(jqXHR.responseText);
413 414
											},
											error: function() {
415
                                                hideLoadIndicator();
416
												reportError('Ajax Request was not successful. 115');
cfoe's avatar
cfoe committed
417
											}
418
									});
419 420

  var topNavObject = jQuery.ajax({			type: "GET",
421 422
											url: "nav.php",
											data: "nav=top",
423
											dataType: "html",
424 425 426
											beforeSend: function() {
												showLoadIndicator();
											},
427
											success: function(data, textStatus, jqXHR) {
428
                                                hideLoadIndicator();
429
												jQuery('#topNav').html(jqXHR.responseText);
430 431
											},
											error: function(o) {
432
                                                hideLoadIndicator();
433
												reportError('Ajax Request was not successful. 116');
cfoe's avatar
cfoe committed
434
											}
435
								});
436 437 438

}

439
function changeTab(tab,target,force) {
440 441
	if(requestsRunning > 0) return false;
	
442 443
	//document.forms[0].next_tab.value = tab;
	document.pageForm.next_tab.value = tab;
444

445 446 447
    var idel = jQuery('form#pageForm').find('[name="id"]');
    var id = null;
    if(idel.length > 0) id = idel.val();
448
    if(tabChangeDiscard == 'y' && !force) {
449
        if((idel.length < 1 || id) && (pageFormChanged == false || window.confirm(tabChangeDiscardTxt))) {
mcramer's avatar
mcramer committed
450
            var next_tab = tab;
451 452
            if(id) loadContent(target, {'next_tab': next_tab, 'id': id});
            else loadContent(target, {'next_tab': next_tab});
mcramer's avatar
mcramer committed
453 454 455 456 457 458 459 460 461
        } else {
            return false;
        }
    } else {
        if(id && tabChangeWarning == 'y' && pageFormChanged == true) {
            if(window.confirm(tabChangeWarningTxt)) {
                submitForm('pageForm', target);
            } else {
                var next_tab = tab;
462 463
                if(id) loadContent(target, {'next_tab': next_tab, 'id': id});
                else loadContent(target, {'next_tab': next_tab});
mcramer's avatar
mcramer committed
464 465 466 467 468
            }
        } else {
            submitForm('pageForm',target);
        }
    }
469
}
470

471 472 473 474 475 476
function del_record(link,confirmation) {
  if(window.confirm(confirmation)) {
          loadContent(link);
  }
}

477 478 479 480 481 482
function confirm_action(link,confirmation) {
  if(window.confirm(confirmation)) {
          loadContent(link);
  }
}

483
function loadContentInto(elementid,pagename) {
484
  var pageContentObject2 = jQuery.ajax({	type: "GET",
485
											url: pagename,
486
											dataType: "html",
487 488 489
											beforeSend: function() {
//												showLoadIndicator();
											},
490
											success: function(data, textStatus, jqXHR) {
491
//                                                hideLoadIndicator();
492
												jQuery('#'+elementid).html(jqXHR.responseText);
493 494
											},
											error: function() {
495
//                                                hideLoadIndicator();
496
												reportError('Ajax Request was not successful. 118');
cfoe's avatar
cfoe committed
497
											}
498
										});
499 500
}

501
function loadOptionInto(elementid,pagename) {
502
	var pageContentObject2 = jQuery.ajax({	type: "GET",
503
											url: pagename,
504
											dataType: "html",
505 506 507
											beforeSend: function() {
//												showLoadIndicator();
											},
508
											success: function(data, textStatus, jqXHR) {
509
//                                                hideLoadIndicator();
510 511 512 513 514 515 516 517 518 519 520 521 522
												var teste = jqXHR.responseText;
												var elemente = teste.split('#');
												el=document.getElementById(elementid);
												el.innerHTML='';
												for (var i = 0; i < elemente.length; ++i){

													var foo2 = document.createElement("option");
													foo2.appendChild(document.createTextNode(elemente[i]));
													foo2.value=elemente[i];
													el.appendChild(foo2);
												}
											},
											error: function() {
523
//                                                hideLoadIndicator();
524
												reportError('Ajax Request was not successful. 119');
cfoe's avatar
cfoe committed
525
											}
526
										});
527 528
}

529
function keepalive() {
530
	var pageContentObject3 = jQuery.ajax({	type: "GET",
531
											url: "keepalive.php",
532
											dataType: "html",
533 534 535 536 537
											success: function(data, textStatus, jqXHR) {
												setTimeout( keepalive, 1000000 );
											},
											error: function() {
												reportError('Session expired. Please login again.');
cfoe's avatar
cfoe committed
538
											}
539
										});
540 541 542 543
  	//setTimeout( keepalive, 1000000 );
}


544 545 546 547 548 549 550
<?php
$min_password_length = 5;
if(isset($server_config_array['misc']['min_password_length'])) {
	$min_password_length = $app->functions->intval($server_config_array['misc']['min_password_length']);
}
?>
var pass_minimum_length = <?php echo $min_password_length; ?>;
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
var pass_messages = new Array();

var pass_message = new Array();
pass_message['text'] = "<?php echo $wb['password_strength_0_txt']?>";
pass_message['color'] = "#d0d0d0";
pass_messages[0] = pass_message;

var pass_message = new Array();
pass_message['text'] = "<?php echo $wb['password_strength_1_txt']?>";
pass_message['color'] = "red";
pass_messages[1] = pass_message;

var pass_message = new Array();
pass_message['text'] = "<?php echo $wb['password_strength_2_txt']?>";
pass_message['color'] = "yellow";
pass_messages[2] = pass_message;

var pass_message = new Array();
pass_message['text'] = "<?php echo $wb['password_strength_3_txt']?>";
pass_message['color'] = "#00ff00";
pass_messages[3] = pass_message;

var pass_message = new Array();
pass_message['text'] = "<?php echo $wb['password_strength_4_txt']?>";
pass_message['color'] = "green";
pass_messages[4] = pass_message;

var pass_message = new Array();
pass_message['text'] = "<?php echo $wb['password_strength_5_txt']?>";
pass_message['color'] = "green";
pass_messages[5] = pass_message;

583 584
var special_chars = "`~!@#$%^&*()_+|\=-[]}{';:/?.>,<\" ";

585 586 587 588 589 590 591
function pass_check(password) {
	var length = password.length;
	var points = 0;
	if (length < pass_minimum_length) {
		pass_result(0);
		return;
	}
592

593 594 595 596
	if (length < 5) {
		pass_result(1);
		return;
	}
597 598 599 600 601 602 603
	
	var different = 0;
	
	if (pass_contains(password, "abcdefghijklnmopqrstuvwxyz")) {
		different += 1;
	}
	
604 605
	if (pass_contains(password, "ABCDEFGHIJKLNMOPQRSTUVWXYZ")) {
		points += 1;
606
		different += 1;
607
	}
608

609 610
	if (pass_contains(password, "0123456789")) {
		points += 1;
611
		different += 1;
612
	}
613

614
	if (pass_contains(password, special_chars)) {
615
		points += 1;
616
		different += 1;
617
	}
618

619
	if (points == 0 || different < 3) {
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
		if (length >= 5 && length <=6) {
			pass_result(1);
		} else if (length >= 7 && length <=8) {
			pass_result(2);
		} else {
			pass_result(3);
		}
	} else if (points == 1) {
		if (length >= 5 && length <=6) {
			pass_result(2);
		} else if (length >= 7 && length <=10) {
			pass_result(3);
		} else {
			pass_result(4);
		}
	} else if (points == 2) {
		if (length >= 5 && length <=8) {
			pass_result(3);
		} else if (length >= 9 && length <=10) {
			pass_result(4);
		} else {
			pass_result(5);
		}
	} else if (points == 3) {
		if (length >= 5 && length <=6) {
			pass_result(3);
		} else if (length >= 7 && length <=8) {
			pass_result(4);
		} else {
			pass_result(5);
		}
	} else if (points >= 4) {
		if (length >= 5 && length <=6) {
			pass_result(4);
		} else {
			pass_result(5);
		}
	}
}



function pass_result(points, message) {
	if (points == 0) {
		width = 10;
	} else {
		width = points*20;
	}
	document.getElementById("passBar").innerHTML = '<div style="float:left; height: 10px; padding:0px; background-color: ' + pass_messages[points]['color'] + '; width: ' + width + 'px;" />';
	document.getElementById("passText").innerHTML = pass_messages[points]['text'];
}
function pass_contains(pass, check) {
	for (i = 0; i < pass.length; i++) {
		if (check.indexOf(pass.charAt(i)) > -1) {
			return true;
		}
	}
	return false;
}

680
var new_tpl_add_id = 0;
681
function addAdditionalTemplate(){
682 683 684 685
    var tpl_add = jQuery('#template_additional').val();
    var addTemplate = jQuery('#tpl_add_select').val().split('|',2);
	var addTplId = addTemplate[0];
	var addTplText = addTemplate[1];
686
	if(addTplId > 0) {
687 688 689 690 691 692 693 694 695 696
        var newVal = tpl_add.split('/');
        new_tpl_add_id += 1;
        var delbtn = jQuery('<a href="#"></a>').attr('class', 'button icons16 icoDelete').click(function(e) {
            e.preventDefault();
            delAdditionalTemplate($(this).parent().attr('rel'));
        });
        newVal[newVal.length] = 'n' + new_tpl_add_id + ':' + addTplId;
	    jQuery('<li>' + addTplText + '</li>').attr('rel', 'n' + new_tpl_add_id).append(delbtn).appendTo('#template_additional_list ul');
	    jQuery('#template_additional').val(newVal.join('/'));
	    alert('additional template ' + addTplText + ' added to customer');
697
	} else {
698
	    alert('no additional template selcted');
699 700 701
	}
}

702 703 704 705 706 707 708
function delAdditionalTemplate(tpl_id){
    var tpl_add = jQuery('#template_additional').val();
	if(tpl_id) {
        // new style
		var $el = jQuery('#template_additional_list ul').find('li[rel="' + tpl_id + '"]').eq(0); // only the first
        var addTplText = $el.text();
        $el.remove();
709

710 711 712 713 714 715 716 717 718 719 720
		var oldVal = tpl_add.split('/');
		var newVal = new Array();
        for(var i = 0; i < oldVal.length; i++) {
            var tmp = oldVal[i].split(':', 2);
            if(tmp.length == 2 && tmp[0] == tpl_id) continue;
            newVal[newVal.length] = oldVal[i];
        }
        jQuery('#template_additional').val(newVal.join('/'));
		alert('additional template ' + addTplText + ' deleted from customer');
    } else if(tpl_add != '') {
        // old style
721 722 723
		var addTemplate = document.getElementById('tpl_add_select').value.split('|',2);
		var addTplId = addTemplate[0];
		var addTplText = addTemplate[1];
724 725 726 727 728 729 730 731 732

		jQuery('#template_additional_list ul').find('li:not([rel])').each(function() {
            var text = jQuery(this).text();
            if(text == addTplText) {
                jQuery(this).remove();
                return false;
            }
            return this;
        });
733

734
		var newVal = tpl_add;
735 736
        var repl = new RegExp('(^|\/)' + addTplId + '(\/|$)');
		newVal = newVal.replace(repl, '');
737
		newVal = newVal.replace('//', '/');
738
		jQuery('#template_additional').val(newVal);
739 740 741 742
		alert('additional template ' + addTplText + ' deleted from customer');
  } else {
  	alert('no additional template selcted');
  }
743

744
}
745 746 747 748 749 750 751 752 753 754 755

function getInternetExplorerVersion() {
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
756

757
function password(minLength, special, num_special){
758
	minLength = minLength || 10;
759
	if(minLength < 8) minLength = 8;
760 761
	var maxLength = minLength + 5;
	var length = getRandomInt(minLength, maxLength);
762 763 764 765 766 767 768 769 770
	
	var alphachars = "abcdefghijklmnopqrstuvwxyz";
	var upperchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var numchars = "1234567890";
    var specialchars = "!@#_";
	
	if(num_special == undefined) num_special = 0;
	if(special != undefined && special == true) {
		num_special = Math.floor(Math.random() * (length / 4)) + 1;
771
	}
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
	var numericlen = getRandomInt(1, 2);
	var alphalen = length - num_special - numericlen;
	var upperlen = Math.floor(alphalen / 2);
	alphalen = alphalen - upperlen;
	var password = "";
	
	for(i = 0; i < alphalen; i++) {
		password += alphachars.charAt(Math.floor(Math.random() * alphachars.length));
	}
	
	for(i = 0; i < upperlen; i++) {
		password += upperchars.charAt(Math.floor(Math.random() * upperchars.length));
	}
	
	for(i = 0; i < num_special; i++) {
		password += specialchars.charAt(Math.floor(Math.random() * specialchars.length));
	}
	
	for(i = 0; i < numericlen; i++) {
		password += numchars.charAt(Math.floor(Math.random() * numchars.length));
792
	}
793 794 795
	
	password = password.split('').sort(function() { return 0.5 - Math.random(); }).join('');
	
796 797 798
	return password;
}

799 800 801 802 803 804 805
<?php
$min_password_length = 10;
if(isset($server_config_array['misc']['min_password_length'])) {
	$min_password_length = $app->functions->intval($server_config_array['misc']['min_password_length']);
}
?>

Falko Timme's avatar
Falko Timme committed
806
function generatePassword(passwordFieldID, repeatPasswordFieldID){
807 808 809 810
	var oldPWField = jQuery('#'+passwordFieldID);
	var newPWField = oldPWField.clone();
	newPWField.attr('type', 'text').attr('id', 'tmp'+passwordFieldID).insertBefore(oldPWField);
	oldPWField.remove();
811
	var pword = password(<?php echo $min_password_length; ?>, false, 1);
Falko Timme's avatar
Falko Timme committed
812
	jQuery('#'+repeatPasswordFieldID).val(pword);
813
	newPWField.attr('id', passwordFieldID).val(pword).trigger('keyup').select();
814
}
815

816 817
var funcDisableClick = function(e) { e.preventDefault(); return false; };

818 819 820 821 822 823 824
function checkPassMatch(pwField1,pwField2){
    var rpass = jQuery('#'+pwField2).val();
    var npass = jQuery('#'+pwField1).val();
    if(npass!= rpass) {
		jQuery('#confirmpasswordOK').hide();
        jQuery('#confirmpasswordError').show();
		jQuery('button.positive').attr('disabled','disabled');
825 826 827 828 829 830
        jQuery('.tabbox_tabs ul li a').each(function() {
            var $this = $(this);
            $this.data('saved_onclick', $this.attr('onclick'));
            $this.removeAttr('onclick');
            $this.click(funcDisableClick);
        });
831 832 833 834 835
        return false;
    } else {
		jQuery('#confirmpasswordError').hide();
        jQuery('#confirmpasswordOK').show();
		jQuery('button.positive').removeAttr('disabled');
836 837 838 839 840
		jQuery('.tabbox_tabs ul li a').each(function() {
            var $this = $(this);
            $this.unbind('click', funcDisableClick);
            if($this.data('saved_onclick') && !$this.attr('onclick')) $this.attr('onclick', $this.data('saved_onclick'));
        });
841 842 843
    }
}

844 845 846 847
function getRandomInt(min, max){
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

848 849 850 851 852
jQuery('.addPlaceholder').live("click", function(){
	var placeholderText = jQuery(this).text();
	var template = jQuery(this).siblings(':input');
	template.insertAtCaret(placeholderText);
});
853 854 855 856 857 858

jQuery('.addPlaceholderContent').live("click", function(){
	var placeholderContentText = jQuery(this).find('.addPlaceholderContent').text();
	var template2 = jQuery(this).siblings(':input');
	template2.insertAtCaret(placeholderContentText);
});
859

860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
jQuery.fn.extend({
	insertAtCaret: function(myValue){
		return this.each(function(i) {
			if (document.selection) {
				//For browsers like Internet Explorer
				this.focus();
				sel = document.selection.createRange();
				sel.text = myValue;
				this.focus();
			} else if (this.selectionStart || this.selectionStart == '0') {
				//For browsers like Firefox and Webkit based
				var startPos = this.selectionStart;
				var endPos = this.selectionEnd;
				var scrollTop = this.scrollTop;
				this.value = this.value.substring(0, startPos)+myValue+this.value.substring(endPos,this.value.length);
				this.focus();
				this.selectionStart = startPos + myValue.length;
				this.selectionEnd = startPos + myValue.length;
				this.scrollTop = scrollTop;
			} else {
				this.value += myValue;
				this.focus();
			}
		})
	}
});