
// image functions
function icms_swapImgRestore() { //v3.0
  var i,x,a=document.icms_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function icms_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.icms_p) d.icms_p=new Array();
    var i,j=d.icms_p.length,a=icms_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.icms_p[j]=new Image; d.icms_p[j++].src=a[i];}}
}

function icms_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=icms_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function icms_swapImage() { //v3.0
  var i,j=0,x,a=icms_swapImage.arguments; document.icms_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=icms_findObj(a[i]))!=null){document.icms_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// number functions
function icms_getRandomNumber(number) {
	day = new Date();
	seed = day.getTime();
	return parseInt(((seed - (parseInt(seed/1000,10) * 1000)) /10) /100 * number + 1,10);
}

// date functions
function icms_chkY2k(number) {
	return (number < 1000) ? number + 1900 : number; 
}

function icms_chkDate(month, day, year) {
	var unvaliddate = day + '/' + month + '/' + year;
	var date = new Date(year-0,month-1,day-0);
	var validdate = date.getDate() + '/' + (date.getMonth()+1) + '/' + icms_chkY2k(date.getYear());
	
	if (unvaliddate != validdate) {
		return false;
	}
	return true;
}

function icms_getValiddateUTC(month, day, year, hour, minute) {
	if (icms_chkDate(month, day, year)) {
		var date = new Date(year-0,month-1,day-0,hour-0,minute-0,0);
		return Date.UTC(icms_chkY2k(date.getYear()),date.getMonth(),date.getDate(),hour,minute,0);
	} else {
		return null;
	}
}

// string functions
function icms_chkName(name) {
	if(!(name.match(/^[a-zA-Z0-9\_]+$/))) return false;
	return true;
}

function icms_chkPassword(password, strength) {
	var space = " ";
	
	// no spaces allowed
	if (password.indexOf(space) != -1) return false;
	
	if(strength > 0) {
		// it must contain at least one number
		if (!(password.match(/\d/))) return false;
		// it must contain at least one lowercase or uppercase letter
		if (!(password.match(/[a-zA-Z]/))) return false;
	}
	
	if(strength > 1) {
		// it must start with at least one letter
		if (!(password.match(/^[a-zA-Z]+/))) return false;
		// it must contain at least one uppercase letter
		if (!(password.match(/[A-Z]/))) return false;
		// it password contain at least one lowercase letter
		if (!(password.match(/[a-z]/))) return false;
		// it must contain at least one special character #,@,%,!
		if (!(password.match(/\W+/))) return false;
	}
	
	return true;
}

function icms_chkEmail(email) {
	if(!(email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/))) return false;
	return true;
}

function icms_chkUrl(url) {
	if(!(url.match(/^https?:\/\/([\w\.]+)+(:\d+)?(\/(\w\/_\.]*(\?\S+)?)?)?$/))) return false;
	return true;
}

function icms_chkNumber(number) {
	if(!(number.match(/^[\d]+$/))) return false;
	return true;
}

// formatting functions
function icms_to2Decimalplaces(n) {
	var s = "" + Math.round(n * 100) / 100
	var i = s.indexOf('.')
	if (i < 0) return s + ".00"
	var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3)
	if (i + 2 == s.length) t += "0"
	return t
}

function icms_reformatName(obj) {
	obj.value = obj.value.toLowerCase();	
}

function icms_reformatPhoneNumber(obj) {
	// extract all numbers
	var number = '';
	for(var i=0; i<obj.value.length; i++) {
		if(obj.value.charAt(i).match(/[\d]/) != null) {
			number += obj.value.charAt(i);
		}
	}
	
	if(number.length == 7) {
		obj.value = number.substr(0,3) + '-' + number.substr(3,4);	
	} else if(number.length == 10) {
		obj.value = '(' + number.substr(0,3) + ') ' + number.substr(3,3) + '-' + number.substr(6,4);	
	} else {
		obj.value = number;
	}
}

