/*
+-------------------------------------------
*   PhpInk - Content Manager System
*   ========================================
*   by Szabo Viktor
*   (c) 2007-2008 SourComp
*   Web: http://www.webaction.hu
*   Email:  a d m i n @ s o u r . h u
*   ========================================
*   Filename: functions.js
*   Created on: 
*   ========================================
+-------------------------------------------
*   CHANGELOG
*   =========
*   - 
*   - 
+-------------------------------------------
*   TODO
*   =========
*   - 
*   - 
*/

function select_all(FormName, FieldName) {
  
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = !objCheckBoxes.checked;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
		  objCheckBoxes[i].checked = !objCheckBoxes[i].checked;
}

function diselect_all(FormName, FieldName) {
  
	if(!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = !objCheckBoxes.checked;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
		  objCheckBoxes[i].checked = false;
}

function enable_all(FormName, FieldName) {
  if(!document.forms[FormName])
		return;
	var objFields = document.forms[FormName].elements[FieldName];
	if(!objFields)
		return;
	var countFields = objFields.length;

	if(!countFields)
		objFields.disabled = !objFields.disabled;
	else
		for(var i = 0; i < countFields; i++)
      objFields[i].disabled = !objFields[i].disabled;
}

function openWindow(url,width,height,title) {
	var leftVal=(screen.width / 2) - width;
	var topVal = (screen.height / 2) - height;
	newWindow = window.open(url, title, "toolbar=no,location=no,scrollbars=yes,status=no,width="+width+",height="+height+",left="+leftVal+",top="+topVal+"");
}

function submitForm(FormName) {
	if(!document.forms[FormName])
		return;
	document.forms[FormName].submit();
}

function hiddenValue(FormName,Value) {
	if(!document.forms[FormName])
		return;
	document.forms[FormName].elements['hiddenValue'].value = Value;
}

function refresh() {
	var sURL = window.location;
	window.location.href = sURL;
}

function _confirm() {
  var result = confirm("Kattints az [OK] -ra ha valóban ezt akarod!");
  return result;
}

function are_u_sure(go_to) {

	var want_to_delete = confirm("Kattints az OK-ra ha valóban törölni szeretnéd!");
	
	if(want_to_delete) 
		window.location = go_to;
	else
		return false;
		
}


function sure(executeThis) {

	var want_to_delete = confirm("Kattints az OK-ra ha valóban törölni szeretnéd!");
	
	if(want_to_delete) 
		eval(executeThis);
	else
		return false;
		
}

function check_fields(fieldsArray) {
  var re = true;
  var x = fieldsArray.length;
  for(i=0;i<x;i++) {
    var field = document.getElementById(fieldsArray[i]);
    var fieldValue = field.value;
    
    if(fieldsArray[i]=='email') {
      if(check_email(fieldValue)) {
        field.style.cssText = 'background:#FFF url(/themes/_icons/tick.png) no-repeat 98% center;';
      }
      else {
        field.style.cssText = 'border:1px solid #ddd;background:#FBE3E4 url(/themes/_icons/error.png) no-repeat 98% center;color:#8a1f11;border-color:#FBC2C4;';
        re = false;
      }
    }
    else {    
      if(fieldValue == "") {
        field.style.cssText = 'border:1px solid #ddd;background:#FBE3E4 url(/themes/_icons/error.png) no-repeat 98% center;color:#8a1f11;border-color:#FBC2C4;';
        re = false;
      }
      else
        field.style.cssText = 'background:#FFF url(/themes/_icons/tick.png) no-repeat 98% center;';
    }
  }
  return re;
}

function check_pass(pass1,pass2) {
  //if(pass1.length<1) return false;
  if(pass1==pass2) return true;
  else {
    alert('A két jelszó nem egyezik!');
    return false;
  }
}

function check_email(email) {
  var reg = /^[0-9A-Za-z\.-_]+@([0-9A-Za-z-\.]+\.)+[a-z]{2,4}$/i;
  re = reg.test(email);
  if(!re) alert('A megadott e-mail cím nem tűnik valósnak! Az email címben kis és NAGY betű szerepelhet ékezet nélkül, valamint az alábbi speciális karakterek: -_.');
  return re;
}

function addFormElement(id) {
    $('formElements').innerHTML = document.getElementById(id).innerHTML;
}

function countchar(l,rid) {
  var c = l.length;
  returnto = document.getElementById(rid);
  returnto.innerHTML = c;
}

function trim(text) {
  pattern = ' ';
  num = text.split(pattern);
  for(i=0;i<num.length;i++)
    text = text.replace(pattern, ''); 
  return text;
}

/** scripted by webaction.hu **/
function price_update(a) {
  var a = trim(a);
  if(isNaN(a)) {
    alert('Csak számokat gépeljen!');
    return '';
  }
  var num = a.length;
  if(num>11) {
    alert('Biztos Ön ebben?');
    return '';
  }
  switch (num) {
  	case 4:
  		return a.charAt(0) + ' ' + a.charAt(1) + a.charAt(2) + a.charAt(3);
  		break;
  	case 5:
  		return a.charAt(0) + a.charAt(1) + ' ' + a.charAt(2) + a.charAt(3) + a.charAt(4);
  		break;
  	case 6:
  		return a.charAt(0) + a.charAt(1) + a.charAt(2) + ' ' + a.charAt(3) + a.charAt(4) + a.charAt(5);
  		break;
  	case 7:
  		return a.charAt(0) + ' ' + a.charAt(1) + a.charAt(2) + a.charAt(3) + ' ' + a.charAt(4) + a.charAt(5) + a.charAt(6);
  		break;
  	case 8:
  		return a.charAt(0) + a.charAt(1) + ' ' + a.charAt(2) + a.charAt(3) + a.charAt(4) + ' ' + a.charAt(5) + a.charAt(6) + a.charAt(7);
  		break;
  	case 9:
  		return a.charAt(0) + a.charAt(1) + a.charAt(2) + ' ' + a.charAt(3) + a.charAt(4) + a.charAt(5) + ' ' + a.charAt(6) + a.charAt(7) + a.charAt(8);
  		break;
  	case 10:
  		return a.charAt(0) + ' ' + a.charAt(1) + a.charAt(2) + a.charAt(3) + ' ' + a.charAt(4) + a.charAt(5) + a.charAt(6) + ' ' + a.charAt(7) + a.charAt(8) + a.charAt(9);
  		break;
  	case 11:
  		return a.charAt(0) + a.charAt(1) + ' ' + a.charAt(2) + a.charAt(3) + a.charAt(4) + ' ' + a.charAt(5) + a.charAt(6) + a.charAt(7) + ' ' + a.charAt(8) + a.charAt(9) + a.charAt(10);
  		break;
  	default:
  		return a;
  }
  
}
