﻿    
     function getResults() {   
       try {
        ToolSetService.getResults(onGetResults,onTimeOut,onError);
        }catch (e){
            alert("toolkit::sendReq() error!\n"+e.message);
        }
    }
    
    function createOutput(zipUrl,logUrl,logContent){
      return "<div style='width: 391px;' id='Process_Output' ><table style='width: 387px;'><tr><td style='height: 1px;' vlign='top'><img src='images/ok.gif' /></td><td style='height: 1px; width: 1726px;' vlign='top'>Done!</td><td style='width: 69px; height: 1px;'></td></tr><tr><td style='height: 11px;' vlign='top'><img src='images/zip.gif' /></td><td style='width: 1726px; height: 11px' vlign='top'><a href='/"+zipUrl+"'>Click to download the processed Zip file</a></td><td style='width: 69px; height: 11px;'></td></tr><tr><td style='height: 8px;' vlign='top'><img src='images/log.gif' /></td><td style='width: 1726px; height: 8px' vlign='top'><a href='/"+logUrl+"'>Click to download the log file</a></td><td style='width: 69px; height: 8px;'></td></tr><tr><td style='height: 8px;' vlign='top'><img src='images/onlinelog.gif' /></td><td style='width: 1726px; height: 8px' vlign='top'>Online log output:</td><td style='width: 69px; height: 8px;'></td></tr><tr><td style='width: 3px; ' colspan='3' valign='top'><textarea id='TextArea1' class='outputResult'>"+logContent+"</textarea></td></tr></table></div>";
    }
    
    var timer;
    function onGetResults(result,userContext, methodName){
        try {   
                
            var xd=result;    
            if (xd.getElementsByTagName("Return")[0].childNodes[0].nodeValue=="0"){ 
                $get('output').innerHTML= createOutput(xd.getElementsByTagName("ZipUrl")[0].childNodes[0].nodeValue,xd.getElementsByTagName("LogUrl")[0].childNodes[0].nodeValue,xd.getElementsByTagName("LogContent")[0].childNodes[0].nodeValue);
                resize();
                clearTimeout(timer);
            } else {
                $get('output').innerHTML= "<div style='width: 391px;' id='Div1' ><table style='width: 387px;'><tr><td style='height: 1px;' vlign='top'><img src='images/loading.gif' /></td><td style='height: 1px; width: 1726px;' vlign='top'>Processing, please waiting...</td><td style='width: 69px; height: 1px;'></td></tr></table></div>";
                timer = setTimeout("getResults();",1000);
            }    
           
        }catch (e) {
            alert("toolkit::xmlCallback() error!\n"+e.message);
        }
    }

// Move an element directly on top of another element (and optionally
// make it the same size)
function Cover(bottom, top, ignoreSize) {
    var location = Sys.UI.DomElement.getLocation(bottom);
    top.style.position = 'absolute';
    //top.style.top = location.y + 'px';
    //top.style.left = location.x + 'px';
    top.style.top = 50 + 'px';
    top.style.left = 50 + 'px';
    if (!ignoreSize) {
        top.style.height = bottom.offsetHeight + 'px';
        top.style.width = bottom.offsetWidth + 'px';
    }
}

function resize() { 
var f = window.parent;
var size; 
size = window.document.body.scrollHeight;
var f1 = f.document;
f1.getElementById('ctl00_SampleContent_contentIframe').style.height = size + "px";
}

function addRequestManager(){
var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_pageLoaded(pageLoaded);
    prm.add_beginRequest(beginRequest);
    prm.add_beginRequest(endRequest);
return prm;
}

function beginRequest()
{
    //alert("beginRequest");
}
function pageLoaded(sender, args)
{
    //alert("pageLoaded");
}

function endRequest()
{
    //alert("endRequest");
}

function rate()
{
   if (!isLogined()) return;
   try{
       var rv =  $get('ctl00_RatingValue');
       var fb = $get('ctl00_FeedBack');
       var tn = $get('ctl00_hfToolID');
       var un = $get('ctl00_userID');
       ToolSetService.rate(rv.value,fb.value,un.value,tn.value,onRated,onTimeOut,onError);
   }catch (e)
   {
       alert("toolkit::rate() error!\n"+e.message);
   }
   
}

