vali_isLogin = false;
function $(id){
	return document.getElementById(id);
}

function id$(id) {
	return document.getElementById(id);
}

function id$set(id,value) {
	var obj = id$(id);
	if(obj) {
		if(obj.tagName.toLowerCase() == "div") {
			obj.innerHTML = value;
		}
		if(obj.tagName.toLowerCase() == "span") {
			obj.innerHTML = value;
		}
		if(obj.tagName.toLowerCase() == "img") {
			obj.src = value;
		}
		if(obj.tagName.toLowerCase() == "a") {
			obj.href = value;
		}
		if(obj.tagName.toLowerCase() == "input" && obj.type.toLowerCase() == "text") {
			obj.value = value;
		}
		if(obj.tagName.toLowerCase() == "input" && obj.type.toLowerCase() == "password") {
			obj.value = value;
		}
		if(obj.tagName.toLowerCase() == "input" && obj.type.toLowerCase() == "hidden") {
			obj.value = value;
		}
		if(obj.tagName.toLowerCase() == "input" && obj.type.toLowerCase() == "checkbox") {
			obj.checked = (value === 'true');
		}
		if(obj.tagName.toLowerCase() == "input" && obj.type.toLowerCase() == "radio") {
			obj.checked = (value === 'true');
		}
		if(obj.tagName.toLowerCase() == "textarea") {
			obj.value = value;
		}
		if(obj.tagName.toLowerCase() == "select") {
			for(i = 0; i < obj.options.length; i++) {
				if(obj.options[i].value == value) {
					obj.selectedIndex = i;
					break;
				}
			}
		}
		
	}
}

function id$value(id) {
	var obj = id$(id);
	if(obj) {
		if(obj.tagName.toLowerCase() == "div") {
			return obj.innerHTML;
		}
		if(obj.tagName.toLowerCase() == "span") {
			return obj.innerHTML;
		}
		if(obj.tagName.toLowerCase() == "a") {
			return obj.href;
		}
		if(obj.tagName.toLowerCase() == "input" && obj.type.toLowerCase() == "text") {
			return obj.value;
		}
		if(obj.tagName.toLowerCase() == "input" && obj.type.toLowerCase() == "password") {
			return obj.value;
		}
		if(obj.tagName.toLowerCase() == "input" && obj.type.toLowerCase() == "hidden") {
			return obj.value;
		}
		if(obj.tagName.toLowerCase() == "input" && obj.type.toLowerCase() == "checkbox") {
			return obj.checked;
		}
		if(obj.tagName.toLowerCase() == "input" && obj.type.toLowerCase() == "radio") {
			return obj.checked;
		}
		if(obj.tagName.toLowerCase() == "textarea") {
			return obj.value;
		}
		if(obj.tagName.toLowerCase() == "select") {
			return obj.options[obj.selectedIndex].value;
		}
		
	}
}

function elnm(nm){
	return document.getElementsByName(nm);
}
// CHECK/UNCHECK ALL CHECKBOX
function checkall(form, prefix, checkall) {
  var checkall = checkall ? checkall : 'chkall';
  for(var i = 0; i < form.elements.length; i++) {
    var e = form.elements[i];
    if(e.name != checkall && (!prefix || (prefix && e.name.match(prefix)))) {
      e.checked = form.elements[checkall].checked;
    }
  }
}
 
function checkValue(id,v){
	if(v){
		v = 1;
	}else{
		v = 0;
	}
	$(id).value = v;
}

function confirmdel(p) {
	var ans;
	ans = window.confirm('Are you sure want to delete this record?');
	return ans;
}

// CONVERT STROKE TO NOTHING
function convertNoStroke(ele) {
  ele = ele.replace(/\|/g,"");
  return ele;
}

// TRIM
function trim(value) {
  return value.replace(/^\s+|\s+$/g, '');
}

function getLoc(el) {
 var c = {
	x: 0,
  y: 0
 };
  
  while (el) {
   c.x += el.offsetLeft;
    c.y += el.offsetTop;
    el = el.offsetParent;
  }

 return c;
}
//=======================================================================================================================
function isNum(value) {
  if (isNaN(value) || value == '' || value == null || value == undefined) {
    return false;
  } else {
    return true;
  }
}

function isEmpty(value) {
  if (value == '' || value == null || value == undefined) {
    return true;
  } else {
    return false;
  }
}

//=======================================================================================================================

