$(function()
{
	$("#firBtn").bind("input",function()
	{
		sedata = "&data="+$(this).val();
		$("#firSel").empty();
		$.get("IdcGetServerYJSON.jso",sedata,function(data)
		{
			for(var i=0;i<data.length;i++)
			{
				var li=$("<li>");
				var a=$("<a>");
				a.attr("value",data[i][0]).html(data[i][0]).addClass("btn btn-default").
				   attr({"price":data[i][1], "spec":data[i][2], "ram":data[i][3], "hdd":data[i][4]});
				li.append(a);
				$("#firSel").append(li);
			}
		});
		$("#firSel").show();
	});
	
	$(document).on("click","#firSel > li > a",function()
	{
		var span = $("<span>");
		span.addClass("caret");
		
		//$("#firBtn").html($(this).html()+"  ").append(span);
		$("#firBtn").val($(this).html());
		$("#cs_code").val($(this).html().split("-")[1]);
		$("#server_code").val($(this).attr("value"));
		$("#amount_cpu").val($(this).attr("price"));
		$("#hidden_amount_cpu").val($(this).attr("price"));
		
		$("#specramhdd").val($(this).attr("spec")+" / "+$(this).attr("ram")+" / "+$(this).attr("hdd"));
		clickTest();
		InCs();
		$("#firSel").hide();
	});
	
	//vpn선택시 vpn등록으로 이동
	$("#service_line").change(function(){
		var service_line = $(this).val();
		var client_code =$(this).attr("client_code");
		
		if(service_line == 'vpn'){
			window.location = "vpnWriteForm.sev?client_code="+client_code;	
		}else if(service_line == 'substitution' || service_line == 'test'){
			$("#amount_rack").val("0");
			$("#amount_line").val("0");
			
			$('#service_defence').val("x");
			$('#amount_defence').val("0");
			$("#amount_cpu").val("0");
			
		}else if(service_line == 'defence'){
			//장비임대 가격 원래대로
			$("#amount_cpu").val($("#hidden_amount_cpu").val());
			
			$('#service_defence').val("normal");
			$('#amount_defence').val("400000");
			clickTest();
		}else{
			//장비임대 가격 원래대로
			$("#amount_cpu").val($("#hidden_amount_cpu").val());
			
			$("#amount_rack").val("100000");
			$("#amount_line").val("300000");
			$('#service_defence').val("x");
			$('#amount_defence').val("0");
			$("#defence_discount").val("0");
		}
	});
	
});
var f = document.frm;
//cs넣기
var InCs = function()
{
	var temp = $(".CSInput");
	for(var i=0; i<temp.length;i++)
	{
		$(temp).eq(i).val($("#cs_code").val()+"-"+(i+1));
	}
}
function clickDefence(selectObj)
{		
	
	if(selectObj.value == "solo" || selectObj.value == "soloLine"){
		document.getElementById("normal_long").style.display = "none";
		document.getElementById("solo_long").style.display = "block";		
	}else{
		document.getElementById("normal_long").style.display = "block";
		document.getElementById("solo_long").style.display = "none";
	}
}

function clickIpCount(selectObj){
	document.getElementById("amount_ip").value = selectObj.value * 100000;				
}

function clickCsCount(selectObj)
{
	document.getElementById("amount_cs").value = (parseInt(selectObj.value)-1) * 50000;//0;//		
	var temp = document.getElementById("amount_cs").value;
	if(parseInt(temp)<0)
		temp=0;
	document.getElementById("amount_cs").value=temp;
}



function clickRack(selectObj){	
	//alert(selectObj.value);
	if(selectObj.value == "etc"){
		document.getElementById("rackDisply").style.display = "block";		
	}else{
		document.getElementById("rackDisply").style.display = "none";
	}	
}

function clickLine(selectObj){
	if(selectObj.value == "etc"){
		document.getElementById("lineDisply").style.display = "block";		
	}else{
		document.getElementById("lineDisply").style.display = "none";
	}
}