function onRated(result,userContext, methodName){
    if (result!=null){
    Set_Cookie($get('ctl00_toolTitle').innerHTML, 'true', '10', '/', '', '');
    $get('ctl00_lblResponse').innerHTML = result;
    $get('btnSubmit').disabled = "disabled";
    $find("RatingBehavior1").set_ReadOnly(true);
    }
}

function onTimeOut(results, userContext, methodName){
    Alert("Sorry, Time out, please try again!");
}

function onError(errors, userContext, methodName){
    Alert("Sorry, Error happens, please try again!\n"+"Error Message:"+errors.get_Message());
}

function setInputParam(key,value)
{
   try{
    ToolSetService.addInputParam(key, value,onSetInputParamComplete,onTimeOut,onError);
   }catch (e)
   {
   alert("toolkit::setInputParam() error!\n"+e.message);
   }
}

function onSetInputParamComplete(result)
{
    //
}

//Firefox doesn't support innerText,but support textContent;
function getInnerText(obj){
    return document.all?obj.innerText:obj.textContent;
}

 // this will run automatically when the page has finished loading
 function pageLoad(sender, args)
    {
        try{
            objExtender = $find("ctl00_cpeDescription");
            objExtender.add_expandComplete(getCollapsibleState);
            objExtender.add_collapseComplete(getCollapsibleState);
            objExtender1 = $find("ctl00_cpeProperties");
            objExtender1.add_expandComplete(getCollapsibleState);
            objExtender1.add_collapseComplete(getCollapsibleState);
            objExtender2 = $find("RatingBehavior1");
            objExtender2.add_EndClientCallback(ratedByStars);
            fixRatingControl();
            resize();
            if($get('ctl00_userID').value=='NULL'||$get('ctl00_userID').value=='') {
                $find("RatingBehavior1").set_ReadOnly(true);
                $get('btnSubmit').disabled = "disabled";
            } else {
                if (Get_Cookie( $get('ctl00_toolTitle').innerHTML )=="true"){
                    $get('ctl00_lblResponse').innerHTML = "You have rated this tool before.<br />You have to wait a few days before you can rate again!"
                    $find("RatingBehavior1").set_ReadOnly(true);
                    $get('btnSubmit').disabled = "disabled";
                }else {
                }
            }

        }catch(e){
            //alert("toolkit::pageLoad() error!\n"+e.message);
        }

    }
    
    function fixRatingControl() {
    
        $get('ctl00_rating1_A').href = "javascript:;"; //fix the bug for Rating control

        if ($find("RatingBehavior1").get_ReadOnly()) {
        
            var rate = ""+$get('ctl00_rating1_A').title;

            if (rate != "0") {

                $get('ctl00_rating1_A').title = getRatingResult($get('ctl00_rating1_A').title);

            }           

        }

        else {
            $get('ctl00_rating1_Star_1').title = "Excellent (100)";
            $get('ctl00_rating1_Star_2').title = "Informative (80)";
            $get('ctl00_rating1_Star_3').title = "Useful (60)";
            $get('ctl00_rating1_Star_4').title = "Average (40)";
            $get('ctl00_rating1_Star_5').title = "Not Useful (20)";
            } 
    }

function getRatingResult(value)
{
    var result;
    switch (parseInt(value)) 
    {
        case 1: 
        result = "You have rated as 'Not Useful'"; 
        break;
        case 2: 
        result = "You have rated as 'Average'"; 
        break;
        case 3: 
        result = "You have rated as 'Useful'"; 
        break;
        case 4: 
        result = "You have rated as 'Informative'"; 
        break;
        case 5: 
        result = "You have rated as 'Excellent'"; 
        break;
        default:
        result = "soso";
    }
    return result;
}

function getRatingResult1(value)
{
    var result;
    switch (parseInt(value)) 
    {
        case 1: 
        result = "20"; 
        break;
        case 2: 
        result = "40"; 
        break;
        case 3: 
        result = "60"; 
        break;
        case 4: 
        result = "80"; 
        break;
        case 5: 
        result = "100"; 
        break;
        default:
        result = "0";
    }
    return result;
}

