dojo.require("dojo.io.*");
dojo.require("dojo.event.*");

var loadingMsg = '<div id=content class="page_width"><img src="/images/loading.gif" width="58" height="16"></div>';
var errorMsg = "<div style='float:left;'>System Error!</div>";

var navBarId = "siteMapPath";
var resultDivId = "mainContent";
var funName = "customLoad";

var innerDivId = "content";
var innerLeftDivId = "left_menu";
var innerRightDivId = "right";


/* Geneal or Common Function */
function ajaxFormSubmit(url, frmId, setResult, getErr)
{
      dojo.io.bind({
                     url: url, 
                     load: setResult,
                     error: getErr,                       
                     formNode: dojo.byId(frmId),
                     mimetype: 'text/plain',
                     method: 'post'                
                  });
} 


function customLoad(link, target){
	var contentDiv;
	if(!target)
		target = getTargetDivId();
	contentDiv = window[target];
    contentDiv.href = link;
    contentDiv.bind();// BindDiv seems didnt inherit the preventCache attr
    document.getElementById(target).innerHTML = loadingMsg;
}

function addLink(rootId, resultDivId, funName){
	var treeRoot = document.getElementById(rootId);
	if(treeRoot){
		var treeItem = treeRoot.getElementsByTagName('A');
		for(var num = 0 ; num < treeItem.length ; num++){
			var href = treeItem[num].getAttribute("href");
			if (href.indexOf(funName) > -1 || href.indexOf('#') == href.length - 1)
				continue;
			if(resultDivId)
				treeItem[num].setAttribute("href","javascript:"+ funName +"('" + href + "','"+resultDivId+"')");
			else
				treeItem[num].setAttribute("href","javascript:"+ funName +"('" + href + "','')");
		}
	}
}

function parseSearchIds(searchIds, toClearCache){
	for(var i = 0; i < searchIds.length; i++){
		djConfig.searchIds.push(searchIds[i]);
	}
}

/*
	Function for Handset
*/
function ajaxHandsetSubmit(url){
	dojo.io.bind({
		url: url, 
		load: function (type, data , evt){
			if (data.search(/handsetBack/i)>0 ){
				if ( dojo.byId('downloadPage') || dojo.byId('content') ){
					window.location.reload();
				}else{				
					dojo.byId('topBanner').innerHTML = data;
					if ( dojo.byId('handsetSelector') ){
						dojo.byId('handsetSelector').style.display = 'none';
						dojo.byId('handsetSelector').innerHTML = '';					
					}
					if (dojo.byId('handsetSelector1')){
						dojo.byId('handsetSelector1').style.display = 'none';
						dojo.byId('handsetSelector1').innerHTML = '';				
					}
					
					if ( dojo.byId('handsetVendorsRow') ){
						dojo.byId('handsetVendorsRow').style.display = 'none';
						dojo.byId('handseListRow').style.display = 'none';
					}
					dojo.byId('content').style.display = 'block';								
				}
			}else{
				dojo.byId('handsetSelector').innerHTML = data ;
			}
		},
		error: function(type, errObj){},
		//method:'post',
		//formNode:dojo.byId('mobileForms'),
		// Backward and forward button handling
		backButton: function() {  },
		forwardButton: function() { }
	});
}
	
function showHandsetSelector(handsetModel, vendorId, handsetOperatorId, contentTypeId, contentId){
	document.getElementById('content').style.display = 'none';
	if (dojo.byId('handsetSelector1')){
		dojo.byId('handsetSelector1').style.display = 'none';
		dojo.byId('handsetSelector1').innerHTML = '';				
	}
	document.getElementById("handsetSelector").style.display = 'block';
	document.getElementById("handsetSelector").innerHTML = loadingMsg;	
	ajaxHandsetSubmit('/handsetSelector.do?userAgentId=' + handsetModel + '&vendorId=' + vendorId + '&handsetOperatorId=' + handsetOperatorId+'&contentTypeId='+contentTypeId+'&contentId='+contentId);
}

var objTimmer;

function showOrderHandsetSelector(contentTypeId,contentId ){	
	ajaxFormSubmit('/handsetSelector.do?contentTypeId='+contentTypeId+'&contentId='+contentId, '', showOrderHandsetSelectorResult, getSupportedHandsetErr )
	objTimmer = setTimeout('showOrderHandsetSelector()', 3000);
}

function showOrderHandsetSelectorResult(type, data, evt, kwh){
	clearTimeout( objTimmer );
	dojo.byId('handsetSelector1').innerHTML = data ;
}


