//=================================
// Title : Javascript functions
// Date : 7/12/2005 3:25PM
// Writer : SooNa Eun
//=================================

//======================================================
// reading cookies
//======================================================
function getCookie(name)
{
	var Found = false;
	var start, end;
	var i = 0;

 	while (i <= document.cookie.length)
 	{
		start = i;
		end = start + name.length;

 		if (document.cookie.substring(start, end) == name)
 		{
 			Found = true;
 			break;
 		}
 		i++
	}
	
	if (Found == true)
 	{
 		start = end + 1;
 		end = document.cookie.indexOf(";", start);

        if (end < start)
        	end = document.cookie.length;

        return document.cookie.substring(start, end);
    }

    return ""
}


//======================================================
// File Check up
//======================================================
function isJPGFile( fileName ) {
    var fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1);
    fileSuffix = fileSuffix.toLowerCase();

	var FileOK = false;
    FileArray=new Array(0);
    
    FileArray[0] = "jpg";
    
	for (i=0; i<=0; i++) {
		if (FileArray[i] == fileSuffix) {
			FileOK = true;
			break;
		}
	}        	
	return FileOK;      
}

function isFile( fileName ) {
    var fileSuffix = fileName.substring(fileName.lastIndexOf(".") + 1);
    fileSuffix = fileSuffix.toLowerCase();

	var FileOK = false;
    FileArray=new Array(1);
    
    FileArray[0] = "jpg";
    FileArray[1] = "gif";        
    
	for (i=0; i<=1; i++) {
		if (FileArray[i] == fileSuffix) {
			FileOK = true;
			break;
		}
	}        
	
	return FileOK;      
}

function strLengthByte(str)
{
	var totLength = 0;
	for (var i=0;i<str.length;i++)
		totLength += strCharByte(escape(str.charAt(i)));
	return totLength;
}

function strCharByte(chStr) {
	if (chStr.substring(0, 2) == '%u')
	{
		if (chStr.substring(2,4) == '00')
			return 1;
		else
			return 2;
	}
	else if (chStr.substring(0,1) == '%')
	{
		if (parseInt(chStr.substring(1,3), 16) > 127)
			return 2;
		else
			return 1;
	}
	else
		return 1;
}

//======================================================
// checking board
//======================================================

function bbs_validate(form) {
if (form.writer.value=="")  {alert("\nPlease enter your name."); form.writer.focus(); return false;}
if (form.email.value != "") {
	var atPos = form.email.value.indexOf('@');
	if (atPos < 1 || atPos == (form.email.value.length - 1))	{
		alert("\nYour email address is invalid.\nPlease check your email address."); form.email.focus(); return false;
	}

	var pointPos = form.email.value.indexOf('.');
	if (pointPos < 1 || pointPos == (form.email.value.length - 1))	{
		alert("\Your email address is invalid.\nPlease check your email address."); form.email.focus(); return false;
	}

}
if (form.subject.value=="") {alert("\nPlease enter a subject."); form.subject.focus(); return false;}
if (form.content.value=="") {alert("\nPlease enter a cotent."); form.content.focus(); return false;}
if (form.passwd.value=="")  {alert("\nPlease enter a password"); form.passwd.focus(); return false;}
if (form.passwd.value.length > 8 ) {alert("\Password should be 6~10 characters."); form.passwd.focus(); return false;}
if (isAlphaNum(form.passwd.value) == false ) {alert("Your password is invalid(only alphabets, numbers)"); form.passwd.focus(); return false;}
    
return true;
}

function bbs_del_validate(form) {

	if (form.passwd.value == '' )	{
		alert ("Please enter a password.");
		form.passwd.focus();
		return false;
	}
	
	return true;

}