function clickServiceDefence(selectObj){
	//var amount_defence = document.getElementById("amount_defence").value;	
	
	if(selectObj.value == "x"){
		document.getElementById("amount_defence").value = "0";
		document.getElementById("service_line").value="normal";
	}	
	if(selectObj.value == "normal"){
		document.getElementById("amount_defence").value = "400000";
		document.getElementById("service_line").value="defence";
		
	}
	if(selectObj.value == "ordinary"){
		document.getElementById("amount_defence").value = "2000000";
		document.getElementById("service_line").value="defence";
		
	}
	if(selectObj.value == "cs-pre"){
		document.getElementById("amount_defence").value = "3000000";
		document.getElementById("service_line").value="defence";
	}	
}


//과금계산
function clickTest(){
	
	//alert("change!!!!!!!!");
	var amount_rack;
	if(document.getElementById("amount_rack").value == "etc"){
		amount_rack = (parseInt(document.getElementById("rack_etc").value)||0);
		//alert("amount_rack =" + amount_rack);		
	}else{
		amount_rack = (parseInt(document.getElementById("amount_rack").value)||0);
	}
	
	var amount_line;
	if(document.getElementById("amount_line").value == "etc"){
		amount_line = (parseInt(document.getElementById("line_etc").value)||0);
		//alert("amount_line =" + amount_line);
	}else{
		amount_line = (parseInt(document.getElementById("amount_line").value)||0);
	}
			
	var amount_cpu = (parseInt(document.getElementById("amount_cpu").value)||0);
	var amount_ip = (parseInt(document.getElementById("amount_ip").value)||0);
	var amount_cs = (parseInt(document.getElementById("amount_cs").value)||0);
		 amount_cs=0;
	var amount_defence = (parseInt(document.getElementById("amount_defence").value)||0);	
	var amount_reseller = (parseInt(document.getElementById("amount_reseller").value)||0);
	var last_reseller;
				
	var amount_long = (parseInt(document.getElementById("amount_long").value)||0);
	var solo_amount_long = (parseInt(document.getElementById("solo_amount_long").value)||0);
	var last_long = 0;
	
	if(document.getElementById("normal_long").style.display == "block"){
		//alert("일반라인");
		if(amount_long == 12){
			last_long = 10;
		}else if(amount_long == 24){
			last_long = 15;	
		}else if(amount_long == 36){
			last_long = 20;
		}else if(amount_long == 48){
			last_long = 25;
		}else if(amount_long == 60){
			last_long = 30;
		}		 
	}else{		
		if(solo_amount_long == 6){
			last_long = 5;	
		}		
	}
	document.getElementById("last_long").value = last_long;	
			
	var amount_pre = (parseInt(document.getElementById("amount_pre").value)||0);
		
	var amount_authority = (parseInt(document.getElementById("amount_authority").value)||0);
	var amount_contact = (parseInt(document.getElementById("amount_contact").value)||0);
			
	var amount_total_discount = (parseInt(document.getElementById("amount_total_discount").value)||0);
	var service_amount = (parseInt(document.getElementById("service_amount").value)||0);
	var amount_sub_discount = (parseInt(document.getElementById("amount_sub_discount").value)||0);
	
	var last_pre = 0;
	
	//고객이 일반 고객인지 리셀러 고객인지에 따라서 선입금 할인율(last_pre)결정 
	if(amount_reseller == 0){		
		if(amount_pre == 0){
			last_pre = 0;
		}else if(amount_pre == 3){
			last_pre = 10;
		}else if(amount_pre == 6){
			last_pre = 15;
		}else if(amount_pre == 12){
			last_pre = 20;
		}
	}else{
		if(amount_pre == 0){
			last_pre = 0;
		}else if(amount_pre == 3){
			last_pre = 5;
		}else if(amount_pre == 6){
			last_pre = 10;
		}else if(amount_pre == 12){
			last_pre = 15;
		}
	}
	document.getElementById("last_pre").value = last_pre;
	//계산식
	var temp = (amount_rack + amount_line + amount_cpu - amount_authority - amount_contact);
	var cost_price = temp + amount_ip +amount_defence;
	var first_dis = 0, second_dis=0;
	if(amount_reseller==0)
		first_dis = temp * last_long / 100;
	else
		first_dis = temp * amount_reseller / 100;
	temp2 = temp-first_dis;
	second_dis = temp2 * last_pre/100;

	//전체 할인금액
	var amount_total_discount = Math.ceil((first_dis+second_dis)/1000)*1000;
	//할인된 서비스 금액
	var service_cost = temp - amount_total_discount-amount_sub_discount;
	//전체 금액
	var service_calc = cost_price - amount_total_discount - amount_sub_discount;
	
	document.getElementById("amount_total_discount").value = (amount_total_discount);
	document.getElementById("amount_cost").value = cost_price;
	document.getElementById("service_amount").value = service_calc;
	document.getElementById("service_cost").value = service_cost;
}