function showSupportedHandsetComboBox( frmId )
{	
	ajaxFormSubmit('/supportedHandset.do', frmId, setSupportedHandsetResult, getSupportedHandsetErr )
	
	document.getElementById("handsetOperatorId").disabled = true;                    
    document.getElementById("vendorId").disabled = true;            
    document.getElementById("targetDiv").innerHTML = '<img src="/images/loading.gif" width="58" height="16">';
    return false; 	 	  
}

function setSupportedHandsetResult(type, data, evt, kwh)
{
	document.getElementById("targetDiv").innerHTML = data;
	document.getElementById("vendorId").disabled = false; 
	document.getElementById("handsetOperatorId").disabled = false;                    	
}

function getSupportedHandsetErr(type, error){
	document.getElementById("targetDiv").innerHTML = errorMsg;
}

function ajaxLogin( frmId ){
	ajaxFormSubmit('/ajaxLogin.do', frmId, setAjaxLoginResult, getAjaxLoginErr )
}

function setAjaxLoginResult(type, data, evt, kwh){
	//document.getElementById("headerformbtm").innerHTML = data;
	//change top banner and update head
	if ( data.match("signInFrm")== null){
		hideLogin();
		updateTop(0,0);
		if (document.getElementById('register') != null ){updateRegister();}
		document.getElementById('forgetpwd').style.display = 'none';
		document.getElementById('forgetpwdLogin').style.display = '';
		if (document.getElementById('loginT')){
		 	document.getElementById('loginT').style.display = 'none';
		 	//make searchT to tag on
		 	document.getElementById('searchT').style.background = "url('../images/tag.gif') no-repeat";
		 	document.getElementById('searchT').style.color = '000000';
		}

	}else{
		dojo.byId('errMsg').style.display = '';
		dojo.byId('signInFrm_password').value = '';
	}
}

function getAjaxLoginErr(type, error){
	document.getElementById("headerformbtm").innerHTML = errorMsg;
}

function updateTop(contentTypeId, contentId){
	ajaxFormSubmit('/selectedHandset.do?&contentTypeId='+contentTypeId+'&contentId='+contentId, '', updateTopResult, getAjaxLoginErr );	
}

function updateTopResult(type, data, evt, kwh){
	document.getElementById('topBanner').innerHTML = data;
}

function updateTopErr(type, error){
	document.getElementById('topBanner').innerHTML = errorMsg;
}

function updateRegister() {
	ajaxFormSubmit('/promoteCampaign.do', '', updateRegisterResult, updateRegisterResultErr);		
}

function updateRegisterResult(type, data, evt, kwh) {
	document.getElementById('register').style.background = "url('../images/getmore.jpg') no-repeat";
	document.getElementById('register').innerHTML = data;
}


function updateRegisterResultErr(type, error) {
	document.getElementById('register').style.background = "url('../images/getmore.jpg') no-repeat";
	document.getElementById('register').innerHTML = errorMsg;
}

function showHandsetVendorsCombo( form ){
	ajaxFormSubmit('/handsetVendors.do', form, showHandsetVendorsComboResult, showHandsetVendorsComboErr );
	if( document.getElementById("handsetOperatorId") )
   		document.getElementById("handsetOperatorId").disabled = true;
   			
	if( document.getElementById("useragentId") && document.getElementById("useragentId").length > 0){
   		document.getElementById("useragentId").selectedIndex=0;
   		document.getElementById("useragentId").disabled = true;      
    }
    
    document.getElementById("handsetVendors").innerHTML = '<img src="/images/loading.gif" width="58" height="16">';
    return false; 	 	  	
}

function showHandsetVendorsComboResult(type, data, evt, kwh){
	document.getElementById('handsetVendors').innerHTML = data;
	document.getElementById("handsetOperatorId").disabled = false; 
}

function showHandsetVendorsComboErr(type, error){
	document.getElementById('handsetVendors').innerHTML = errorMsg;
}

function showHandsetOperatorsCombo(){
	ajaxFormSubmit('/handsetOperators.do', '', showHandsetOperatorsComboResult, showHandsetOperatorsComboErr );
}

function showHandsetOperatorsComboResult(type, data, evt, kwh){
	document.getElementById('handsetOperators').innerHTML = data;
}

function showHandsetOperatorsComboErr(type, error){
	document.getElementById('handsetOperators').innerHTML = errorMsg;
}

/*
	Function for Searh Box
*/


