function isBlank(val) {
  if (val.split(" ").join("") == "") {
    return true;
  } else {
    return false;
  }
}

function chkNull(obj) {
    //try{
  switch (obj.type) {
    // text, password, file, textarea
    case "text" :
    case "password" :
    case "file" :
    case "textarea" :
    case "hidden" :

      if (isBlank(obj.value)) {
          return false;
      } else {
        return true;
      }
      break;
    // checkbox
    case "checkbox" :
      if (!obj.checked) {
          return false;
      } else {
        return true;
      }
      break;
    // radiobutton
    case "radio" :
      var obj_len = obj.length;

      for (i=0; i < obj_len; i++) {
        if (obj[i].checked == true)
          return false;
      }

      if (i == obj_len) {
        return true;
      }
      break;
  }

     // select list
  if (obj.type.indexOf("select") != -1) {
    if (obj.options[obj.selectedIndex].value == 0 || obj.options[obj.selectedIndex].value == ""){
      return false;
    }else{
      return true;
    }
  }

  return true;
    //}
    //catch(e)
    //{
    //    err_handle(e);
    //}
}

function nullChk(obj) {
	var str = obj.value; 
	str = str.replace(/^\s+|\s+$/,"");

	return str;
}

function isNumber(numval){
	for(i=0; i<numval.length; i++){
		str=numval.charCodeAt(i);
		if(str<48 || str>57)
			return false;
	}
	return true;
}	

function isEmail(str) {
	  // regular expression 
	  var supported = 0;
	  if (window.RegExp) {
		var tempStr = "a";
		var tempReg = new RegExp(tempStr);
		if (tempReg.test(tempStr)) supported = 1;
	  }
	  if (!supported) 
		return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	  var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	  var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	  return (!r1.test(str) && r2.test(str));
}

function winOPC(url,popname,w,h) {
	var pop = window.open( url, popname, "width="+w+",height="+h+",left="+((screen.width - w)/2)+",top="+((screen.height - h)/2) + ",status=0,resizable=0, scrollbars=0");
	if(pop){
		pop.focus();
	}
}

function winOPC2(url,popname,w,h) {

	
	//var pop = window.open( url, popname, "width="+w+",height="+h+",left="+((screen.width - w)/2)+",top="+((screen.height - h)/5) + ",resizable=0, scrollbars=0");
	
	//if(pop)
		//pop.focus();
	
	var myString = url;
    var urlLink;
    var linkPop;
    
    var form = document.createElement("form");
    var urlPos = myString.indexOf("?");
    if (urlPos > -1) {
        urlLink = myString.substring(0, urlPos);
        linkPop = urlLink.substring(0, urlLink.lastIndexOf("/"));
        myString = myString.substring(urlPos + 1);
        form.setAttribute("name", "Prelisten");
        form.setAttribute("method", "POST");
        form.setAttribute("action", urlLink);
        form.setAttribute("target", "Prelisten");
    }
    var count = 0;
    while (count != 1) {
        var pos = myString.indexOf("&");
        var tmp;
        if (pos != -1) {
            tmp = myString.substring(0, pos);
            myString = myString.substring(pos + 1);
        }
        else {
            tmp = myString;
            count = 1;
        }
        var paramName;
        var paramValue;
        var eqPos = tmp.indexOf("=");
        if (eqPos != -1) {
            paramName = tmp.substring(0, eqPos);
            paramValue = tmp.substring(eqPos + 1);
            
        }
        else {
            continue;
        }
           // alert("position :" + pos + "     tmp:" + tmp + "       mystring:" + myString);
          //alert("paramName :" + paramName + "       paramValue:" + paramValue);
		if(paramName=="title"||paramName=="artist" ||paramName=="album" ){
			paramValue = decodeURI(paramValue);						
		}					
        var elem = document.createElement("input");
        elem.setAttribute("type", "hidden");
        elem.setAttribute("name", paramName);
        elem.setAttribute("value", paramValue);
        form.appendChild(elem);
    }
   
    var win = window.open(urlLink.substring(0, urlLink.lastIndexOf("/"))+"/index.jsp", "Prelisten", "width=" + w + ", height=" +
    	    h +
    	    ",left=" +
    	    ((screen.width - w) / 2) +
    	    ",top=" +
    	    ((screen.height - h) / 5) +
    	    ",resizable=0, scrollbars=0");
   
    var body = document.getElementsByTagName("body")[0];
    body.appendChild(form);
    form.submit();
    body.removeChild(form);
    
}

function winOPCS(url,popname, w,h) {
	var pop = window.open( url, popname, "width="+w+",height="+h+",left="+((screen.width/2)-200)+",top="+((screen.height/2)-200) + ",status=0,resizable=0, scrollbars=yes");
	if(pop)
	{
		pop.focus();
	}

}

function winOPCF(url,popname,w,h) {
	var pop = window.open( url, popname, "width="+w+",height="+h+",left="+((screen.width - w)/2)+",top="+((screen.height - h)/2) + ",status=0,resizable=0, scrollbars=0");
	if (pop) {
		pop.focus();
	}
}

function winRPCIE(w,h) {
	if(window.navigator.userAgent.indexOf("MSIE 7") != -1)
		self.resizeTo(w,h);
	else
		//self.resizeTo(w,h-36);
		self.resizeTo(w,h-11);
}