//할인 금액이 서버 원가보다 크지 못하게 처리
function validateDiscount(){
	var amount_authority = (parseInt(document.getElementById("amount_authority").value)||0);
	var amount_contact = (parseInt(document.getElementById("amount_contact").value)||0);
	var service_cost = (parseInt(document.getElementById("service_cost").value)||0);
	
	if(amount_authority + amount_contact >= service_cost){
		alert("총 할인 금액이 서버 금액 보다 클 수 없습니다.");
		document.getElementById("amount_authority").value = "0";
		document.getElementById("amount_contact").value = "0";
		clickTest();
	}	
}
//일할 과금이 서버 금액보다 크면 안됨
function validateDeduction(){
	var service_cost = (parseInt(document.getElementById("service_cost").value)||0);
	var service_deduction = (parseInt(document.getElementById("service_deduction").value)||0);
	
	if( service_deduction > service_cost){
		alert("일할 과금 금액이 서버 금액 보다 클 수 없습니다.");
		document.getElementById("service_deduction").value = "0";
		clickTest();
	}
}

function validateTest(){
	//아이피 입력 부분 유효성 테스트
	var ip_count = parseInt(document.getElementById("ip_count").value);
	
	var getIP=$(".getIP");
	//alert(getIP[0].value);
	for(var i=0;i<getIP.length-1;i++)
	{
		for(var j=i+1;j<getIP.length;j++)
		{
			if(getIP[i].value!="" && getIP[i].value==getIP[j].value)
			{
				alert("ip가 중복되었습니다. ");
				return false;
			}
		}
	}
	
	/*
	var getIPc=$(".getIPc");
	for(var i=0;i<=ip_count;i++)
	{
		if($(getIPc[i]).val()=="")
		{
			if(i==0)
				alert("main IP를 선택해주세요 ");
			else
				alert(i+"번 IP를 선택해주세요");
				
			return false;
		}
	}
	*/
	
	var client_code = document.getElementById("client_code").value;
	if(client_code != null && client_code != "" && client_code != undefined){
		
	}else{
		alert("고객 번호를 입력해주세요");
		return false;
	}
	
	var server_code = document.getElementById("server_code").value;
	if(server_code != null && server_code != "" && server_code != undefined){
		
	}else{
		alert("장비 번호를 입력해주세요");
		return false;
	}

	var service_ip = document.getElementById("service_ip").value;
	if(service_ip != null && service_ip != "" && service_ip != undefined){
		
	}else{
		alert("기본 ip를 선택해주세요");
		return false;
	}
	
	
	var service_payment_date = document.getElementById("service_payment_date").value;
	if(service_payment_date != null && service_payment_date != "" && service_payment_date != undefined){
		
	}else{
		alert("결제일을 선택해주세요");
		return false;
	}
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////	

	
	var ip1= document.getElementById("ip1").value;
	var ip2= document.getElementById("ip2").value;
	var ip3= document.getElementById("ip3").value;
	var ip4= document.getElementById("ip4").value;
	var ip5= document.getElementById("ip5").value;
	var ip6= document.getElementById("ip6").value;
	var ip7= document.getElementById("ip7").value;
	var ip8= document.getElementById("ip8").value;
	
	var t_ip1= document.getElementById("t_ip1").value;
	var t_ip2= document.getElementById("t_ip2").value;
	var t_ip3= document.getElementById("t_ip3").value;
	var t_ip4= document.getElementById("t_ip4").value;
	var t_ip5= document.getElementById("t_ip5").value;
	var t_ip6= document.getElementById("t_ip6").value;
	var t_ip7= document.getElementById("t_ip7").value;
	var t_ip8= document.getElementById("t_ip8").value;
	
	
	var ip_flag;
	switch (ip_count) {
	case 8:
		if(ip8 == null || ip8 == "" || ip8 == undefined || t_ip8 == ""){
			alert("IP 추가 선택 갯수에 맞게 IP를 선택해주세요.");
			ip_flag = false;
			break;
		}
	case 7:
		if(ip7 == null || ip7 == "" || ip7 == undefined || t_ip7 == ""){
			alert("IP 추가 선택 갯수에 맞게 IP를 선택해주세요.");
			ip_flag = false;
			break;
		}		
	case 6:
		if(ip6 == null || ip6 == "" || ip6 == undefined || t_ip6 == ""){
			alert("IP 추가 선택 갯수에 맞게 IP를 선택해주세요.");
			ip_flag = false;
			break;
		}
		
	case 5:
		if(ip5 == null || ip5 == "" || ip5 == undefined || t_ip5 == ""){
			alert("IP 추가 선택 갯수에 맞게 IP를 선택해주세요.");
			ip_flag = false;
			break;
		}
		
	case 4:
		if(ip4 == null || ip4 == "" || ip4 == undefined || t_ip4 == ""){
			alert("IP 추가 선택 갯수에 맞게 IP를 선택해주세요.");
			ip_flag = false;
			break;
		}
		
	case 3:
		if(ip3 == null || ip3 == "" || ip3 == undefined || t_ip3 == ""){
			alert("IP 추가 선택 갯수에 맞게 IP를 선택해주세요.");
			ip_flag = false;
			break;
		}
		
	case 2:
		if(ip2 == null || ip2 == "" || ip2 == undefined || t_ip2 == ""){
			alert("IP 추가 선택 갯수에 맞게 IP를 선택해주세요.");
			ip_flag = false;
			break;
		}
		
	case 1:					
		if(ip1 == null || ip1 == "" || ip1 == undefined || t_ip1 == ""){
			alert("IP 추가 선택 갯수에 맞게 IP를 선택해주세요.");
			ip_flag = false;
			break;
		}
	default:
		ip_flag = true;
		break;
	}
	
	if(!ip_flag){
		return false;
	}
	
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	//상면비, 회선비
	var amount_rack = document.getElementById("amount_rack").value;
	if(amount_rack == "etc"){
		var rack_etc = document.getElementById("rack_etc").value;
		if(rack_etc != null && rack_etc != "" && rack_etc != undefined){
			
		}else{
			alert("기타 상면비를 입력해주세요");
			return false;
		}
	}else if(amount_rack == "" || amount_rack == null){
		alert("상면비를 입력해주세요");
		return false;
	}
	
	var amount_line = document.getElementById("amount_line").value;
	if(amount_line == "etc"){
		var line_etc = document.getElementById("line_etc").value;
		if(line_etc != null && line_etc != "" && line_etc != undefined){
			
		}else{
			alert("기타 회선비를 입력해주세요");
			return false;
		}
	}else if(amount_line == "" || amount_line == null){
		alert("회선비를 입력해주세요");
		return false;
	}
	
	var amount_cpu = document.getElementById("amount_cpu").value;		
	if(amount_cpu != null && amount_cpu != "" && amount_cpu != undefined){
		
	}else{
		alert("장비 임대 금액을 입력해주세요");
		return false;
	}
	
	
	var service_deduction = document.getElementById("service_deduction").value;
	var service_amount = document.getElementById("service_amount").value;
	
	if(service_deduction != null && service_deduction != "" && service_deduction != undefined){
				
	}else{
		alert("일할 과금 금액을 입력해주세요");
		return false;
	}
	
	if(parseInt(service_amount||0) < parseInt(service_deduction||0)){			
		alert("일할 과금이 서비스 과금을 초과할 수 없습니다. ");
		return false;
	}
	
	var service_line = document.getElementById("service_line").value;
	if(service_line == "vpn"){
		alert("VPN은 등록할 수 없습니다. 라인 종류를 선택해 주세요.");
		return false;
	}
	
	//alert("체크 끄읏");
	return true;
}

