String.prototype.getBytes = function() {   
     var cArr = this.match(/[^\x00-\xff]/ig);   
     return this.length + (cArr == null ? 0 : cArr.length);   
}    

function getXMLDoc(xmlText){
	var xmlDoc;
	if(document.all){ 
		var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
　　		xmlDoc.loadXML(xmlText); 
	} else {
		var parser = new DOMParser();      
    	xmlDoc = parser.parseFromString(xmlText,"text/xml");
	}
	return xmlDoc;
}

function _UpdateForecast(xml) {
	var currentNodes = xml.getElementsByTagName("current_conditions");  
	var ccondition,temp_c,cicon,temp_low,temp_high;
	
	ccondition = getData(currentNodes[0],"condition");
	temp_c = getData(currentNodes[0],"temp_c");
	cicon = getData(currentNodes[0],"icon");
	
	currentNodes = xml.getElementsByTagName("forecast_conditions");  
	temp_low = getData(currentNodes[0],"low");
	temp_high = getData(currentNodes[0],"high");
	
	//return ccondition+"||"+temp_c+"||"+cicon;
	return ccondition+"||"+temp_low+"||"+temp_high+"||"+cicon;
}

function getData(parentNode , nodeName)  {   
	return parentNode.getElementsByTagName(nodeName)[0].getAttribute("data");  
} 

// Check Valid E-mail
function CheckEmail(strEmail) {
	var i;
	var getChar;

	if ((Left(strEmail, 1) == "@") || (Right(strEmail, 1) == "@") ){
		return false;}
	
		getChar = 0;
	for (i = 0 ; i < strEmail.length ; i++ ) {
		if (strEmail.charAt(i)=='@'){
			getChar++;
		}
	}	    

	if ( getChar != 1 ){ return false;}

	var email1;
	var email2;
	var temail;
	
	temail = strEmail.split('@');
	email1 = temail[0].replace(/\s/g, "");
	email2 = temail[1].replace(/\s/g, "");	
	
	var cemail;
	cemail = strEmail;
	cemail = cemail.replace(/\@/g, "");	
	cemail = cemail.replace(/\./g, "");	
	
	if (cemail.length == 0){ return false; }
	
	var allow;
	allow = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-";
	for(i = 0 ; i < cemail.length; i++ )
		if (allow.indexOf(cemail.charAt(i)) < 0 ){ return false; }

	if (Left(email1, 1) == '.'){ return false; }

	
	if (Left(email2, 1) == '.' || Right(email2, 1) == '.' || email2.replace(/\./g, "").length == email2.length){
		return false;
	}

	if (email2.indexOf("..") >= 0 ){
		return false;
	}

	if (Left(email2, 1) == '-' || Right(email2, 1) == '-'){
		return false;
	}
	if (Left(email2, 1) == '_' || Right(email2, 1) == '_'){
		return false;
	}
	return true;
}	

//E-mail Check
function Left( sourceStr, charIdx ) {
	if((sourceStr==null) || (sourceStr=="")) { return ""; }
	return sourceStr.substring(0, charIdx);
}

//E-mail Check
function Right( sourceStr, charIdx ) {	
	if((sourceStr==null) || (sourceStr=="")) {return ""; }
	return sourceStr.substring(sourceStr.length-charIdx, sourceStr.length);
}

function stripHTML(str) {
	return str.stripScripts().stripTags().strip();
}

function isNumber(oNum)
{
	if(!oNum) { return false; }
	var strP=/^\d+(\.\d+)?$/;
	
	if(!strP.test(oNum)) { return false; }
	
	try{
		if(parseFloat(oNum)!=oNum) return false;
	} catch(ex) {
		return false;
	}
	
	return true;
}

function IsDigital(sText)
{	
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
 }

function _setInputBG(id) {
	$(id).addClassName("input-text-cur");
}

function _removeInputBG(id) {
	$(id).removeClassName("input-text-cur");
}


function getCookie(cookie_name)
{
	var allcookies = document.cookie;
	var cookie_pos = allcookies.indexOf(cookie_name);
	var value="";
	if (cookie_pos != -1)
	{
		cookie_pos += cookie_name.length + 1;
		var cookie_end = allcookies.indexOf(";", cookie_pos);
		
		if (cookie_end == -1)
		{
			cookie_end = allcookies.length;
		}
		value = unescape(allcookies.substring(cookie_pos, cookie_end));
	}
	return value;
}

