/*
	Standards Compliant Rollover Script
	Author : Daniel Nolan
	http://www.bleedingego.co.uk/webdev.php
*/

function initRollovers() {
	if (!document.getElementById) return
	
	var aPreLoad = new Array();
	var sTempSrc;
	var aImages = document.getElementsByTagName('img');

	for (var i = 0; i < aImages.length; i++) {		
		if (aImages[i].className == 'imgover') {
			var src = aImages[i].getAttribute('src');
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, '_ov'+ftype);
			aImages[i].setAttribute('hsrc', hsrc);
			
			aPreLoad[i] = new Image();
			aPreLoad[i].src = hsrc;
			
			aImages[i].onmouseover = function() {
				sTempSrc = this.getAttribute('src');
				this.setAttribute('src', this.getAttribute('hsrc'));
			}
			
			aImages[i].onmouseout = function() {
				if (!sTempSrc) sTempSrc = this.getAttribute('src').replace('_ov'+ftype, ftype);
				this.setAttribute('src', sTempSrc);
			}
		}
	}
}

function sabiamailer(){
	//object = { string : value,string : value,... }
    //array = [ value,value ]
	//formSerializeを実行
	//var queryString = $('#contactForm').formSerialize();
	//alert(queryString);

	//各種プロパティの配列
	var options = {
		//target: '#result', // サーバの戻りを出力する場所を指定
		beforeSubmit: bfsRequest, // 送信前に呼び出す関数
		success: scResponse, // サーバからの応答時に呼び出す関数
		error : erResponse,
		url: '../lib/mail.php', // formタグのactionをここで変更可能
		type: 'post', // postまたはget
		datatype:'json', //サーバ側のレスポンスのデータタイプを指定
		timeout: 10000, //タイムアウトを設定
		//skipEncodingOverride: 1,
		encoding : 'utf-8',
		enctype : 'application/x-www-form-urlencoded'
	};

	// 非同期通信(Ajax)したいフォームにプロパティ配列を引数にajaxForm関数で設定
	$('#contactForm').ajaxForm(options);

	//送信前実行関数
	function bfsRequest(formData, jqForm, options) {
		if($("#mode").val() == "send"){
            $('#btnset').css("display", "none");
        }
		$('#result').css("display", "block");
		$('#result').html('<div class="center"><img src="../img/common/sending.gif" id="sendingimg" alt="sending..." /></div>');
		window.location.hash = "main";
		//alert($("#mode").val());

		//フォーム各種値をクエリストリングに変換
		//var queryString = $.param(formData);
		//alert(queryString);
		//alert('About to submit: \n\n' + decodeURIComponent(queryString));
		return true;
	}

	//サーバ応答時実行関数
	function scResponse(json, statusText) {
		$('#result').css("display", "none");
	    //alert('status: ' + statusText + '\n\nresponseText: \n' + json);
		json = eval('('+json+')');
	   if(json.finish == undefined ){
			resp = '';
			if(json.error == undefined ){
				resp += "<h4>内容確認</h4><table>";
			    for (var i in json) {
			        // ループカウンタにメンバ名が入る。
			        // 括弧記法でメンバを参照できる。
					resp += "<tr><th>" + i.replace(/^_/, "") + "</th><td>" + json[i] + '</td></tr>';
			    }
			    resp += "</table>";
			    $("#f-def").css("display", "none");
			    $("#f-kind").css("display", "none");
			    $("#f-inc").css("display", "none");
			    $("#mode").val('send');
			    $('#btnset').html('<a href="javascript:;" onclick="appForm()"><img src="../img/common/btn_back.gif" value="back" /></a> <input type="image" alt="上記の内容でを送信" src="../img/common/btn_submit.gif" />');
		    }else{
				resp += '<div id="error">' + json.error + '</div>';
			}
		    $('#result').html(resp);
			$('#result').fadeIn("normal");
			window.location.hash = "main";
			
		}else{
			//$('#result').html("finish");
	        window.location.href = "http://sabias-blanc.jp/contact/thanks.html";
		}
	}
	function erResponse(){
		$('#result').html("<strong>ERROR</strong>");
	    window.location.hash = "main";
	}
	return true;
}


function appForm(){
	$("#result").css("display", "none");
	$("#f-def").css("display", "block");
	$("#f-kind").css("display", "block");
	$("#f-inc").css("display", "block");
	$("#mode").val('confirm');
	$('#btnset').html('<input type="image" alt="お見積り（お問合せ）を送信" src="../img/common/btn_confirm.gif" />');
}


function changeType(){
	var ty = $("input:radio[@name='_問合せ種別']:checked").val();
	//$('#f-inc').css("display", "none");
	if (ty == 'お問合せ') {
		$('#submitbtn').css("display", "inline");
		$("#f-inc").load("../inc_contact.php",{} , function(){
			enableCheck();
			sabiamailer();
			//$('#f-inc').fadeIn("normal");
		});
	}else 	if (ty == 'お見積') {
		$('#submitbtn').css("display", "inline");
		$("#f-inc").load("../inc_estimate.php",{} , function(){
			enableCheck();
			sabiamailer();
			//$('#f-inc').fadeIn("normal");
		});
	}
}

function enableCheck(){
   if ($("#f-estimate").val() != undefined) {
        //alert($("#f-es-1").val());
		if ($("#f-es-8:checked").val() != undefined) {
			$("#f-other").removeAttr("disabled");
		}else{
			$("#f-other").attr("disabled", "disabled");
		}
		if ($("#f-es-2:checked").val() != undefined) {
            $("#f-es-2p").removeAttr("disabled");
        }else{
            $("#f-es-2p").attr("disabled", "disabled");
        }
		if ($("#f-es-3:checked").val() != undefined) {
            $("#f-es-3p").removeAttr("disabled");
			$("#f-es-3url").removeAttr("disabled");
        }else{
            $("#f-es-3p").attr("disabled", "disabled");
			$("#f-es-3url").attr("disabled", "disabled");
        }
		if ($("#f-es-4:checked").val() != undefined) {
            $("#f-es-4p").removeAttr("disabled");
        }else{
            $("#f-es-4p").attr("disabled", "disabled");
        }
    }
	
}



function topSwf(){
	if(document.getElementById('swfArea')){
		var so = new SWFObject("./top.swf", "top", "940", "298", "8", "#F7F5F0");
		so.write("swfArea");
	}
}


window.onload = function(){
	initRollovers();
	topSwf();
	if (document.getElementById('f-inc')) {
		AjaxZip2.JSONDATA = '../js/zipdata';
		//sabiamailer();
		changeType();
	}
}

