//Submit form after setting the value of "mode"
function modeSubmit(control, mode, page, keyname, keyid) {
	var fm;
	switch(mode) {	
	case 'btnDeleted':
		if(!window.confirm('データを削除します。よろしいですか？')){
			return;
		}
		break;
	case 'delete_image':
		if(!window.confirm('一度削除したデータは、元に戻せません。\n削除しても宜しいですか？')){
			return;
		}
		break;
	default:
		break;
	}	
	if(typeof control.form == 'undefined'){
		fm = document.getElementById(control);
	}
	else{
		fm = control.form;
	}
	
	if (typeof fm.mode  == 'undefined' ) {
		try{
			hiddenCtr = document.createElement("<input type='hidden' name='mode' value='" + mode + "'>");
		}catch(err){
			hiddenCtr = document.createElement('input');
			hiddenCtr.setAttribute('type','hidden');
			hiddenCtr.setAttribute('name', 'mode');
			hiddenCtr.setAttribute('value', mode);
		}
		fm.appendChild(hiddenCtr);
	}
	else{
		fm.mode.value = mode;
	}
	if(typeof page != 'undefined'){
		fm.action = page;
	}
	if(typeof keyname != 'undefined' && typeof keyid != 'undefined') {
		fm[keyname].value = keyid;
	}
	fm.submit();
}

// Clear Content Item
function modeClear(fieldId, fieldName) {
	document.getElementById(fieldId).value = '';
	document.getElementById(fieldName).value = '';
}

var win;
var tmpValue = null;
var tmpField = null;
//Show Open Dialog
function openModallessDialog(w,h,url) { 
	/*var newWin = window.open(url,'','toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=Yes,resizable=no,copyhistory=no,width='+w+',height='+h); 
	newWin.moveTo(screen.availWidth/2-(w/2),screen.availHeight/2-(h/2)); */
	
	LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes,scrollbars=Yes'; 
		
	win = window.open(url,'',settings); 

} 

//Show Action Page
function actionPage(url,noSort){

	window.onfocus = function( ) {
		if (win != null && win.closed) {
			window.onfocus = null;
			if ( tmpValue != null && tmpField != null && noSort==undefined){
				getListSort(tmpValue, tmpField, 'sort');
				tmpValue = null;
				tmpField = null;
			}
			
		}
	}
	openModallessDialog(800,600,url);
}

//Insert Data for Control Textarea
function insertValue(control, field, fieldName){
	var frm, value, valueName;
	var array = new Array();
	var arrayName = new Array();
	
	if(typeof control.form == 'undefined'){
		frm = document.getElementById(control);
	}
	else{
		frm = control.form;
	}
	
	//process id
	if(typeof frm.arrCheckbox.length == 'undefined'){
		if(frm.arrCheckbox.type == 'checkbox'){
			if (eval("frm.arrCheckbox.checked") == true) {
					value = eval("frm.arrCheckbox.value");
				}
		}else{
			value = eval("frm.arrCheckbox.value");
		}	
	}else{
		var max = frm.arrCheckbox.length;
		for (var idx = 0; idx < max; idx++) {
			if (eval("frm.arrCheckbox[" + idx + "].checked") == true) {
				array.push(eval("frm.arrCheckbox[" + idx + "].value"));
			}
		}
		value = array.join(",");
	}

	//process name
	if(typeof frm.arrName.length == 'undefined'){
		if(frm.arrName.value != ''){
			valueName = eval("frm.arrName.value");
		}	
	}else{
		var max = frm.arrName.length;
		for (var idx = 0; idx < max; idx++) {
			if (eval("frm.arrName[" + idx + "].value")) {
				arrayName.push(eval("frm.arrName[" + idx + "].value"));
			}
		}
		valueName = arrayName.join(",");
	}

	window.opener.addValue(value, valueName, field, fieldName);
	window.close();
//	window.child.close();

}

//----------------- ajax related functions ----------------------
var xmlHttp;
var resultSet;