function setCookie(cookie_name,cookie_value)
{
	var expiration = new Date((new Date()).getTime() + 365 * 24 * 60 * 60000);
	document.cookie = cookie_name + "=" + escape(cookie_value)+ "; expires=" + expiration.toGMTString() + "; path=" + "/" + "; domain=" + domain;
}

function setDailyCookie(cookie_name,cookie_value)
{
	var expiration = new Date((new Date()).getTime() + 24 * 60 * 60000);
	document.cookie = cookie_name + "=" + escape(cookie_value)+ "; expires=" + expiration.toGMTString() + "; path=" + "/" + "; domain=" + domain;
}

function setCookieExpire(cookie_name,cookie_value)
{
	var expiration = new Date((new Date()).getTime() -1);
	document.cookie = cookie_name + "=" + escape(cookie_value)+ "; expires=" + expiration.toGMTString() + "; path=" + "/" + "; domain=" + domain;
}

function _CreatSystemMask() {
	if ($('mask')) { return false; }
	var _width= (document.viewport.getWidth()<parseInt(document.documentElement.scrollWidth))?parseInt(document.documentElement.scrollWidth):document.viewport.getWidth(); 
 	var _height=(document.viewport.getHeight()<parseInt(document.documentElement.scrollHeight))?parseInt(document.documentElement.scrollHeight):document.viewport.getHeight(); 
	
	var mask = new Element('div', { 'id': 'mask','class':'mask'});
	$('body').insert({ bottom: mask});
	$('mask').setStyle({ top: '0px',   left: '0px', width:_width+'px', height:_height+'px' });
}

function _createMsgBox(obj) {
	_CreatSystemMask();
	if ($("msgbox")) { return false; }
	var html = $(obj).innerHTML;
	var popup = new Element('div', { 'id': 'msgbox','class':'popup'});
	
	$('body').insert({ bottom: popup});
	$("msgbox").update(html);
	
	var scrolls = document.viewport.getScrollOffsets();
	
	var _x = (document.viewport.getWidth()-364)/2;
	var _y = (document.viewport.getHeight()-200)/2+scrolls[1]-100;
	$('msgbox').setStyle({ top: _y+'px',   left: _x+'px' }); 
}

function _createBigMsgBox(obj) {
	_CreatSystemMask();
	if ($("msgbox")) { return false; }
	var html = $(obj).innerHTML;
	var popup = new Element('div', { 'id': 'msgbox','class':'popup'});
	
	$('body').insert({ bottom: popup});
	$("msgbox").addClassName("wider-popup");
	$("msgbox").update(html);
	
	var scrolls = document.viewport.getScrollOffsets();
	
	var _x = (document.viewport.getWidth()-755)/2;
	var _y = scrolls[1]+50;
	$('msgbox').setStyle({ top: _y+'px',   left: _x+'px' }); 
}

function _removeMsgBox() {
	if ($("msgbox")) { $("msgbox").remove(); }
	if ($("mask")) { $("mask").remove(); }
}

function _createBuyBox() {
	_CreatSystemMask();

	if ($("msgbox")) { return false; }
	var html = $("branch-selecter").innerHTML;
	var popup = new Element('div', { 'id': 'msgbox','class':'branch-selecter'});
	
	$('body').insert({ bottom: popup});
	$("msgbox").update(html);

	var scrolls = document.viewport.getScrollOffsets();
	
	var _x = (document.viewport.getWidth()-468)/2;
	var _y = scrolls[1]+100;
	$('msgbox').setStyle({ top: _y+'px',   left: _x+'px' }); 
}

function initCPL() {
	clip = new ZeroClipboard.Client();
	clip.setHandCursor( true );
	
	//clip.addEventListener('load', my_load);
	clip.addEventListener('mouseOver', my_mouse_over);
	clip.addEventListener('complete', my_complete);

			
	clip.glue( 'CPL' );
}

function my_load(client) {
	debugstr("Flash movie loaded and ready.");
}
		
function my_mouse_over(client) {
	clip.setText( $('link').value );
}
		
function my_complete(client, text) {
	//debugstr("Copied text to clipboard: " + text );
	_createMsgBox("copy_popup");
	setTimeout('_removeMsgBox();',3000);
}
		
function debugstr(msg) {
	var p = document.createElement('p');
	p.innerHTML = msg;
	$('d_debug').appendChild(p);
}