function alphanumbersonly (myfield, e, dec) {
	var key;
	var keychar;

	if (window.event) key = window.event.keyCode;
	else if (e) key = e.which;
	else return true;
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27)) {
		return true;
	// numbers
	} else if ((("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").indexOf(keychar) > -1)) return true;
	// decimal point jump
	else if (dec && (keychar == ".")) {
		myfield.form.elements[dec].focus();
		return false;
	} else return false;
} // numbersonly

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function mouseDown(e) {
	// alert('in');
	var obj,len,el,i;
	el = e.target ? e.target : e.srcElement;
	while (el.nodeType != 1) el = el.parentNode;

	if(el.className != 'divbox' && el.parentNode.className != 'divbox') {
		var div = document.getElementsByTagName('DIV')
		for(i=0;i<div.length;i++) {
			if (div[i].className == 'divbox') {
				div[i].style.display = 'none';
			}
		}
	}
}

if (document.addEventListener) {
	document.addEventListener("mousedown", mouseDown, false );
} else if (document.attachEvent) {
	document.attachEvent("onmousedown", function () { mouseDown(window.event); } );
}

function changeVisible(id){
	var obj = document.getElementById(id);
	var selIndex = obj.selectedIndex;
	var val = obj.options[selIndex].value;	
	if(val=="Others"){
		$(id+"_other").style.visibility="visible";
	}else{
		$(id+"_other").style.visibility="hidden";
	}
}



function chkPasswordStrength (v) {
	var strVerdict = 0;
	var strLevel = 0;
	
	if (v == '' || v.length < 6) {
		strLevel = 0;
		strVerdict = 'Invalid';
		
	} else if (v.length >= 6 && v.length <= 12) {
		strLevel = 1;
		strVerdict = 'Weak';
		
		if (v.match(/[a-zA-Z]/)) {
			strLevel = 1;
			strVerdict = 'Weak';
		}
		
		if ((v.match(/[a-zA-Z]/)) && v.match(/([0-9])/)){
			strLevel = 1;
			strVerdict = 'Weak';
		}
		
		if ((v.match(/[a-zA-Z]/)) && v.match(/([0-9])/) && v.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)){
			strLevel = 2;
			strVerdict = 'Normal';
		}
		
	} else if (v.length > 8) {
		strVerdict = 1;
		strVerdict = 'Weak';
		
		if (v.match(/[a-zA-Z]/)) {
			strLevel = 1;
			strVerdict = 'Weak';
		}
		
		if ((v.match(/[a-zA-Z]/)) && v.match(/([0-9])/)){
			strLevel = 2;
			strVerdict = 'Normal';
		}
		
		if ((v.match(/[a-zA-Z]/)) && v.match(/([0-9])/) && v.match(/.[!,@,#,$,%,^,&,*,?,_,~]/)){
			strLevel = 3;
			strVerdict = 'Strong';
		}
		
	}
	document.getElementById('sp').innerHTML = '<b>' + strVerdict + '</b>';
	if (strLevel == 0) {
		$('sp_1').style.backgroundColor = '#E0E0E0';
		$('sp_2').style.backgroundColor = '#E0E0E0';
		$('sp_3').style.backgroundColor = '#E0E0E0';
	} else {
		for (i=1;i<=3;i++) {
			if (strLevel >= i) {
				$('sp_'+i).style.backgroundColor = '#595959';
			}else {
				$('sp_'+i).style.backgroundColor = '#E0E0E0';
			}
		}
	}
	$('sp').innerHTML = '<font color="red">' + strVerdict + '</font>';
	
}

function chkPassField(obj, type, msg) {
	if (type == 'F') {
		if (obj.id.indexOf('txt_') > -1) {
			var id = obj.id.replace('txt_','');
			if (obj.value == msg) {
				obj.style.display = 'none';
				$(id).style.display = 'block';
				$(id).focus();
			}
		} else {
		}
	} else {
		if (obj.id.indexOf('txt_') > -1) {
		} else {
			var id = 'txt_' + obj.id;
			if (obj.value == '') {
				obj.style.display = 'none';
				$(id).style.display = 'block';
			}
		}
	}
}

function clearField (elem) {
	if (!$F(elem) || $F(elem) == 'email address') {
		 $(elem).value = '';
	}

	if (elem == 'txt_password' && $(elem).value == 'password') {
		$(elem).value = '';
		$('password').value = '';
		$('txt_password').style.display = 'none';
		$('password').style.display = 'block';
		$('password').focus();
	}
} // clearField

function resetField (elem) {
	if (!$F(elem) && elem == 'username') {
		$(elem).value = 'email address';
	}

	if (elem == 'password' && $(elem).value == '') {
		$('txt_password').value = 'password';
		$('password').value = '';
		$('password').style.display = 'none';
		$('txt_password').style.display = 'block';
		// $('txt_password').focus();
	}
} // resetField

function clearField (obj,v) {
	if (obj.value == v) {
		 obj.value = '';
	}
}

function resetField (obj,v) {
	if (obj.value == '') {
		obj.value = v;
	}
}

function chkField(obj, type, msg) {
	if (type == 'F') {
		if(obj.value==msg) {
			obj.value = '';
			obj.style.color = '#000000';
		}
	} else {
		if(obj.value=='') {
			obj.value = msg;
			obj.style.color = '#888888';
		}
	}
}

function openIWin(title,url,w,h,refresh) {
	if (isEmpty(refresh)) refresh = 0;
  if (document.all || document.layers) {
   sw = screen.availWidth;
   sh = screen.availHeight;
  } else {
   sw = 800;
   sh = 600;
  }
	
	Lightview.show({
	  href: url,
	  rel: 'iframe',
	  title: title,
	  options: {
			width: w, height: h
		}
	});
	
	if (refresh == 1) {
		document.observe('lightview:hidden', function() {
			window.location.href = window.location.href;
		});
	}
}

function openWin(url,w,h,sc) {
  if (document.all || document.layers) {
   sw = screen.availWidth;
   sh = screen.availHeight;
  } else {
   sw = 800;
   sh = 600;
  }
  if (isEmpty(w)) w = "500";
  if (isEmpty(h)) h = "500";
	if (isEmpty(sc)) sc = 'yes';
  
  var leftPos = (sw-w)/2, topPos = (sh-h)/2;
  
  //var child = window.open(url,null,'width='+w+',height='+h+',top='+topPos+',left='+leftPos+',status=no,toolbar=no,menubar=no,resizable=yes,location=no,scrollbars='+sc+'');
  var child = window.open(url,null,'width='+w+',height='+h+',top='+topPos+',left='+leftPos+',status=no,toolbar=yes,menubar=yes,resizable=yes,location=yes,scrollbars=yes');
  child.focus();
}

function redirect(page){
	location.href = page;
}

function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) {
	docHt = doc.height;
  }else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);

  }

  return docHt;
}