// display functions
function icms_display_required(required) {
	var count = 0;
	var message = lang.getLang('icms_functions_fieldsrequired') + "\n";
	for(var i=0; i<required.length; i++) {
		if(required[i] != "" || required[i] != null) {
			message = message + " - " + required[i] + "\n";
			count += 1;
		}
	}
	
	if(count > 0) {
		alert(message);
		return false;
	} else {
		return true;
	}
}


function icms_display_error(errors) {
	var count = 0;
	var message = lang.getLang('icms_functions_errorsoccured') + "\n";
	for(var i=0; i<errors.length; i++) {
		if(errors[i] != "" || errors[i] != null) {
			message = message + " - " + errors[i] + "\n";
			count += 1;
		}
	}
	
	if(count > 0) {
		alert(message);
		return false;
	} else {
		return true;
	}
}

// window functions
function icms_popUpWin(url, win, width, height, options, position) {
	var defaultPos = 20;
		
	if(position == "cs") {
		leftPos = (screen.width) ? (screen.width-width)/2 : defaultPos;
		topPos = (screen.height) ? (screen.height-height)/2 : defaultPos;
	} else if(position == "cw") {
		leftPos = (document.body.clientWidth) ? (document.body.clientWidth-width)/2 : defaultPos;
		topPos = (document.body.clientHeight) ? (document.body.clientHeight-height)/2 : defaultPos;
		leftPos += (window.screenLeft) ? window.screenLeft : window.screenX;
		if(leftPos.toString() == "NaN") {
			leftPos = (screen.width) ? (screen.width-width)/2 : defaultPos;			
		}
		topPos = (topPos < 100) ? 100 : topPos;
	} else {
		leftPos = defaultPos;
		topPos = defaultPos;
	}
	
	options += (options != '') ? ',width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos : '';
	newWin = window.open(url, win, options);
	if(window.focus) newWin.focus();
	
	return newWin;
}

function icms_openDialog(url) {
	Dialog = icms_popUpWin(url, 'Dialog', 550, 350, 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0');
}

// base64 encode functions
var END_OF_INPUT = -1;

var base64Chars = new Array(
    'A','B','C','D','E','F','G','H',
    'I','J','K','L','M','N','O','P',
    'Q','R','S','T','U','V','W','X',
    'Y','Z','a','b','c','d','e','f',
    'g','h','i','j','k','l','m','n',
    'o','p','q','r','s','t','u','v',
    'w','x','y','z','0','1','2','3',
    '4','5','6','7','8','9','+','/'
);

var reverseBase64Chars = new Array();
for (var i=0; i < base64Chars.length; i++){
    reverseBase64Chars[base64Chars[i]] = i;
}

var base64Str;
var base64Count;
function setBase64Str(str){
    base64Str = str;
    base64Count = 0;
}

function readBase64(){    
    if (!base64Str) return END_OF_INPUT;
    if (base64Count >= base64Str.length) return END_OF_INPUT;
    var c = base64Str.charCodeAt(base64Count) & 0xff;
    base64Count++;
    return c;
}

function encodeBase64(str){
    setBase64Str(str);
    var result = '';
    var inBuffer = new Array(3);
    var lineCount = 0;
    var done = false;
    while (!done && (inBuffer[0] = readBase64()) != END_OF_INPUT){
        inBuffer[1] = readBase64();
        inBuffer[2] = readBase64();
        result += (base64Chars[ inBuffer[0] >> 2 ]);
        if (inBuffer[1] != END_OF_INPUT){
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30) | (inBuffer[1] >> 4) ]);
            if (inBuffer[2] != END_OF_INPUT){
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c) | (inBuffer[2] >> 6) ]);
                result += (base64Chars [inBuffer[2] & 0x3F]);
            } else {
                result += (base64Chars [((inBuffer[1] << 2) & 0x3c)]);
                result += ('=');
                done = true;
            }
        } else {
            result += (base64Chars [(( inBuffer[0] << 4 ) & 0x30)]);
            result += ('=');
            result += ('=');
            done = true;
        }
        lineCount += 4;
        if (lineCount >= 76){
            result += ('\n');
            lineCount = 0;
        }
    }
    return result;
}