//ポップアップウィンドウ
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function orderWindow(target) {
    // parent.window.close();
    var url = '/orders/';
    if (target != undefined) {
	url += target;
    }
    w = 850;
    h = 800;
    x = (screen.width - w) / 2;
    y = (screen.height - h) / 2;
    window.open(url, 'orders', 'location=yes,scrollbars=yes,resizable=yes,' + "screenX="+x+",screenY="+y+",left="+x+",top="+y+",width="+w+",height="+h );
}

//ブックマーク
function addBookmark(title,url) {
    if (window.sidebar) {
        window.sidebar.addPanel(title, url,"");
    } else if( document.all ) {
        window.external.AddFavorite( url, title);
    } else if( window.opera && window.print ) {
        return true;
    }
}

function step1() {
    document.body.className = 'flow_step1';
}

function step2() {
    document.body.className = 'flow_step2';
}

function cal_price() {
    var url = "/orders/price";
    new Ajax.Request(url, {
        method : 'get',
        onSuccess : function (res) {
            json = res.responseJSON;
            for (k in json) {
		$(k).innerHTML = json[k];
            }
        }
    });
}

function z2h_digit(src) {
    var str = new String;
    var len = src.length;
    for (var i = 0; i < len; i++) {
        var c = src.charCodeAt(i);
        if (c >= 65296 && c <= 65305) {
            str += String.fromCharCode(c - 65248);
        } else {
            str += src.charAt(i);
        }
    }
    return str;
}

function searchAddress(type) {
    var callBack = function(req, json) {
        $(type + 'PrefectureId').value = json["prefecture_id"];
        $(type + 'Address1').value = json["address1"];
        $(type + 'Address2').value = json["address2"];
    };

    // zenkaku to hankaku
    var han_zip1 = z2h_digit($(type + 'Zip1').value);
    var han_zip2 = z2h_digit($(type + 'Zip2').value);

    $(type + 'Zip1').value = han_zip1;
    $(type + 'Zip2').value = han_zip2;
    var url = "/postals/view/" +
        $(type + 'Zip1').value + $(type + 'Zip2').value;
    new Ajax.Request(
        url,
        {
            method : "get",
            onComplete : callBack
        }
    );
}

function setAddress(id) {
    var url = "/addresses/get/" + id;
    new Ajax.Request(url, {
        method : 'get',
        onSuccess : function (res) {
            json = res.responseJSON;
            for (k in json) {
                if ($("Order" + k)) {
                    $("Order" + k).value = json[k];
                }
            }
        }
    });
}

function toggleAddress(target) {
    if (target == "my") {
	$('my_address').setAttribute('class','block active');
	$('my_address').setAttribute('className','block active');
	$('others_address').setAttribute('class', 'block');
	$('others_address').setAttribute('className', 'block');
	$('add1').checked = true;
    } else {
	$('others_address').setAttribute('class','block active');
	$('others_address').setAttribute('className','block active');
	$('my_address').setAttribute('class', 'block');
	$('my_address').setAttribute('className', 'block');
	$('add2').checked = true;
    }
}

function pwStrength(pw) {
    var strength = 100/32 * getPWStrength(pw);
    if (strength > 75) {
        strength_val = '強';
    } else if (strength > 45) {
        strength_val = '中';
    } else {
        strength_val = '弱';
    }
    $('PWStrength').innerHTML = strength_val;
}

function getPWStrength(passwd) {
    intScore = 0;
    if (passwd.match(/[a-z]/)) // [verified] at least one lower case letter
    {
        intScore = (intScore+1)
    } if (passwd.match(/[A-Z]/)) // [verified] at least one upper case letter
    {
        intScore = (intScore+5)
    } // NUMBERS
    if (passwd.match(/\d+/)) // [verified] at least one number
    {
        intScore = (intScore+5)
    } if (passwd.match(/(\d.*\d.*\d)/)) // [verified] at least three numbers
    {
        intScore = (intScore+5)
    } // SPECIAL CHAR
    if (passwd.match(/[!,@#$%^&*?_~]/)) // [verified] at least one special character
    {
        intScore = (intScore+5)
    } if (passwd.match(/([!,@#$%^&*?_~].*[!,@#$%^&*?_~])/)) // [verified] at least two special characters
    {
        intScore = (intScore+5)
    } // COMBOS
    if (passwd.match(/[a-z]/) && passwd.match(/[A-Z]/)) // [verified] both upper and lower case
    {
        intScore = (intScore+2)
    } if (passwd.match(/\d/) && passwd.match(/\D/)) // [verified] both letters and numbers
    {
        intScore = (intScore+2)
    } // [Verified] Upper Letters, Lower Letters, numbers and special characters
    if (passwd.match(/[a-z]/) && passwd.match(/[A-Z]/) && passwd.match(/\d/) && passwd.match(/[!,@#$%^&*?_~]/))
    {
        intScore = (intScore+2)
    }
    return intScore;
}