function ratedByStars(sender,args){
    try{
        var responseTag = $get('ctl00_lblResponse'); 
        //responseTag.innerHTML = args.get_CallbackResult();
        responseTag.innerHTML = "Your rated as "+getRatingResult1($get('ctl00_rating1_A').title)+"<br />Thanks for your rate.";
        $get('ctl00_rating1_Star_1').title = getRatingResult($get('ctl00_rating1_A').title);
        $get('ctl00_rating1_Star_2').title = getRatingResult($get('ctl00_rating1_A').title);
        $get('ctl00_rating1_Star_3').title = getRatingResult($get('ctl00_rating1_A').title);
        $get('ctl00_rating1_Star_4').title = getRatingResult($get('ctl00_rating1_A').title);
        $get('ctl00_rating1_Star_5').title = getRatingResult($get('ctl00_rating1_A').title);
        $find("RatingBehavior1").set_ReadOnly(true);
        $get('btnSubmit').disabled = "disabled";
        Set_Cookie($get('ctl00_toolTitle').innerHTML, 'true', '10', '/', '', '');
    }
    catch(e){
        alert("toolkit::ratedByStars() error!\n"+e.message);
    }
}

    function getCollapsibleState(sender,args)
    {
        if(sender.get_Collapsed())
        {   
            // code for when it is closed
           resize();
        }
        else
        {   
            // code for when it is opened
           resize();
        }
    }  


//////////////////////////////Upload control related scripts///////////////////////////////////////////////////////////////////////

function progressBar() { 
try{
    if (isLogined()){
        if (document.getElementById('fileHodler').value=="Select a file to upload"){
            return false;
        }
        document.getElementById('loading1').contentDocument.getElementById("iframe_loading1").innerHTML = "<img src='images/loading1.gif' />";
        //getResults();
        return true;
    }else{
        return false;
    }
}catch(e){
    //alert("Uploader::processBar() error:" + e.message);
}
}

function dummy(){
    
}

function setFile(){
    //document.getElementById('fileHodler').value = document.getElementById('Uploader1_upUserCtrl').value;
    document.getElementById('fileHodler').value = document.getElementById('ctl00_Tabs_Panel1_upUserCtrl').value;
    //$get('ctl00_Tabs_Panel1_loading').innerHTML="";
    $get('ctl00_Tabs_Panel1_output').innerHTML="";
    resize(); 
}

function isLogined(){
    if ($get('ctl00_userID').value!=''&&$get('ctl00_userID').value!="NULL"){
        return true;
    } else {
        alert("Please log in first before processing files.");
        return false;
    }
}

/*function isLogined()
{
   try{
       LoginWebService.isLogined(onIsLogined,onTimeOut,onError);
   }catch (e)
   {
       alert("tool.js::isLogined() error!\n"+e.message);
   }
   
}

function onIsLogined(result,userContext, methodName){
    if (result){
        if (ctrl.GetFiles().length > 0 && !ctrl.Uploading) {
		    ctrl.Upload();
		    UploadDialog.Show();
	    }
    } else {
        alert("Please sign in first before processing files.");
    }
}

function onTimeOut(results, userContext, methodName){
    alert("Sorry, Time out, please try again!");
}

function onError(errors, userContext, methodName){
    alert("Sorry, Error happens, please try again!\n"+"Error Message:"+errors.get_Message());
}
*/

/*
Script Name: Javascript Cookie Script
Author: Public Domain, with some modifications
Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php
Version 1.1.1
Last Update: 4 October 2007

Changes:
1.1.1 fixes a problem with Get_Cookie that did not correctly handle case
where cookie is initialized but it has no "=" and thus no value, the 
Get_Cookie function generates a NULL exception. This was pointed out by olivier, thanks

1.1.0 fixes a problem with Get_Cookie that did not correctly handle
cases where multiple cookies might test as the same, like: site1, site

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
*/

// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );

// To use, simple do: Get_Cookie('cookie_name'); 
// replace cookie_name with the real cookie name, '' are required
function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) 
	{
		return null;
	}
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}