function checkValidation(formInput) {
    if (stringEmpty(formInput)) {
		return (false);
    } else if (noAtSign(formInput)) {
		return (false);
    } else if (nothingBeforeAt(formInput)) {
		return (false);
    } else if (noLeftBracket(formInput)) {
		return (false);
    } else if (noRightBracket(formInput)) {
		return (false);
    } else if (noValidPeriod(formInput)) {
		return (false);
    } else if (noValidSuffix(formInput)) {
		return (false);
    } else {
		return (true);
    }
}

function checkValid (formField) {
    if ( checkValidation ( formField ) == true ) {
        alert ( 'E-Mail Address Validates OK' );
    }

    return ( false );
}

function stringEmpty (formField) {
    // CHECK THAT THE STRING IS NOT EMPTY
    if ( formField.length < 1 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function noAtSign (formField) {
    // CHECK THAT THERE IS AN '@' CHARACTER IN THE STRING
    if (formField.indexOf ('@', 0) == -1) {
        return ( true )
    } else {
        return ( false );
    }
}

function nothingBeforeAt (formField) {
    // CHECK THERE IS AT LEAST ONE CHARACTER BEFORE THE '@' CHARACTER
    if ( formField.indexOf ( '@', 0 ) < 1 ) {
        return ( true )
    } else {
        return ( false );
    }
}

function noLeftBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR LEFT BRACKET
    if ( formField.indexOf ( '[', 0 ) == -1 && formField.charAt (formField.length - 1) == ']') {
        return ( true )
    } else {
        return ( false );
    }
}

function noRightBracket (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN CHECK FOR RIGHT BRACKET
    if (formField.indexOf ( '[', 0 ) > -1 && formField.charAt (formField.length - 1) != ']') {
        return ( true );
    } else {
        return ( false );
    }
}

function noValidPeriod (formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf ( '@', 0 ) > 1 && formField.charAt (formField.length - 1 ) == ']')
        return ( false );

    // CHECK THAT THERE IS AT LEAST ONE PERIOD IN THE STRING
    if (formField.indexOf ( '.', 0 ) == -1)
        return ( true );

    return ( false );
}

function noValidSuffix(formField) {
    // IF EMAIL ADDRESS IN FORM 'user@[255,255,255,0]', THEN WE ARE NOT INTERESTED
    if (formField.indexOf('@', 0) > 1 && formField.charAt(formField.length - 1) == ']') {
        return ( false );
    }

    // CHECK THAT THERE IS A TWO OR THREE CHARACTER SUFFIX AFTER THE LAST PERIOD
    var len = formField.length;
    var pos = formField.lastIndexOf ( '.', len - 1 ) + 1;
    if ( ( len - pos ) < 2 || ( len - pos ) > 4 ) {
        return ( true );
    } else {
        return ( false );
    }
}