//English & Number
function isAlphaNum( inStr ) {

  if (inStr.length > 0) { 
  
    for (i=0; i < inStr.length; i++) {

	if (!((inStr.charAt(i) >= 'a' && inStr.charAt(i) <= 'z') || (inStr.charAt(i) >= 'A' && inStr.charAt(i) <= 'Z') || (inStr.charAt(i) >= '0' && inStr.charAt(i) <= '9')) ) {
	return false;

	}
	
    }
    
    return true;
  }
  else { return false; }		    
}	

function isFloat( rNumber ) {

   var strlen = rNumber.length;
   var strVal = rNumber;
   var validChar = '0123456789.'; 
   var strDotCnt = 0;

   for (var j = 0; j < strlen; j++ ) {                                    
	if (validChar.indexOf(strVal.charAt(j)) < 0) {
		return false;	
	}
	if (strVal.charAt(j) == ".") {
		strDotCnt = strDotCnt + 1;
	}	
   }	    
   if (strDotCnt>1) {
   	return false;
   }
   else {
   	return true;	
   }	
 } 
 
//Number
function isNum( inStr ) {

  if (inStr.length > 0) { 

  for (i=0; i < inStr.length; i++) {

    if (!(inStr.charAt(i) >= '0' && inStr.charAt(i) <= '9')) {
    return false; 
    }
	
  }
  
  return true;
}
else { return false; }		    
}

// Number and Dot : price, weight validation
function SetNum(obj){
	val = obj.value;
	re = /[^0-9.]/gi;
	obj.value = val.replace(re,"");
}

// Number : zipcode, phone number
function OnlyNum(obj){
	val = obj.value;
	re = /[^0-9]/gi;
	obj.value = val.replace(re,"");
}
	

//number and comma
function isCommaNum( rNumber ) {

   var strlen = rNumber.length;
   var strVal = rNumber;
   var validChar = '0123456789.'; 
   var strDotCnt = 0;

   for (var j = 0; j < strlen; j++ ) {                                    
	if (validChar.indexOf(strVal.charAt(j)) < 0) {
		return false;	
	}
   }	    
   return true;	
 } 


function ValidfileName(str){
	pos = 0;
	while(pos>-1){
		pos = str.indexOf("\\");
		str  = str.substr(pos+1);
	}
  var fileSuffix = str.substring(str.lastIndexOf(".") + 1);
  var fileSuffixlength = fileSuffix.length+1;
  var totallength = str.length;
  var filelength = totallength - fileSuffixlength;
  var filenamevalue = str.substring(0,filelength); 
	return isFileNameFormat(filenamevalue);
}

function isFileNameFormat( rValue ) {
   var strlen = rValue.length;
   var strVal = rValue;
   var validChar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789. ()_-+'; 
   var strDotCnt = 0;

   for (var j = 0; j < strlen; j++ ) {                                    
	if (validChar.indexOf(strVal.charAt(j)) < 0) {
		return false;	
	}
   }	    
   return true;	
} 

function isEmail(inStr){
var chkcnt;
chkcnt = 0;
	if (inStr.length > 0) {
	    for(i=0; i<inStr.length; i++){	    
	       if(inStr.charAt(i)=='@') {
	       	chkcnt = chkcnt+1
	       }
	       if(inStr.charAt(i)=='.') {
	       	chkcnt = chkcnt+1
	       }   
	       
	       if ((inStr.charAt(i)==',') || (inStr.charAt(i)=='!') || (inStr.charAt(i)=='*') || (inStr.charAt(i)=='$') || (inStr.charAt(i)=='%') || (inStr.charAt(i)=='^') || (inStr.charAt(i)=='&') || (inStr.charAt(i)=='`') || (inStr.charAt(i)=='#')) {
	       	chkcnt = chkcnt-1	       	
	       }	
	    }   
	    
	    if ((chkcnt == 2) || (chkcnt == 3)) {
	    	
				if (inStr.indexOf(" ") != -1) {
					return false;
				} 
					
				if (inStr.indexOf("@") < 1) {
					return false; 
				} 
				if (inStr.indexOf(".") == -1) {
					return false;
				}	
				if (inStr.indexOf(".") - inStr.indexOf("@") == 1) {
					return false;
				}	
				if (inStr.charAt(inStr.length-1) == '.') {
					return false;
				}
				if (inStr.charAt(inStr.length-1) == '@') {
					return false;
				} 			
				for(i=0;i<inStr.length;i++) {
					c = inStr.charAt(i);
					if((c < '0' || c > '9')&&(c < 'a' || c > 'z')&&(c < 'A' || c > 'Z')&&(c != '.')&&(c != '-')&&(c != '_')&&(c != '@')) {
						return false;
					}
				}		
	    	return true;    
	    }
	    else {
	    	return false;
	    }
	}
	
	else { 
		return false; 
	}
}

