﻿//alert('common.js');

AX_UserAgent = navigator.userAgent;
AX_Opera = (AX_UserAgent.indexOf("Opera") != -1);
AX_DOM = (document.getElementById) ? true : false;
AX_NS4 = (document.layers) ? true : false;
AX_IE = (document.all && !AX_Opera ) ? true : false;
AX_IE4 = AX_IE && !AX_DOM;
AX_Mac = (navigator.appVersion.indexOf("Mac") != -1);
AX_IE4M = AX_IE4 && AX_Mac;

//alert('AX_DOM=' + AX_DOM + 'AX_NS4=' + AX_NS4 + 'AX_IE=' + X_IE + 'AX_IE4=' + AX_IE4);

function selectTheValue(selObj, sValue)
{
	for(var i=0; i<selObj.options.length; i++)
	{
		if(selObj.options[i].value == sValue)
		{
			selObj.selectedIndex = i;
			return;
		}
	}
}
function selectTheText(selObj, sText){
	for(var i=0; i<selObj.options.length; i++)
	{
		if(selObj.options[i].text == sText)
		{
			selObj.selectedIndex = i;
			break;
		}
	}
	return false;
}
function selectedText(selObj){
    return selObj[selObj.selectedIndex].text;
}
function selectedValue(selObj){
    return selObj[selObj.selectedIndex].value;
}

function isChecked(selObj){
    ISCHECKED = false;  

	if (selObj.length == undefined){
		if(selObj.checked){
			ISCHECKED = true;
		}
	}
	else {
	    for(i=0;i<selObj.length;i++){
	        if(selObj[i].checked){
	            ISCHECKED = true;   
	            break;
	        }
	    }
	}
    return ISCHECKED;
}

function checkedValue(selObj){
    CHECKEDVALUE = new Array(); 
    for(i=0;i<selObj.length;i++){
        if(selObj[i].checked){
            CHECKEDVALUE[CHECKEDVALUE.length] = selObj[i].value;
        }
    }
    return CHECKEDVALUE;    
}

/*
 * -----------------------------------------------------------------------------------
 * resize the window START
 *-----------------------------------------------------------------------------------
 */

var windowBorder = 6;
var windowTitleHeight = 24;
var windowXpBorderOffset = 6;

function heightPercentToInt(r_Height){
	if(r_Height.indexOf('%')!=-1){		
		r_Height=eval((parseInt(r_Height.replace('%',''))/100)*parseInt(window.top.document.body.clientHeight));
	}
	return r_Height;
}
function widthPercentToInt(r_Width){
 	if(r_Width.indexOf('%')!=-1){		
		r_Width=eval((parseInt(r_Width.replace('%',''))/100)*parseInt(window.top.document.body.clientWidth));
	}
	return r_Width;
}
 
 function docAlignVertical(d_myHeight, orientation){
	if(orientation == 'middle'){
		docTop = (parseInt(window.top.document.body.clientHeight) - parseInt(d_myHeight) ) / 2 ;	
	}else if(orientation == 'top'){
		docTop = 0;
	}else if(orientation == 'bottom'){
		docTop = parseInt(window.top.document.body.clientHeight) - parseInt(d_myHeight) - 5;
	}
 	return docTop;
}
function docAlignHorizontal(d_myWidth, orientation){
	if(orientation == 'center'){
		docLeft = (parseInt(window.top.document.body.clientWidth) - parseInt(d_myWidth)) / 2 ;  	
	}else if(orientation == 'left'){
		docLeft = 0 ; 
	}else if(orientation == 'right'){
		docLeft = parseInt(window.top.document.body.clientWidth) - parseInt(d_myWidth) - 5;  	
	}
	return docLeft;
}
function windowTop(P_myHeight){
 	winTop = (screen.AvailHeight -  windowTitleHeight - windowBorder - P_myHeight - windowXpBorderOffset ) / 2 ;
 	return winTop;
}
function windowLeft(P_myWidth){
	winLeft = ((screen.AvailWidth - windowBorder*2) - P_myWidth ) / 2 ;  	
	return winLeft;
}
	
function resizeTo800600(){
	try{
		window.moveTo(parseInt(windowLeft(800)), parseInt(windowTop(570)));
		window.resizeTo(800,570);
		window.top.windowSize = '800600';
	}catch(e){
		//alert(e.description);		
	}
}
function resizeTo1024768(){
	try{
		window.moveTo(parseInt(windowLeft(1020)), parseInt(windowTop(710)));
		//window.moveTo(0, 0);
		window.resizeTo(1032,746);
		window.top.windowSize = '1024768';
	}catch(e){
		//alert(e.description);		
	}
}
function resizeToMax(){	
	try{
		window.top.windowSize = 'max';
		if(!AX_IE){		
			toWidth = screen.width;
			toHeight = screen.height - 60;
		}else{
			toWidth = screen.AvailWidth;
			toHeight = screen.AvailHeight;		

		}		
		window.top.resizeTo(toWidth, toHeight);	
		window.top.moveTo(0, 0);		
	}catch(e){
		//alert(e.description);		
	}
}


