// getElementById ---
function $gEId(pstr_id){
	return document.getElementById(pstr_id);
}
// Validate Correct URL or Email
//pstr element with URL or Email
//pw - name (URL or Email)
function validateURL(pstr, pw){
	var tmp = pstr.substr(pstr.indexOf(".")+1, (pstr.length - pstr.indexOf(".")-1))
	if(pstr.indexOf("@") == -1 || pstr.indexOf(".") == -1) {
			alert("The "+pw+" is not correct. Please correct it.");
		return false;
	} 
	return true;
}
function validateURL_v2(pstr){
	var tmp = pstr.substr(pstr.indexOf(".")+1, (pstr.length - pstr.indexOf(".")-1))
	if(pstr.indexOf("@") == -1 || pstr.indexOf(".") == -1) {
		return false;
	} 
	return true;
}
//Validate prohibit symbols in string
//pstr - string value
//field - name of feild
function validateText(pstr, field){
	if( pstr.indexOf('</') != -1 || pstr.indexOf('/>') != -1 ){
		alert("The field \'"+field+"\' includes prohibited symbol - </ \nPlease correct it.");
		return false;
	}
	return true;
}
//Validate prohibit symbols in string
//pstr - string value
//field - name of feild
function validateText(pstr, field){
	if( pstr.indexOf('</') != -1 || pstr.indexOf('/>') != -1 ){
		alert("The field \'"+field+"\' includes prohibited symbol - </ \nPlease correct it.");
		return false;
	}
	return true;
}
function validateText_v2(pstr){
	if( pstr.indexOf('</') != -1 || pstr.indexOf('/>') != -1 ){
		return false;
	}
	return true;
}

function replaceAll(pstr, orig, to){
	while (pstr.indexOf(orig) != -1){
		pstr = pstr.replace(orig, to);
	}	
	return pstr;
}
//Validate Numeric
//pstr - string value
//field - name of feild
function validateNum(pstr, field){
	if( isNaN(pstr)){
		alert("The field \'"+field+"\' Should be numeric. \nPlease correct it.");
		return false;
	}	
	return true;
}
//Validate Integer
//pstr - string value
//field - name of feild
function validateInt(pstr, field){
	if( !validateNum(pstr,field)){return false;}	
	var iVal = parseInt(pstr);
	if(parseInt(pstr) < parseFloat(pstr) ){
		alert("The field \'"+field+"\' Should be Integer. \nPlease correct it.");
		return false;		
	}
	return true;
}


//Convert form date in to query string
function formD2QStr(docForm, formatOpts) {
	var opts = formatOpts || {};
	var str = '';
	var formElem;
	var lastElemName = '';
	for (i = 0; i < docForm.elements.length; i++) {
		formElem = docForm.elements[i];
			switch (formElem.type) {
			// Text fields, hidden form elements
			case 'text':
			case 'hidden':
			case 'password':
			case 'textarea':
			case 'select-one':
				var str_value = formElem.value;
			
				while (str_value.indexOf('&') != -1){
					str_value = str_value.replace('&', '%26');
				}
				while (str_value.indexOf('=') != -1){
					str_value = str_value.replace('=', '%3D');
				}				
				str += formElem.name + '=' + encodeURI(str_value) + '&'
			break;


			// Multi-option select
			case 'select-multiple':
				var isSet = false;
				for(var j = 0; j < formElem.options.length; j++) {
					var currOpt = formElem.options[j];
					if(currOpt.selected) {
						if (opts.collapseMulti) {
							if (isSet) {
								str += ',' + encodeURI(currOpt.value);
							} else {
								str += formElem.name + '=' + encodeURI(currOpt.value);
							isSet = true;
							}
						} else {
							str += formElem.name + '=' + encodeURI(currOpt.value) + '&';
						}
					}
				}
				if (opts.collapseMulti) { str += '&'; }
				break;

			// Radio buttons
			case 'radio':
				if (formElem.checked) 
					str += formElem.name + '=' + encodeURI(formElem.value) + '&'
				break;

			// Checkboxes
			case 'checkbox':
				if (formElem.checked) {
					// Collapse multi-select into comma-separated list
					if (opts.collapseMulti && (formElem.name == lastElemName)) {
						// Strip of end ampersand if there is one
						if (str.lastIndexOf('&') == str.length-1) {
							str = str.substr(0, str.length - 1);
						}
						// Append value as comma-delimited string
						str += ',' + encodeURI(formElem.value);
					} else {
						str += formElem.name + '=' + encodeURI(formElem.value);
					}
					str += '&';
					lastElemName = formElem.name;
				}
				break;
		}
	}
	// Remove trailing separator
	str = str.substr(0, str.length - 1);
	return str;
}
function leftTrim(pstr){ while(pstr.indexOf(" ") == 0 ) {pstr = pstr.replace(" ", "");} return pstr;}
function rightTrim(pstr){var s_sp = " ";  while(pstr.charAt(pstr.length-1) == s_sp.charAt(0)){pstr = pstr.substr(0, pstr.length-1);} return pstr;}
function lrTrim(pstr){return rightTrim(leftTrim(pstr));}