function CheckEmail(inStr)
{
	var Bool = false;
	var i, ch;
	
	if (inStr.length > 0) {	
		for (i=0; i< inStr.length; i++)
		{
			ch = inStr.charAt(i);
			if (ch == "@")
			{
	      		Bool = true; 
	      		break;
	   		}
		}
		if (Bool == false) {
			return false;
		}	
		Bool = false;
	
		for (i=0; i< inStr.length; i++)
		{
			ch = inStr.charAt(i);
			if (ch == ".")
			{
	      		Bool = true; 
	      		break;
	      	}
		}
		if (Bool == false) {
			return false;
		}	
		return true;	
	}	
	else {
		return false;	
	}	
}

//======================================================
// printing
//======================================================

function MakeWinPrint(temp)
{
    window.open(temp, 'MakeWinPrint', 'width=677,height=500,scrollbars=1');  
}

//======================================================
// roll & over
//======================================================

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

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

function MM_findObj(n, d) { //v4.0
  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=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

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

//======================================================
// top menu
//======================================================

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_BlockNoneLayers() {
  var i,p,v,obj,args=MM_BlockNoneLayers.arguments;
  
  for (i=0; i<(args.length-2); i+=3) 

    if ((obj=MM_findObj(args[i]))!=null) { 
        v=args[i+2];
      if (obj.style) { 
        obj=obj.style; v=(v=='show')?'block':(v=='hide')?'none':v; 
        }
      obj.display=v; 
    }
}

function MM_bgColorActive(strVal,strColor) {
  var obj = document.getElementById(strVal);
  obj.bgColor = strColor;
}

function MM_bgColorInActive(strVal,strColor) {
  var obj = document.getElementById(strVal);
  obj.bgColor = strColor;
}

//======================================================
// new windows
//======================================================
function opensh(urlc,wsize,hsize,scroll,resize){

e4 = (document.all) ? 1 : 0;

   shwin = window.open(urlc,"INIT","width="+wsize+","+"height="+hsize+",resizable="+resize+","+"status=0,menubar=0,scrollbars="+scroll+","+"left=20,top=10");
   if(shwin && (shwin.opener == null)) shwin.opener=window;
   if(!e4) shwin.focus();
   	
}   

function MM_openBrWindow(theURL,winName,features) {
	
	window.open(theURL,winName,features);
	
}

function validEmail(inStr) {
	
   var InvalidChar = "~`!#$%^&*()+={}[]:;'|\,/<>?"; 
   var strDotCnt = 0;
   var strlen = inStr.length;
   
  for (var j = 0; j < strlen; j++ ) {                                    
		if (InvalidChar.indexOf(inStr.charAt(j)) >-1) {
			return false;	
			break;
		}
	}
  if (inStr.indexOf("@")==-1){
		return false;
  }
  if (inStr.indexOf(" ") != -1){
		return false;
  }
  if (window.RegExp) {
    var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
    var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
    var reg1 = new RegExp (reg1str);
    var reg2 = new RegExp (reg2str);
 
    if (reg1.test(inStr) || !reg2.test(inStr)) {
      return false;
    }
  }
  return true;
} 