/*
 * -----------------------------------------------------------------------------------
 * resize the window End
 *-----------------------------------------------------------------------------------
 */



/**
 * Sets a Cookie with the given name and value.     START
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */

function setCookie(name, value, expires, path, domain, secure)
{
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */

function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */

function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}



/**
 * Sets a Cookie with the given name and value.  End
 *
 */
 
 
 
 function Trim(str){
    var tmp_value = str;
    var tmp_index;
    tmp_index_1 = "" ;
    while (tmp_index_1 != -1)
   {			
			tmp_index_1 = tmp_value.substr(0,1).indexOf(" ");
			if(tmp_index_1 != -1){
				tmp_value = tmp_value.replace(" ", "");
			}
   }
   tmp_index_1 = "" ;
    while (tmp_index_1 != -1)
   {    
			tmp_index_1 = tmp_value.substr(tmp_value.length-1,1).indexOf(" ");      
			if(tmp_index_1 != -1){
        		tmp_value = tmp_value.substr(0,tmp_value.length-1);
			}        
   }
    return(tmp_value);
}

function TrimAllSpace(StringObj){
    var tmp_value = StringObj;
    var tmp_index;
    tmp_index_1 = "" ;
    while (tmp_index_1 != -1)
   {
        tmp_value = tmp_value.replace(" ", "");
        tmp_index_1 = tmp_value.indexOf(" ");
   }
    return(tmp_value);
}

function ReplaceALLChar(StringObj,StrForm, StrTo){

    var tmp_value = StringObj;
    var tmp_index;
    tmp_index_1 = "" ;

    while (tmp_index_1 != -1){
            tmp_value = tmp_value.replace(StrForm, StrTo);
            tmp_index_1 = tmp_value.indexOf(StrForm);
    }
    return(tmp_value);
}

function isPositiveInt(NUM){
    if(isNaN(NUM)){
        return false;
    }else if(NUM < 0){
        return false;
    }else if(NUM.toString().indexOf('.')!=-1){
        return false;
    }   
    return true;
}

//+ Check enter key ----------------------------------------------------------

function enterToSubmit(func, thisObject, event){
	if(document.all){
    	if(event.keyCode == 13){
			func();
		}		
	}else if(thisObject != undefined && event != undefined){
		document.captureEvents(Event.KEYPRESS);
    	if(event.keyCode == '13'){		
			func();
		}		
	}
}

/**
	*
	* Merge Form method  ----------------- ----------------- -----------------
	*
	*/

function cloneField(oField){
				nField = document.createElement('INPUT');
				nField.type = oField.type;
				nField.name = oField.name
				nField.id = oField.id;
				nField.value = oField.value;
				return nField ;
}

/*
function SubmitForm(){
				formAry = [document.all.formA, document.all.formB, document.all.formC];
				myForm = MergeForm(formAry);        
				myForm.action = '';
				myForm.method = 'get';      
				myForm.target = '_self';
				//myForm.submit();
}
*/

function MergeForm(oformAry){

				if(oformAry.length == 0){
								iframeObj = oformAry;
								oformAry = new Array();
								for(i=0;i<iframeObj.document.forms.length;i++){
												oformAry[i] = iframeObj.document.forms[i];
								}
				}
				tmpForm = document.createElement('FORM');
				tmpForm.name = 'tmpForm';
				tmpForm.style.display = 'inline';
				document.body.appendChild(tmpForm);

				for(i=0;i<oformAry.length;i++){
								for(k=0;k<oformAry[i].elements.length;k++){
												originalField = oformAry[i].elements[k];
												if(!originalField.readOnly && !originalField.disabled && (originalField.tagName == 'INPUT' || originalField.tagName == 'SELECT' || originalField.tagName == 'TEXTAREA' ) ){
																newField = cloneField(originalField);
																tmpForm.appendChild(newField);                  
																if(originalField.type == 'checkbox' ){
																				newField.checked = originalField.checked;
																}else if(originalField.type == 'select-one' ){                      
																				newField.value = originalField[originalField.selectedIndex].value;
																}else if(originalField.type == 'radio' ){
																				newField.checked = originalField.checked;
																}
																
												}
								}
				}
				/*
				tmpStr = '';    
				for(j=0;j<tmpForm.elements.length;j++){ 
								pfield = tmpForm.elements[j];
								tmpStr = tmpStr + '<input type=\"' + pfield.type + '\" name=\"' + pfield.name + '\" id=\"' + pfield.id + '\" value=\"' + pfield.value + '\" > ' + '\n';
				}
				alert(tmpStr);
				*/
				return tmpForm ;
}