function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null;

	if (navigator.userAgent.indexOf("Opera")>=0){
		alert("This example doesn't work in Opera") ;
		return ;
	}
	if (navigator.userAgent.indexOf("MSIE")>=0){ 
		var strName="Msxml2.XMLHTTP";
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
			strName="Microsoft.XMLHTTP";
		} 
		try{ 
			objXmlHttp=new ActiveXObject(strName);
			objXmlHttp.onreadystatechange=handler ;
			return objXmlHttp;
		}catch(e){ 
			alert("Error. Scripting for ActiveX might be disabled"); 
			return ;
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0){
		objXmlHttp=new XMLHttpRequest();
		objXmlHttp.onload=handler;
		objXmlHttp.onerror=handler ;
		return objXmlHttp;
	}
} 

function getListSort(value, field, ctrName, selectSort){
	// define callback function
	stateChanged = function( ) {
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			
			ctr = document.getElementById(ctrName);
			for(i in ctr.options){
				ctr.remove(i);
			}
			
			resText  = xmlHttp.responseText;
			eval(xmlHttp.responseText);
			
			i = 0;
			for(i in sorts){
				items = sorts[i];
				items = items.split(";");
				ctr.options[i] = new Option(items[1], items[0]);
				if(selectSort == items[0]) {
					ctr.options[i].selected = true;
				}
				i++;
			}
		}
    }

	//send ajax request
	url="sort.php?field=" + field + "&value=" + value + "&sid=" + Math.random();
	xmlHttp=GetXmlHttpObject(stateChanged);
	xmlHttp.open("GET", url , true);
	xmlHttp.send(null);
}

function addValue(value, valueName, field, fieldName){
	key = document.getElementById(field);
	key.value = value;
	keyName = document.getElementById(fieldName);
	keyName.value = valueName;

	if( ('helmet_id' == field) && (35 == location.href.toLowerCase().indexOf('parts_detail_regist.php')) ){

		return;	
	}

	if(('helmet_id' == field) && (35 == location.href.toLowerCase().indexOf('parts_detail_edit.php')) ){
		
		return;	
	}

	if(35 == location.href.toLowerCase().indexOf('parts_regist.php')){
		return;
	}

	if(35 == location.href.toLowerCase().indexOf('parts_edit')){
		return;
	}
	tmpValue = value;
	tmpField = field;
}

function openImage(fieldName,name,w,h) {
	value = document.getElementById(fieldName).value;
	if(typeof value != 'undefined' && value != ''){
		if(value.match('products') == 'products'){
			url = value;
		}else if(value.match('.') != ''){
			url = '../../upload/temp_image/' + value;
		}else{
			url = '../../products/common/images/noimage450_360.jpg';
		}
	}else{
		url = '../../products/common/images/noimage450_360.jpg';
	}
	
	LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes'; 
		
	window.open(url,name,settings); 
} 

 function checkboxSubmit(frmName, index, value){
 	if(typeof frmName.arrCheckbox.length == 'undefined'){
 		if(frmName.arrCheckbox.checked == true){
			frmName.arrName.value = value;
		}else{
			frmName.arrName.value = '';
		}
 	}else{
		if(frmName.arrCheckbox[index].checked == true){
			frmName.arrName[index].value = value;
		}else{
			frmName.arrName[index].value = '';
		}
	}
}

function openPdf(fieldName,name,w,h) {
	value = document.getElementById(fieldName).value;
	if(typeof value != 'undefined' && value != ''){
		if(value.match('products') == 'products'){
			url = value;
		}else if(value.match('.') != ''){
			url = '../../upload/temp_image/' + value;
		}else{
			
		}
	}else{

	}
	window.open(url,name,'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=Yes,resizable=no,copyhistory=no,width='+w+',height='+h); 
} 


function multiply_regist(fieldname){
	var value = document.getElementById(fieldname).value;
	document.getElementById('taxprice').value = Math.ceil(value * 1.05);		
}


function multiply_edit(fieldname){
	var value = document.getElementById(fieldname).value;
	document.getElementById('taxprice_form').value = Math.ceil(value * 1.05);		
}