function ipInputDisplay(){
	//document.getElementById("ip1").style.display = "none";
	var ip_count = parseInt(document.getElementById("ip_count").value);
	
	switch (ip_count) {
	case 8:
		document.getElementById("ip8").style.display = "block";
		document.getElementById("ip7").style.display = "block";
		document.getElementById("ip6").style.display = "block";
		document.getElementById("ip5").style.display = "block";
		document.getElementById("ip4").style.display = "block";
		document.getElementById("ip3").style.display = "block";
		document.getElementById("ip2").style.display = "block";
		document.getElementById("ip1").style.display = "block";
		break;
	case 7:
		document.getElementById("ip8").style.display = "none";
		document.getElementById("ip7").style.display = "block";
		document.getElementById("ip6").style.display = "block";
		document.getElementById("ip5").style.display = "block";
		document.getElementById("ip4").style.display = "block";
		document.getElementById("ip3").style.display = "block";
		document.getElementById("ip2").style.display = "block";
		document.getElementById("ip1").style.display = "block";
		break;
	case 6:
		document.getElementById("ip8").style.display = "none";
		document.getElementById("ip7").style.display = "none";
		document.getElementById("ip6").style.display = "block";
		document.getElementById("ip5").style.display = "block";
		document.getElementById("ip4").style.display = "block";
		document.getElementById("ip3").style.display = "block";
		document.getElementById("ip2").style.display = "block";
		document.getElementById("ip1").style.display = "block";
		break;
	case 5:
		document.getElementById("ip8").style.display = "none";
		document.getElementById("ip7").style.display = "none";
		document.getElementById("ip6").style.display = "none";
		document.getElementById("ip5").style.display = "block";
		document.getElementById("ip4").style.display = "block";
		document.getElementById("ip3").style.display = "block";
		document.getElementById("ip2").style.display = "block";
		document.getElementById("ip1").style.display = "block";
		break;
	case 4:
		document.getElementById("ip8").style.display = "none";
		document.getElementById("ip7").style.display = "none";
		document.getElementById("ip6").style.display = "none";
		document.getElementById("ip5").style.display = "none";
		document.getElementById("ip4").style.display = "block";
		document.getElementById("ip3").style.display = "block";
		document.getElementById("ip2").style.display = "block";
		document.getElementById("ip1").style.display = "block";
		break;
	case 3:
		document.getElementById("ip8").style.display = "none";
		document.getElementById("ip7").style.display = "none";
		document.getElementById("ip6").style.display = "none";
		document.getElementById("ip5").style.display = "none";
		document.getElementById("ip4").style.display = "none";
		document.getElementById("ip3").style.display = "block";
		document.getElementById("ip2").style.display = "block";
		document.getElementById("ip1").style.display = "block";
		break;
	case 2:
		document.getElementById("ip8").style.display = "none";
		document.getElementById("ip7").style.display = "none";
		document.getElementById("ip6").style.display = "none";
		document.getElementById("ip5").style.display = "none";
		document.getElementById("ip4").style.display = "none";
		document.getElementById("ip3").style.display = "none";
		document.getElementById("ip2").style.display = "block";
		document.getElementById("ip1").style.display = "block";
		break;
	case 1:					
		document.getElementById("ip8").style.display = "none";
		document.getElementById("ip7").style.display = "none";
		document.getElementById("ip6").style.display = "none";
		document.getElementById("ip5").style.display = "none";
		document.getElementById("ip4").style.display = "none";
		document.getElementById("ip3").style.display = "none";
		document.getElementById("ip2").style.display = "none";
		document.getElementById("ip1").style.display = "block";
		break;
	default:
		document.getElementById("ip8").style.display = "none";
		document.getElementById("ip7").style.display = "none";
		document.getElementById("ip6").style.display = "none";
		document.getElementById("ip5").style.display = "none";
		document.getElementById("ip4").style.display = "none";
		document.getElementById("ip3").style.display = "none";
		document.getElementById("ip2").style.display = "none";
		document.getElementById("ip1").style.display = "none";
		break;
	}
}