function alpha_chk(str) {
	var eval = "abcdefghijklmnopqrstuvwxyz0123456789";
	var chk_val;
	
	if (null_chk(str) == false) {return false;}
	else {
		for (var ii = 0 ; ii < str.length ; ii++) {
			chk_val = 0;
			for (var jj = 0 ; jj < eval.length ; jj++) {
				if (str.substring(ii,ii+1) == eval.substring(jj,jj+1)) {
					chk_val = 1;
					break;
				}
			}
			if (chk_val == 0) {
				return false;
			}
		}
	}
	return true;
}

function isNull(str)
{
	if (str == "") 
		return true;
		
	var len = str.length;
	for (var i = 0; i < len; i++)
	{
		if (str.substring(i,i+1) != " ") 
			return false;
	}
	
	return true;
}

function null_chk(str) {
	if (str.length == 0) {return false;}
	else {
		for (var ii=0 ; ii < str.length ; ii++) { 
			if (str.substring(ii, ii+1) == " ") {
				return false;
			}
		}
	}
	return true;
}

function fnChkLogin() {

	winOPCF('<%=contextPath%>/web/popup/check_pop.jsp?checkgubun=login','checkPop','270','130');
}


function chkPhoneFormat(Min, contextPath){
	var url1 = contextPath+"/web/popup/check_pop.jsp?checkgubun=invalidphonenum";
	var url2 = contextPath+"/web/popup/check_pop.jsp?checkgubun=invalidphonenumlength";
	if ((Min.value.substring(0,2)!="08") && (Min.value.substring(0,3)!="668")
			&& (Min.value.substring(0,2)!="09") && (Min.value.substring(0,3)!="669"))
	{
		winOPC(url1, "checkPop", "270", "130");
		return false;	
	} 
	if (Min.value.length >= 11)
	{
		winOPC(url2, "checkPop", "270", "130");
		return false;
	} 
	return true;
}

function getContextPath(){
    var offset=location.href.indexOf(location.host)+location.host.length;
    var ctxPath=location.href.substring(offset,location.href.indexOf('/',offset+1));
    return ctxPath;
}

function transPhoneFormat(obj){
	var phoneNumber = obj.value;
	if (phoneNumber.substring(0,3) == "668" || phoneNumber.substring(0,3) == "669"){
		phoneNumber = "0"+phoneNumber.substring(2);
	}
	obj.value=phoneNumber;
}

//********** Facebook Function Start **********
function sharePrelisten(title, imgPath, fileId, closeWindow) {
	var indexLink = 'http://'+location.host;
	imgPath= indexLink + imgPath;	
	var preListenLink = 'http://apps.facebook.com/dtacmusic/songDetail.jsp?fileId=' + fileId;
	var callback = '';
	if(closeWindow == 'y')callback='callbackCloseWindow';
	
	streamPublish(title, preListenLink, 'http://apps.facebook.com/dtacmusic/', '', '', '', 'image', imgPath, indexLink, indexLink, indexLink, callback);
}

//titles and fileIds not end with separator (#$)
//example titles = 'title#$title#$title', fileIds = '11111#$22222#$33333';
function shareWishlist(titles, imgPath, fileIds) {	
	titles = decodeURI(titles);
	var indexLink = 'http://'+location.host;
	imgPath= indexLink + imgPath;
	var preListenLink = 'http://apps.facebook.com/dtacmusic/songDetail.jsp?fileId=';
	var preListenLinks = preListenLink + fileIds.replace(/\#\$/g,'#$'+preListenLink);
	
	streamPublish('dtacmusic.com', indexLink, 'http://apps.facebook.com/dtacmusic/', 'My Wish List ...', titles, preListenLinks, 'image', imgPath, indexLink, indexLink, indexLink, '');
}

function streamPublish(name, nameLink, caption, desc, propValueText, propValueLink, mediaType, mediaSrc, mediaLink, action, actionLink, callback) {
	var properties = null;
	if(propValueText != '' && propValueLink != '') {
		var texts = propValueText.split('#$');
		var links = propValueLink.split('#$');
		var len = texts.length;

		var jsonObj = [];
		for(var i=0 ; i<len ; i++) {
	        jsonObj.push({'text':texts[i], "href": links[i]});
	    }
	}
	FB.ui(
			{
				 method: 'feed',
				 display : 'popup',
				 name: name,
				 link: nameLink,
				 caption: caption,
				 description: desc,
				 picture: mediaSrc,
				 properties : jsonObj
			 },
			 function(response) {
				 if (response && response.post_id) {
					 if(callback != '')
						 callbackClose();
				 } 
			 }
	);
	/*FB.login(function(response){
		if(response.session){
			if (response.perms) {
				FB.ui(
						{
							 method: 'feed',
							 display : 'popup',
							 name: name,
							 link: nameLink,
							 caption: caption,
							 description: desc,
							 picture: mediaSrc,
							 properties : jsonObj
						 },
						 function(response) {
							 if (response && response.post_id) {
								 if(callback != '')
									 callbackClose();
							 } 
						 }
				);
			}
	  } 
	}, {perms:'read_stream,publish_stream,offline_access'});*/
}

function callbackClose() {
	self.close();
}

//********** Facebook Function End **********

function wishlistAction(action, uMID){
	$.ajax({
		url: "/ring4u/web/popup/wishlist_action.jsp",
		data : "action="+action+"&UMId="+uMID,
		cache: false,
		async : false,
		success: function(html){	
			winOPCF(html,'checkPop','270','130');
			if(action == "del"){
				window.location.reload()
			}
		}});		
}