function isValidEmail(emailStr) {
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]!~\\'#$%&*\\^+\\=\\{\\}\\|?/";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
					//alert("Email address seems incorrect (check @ and .'s)");
					return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
					if (user.charCodeAt(i)>127) {
									//alert("Ths username contains invalid characters.");
									return false;
					}
	}
	for (i=0; i<domain.length; i++) {
					if (domain.charCodeAt(i)>127) {
									//alert("Ths domain name contains invalid characters.");
									return false;
					}
	}
	if (user.match(userPat)==null) {
					//alert("The username doesn't seem to be valid.");
					return false;
	}

	var IPArray=domain.match(ipDomainPat);
	
	if (IPArray!=null) {
					for (var i=1;i<=4;i++) {
									if (IPArray[i]>255) {
													//alert("Destination IP address is invalid!");
													return false;
									}
					}
					return true;
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
					if (domArr[i].search(atomPat)==-1) {
									//alert("The domain name does not seem to be valid.");
									return false;
					}
	}

	if (checkTLD && domArr[domArr.length-1].length!=2 &&
					domArr[domArr.length-1].search(knownDomsPat)==-1) {
					//alert("The address must end in a well-known domain or two letter " + "country.");
					return false;
	}
	if (len<2) {
					//alert("This address is missing a hostname!");
					return false;
	}
	return true;
}

function checkChars(obj){
    var Str = obj.value;
    var charCode = '';
    for(i=0;i<Str.length;i++){
        //char = Str.charAt(i);
        charCode = Str.charCodeAt(i);       
        if( !((charCode >= 97 && charCode <= 122 ) ||               //a-z
            (charCode >= 65 && charCode <= 90 ) ||                  //A-Z
            (charCode >= 48 && charCode <= 57 ) ||                  //0-9
            (charCode == 32 )                                       //space
            )){
            return false;           
        }
    }
    return true;
}


/* ----------- Select All Checkbox Colection (START) ----------------------------- */
/*
id="fieldName_ALL" name="fieldName_ALL" onclick="checkboxSelectAll(this, event)" 
id="fieldName" name="fieldName" onclick="checkboxSelectAllItemCheckbox(this, event)" 
<!-- script --> checkboxSelectAllOnload('fieldName') <!-- /script -->
*/

function checkboxSelectAll(thisObj, e){
	var objName = thisObj.name.substr(0, eval(thisObj.name.length - 4));
	var checkboxCol = document.getElementsByName(objName);
	for(i=0; i<checkboxCol.length; i++){
		checkboxCol[i].checked = thisObj.checked;
	}	
}
function checkboxSelectAllItemCheckbox(thisObj, e){
	var objName = thisObj.name;
	var checkboxCol = document.getElementsByName(objName);	
	var objAll = document.getElementById(thisObj.name + '_All');
	var selectedAll = true;
	for(i=0; i<checkboxCol.length; i++){
		if(!checkboxCol[i].checked){
			selectedAll = false;
		}
	}
	objAll.checked = selectedAll;
}
function checkboxSelectAllOnload(objName){
	var checkboxCol = document.getElementsByName(objName);	
	var objAll = document.getElementById(objName + '_All');
	var selectedAll = true;
	for(i=0; i<checkboxCol.length; i++){
		if(!checkboxCol[i].checked){
			selectedAll = false;
		}
	}
	objAll.checked = selectedAll;
}		

/* ----------- Select All Checkbox Colection (END) ----------------------------- */





function getCoords(element, c_type, c_dir){
	col=element.getClientRects();
	bndrct=element.getBoundingClientRect();
	for(rctnum = 0; rctnum <  col.length;) {
		try{
			rct = col[rctnum];      
			if(c_type == 'screen'){
							typeObj = rct;
			}else if(c_type == 'document'){
							typeObj = bndrct;
			}
			if(c_dir == 'top'){
							return typeObj.top;
			}else if(c_dir == 'left'){
							return typeObj.left;
			}else if(c_dir == 'bottom'){
							return typeObj.bottom;
			}else if(c_dir == 'right'){
							return typeObj.right;
			}
			rctnum++;
		}catch(e){
			alert(e.description);
		}
	}
}


/* ---------------------------------------- FileField ---------------------------------------- */
function deleteUploadFile(thisObj, fileName, fileWidth){
	fileWidth = (fileWidth == undefined) ? '73' : fileWidth;
	var thisDiv = thisObj.parentNode;
	thisDiv.innerHTML = '<input class="inputFile" type="file" id="' + fileName + '" name="' + fileName + '" value="" size="' + fileWidth + '" />';
}	
/*
<input class="inputFile" type="file" id="" name="" value="" size="73" />
<div class="inputFileDiv" >
<input class="inputFileText" type="text" value="phone_image_front.jpg" readonly="readOnly" size="73" />
<input class="inputFileButton" type="button" value="Delete" onClick="deleteUploadFile(this, 'Material_1')" />
</div>
*/
/* ---------------------------------------- FileField ---------------------------------------- */








function getUrlPmr(pmrStr, pName){
		if(pmrStr != ''){
				pmrStr = pmrStr.substr(pmrStr.indexOf('?')+1, pmrStr.length).toString();
				var pmr = pmrStr.replace('?', '').toString(); 
				var tmpPmrAry = pmr.split('&');
				for(i=0; i<tmpPmrAry.length; i++){
						if(tmpPmrAry[i].indexOf(pName) != -1){
								return tmpPmrAry[i].replace( pName +'=', '');           
						}
				}
		}
		return false;
}