function csInputDisplay(){	
	var cs_count = parseInt(document.getElementById("cs_count").value);
	
	switch (cs_count) {
	case 8:
		document.getElementById("cs8").style.display = "block";
		document.getElementById("cs7").style.display = "block";
		document.getElementById("cs6").style.display = "block";
		document.getElementById("cs5").style.display = "block";
		document.getElementById("cs4").style.display = "block";
		document.getElementById("cs3").style.display = "block";
		document.getElementById("cs2").style.display = "block";
		document.getElementById("cs1").style.display = "block";
		break;
	case 7:
		document.getElementById("cs8").style.display = "none";
		document.getElementById("cs7").style.display = "block";
		document.getElementById("cs6").style.display = "block";
		document.getElementById("cs5").style.display = "block";
		document.getElementById("cs4").style.display = "block";
		document.getElementById("cs3").style.display = "block";
		document.getElementById("cs2").style.display = "block";
		document.getElementById("cs1").style.display = "block";
		break;
	case 6:
		document.getElementById("cs8").style.display = "none";
		document.getElementById("cs7").style.display = "none";
		document.getElementById("cs6").style.display = "block";
		document.getElementById("cs5").style.display = "block";
		document.getElementById("cs4").style.display = "block";
		document.getElementById("cs3").style.display = "block";
		document.getElementById("cs2").style.display = "block";
		document.getElementById("cs1").style.display = "block";
		break;
	case 5:
		document.getElementById("cs8").style.display = "none";
		document.getElementById("cs7").style.display = "none";
		document.getElementById("cs6").style.display = "none";
		document.getElementById("cs5").style.display = "block";
		document.getElementById("cs4").style.display = "block";
		document.getElementById("cs3").style.display = "block";
		document.getElementById("cs2").style.display = "block";
		document.getElementById("cs1").style.display = "block";
		break;
	case 4:
		document.getElementById("cs8").style.display = "none";
		document.getElementById("cs7").style.display = "none";
		document.getElementById("cs6").style.display = "none";
		document.getElementById("cs5").style.display = "none";
		document.getElementById("cs4").style.display = "block";
		document.getElementById("cs3").style.display = "block";
		document.getElementById("cs2").style.display = "block";
		document.getElementById("cs1").style.display = "block";
		break;
	case 3:
		document.getElementById("cs8").style.display = "none";
		document.getElementById("cs7").style.display = "none";
		document.getElementById("cs6").style.display = "none";
		document.getElementById("cs5").style.display = "none";
		document.getElementById("cs4").style.display = "none";
		document.getElementById("cs3").style.display = "block";
		document.getElementById("cs2").style.display = "block";
		document.getElementById("cs1").style.display = "block";
		break;
	case 2:
		document.getElementById("cs8").style.display = "none";
		document.getElementById("cs7").style.display = "none";
		document.getElementById("cs6").style.display = "none";
		document.getElementById("cs5").style.display = "none";
		document.getElementById("cs4").style.display = "none";
		document.getElementById("cs3").style.display = "none";
		document.getElementById("cs2").style.display = "block";
		document.getElementById("cs1").style.display = "block";
		break;
	case 1:					
		document.getElementById("cs8").style.display = "none";
		document.getElementById("cs7").style.display = "none";
		document.getElementById("cs6").style.display = "none";
		document.getElementById("cs5").style.display = "none";
		document.getElementById("cs4").style.display = "none";
		document.getElementById("cs3").style.display = "none";
		document.getElementById("cs2").style.display = "none";
		document.getElementById("cs1").style.display = "block";
		break;
	default:
		document.getElementById("cs8").style.display = "none";
		document.getElementById("cs7").style.display = "none";
		document.getElementById("cs6").style.display = "none";
		document.getElementById("cs5").style.display = "none";
		document.getElementById("cs4").style.display = "none";
		document.getElementById("cs3").style.display = "none";
		document.getElementById("cs2").style.display = "none";
		document.getElementById("cs1").style.display = "none";
		break;
	}
}

