//<!-- start hide Javascript
// iCMS 2.0 functions
// (c) Iradesso Communications 2001-2006.
//
// Legal Disclaimer
// [enter it here]

// language functions

function icms_lang() {
	this.keys = Array();
	this.values = Array();
	
	this.addLang = icms_addLang;
	this.getLang = icms_getLang;
}

function icms_addLang() {
	langs = icms_addLang.arguments;
	for(i=0; i<langs.length; i+=2) {
		if(langs[i] != "" || langs[i+1] != "") {
			this.keys.push(langs[i]);
			this.values.push(langs[i+1]);
		}
	}
}

function icms_getLang(key) {
	for(i=0; i<this.keys.length; i++) {
		if(this.keys[i] == key) return this.values[i];
	}
}

var lang = new icms_lang();

// 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, strong) {
	var space = " ";
	
	// no spaces allowed
	if (password.indexOf(space) != -1) {
		return false;
	}
	
	if(strong == true) {
		// it must start with at least one letter     
		if (!(password.match(/^[a-zA-Z]+/))) return false;
		// it must contain at least one number
		if (!(password.match(/\d/))) 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_chkHexidecimal(hexidecimal) {
	if(!(hexidecimal.match(/^([a-fA-F0-9]{6})$/))) 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_chkPhone(phone) {
	if(!(phone.match(/^([0-9_\-]{7,})+$/))) 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;
}

// window functions

function icms_setStatusText(text) {
	window.status = text;
}

function icms_returnStatusText() {
	window.status = '';	
}

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 += ',width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
	newWin = window.open(url, win, options);
	newWin.focus();
	
	return newWin;
}

function icms_openDialog(url, name, width, height, options, position) {
	name = (name != null) ? name : 'Dialog';
	width = (width != null) ? width : 550;
	height = (height != null) ? height : 350;
	options = (options != null) ? options : 'resizable=yes,status=yes';
	position = (position != null) ? position : 'cw';
	
	Dialog = icms_popUpWin(url, name, width, height, options, position);
}

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

// base64 encode functions
// taken from http://ostermiller.org/calc/encode.html

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