function currentDatetime(){
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	var second = currentTime.getSeconds() ;
	if(day < 10){
		day = "0" + day;
	}
	if(month < 10){
		month = "0" + month;
	}
	if(hours < 10){
		hours = "0" + hours;
	}
	if(minutes < 10){
		minutes = "0" + minutes;
	}
	if(second < 10){
		second = "0" + second;
	}
	var current = day + "/" + month + "/" + year + " " + hours + ":" + minutes + ":" + second;
	
	return current;
}

function currentDate(){
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	var second = currentTime.getSeconds() ;

	if(day < 10){
		day = "0" + day;
	}
	if(month < 10){
		month = "0" + month;
	}
	var current = day + "/" + month + "/" + year;
	
	return current;
}

function currentTime(){
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	var second = currentTime.getSeconds() ;
	
	if(hours < 10){
		hours = "0" + hours;
	}
	if(minutes < 10){
		minutes = "0" + minutes;
	}
	if(second < 10){
		second = "0" + second;
	}
	var current = hours + ":" + minutes + ":" + second;
	
	return current;
}

function getRadioCheckedValue(radioObj) {
	for(var i = 0; i < radioObj.length; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
			break;
		}
	}
	return 0;
}

function addopt(id,value,display) {
	var elSel = document.getElementById(id);
	var elOptNew = document.createElement("option");
	elOptNew.text = display;
	elOptNew.value = value;
	var elOptOld = elSel.options[elSel.options.length];  
	try {
	  elSel.add(elOptNew, elOptOld);
	} catch(ex) {
	  elSel.add(elOptNew, elSel.options.length);
	}
}

function removeallopt(id) {
	while(document.getElementById(id).options.length > 0)
		document.getElementById(id).remove(0);
}

function fireEvent(obj,evt){
	var fireOnThis = obj;
	if( document.createEvent ) {
	  var evObj = document.createEvent('MouseEvents');
	  evObj.initEvent( evt, true, false );
	  fireOnThis.dispatchEvent(evObj);
	} else if( document.createEventObject ) {
	  fireOnThis.fireEvent('on'+evt);
	}
}

function enter(e,func){
	var unicode=e.keyCode? e.keyCode : e.charCode
	if(unicode == 13) {
		setTimeout(func,100);
	}
}

var Url = {
 
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}

function node_remove(theVar){
	var theParent = theVar.parentNode;
	theParent.removeChild(theVar);
}
function node_addEvent(obj, evType, fn){
	if(obj.addEventListener)
	    obj.addEventListener(evType, fn, true)
	if(obj.attachEvent)
	    obj.attachEvent("on"+evType, fn)
}
function node_removeEvent(obj, type, fn){
	if(obj.detachEvent){
		obj.detachEvent('on'+type, fn);
	}else{
		obj.removeEventListener(type, fn, false);
	}
}

function prepare_submission() {
	var v = "";
	var doc = document.getElementsByTagName("input");
	for(var i = 0; i < doc.length; i++) {
		if(doc[i].id == "") continue;
		if(v == "") {
			if(doc[i].type=="checkbox") {
				if(doc[i].checked) {
					v += doc[i].id + "|||||" + doc[i].value;
				} else {
					v += doc[i].id + "|||||";
				}
			} else {
				v += doc[i].id + "|||||" + id$value(doc[i].id);
			}
		} else {
			if(doc[i].type=="checkbox") {
				
				if(doc[i].checked) {
					v += "|||||" + doc[i].id + "|||||" + doc[i].value;
				} else {
					v += "|||||" + doc[i].id + "|||||";
				}
			} else {
				v += "|||||" + doc[i].id + "|||||" + id$value(doc[i].id);
			}
		}
	}
	doc = document.getElementsByTagName("select");
	for(var i = 0; i < doc.length; i++) {
		if(v == "") {
			v += doc[i].id + "|||||" + id$value(doc[i].id);
		} else {
			v += "|||||" + doc[i].id + "|||||" + id$value(doc[i].id);
		}
	}
	doc = document.getElementsByTagName("textarea");
	for(var i = 0; i < doc.length; i++) {
		if(v == "") {
			v += doc[i].id + "|||||" + id$value(doc[i].id);
		} else {
			v += "|||||" + doc[i].id + "|||||" + id$value(doc[i].id);
		}
	}
	return v;
}

