﻿function DisplayHideSection(sectionId)
{
    if(document.getElementById(sectionId).style.display == 'block')
    {
        document.getElementById(sectionId).style.display = 'none';
        if(sectionId.search('CommentPostId')!=-1 || sectionId.search('QuestionComment')!=-1 || sectionId.search('AnswerComment')!=-1)
        {
            elt = document.getElementById("Div"+sectionId);
            if(elt!=null)
                elt.className="headerHidden";          
        }            
    }
    else
    {
        document.getElementById(sectionId).style.display = 'block'            
        if(sectionId.search('CommentPostId')!=-1 || sectionId.search('QuestionComment')!=-1 || sectionId.search('AnswerComment')!=-1)
        {
            elt = document.getElementById("Div"+sectionId);                
            if(elt!=null)
                elt.className="headerShown";
        }
    }
    return false;                
}

function ConfirmDelete()
{
    return confirm("Are You sure?");
}

function isInteger(s)
{
    var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}    

function FocusField(TheField, comparer) 
{
   if (TheField.value == document.getElementById(comparer).value) 
        TheField.value = '';
}

function BlurField(TheField, comparer) 
{
   if (TheField.value == '') 
        TheField.value = document.getElementById(comparer).value;
}

function getPrint(print_area, print_area2)
{ 
    //Creating new pagevar 
    pp = window.open();
    //Adding HTML opening tag with <HEAD> … </HEAD> portion 
    pp.document.writeln('<HTML><HEAD>');
    pp.document.writeln('<base target="_self"></HEAD>');
    //Adding Body Tag
    pp.document.writeln('<body MS_POSITIONING="GridLayout" bottomMargin="0"');
    pp.document.writeln(' leftMargin="0" topMargin="0" rightMargin="0">');
    //Adding form Tag
    pp.document.writeln('<form method="post">');
    pp.document.writeln(document.getElementById(print_area).innerHTML);
    pp.document.writeln('<scr' + 'ipt language="javascript" type=text/javascript>window.print(); </scr' + 'ipt>');
    //Ending Tag of </form>, </body> and </HTML>
    pp.document.writeln('</form></body></HTML>'); 
} 

function InsertFlashObj(divId, objectId, width, height, url)
{           
    var divElement = document.getElementById(divId);
    str = '';
    str += '<object type="application/x-shockwave-flash" data="'+url+'" width="'+width+'" height="'+height+'" name="'+objectId+'" id="'+objectId+'">'
    str += '<param name="movie" value="'+url+'" />'
    str += '<param name="quality" value="high" />'
    str += '<param name="wmode" value="transparent" />'
    str += '<param name="swliveconnect" value="true" />'
    str += '</object>'
    divElement.innerHTML = str;
}

function setCookie (name,value,expires,path,domain,secure) 
{
  document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

function getCookie(name)
{ 
  var pos
  var token = name + "=";
  var tnlen = token.length;
  var cklen = document.cookie.length;
  var i = 0;
  var j;
  while (i < cklen)
  { j = i + tnlen;
    if (document.cookie.substring(i, j) == token)
    { pos = document.cookie.indexOf (";", j);
      if (pos == -1)
        pos = document.cookie.length;
      return unescape(document.cookie.substring(j, pos));
    }
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break;
  }
  return null;
}

function fetchCookie(c_name) {
    if (document.cookie.length > 0) {
        c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);
            if (c_end == -1) c_end = document.cookie.length;
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}

function Redirect(to)
{    
    location.href=to;   
}

function SwitchOwnChoice(divId)
{                    
    formInputs = document.getElementById('TblResult').getElementsByTagName("div");                   
    for (var i = 0; i < formInputs.length; i++)
    {                
        if(formInputs.item(i).id.substring(0, 12)  == "divOwnChoice")                
            formInputs.item(i).style.display='none';
        if(formInputs.item(i).id.substring(0, 13)  == "divOwnChoice" + divId)
            formInputs.item(i).style.display='block';
    }    
    setCookie('SwitchOwnChoice', divId, null, "/");
}

function checkPrice(priceFromId, priceToValue) 
{
    if (document.getElementById(priceFromId) && priceToValue != '') {
        var from = parseInt(document.getElementById(priceFromId).value);
        var to = parseInt(priceToValue);
        if (from > to)
            alert('Your "price from" is larger than your "price to", \r\n \r\n How do we present this to the user?');
    }
}

function checkValue(e) {
    var val;
    if (typeof e.which != 'undefined') {
        val = e.which;
    }
    else {
        val = e.keyCode;
    }

    switch (val) {
        case 0: //navigation keys in Mozilla
        case 8: //backspace
        case 9: //tab
        case 13: //enter
        case 37: // left arrow
        case 38: // up arrow
        case 39: // right arrow
        case 40: // down arrow
        case 46: // delete
        case 48: //0
        case 49: //1
        case 50: //2
        case 51: //3
        case 52: //4
        case 53: //5
        case 54: //6
        case 55: //7
        case 56: //8
        case 57: //9
        case 96: // 0 num
        case 97: // 1 num
        case 98: // 2 num
        case 99: // 3 num
        case 100: // 4 num
        case 101: // 5 num
        case 102: // 6 num
        case 103: // 7 num
        case 104: // 8 num
        case 105: // 9 num
            return true;
        default:
            return false;
    }
}